From ee971706d31c6eae07544a6d7bd6117734ae4ee8 Mon Sep 17 00:00:00 2001 From: Marco-Byte-1 <65880363+Marco-Byte-1@users.noreply.github.com> Date: Fri, 5 Mar 2021 21:44:28 +0530 Subject: [PATCH] ItemManager.java --- .../simplexdev/strike/StrikePlugin.java | 4 +- .../simplexdev/strike/listeners/Gun.java | 11 ++- .../strike/listeners/HealthPackage.java | 24 ------ .../strike/listeners/ItemManager.java | 73 +++++++++++++++++++ .../strike/listeners/SpawnController.java | 5 ++ 5 files changed, 88 insertions(+), 29 deletions(-) create mode 100644 src/main/java/io/github/simplexdev/strike/listeners/ItemManager.java diff --git a/src/main/java/io/github/simplexdev/strike/StrikePlugin.java b/src/main/java/io/github/simplexdev/strike/StrikePlugin.java index 30c850f..927c363 100644 --- a/src/main/java/io/github/simplexdev/strike/StrikePlugin.java +++ b/src/main/java/io/github/simplexdev/strike/StrikePlugin.java @@ -22,18 +22,20 @@ public final class StrikePlugin extends JavaPlugin { SpawnController spawnController = new SpawnController(this); HealthPackage healthPackage = new HealthPackage(this); Grenade grenade = new Grenade(this); + ItemManager itemManager = new ItemManager(this); getServer().getPluginManager().registerEvents(gun, this); getServer().getPluginManager().registerEvents(jumper, this); getServer().getPluginManager().registerEvents(spawnController, this); getServer().getPluginManager().registerEvents(healthPackage, this); getServer().getPluginManager().registerEvents(grenade, this); + getServer().getPluginManager().registerEvents(itemManager, this); getServer().getPluginManager().registerEvents(new InventoryEditGUI(this), this); getCommand("strike").setExecutor(new StrikeCommand(this)); - StrikeCommand.loadInstances(gun, jumper, spawnController, grenade); + StrikeCommand.loadInstances(gun, jumper, spawnController, grenade, itemManager); } @Override diff --git a/src/main/java/io/github/simplexdev/strike/listeners/Gun.java b/src/main/java/io/github/simplexdev/strike/listeners/Gun.java index d715c16..04ee7d9 100644 --- a/src/main/java/io/github/simplexdev/strike/listeners/Gun.java +++ b/src/main/java/io/github/simplexdev/strike/listeners/Gun.java @@ -64,6 +64,7 @@ public class Gun implements ConfigUser { blocks.add(Material.DARK_OAK_FENCE_GATE); blocks.add(Material.WATER); blocks.add(Material.LAVA); + blocks.add(Material.AIR); return blocks; } @@ -105,7 +106,7 @@ public class Gun implements ConfigUser { return; } - String ammoText = (Gun.ammoMap.containsKey(mainHandItem) ? ((Integer) Gun.ammoMap.get(mainHandItem)).intValue() : Gun.this.maxAmmo) + " | " + Gun.this.maxAmmo; + String ammoText = (Gun.ammoMap.containsKey(player) ? ((Integer) Gun.ammoMap.get(player)).intValue() : Gun.this.maxAmmo) + " | " + Gun.this.maxAmmo; player.sendActionBar(ammoText); } @@ -130,11 +131,11 @@ public class Gun implements ConfigUser { } int ammo = this.maxAmmo; - if (!ammoMap.containsKey(itemStack)) { + if (!ammoMap.containsKey(player)) { ammoMap.put(player, Integer.valueOf(this.maxAmmo - 1)); } else { - ammo = ((Integer) ammoMap.get(itemStack)).intValue(); + ammo = ((Integer) ammoMap.get(player)).intValue(); if (ammo == 1) { (new BukkitRunnable() { @@ -144,7 +145,7 @@ public class Gun implements ConfigUser { }).runTaskLater((Plugin) this.plugin, 20L * this.plugin.getConfig().getInt("gun.reload-time")); } - if (((Integer) ammoMap.get(itemStack)).intValue() != 0) { + if (((Integer) ammoMap.get(player)).intValue() != 0) { ammoMap.replace(player, Integer.valueOf(ammo - 1)); } } @@ -154,6 +155,8 @@ public class Gun implements ConfigUser { Entity entity = getEntity(player, player.getEyeLocation().clone(), 0.0D); + System.out.println(entity); + if (!(entity instanceof LivingEntity)) { return; } diff --git a/src/main/java/io/github/simplexdev/strike/listeners/HealthPackage.java b/src/main/java/io/github/simplexdev/strike/listeners/HealthPackage.java index 695afde..e101dfa 100644 --- a/src/main/java/io/github/simplexdev/strike/listeners/HealthPackage.java +++ b/src/main/java/io/github/simplexdev/strike/listeners/HealthPackage.java @@ -65,30 +65,6 @@ return healthPackage.clone(); } - @EventHandler - public void onDeath(PlayerDeathEvent e) { - Player player = e.getEntity(); - - if (player.getWorld().equals(Spawn.getWorld())) { - - if (player.getKiller() != null) - player.getKiller().getInventory().addItem(new ItemStack[]{this.healthPackage}); - - e.getDrops().clear(); - } - } - - @EventHandler - private void onDeath(GunKillEvent e) { - if (e.getDead() instanceof Player) - e.getKiller().getInventory().addItem(new ItemStack[]{this.healthPackage}); - } - - @EventHandler - private void onDeath(GrenadeKillEvent e) { - e.getKiller().getInventory().addItem(this.healthPackage); - } - public void refresh() { this.usedMessage = plugin.getConfig().getString("health-package.restore-health-message", "You have restored your health"); this.regainHealth = plugin.getConfig().getInt("health-package.restore-health"); diff --git a/src/main/java/io/github/simplexdev/strike/listeners/ItemManager.java b/src/main/java/io/github/simplexdev/strike/listeners/ItemManager.java new file mode 100644 index 0000000..3006ccf --- /dev/null +++ b/src/main/java/io/github/simplexdev/strike/listeners/ItemManager.java @@ -0,0 +1,73 @@ +package io.github.simplexdev.strike.listeners; + +import io.github.simplexdev.strike.api.ConfigUser; +import io.github.simplexdev.strike.api.Spawn; +import io.github.simplexdev.strike.api.utils.InventoryEditConfigManager; +import io.github.simplexdev.strike.events.GrenadeKillEvent; +import io.github.simplexdev.strike.events.GunKillEvent; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.entity.PlayerDeathEvent; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; +import org.bukkit.plugin.java.JavaPlugin; + +public class ItemManager implements ConfigUser { + + private final JavaPlugin plugin; + private ItemStack healthPackage; + private ItemStack grenade; + + public ItemManager(JavaPlugin plugin) { + this.plugin = plugin; + refresh(); + } + @EventHandler + public void onDeath(PlayerDeathEvent e) { + Player player = e.getEntity(); + + if (player.getWorld().equals(Spawn.getWorld())) { + + addItem(player); + + if (player.getKiller() != null); + } + } + + @EventHandler + private void onDeath(GunKillEvent e) { + if (e.getDead() instanceof Player) + addItem(e.getKiller()); + } + + @EventHandler + private void onDeath(GrenadeKillEvent e) { + addItem(e.getKiller()); + } + + private void addItem(Player player) { + InventoryEditConfigManager configManager = new InventoryEditConfigManager(plugin); + + if (!hasItem(player.getInventory(), grenade)) + player.getInventory().setItem(configManager.getItemSlot(grenade, player), grenade); + + if (!hasItem(player.getInventory(), healthPackage)) + player.getInventory().setItem(configManager.getItemSlot(healthPackage, player), healthPackage); + + + } + + private boolean hasItem(Inventory inventory, ItemStack itemStack) { + for (int i = 0; i < inventory.getSize(); i++) + if (inventory.getItem(0).isSimilar(itemStack)) + return true; + + return false; + } + + @Override + public void refresh() { + grenade = new Grenade(plugin).createItem(); + healthPackage = new HealthPackage(plugin).createItem(); + } +} diff --git a/src/main/java/io/github/simplexdev/strike/listeners/SpawnController.java b/src/main/java/io/github/simplexdev/strike/listeners/SpawnController.java index 4997699..3d791cd 100644 --- a/src/main/java/io/github/simplexdev/strike/listeners/SpawnController.java +++ b/src/main/java/io/github/simplexdev/strike/listeners/SpawnController.java @@ -3,20 +3,25 @@ package io.github.simplexdev.strike.listeners; import io.github.simplexdev.strike.api.ConfigUser; import io.github.simplexdev.strike.api.Spawn; import io.github.simplexdev.strike.api.utils.InventoryEditConfigManager; +import io.github.simplexdev.strike.events.GrenadeKillEvent; +import io.github.simplexdev.strike.events.GunKillEvent; import org.bukkit.Location; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.player.PlayerRespawnEvent; +import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.java.JavaPlugin; public class SpawnController implements ConfigUser { private final JavaPlugin plugin; + public SpawnController(JavaPlugin plugin) { this.plugin = plugin; + } @EventHandler