From 27f4ecbacf0eed8ad105f195ea630d05d5f08bce Mon Sep 17 00:00:00 2001 From: iTwins Date: Fri, 18 Aug 2023 23:55:28 +0200 Subject: [PATCH] fix getting radiated when not supposed to --- .../slimefun4/api/player/PlayerProfile.java | 17 +++----- .../slimefun4/implementation/Slimefun.java | 2 + .../listeners/JoinListener.java | 43 +++++++++++++++++++ .../tasks/armor/RadiationTask.java | 2 + 4 files changed, 52 insertions(+), 12 deletions(-) create mode 100644 src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/JoinListener.java diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/player/PlayerProfile.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/player/PlayerProfile.java index 29accb950..2362a91d4 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/api/player/PlayerProfile.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/player/PlayerProfile.java @@ -493,24 +493,17 @@ public class PlayerProfile { for (HashedArmorpiece armorpiece : armor) { Optional armorPiece = armorpiece.getItem(); - - if (!armorPiece.isPresent()) { - setId = null; - } else if (armorPiece.get() instanceof ProtectiveArmor protectedArmor) { - if (setId == null && protectedArmor.isFullSetRequired()) { - setId = protectedArmor.getArmorSetId(); - } - - for (ProtectionType protectionType : protectedArmor.getProtectionTypes()) { + if (armorPiece.isPresent() && armorPiece.get() instanceof ProtectiveArmor protectiveArmor) { + for (ProtectionType protectionType : protectiveArmor.getProtectionTypes()) { if (protectionType == type) { - if (setId == null) { + if (!protectiveArmor.isFullSetRequired()) { return true; - } else if (setId.equals(protectedArmor.getArmorSetId())) { + } else if (setId == null || setId.equals(protectiveArmor.getArmorSetId())) { armorCount++; + setId = protectiveArmor.getArmorSetId(); } } } - } } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/Slimefun.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/Slimefun.java index f1fffb2b8..2b8dd78f0 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/Slimefun.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/Slimefun.java @@ -82,6 +82,7 @@ import io.github.thebusybiscuit.slimefun4.implementation.listeners.GrapplingHook import io.github.thebusybiscuit.slimefun4.implementation.listeners.HopperListener; import io.github.thebusybiscuit.slimefun4.implementation.listeners.ItemDropListener; import io.github.thebusybiscuit.slimefun4.implementation.listeners.ItemPickupListener; +import io.github.thebusybiscuit.slimefun4.implementation.listeners.JoinListener; import io.github.thebusybiscuit.slimefun4.implementation.listeners.MiddleClickListener; import io.github.thebusybiscuit.slimefun4.implementation.listeners.MiningAndroidListener; import io.github.thebusybiscuit.slimefun4.implementation.listeners.MultiBlockListener; @@ -646,6 +647,7 @@ public final class Slimefun extends JavaPlugin implements SlimefunAddon { new BeeWingsListener(this, (BeeWings) SlimefunItems.BEE_WINGS.getItem()); new PiglinListener(this); new SmithingTableListener(this); + new JoinListener(this); // Item-specific Listeners new CoolerListener(this, (Cooler) SlimefunItems.COOLER.getItem()); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/JoinListener.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/JoinListener.java new file mode 100644 index 000000000..202fe079c --- /dev/null +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/JoinListener.java @@ -0,0 +1,43 @@ +package io.github.thebusybiscuit.slimefun4.implementation.listeners; + +import javax.annotation.Nonnull; + +import org.bukkit.Material; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.inventory.ItemStack; + +import io.github.thebusybiscuit.slimefun4.api.items.HashedArmorpiece; +import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; +import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile; +import io.github.thebusybiscuit.slimefun4.implementation.Slimefun; +import io.github.thebusybiscuit.slimefun4.implementation.items.armor.SlimefunArmorPiece; +import io.github.thebusybiscuit.slimefun4.implementation.tasks.armor.RadiationTask; + +/** + * This {@link Listener} caches the armor of the player on join. + * This is mainly for the {@link RadiationTask}. + * + * @author iTwins + */ +public class JoinListener implements Listener { + + public JoinListener(@Nonnull Slimefun plugin) { + plugin.getServer().getPluginManager().registerEvents(this, plugin); + } + + @EventHandler + public void onJoin(@Nonnull PlayerJoinEvent e) { + PlayerProfile.get(e.getPlayer(), playerProfile -> { + final ItemStack[] armorContents = e.getPlayer().getInventory().getArmorContents(); + final HashedArmorpiece[] hashedArmorpieces = playerProfile.getArmor(); + for (int i = 0; i < 4; i++) { + if (armorContents[i] != null && armorContents[i].getType() != Material.AIR && SlimefunItem.getByItem(armorContents[i]) instanceof SlimefunArmorPiece sfArmorPiece) { + hashedArmorpieces[i].update(armorContents[i], sfArmorPiece); + } + } + }); + } + +} diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/tasks/armor/RadiationTask.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/tasks/armor/RadiationTask.java index c2b40ae1e..29ac85080 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/tasks/armor/RadiationTask.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/tasks/armor/RadiationTask.java @@ -85,6 +85,8 @@ public class RadiationTask extends AbstractArmorTask { BaseComponent[] components = new ComponentBuilder().append(ChatColors.color(msg)).create(); p.spigot().sendMessage(ChatMessageType.ACTION_BAR, components); } + } else { + RadiationUtils.removeExposure(p, 1); } }