Skip to content

Commit

Permalink
Merge branch 'develop' into dbimproved
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbertoRutigliano authored May 29, 2024
2 parents 893ef9f + e2f9309 commit 977df61
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 54 deletions.
104 changes: 69 additions & 35 deletions src/main/java/lar/minecraft/hg/ServerSchedulers.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package lar.minecraft.hg;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

import org.bukkit.Bukkit;
Expand All @@ -23,6 +25,7 @@
import org.bukkit.inventory.meta.CompassMeta;
import org.bukkit.inventory.meta.FireworkMeta;

import lar.minecraft.hg.entities.ItemStackProbability;
import lar.minecraft.hg.enums.HGPhase;
import lar.minecraft.hg.managers.DatabaseManager;
import lar.minecraft.hg.managers.PlayerClassManager;
Expand Down Expand Up @@ -257,13 +260,14 @@ public void run() {
}
SpigotPlugin.server.getScheduler().cancelTask(playingPhaseTaskId);
}

}



}, 20, 20); // 1 second = 20 ticks

supplyDrop();
supplyDrop(config.getInt("chest-spawn-num", 3));

}

Expand Down Expand Up @@ -302,47 +306,56 @@ public void run() {
}, 20, 20); // 1 second = 20 ticks
}

private void supplyDrop() {
supplyDropTime = 0;
supplyDropTaskId = server.getScheduler().scheduleSyncRepeatingTask(SpigotPlugin.getPlugin(SpigotPlugin.class), new Runnable() {
@Override
public void run() {
long execTime = world.getTime();

// Spawn a supply drop chest after durations.supply-drop seconds
if (supplyDropTime == 0) {
supplyDropTime = execTime + (20 * config.getInt("durations.supply-drop", 10));
}

long passedSeconds = (execTime - supplyDropTime) / 20;

if (Math.abs(passedSeconds) <= 10) {
for(Player p : SpigotPlugin.server.getOnlinePlayers()) {
p.spigot().sendMessage(
ChatMessageType.ACTION_BAR,
TextComponent.fromLegacyText("A supply drop chest will be spawned in " + Math.abs(passedSeconds) + " seconds."));
private void supplyDrop(int index) {
if (index > 0) {
supplyDropTime = 0;
supplyDropTaskId = server.getScheduler().scheduleSyncRepeatingTask(SpigotPlugin.getPlugin(SpigotPlugin.class), new Runnable() {
@Override
public void run() {
long execTime = world.getTime();

// Spawn a supply drop chest after durations.supply-drop seconds
if (supplyDropTime == 0) {
// If is the first supply drop, get the first-supply-drop duration property
if (index == config.getInt("chest-spawn-num", 3)) {
supplyDropTime = execTime + (20 * config.getInt("durations.first-supply-drop", 60));
} else {
supplyDropTime = execTime + (20 * config.getInt("durations.supply-drop", 10));
}
}

long passedSeconds = (execTime - supplyDropTime) / 20;

if (Math.abs(passedSeconds) <= 10) {
for(Player p : SpigotPlugin.server.getOnlinePlayers()) {
p.spigot().sendMessage(
ChatMessageType.ACTION_BAR,
TextComponent.fromLegacyText("A supply drop chest will be spawned in " + Math.abs(passedSeconds) + " seconds."));
}
}

if (passedSeconds == 0) {
spawnSupplyDrop();
server.getScheduler().cancelTask(supplyDropTaskId);
supplyDrop(index-1);
}
}

if (passedSeconds == 0) {
spawnSupplyDrop();
server.getScheduler().cancelTask(supplyDropTaskId);
}
}
}, 20, 20); // 1 second = 20 ticks
}

}, 20, 20); // 1 second = 20 ticks
}

private static void spawnSupplyDrop() {
ServerManager.sendSound(Sound.BLOCK_BELL_USE);


public static void spawnSupplyDrop() {
// Get the spawn location
Location spawnLocation = Bukkit.getWorld("world").getSpawnLocation();

// Generate random offsets for X and Z coordinates
Random random = new Random();
int offsetX = random.nextInt(21) - 10; // Random value between -10 and 10
int offsetZ = random.nextInt(21) - 10; // Random value between -10 and 10
int worldMaxSize = SpigotPlugin.config.getInt("world-border.max-size", 128);
int offsetX = random.nextInt((worldMaxSize/2)+1) - worldMaxSize/2; // Random value between -worldMaxSize/2 and worldMaxSize/2
int offsetZ = random.nextInt((worldMaxSize/2)+1) - worldMaxSize/2; // Random value between -worldMaxSize/2 and worldMaxSize/2

// Apply offsets to the spawn location
Location randomLocation = spawnLocation.clone().add(offsetX, 0, offsetZ);
Expand All @@ -358,12 +371,33 @@ private static void spawnSupplyDrop() {
Chest chest = (Chest) block.getState();
Inventory chestInventory = chest.getBlockInventory();

// Add items to the chest's inventory
chestInventory.addItem(new ItemStack(Material.DIAMOND, 5));
chestInventory.addItem(new ItemStack(Material.GOLD_INGOT, 10));
chestInventory.addItem(new ItemStack(Material.IRON_INGOT, 20));
do {
// Items that can spawn in a chest
ArrayList<ItemStackProbability> items = new ArrayList<>();
items.add(new ItemStackProbability(Material.IRON_SWORD, 0.10));
items.add(new ItemStackProbability(Material.IRON_PICKAXE, 0.20));
items.add(new ItemStackProbability(Material.GRASS_BLOCK, 0.20, 8, 24));
items.add(new ItemStackProbability(Material.BREAD, 0.20, 6, 10));
items.add(new ItemStackProbability(Material.IRON_INGOT, 0.15, 5, 13));
items.add(new ItemStackProbability(Material.LAVA_BUCKET, 0.15));
items.add(new ItemStackProbability(Material.WATER_BUCKET, 0.15));
items.add(new ItemStackProbability(Material.DIAMOND_SWORD, 0.05));
items.add(new ItemStackProbability(Material.ENDER_PEARL, 0.25, 4, 12));

/* This code should add empty spaces in the chest but seems not working
int itemsToAdd = items.size();
for (int i = itemsToAdd; i < 27; i++) { // 27 is the max inventory size
items.add(new ItemStackProbability(Material.AIR, 1.0)); // Add an empty slot
}*/

Collections.shuffle(items);
items.forEach(i-> chestInventory.addItem(i));
} while (chestInventory.isEmpty()); // To make sure that the chest is not completely empty

ServerManager.sendSound(Sound.BLOCK_BELL_USE);
chest.getWorld().strikeLightning(chestLocation);
Bukkit.broadcastMessage("Supply chest dropped at (x = " + chestLocation.getX() + ", y = " + chestLocation.getY() + ", z = " + chestLocation.getZ() + ")");

}

}
4 changes: 4 additions & 0 deletions src/main/java/lar/minecraft/hg/SpigotPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import org.bukkit.Difficulty;
import org.bukkit.Server;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;

import lar.minecraft.hg.commands.ClassCommand;
Expand All @@ -22,13 +23,16 @@ public class SpigotPlugin extends JavaPlugin {

public static Server server;

public static FileConfiguration config;

public static HGPhase phase;

public static int serverId;

@Override
public void onLoad() {
server = getServer();
config = getConfig();
saveDefaultConfig();
}

Expand Down
74 changes: 74 additions & 0 deletions src/main/java/lar/minecraft/hg/entities/ItemStackProbability.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package lar.minecraft.hg.entities;

import java.util.Random;

import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;

public class ItemStackProbability extends ItemStack {

private double probability;
private int minAmount;
private int maxAmount;

/**
* Constructs an ItemStackProbability with a given material, probability, and a range for the amount.
* The constructor will set the amount to a random value between minAmount and maxAmount (inclusive).
* If the probability check fails, the amount is set to 0.
*
* @param material The material of the item stack.
* @param probability The probability (0.0 to 1.0) that the item will be included in the chest.
* @param minAmount The minimum amount of the item stack.
* @param maxAmount The maximum amount of the item stack.
*/
public ItemStackProbability(Material material, double probability, int minAmount, int maxAmount) {
super(material, minAmount == maxAmount ? minAmount : new Random().nextInt((maxAmount - minAmount) + 1) + minAmount);

this.probability = probability;
this.minAmount = minAmount;
this.maxAmount = maxAmount;

// If the probability check fails, set the amount to 0
if (probability != 1.0 && new Random().nextDouble() > probability) {
this.setAmount(0);
}
}

/**
* Constructs an ItemStackProbability with a given material and probability.
* This constructor sets the item stack amount to 1.
* If the probability check fails, the amount is set to 0.
*
* @param material The material of the item stack.
* @param probability The probability (0.0 to 1.0) that the item will be included in the chest.
*/
public ItemStackProbability(Material material, double probability) {
this(material, probability, 1, 1);
}

public double getProbability() {
return probability;
}

public void setProbability(double probability) {
this.probability = probability;
}

public int getMinAmount() {
return minAmount;
}

public void setMinAmount(int minAmount) {
this.minAmount = minAmount;
}

public int getMaxAmount() {
return maxAmount;
}

public void setMaxAmount(int maxAmount) {
this.maxAmount = maxAmount;
}


}
47 changes: 28 additions & 19 deletions src/main/java/lar/minecraft/hg/managers/PlayerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,17 @@ public class PlayerManager implements Listener {
*/
@EventHandler
public void onPlayerDeath(PlayerDeathEvent event){
ServerManager.sendSound(Sound.ENTITY_LIGHTNING_BOLT_THUNDER);
Player killedPlayer = event.getEntity().getPlayer();
killedPlayer.getWorld().strikeLightningEffect(killedPlayer.getLocation());
killedPlayer.setGameMode(GameMode.SPECTATOR);

// Stop reproducing particles of the winner player
if (PlayerManager.playerExtras.get(killedPlayer.getUniqueId()).isLastWinner()) {
SpigotPlugin.server.getScheduler().cancelTask(winnerParticleEffectTaskId);
}

// Check if the killer is a player
if (event.getEntity().getKiller() != null) {
// Get the player who was killed and the killer
Player killer = killedPlayer.getKiller();

// Create the player head item
ItemStack playerHead = new ItemStack(Material.PLAYER_HEAD, 1);
SkullMeta skullMeta = (SkullMeta) playerHead.getItemMeta();
if (skullMeta != null) {
skullMeta.setOwningPlayer(killedPlayer);
playerHead.setItemMeta(skullMeta);
}

// Give the killer the head of the killed player
killer.getInventory().addItem(playerHead);
}

ServerManager.sendSound(Sound.ENTITY_LIGHTNING_BOLT_THUNDER);
retrieveKilledPlayerHead(event);
}

/**
Expand All @@ -66,6 +52,8 @@ public void onPlayerDeath(PlayerDeathEvent event){
@EventHandler
public void onPlayerQuit(PlayerQuitEvent event){
if (SpigotPlugin.isSafeArea() || SpigotPlugin.isWinning() || SpigotPlugin.isPlaying()) {
Player player = event.getPlayer();
player.getWorld().strikeLightningEffect(player.getLocation());
ServerManager.sendSound(Sound.ENTITY_LIGHTNING_BOLT_THUNDER);
}
// Stop reproducing particles of the winner player
Expand Down Expand Up @@ -141,4 +129,25 @@ public static Player getNearestPlayer(Player player, double range) {
return target;
}

/*
* If a Player kill another Player he receive the killedPlayer head as prize
*/
private void retrieveKilledPlayerHead(PlayerDeathEvent event) {
Player killedPlayer = event.getEntity().getPlayer();
// Check if the killer is a player
if (event.getEntity().getKiller() != null) {
// Get the player who was killed and the killer
Player killer = killedPlayer.getKiller();

// Create the player head item
ItemStack playerHead = new ItemStack(Material.PLAYER_HEAD, 1);
SkullMeta skullMeta = (SkullMeta) playerHead.getItemMeta();
if (skullMeta != null) {
skullMeta.setOwningPlayer(killedPlayer);
playerHead.setItemMeta(skullMeta);
}
// Give the killer the head of the killed player
killer.getInventory().addItem(playerHead);
}
}
}
4 changes: 4 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Minimum players to start the Hunger Games
min-players: 3
# Number of supply drop chest to spawn
chest-spawn-num: 3
durations:
# Lobby phase duration in seconds where players can login and choose their class
lobby: 10
Expand All @@ -10,6 +12,8 @@ durations:
# Fireworks celebrations duration in seconds
fireworks: 15
# How many seconds after the start of the game (after safe-area phase) is the supply chest dropped
first-supply-drop: 10
# Interval seconds between every supply chest drop
supply-drop: 60
# How many seconds a player can be stationary during the playing phase before getting kicked (minutes)
idle-timeout: 2
Expand Down

0 comments on commit 977df61

Please sign in to comment.