From 2ada0604ae3dcaf608547ae642496c360ad53869 Mon Sep 17 00:00:00 2001 From: Genny Date: Thu, 20 Feb 2020 23:43:16 -0600 Subject: Fix bug where first players didn't get safe helmet --- .../genbyte/sunfright/events/HelmetHandler.java | 29 ++++++++++++++++------ 1 file changed, 22 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/main/java/dev/genbyte/sunfright/events/HelmetHandler.java b/src/main/java/dev/genbyte/sunfright/events/HelmetHandler.java index c164536..a583f44 100644 --- a/src/main/java/dev/genbyte/sunfright/events/HelmetHandler.java +++ b/src/main/java/dev/genbyte/sunfright/events/HelmetHandler.java @@ -2,24 +2,28 @@ package dev.genbyte.sunfright.events; import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; +import org.bukkit.entity.Player; 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.event.player.PlayerJoinEvent; 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(); + setRespawnHelmet(event.getPlayer()); + } - ItemStack stack = new ItemStack(Material.LEATHER_HELMET); - stack.addUnsafeEnchantment(Enchantment.BINDING_CURSE, 1); - stack.addUnsafeEnchantment(Enchantment.VANISHING_CURSE, 2); - stack.addEnchantment(Enchantment.PROTECTION_FIRE, 1); + @EventHandler + public void onPlayerJoin(PlayerJoinEvent event) { + Player player = event.getPlayer(); - inv.setHelmet(stack); + if (!player.hasPlayedBefore()) { + setRespawnHelmet(player); + } } @EventHandler @@ -31,4 +35,15 @@ public class HelmetHandler implements Listener { event.getView().setItem(event.getRawSlot(), new ItemStack(Material.AIR)); } } -} \ No newline at end of file + + private void setRespawnHelmet(Player player) { + PlayerInventory inv = player.getInventory(); + + ItemStack stack = new ItemStack(Material.LEATHER_HELMET); + stack.addUnsafeEnchantment(Enchantment.BINDING_CURSE, 1); + stack.addUnsafeEnchantment(Enchantment.VANISHING_CURSE, 2); + stack.addEnchantment(Enchantment.PROTECTION_FIRE, 1); + + inv.setHelmet(stack); + } +} -- cgit 1.4.1-3-g733a5