about summary refs log tree commit diff
path: root/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/dev/genbyte/sunfright/Damager.java14
-rw-r--r--src/main/java/dev/genbyte/sunfright/Sunfright.java4
-rw-r--r--src/main/java/dev/genbyte/sunfright/events/HelmetHandler.java33
3 files changed, 48 insertions, 3 deletions
diff --git a/src/main/java/dev/genbyte/sunfright/Damager.java b/src/main/java/dev/genbyte/sunfright/Damager.java
index c592647..39cdab1 100644
--- a/src/main/java/dev/genbyte/sunfright/Damager.java
+++ b/src/main/java/dev/genbyte/sunfright/Damager.java
@@ -5,6 +5,7 @@ import java.util.Collection;
 import org.bukkit.entity.Player;
 import org.bukkit.inventory.ItemStack;
 import org.bukkit.inventory.meta.Damageable;
+import org.bukkit.inventory.meta.ItemMeta;
 import org.bukkit.scheduler.BukkitRunnable;
 
 public class Damager extends BukkitRunnable {
@@ -37,9 +38,16 @@ public class Damager extends BukkitRunnable {
 
 		public void run() {
 			ItemStack helmet = player.getInventory().getHelmet();
-			if (helmet != null && helmet instanceof Damageable) {
-				Damageable damageable = (Damageable) helmet;
-				damageable.setDamage(damageable.getDamage() - damage);
+
+			if (helmet != null) {
+				ItemMeta helmetMeta = helmet.getItemMeta();
+
+				if (helmetMeta instanceof Damageable) {
+					Damageable helmetDamageable = (Damageable) helmetMeta;
+
+					helmetDamageable.setDamage(helmetDamageable.getDamage() + damage);
+					helmet.setItemMeta((ItemMeta) helmetDamageable);
+				}
 			} else {
 				player.damage(damage);
 			}
diff --git a/src/main/java/dev/genbyte/sunfright/Sunfright.java b/src/main/java/dev/genbyte/sunfright/Sunfright.java
index d05774f..87ab46b 100644
--- a/src/main/java/dev/genbyte/sunfright/Sunfright.java
+++ b/src/main/java/dev/genbyte/sunfright/Sunfright.java
@@ -6,6 +6,8 @@ import java.util.logging.Logger;
 import org.bukkit.plugin.java.JavaPlugin;
 import org.bukkit.scheduler.BukkitTask;
 
+import dev.genbyte.sunfright.events.HelmetHandler;
+
 public class Sunfright extends JavaPlugin {
     private Logger logger;
     private BukkitTask damager;
@@ -20,6 +22,8 @@ public class Sunfright extends JavaPlugin {
         int time = 20;
         damager = new Damager(this).runTaskTimerAsynchronously(this, time, time);
         logger.log(Level.INFO, "Damager task started. and will run every " + (time / 20) + " seconds");
+
+        this.getServer().getPluginManager().registerEvents(new HelmetHandler(), this);
     }
 
     @Override
diff --git a/src/main/java/dev/genbyte/sunfright/events/HelmetHandler.java b/src/main/java/dev/genbyte/sunfright/events/HelmetHandler.java
new file mode 100644
index 0000000..47be317
--- /dev/null
+++ b/src/main/java/dev/genbyte/sunfright/events/HelmetHandler.java
@@ -0,0 +1,33 @@
+package dev.genbyte.sunfright.events;
+
+import org.bukkit.Material;
+import org.bukkit.enchantments.Enchantment;
+import org.bukkit.event.EventHandler;
+import org.bukkit.event.Listener;
+import org.bukkit.event.inventory.InventoryClickEvent;
+import org.bukkit.event.player.PlayerRespawnEvent;
+import org.bukkit.inventory.ItemStack;
+import org.bukkit.inventory.PlayerInventory;
+
+public class HelmetHandler implements Listener {
+	@EventHandler
+	public void onPlayerRespawn(PlayerRespawnEvent event) {
+		PlayerInventory inv = event.getPlayer().getInventory();
+
+		ItemStack stack = new ItemStack(Material.IRON_HELMET);
+		stack.addUnsafeEnchantment(Enchantment.BINDING_CURSE, 2);
+		stack.addUnsafeEnchantment(Enchantment.VANISHING_CURSE, 2);
+
+		inv.setHelmet(stack);
+	}
+
+	@EventHandler
+	public void onInventoryClick(InventoryClickEvent event) {
+		if (event.getClickedInventory() instanceof PlayerInventory
+				&& event.getCurrentItem().getEnchantmentLevel(Enchantment.VANISHING_CURSE) == 2) {
+
+			event.setCancelled(true);
+			event.getView().setItem(event.getRawSlot(), new ItemStack(Material.AIR));
+		}
+	}
+}
\ No newline at end of file