ItemManager.java

This commit is contained in:
Marco-Byte-1 2021-03-05 21:44:28 +05:30
parent 51770dee82
commit ee971706d3
5 changed files with 88 additions and 29 deletions

View File

@ -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

View File

@ -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;
}

View File

@ -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");

View File

@ -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();
}
}

View File

@ -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