From 64a1b247b4518577a32006ce1692218a5399af52 Mon Sep 17 00:00:00 2001 From: LinoxGH Date: Sat, 27 Jun 2020 17:30:28 +0300 Subject: [PATCH] Did the requested changes + fixes. --- .../slimefun4/api/items/HashedArmorpiece.java | 2 +- .../slimefun4/api/player/PlayerProfile.java | 37 ++++++++++++++++ .../core/attributes/CustomProtection.java | 9 +--- .../core/attributes/ProtectionType.java | 8 ++++ .../items/armor/HazmatArmorPiece.java | 3 +- .../items/armor/SlimefunArmorPiece.java | 16 ++++--- .../implementation/listeners/BeeListener.java | 40 ++--------------- .../implementation/tasks/ArmorTask.java | 43 ++----------------- 8 files changed, 65 insertions(+), 93 deletions(-) create mode 100644 src/main/java/io/github/thebusybiscuit/slimefun4/core/attributes/ProtectionType.java diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/items/HashedArmorpiece.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/items/HashedArmorpiece.java index afb0cd4be..a848cadca 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/api/items/HashedArmorpiece.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/items/HashedArmorpiece.java @@ -78,7 +78,7 @@ public final class HashedArmorpiece { */ public boolean hasDiverged(ItemStack stack) { if (stack == null || stack.getType() == Material.AIR) { - return hash == 0; + return hash != 0; } else { ItemStack copy = stack.clone(); 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 ddf5fcde5..793438edb 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 @@ -19,6 +19,7 @@ import org.apache.commons.lang.Validate; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Location; +import org.bukkit.NamespacedKey; import org.bukkit.OfflinePlayer; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -31,9 +32,12 @@ import io.github.thebusybiscuit.cscorelib2.chat.ChatColors; import io.github.thebusybiscuit.cscorelib2.config.Config; import io.github.thebusybiscuit.slimefun4.api.gps.Waypoint; import io.github.thebusybiscuit.slimefun4.api.items.HashedArmorpiece; +import io.github.thebusybiscuit.slimefun4.core.attributes.CustomProtection; +import io.github.thebusybiscuit.slimefun4.core.attributes.ProtectionType; import io.github.thebusybiscuit.slimefun4.core.guide.GuideHistory; import io.github.thebusybiscuit.slimefun4.core.researching.Research; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; +import io.github.thebusybiscuit.slimefun4.implementation.items.armor.SlimefunArmorPiece; import io.github.thebusybiscuit.slimefun4.utils.NumberUtils; import io.github.thebusybiscuit.slimefun4.utils.PatternUtils; import me.mrCookieSlime.Slimefun.api.Slimefun; @@ -449,6 +453,39 @@ public final class PlayerProfile { } } + public boolean isProtected(ProtectionType type) { + int armorCount = 0; + boolean first = true; + + NamespacedKey setId = null; + for (HashedArmorpiece armor : armor) { + Optional armorPiece = armor.getItem(); + if (!armorPiece.isPresent()) return false; + + if (armorPiece.get() instanceof CustomProtection) { + CustomProtection protectedArmor = (CustomProtection) armorPiece.get(); + + if (first) { + if (protectedArmor.isFullSetRequired()) setId = armorPiece.get().getSetId(); + first = false; + } + + for (ProtectionType protectionType : protectedArmor.getProtectionTypes()) { + if (protectionType == type) { + if (setId == null) { + return true; + } else if (setId.equals(armorPiece.get().getSetId())) { + armorCount++; + } + } + } + + } + } + + return armorCount == 4; + } + @Override public int hashCode() { return uuid.hashCode(); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/attributes/CustomProtection.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/attributes/CustomProtection.java index c142ca7fc..f6b7af88e 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/attributes/CustomProtection.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/attributes/CustomProtection.java @@ -37,12 +37,5 @@ public interface CustomProtection extends ItemAttribute { * * @return The {@link ProtectionType}s */ - boolean requireFullSet(); - - enum ProtectionType { - - RADIATION, - - BEES; - } + boolean isFullSetRequired(); } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/attributes/ProtectionType.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/attributes/ProtectionType.java new file mode 100644 index 000000000..c4b74703f --- /dev/null +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/attributes/ProtectionType.java @@ -0,0 +1,8 @@ +package io.github.thebusybiscuit.slimefun4.core.attributes; + +public enum ProtectionType { + + RADIATION, + BEES; + +} diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/armor/HazmatArmorPiece.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/armor/HazmatArmorPiece.java index 0216d1036..16db6724a 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/armor/HazmatArmorPiece.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/armor/HazmatArmorPiece.java @@ -4,6 +4,7 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.potion.PotionEffect; import io.github.thebusybiscuit.slimefun4.core.attributes.CustomProtection; +import io.github.thebusybiscuit.slimefun4.core.attributes.ProtectionType; import me.mrCookieSlime.Slimefun.Lists.RecipeType; import me.mrCookieSlime.Slimefun.Objects.Category; import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; @@ -24,7 +25,7 @@ public class HazmatArmorPiece extends SlimefunArmorPiece implements CustomProtec } @Override - public boolean requireFullSet() { + public boolean isFullSetRequired() { return true; } } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/armor/SlimefunArmorPiece.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/armor/SlimefunArmorPiece.java index 40c8540ad..9d48d6a40 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/armor/SlimefunArmorPiece.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/armor/SlimefunArmorPiece.java @@ -1,9 +1,11 @@ package io.github.thebusybiscuit.slimefun4.implementation.items.armor; +import org.bukkit.NamespacedKey; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.potion.PotionEffect; +import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; import me.mrCookieSlime.Slimefun.Lists.RecipeType; import me.mrCookieSlime.Slimefun.Objects.Category; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; @@ -11,7 +13,7 @@ import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; public class SlimefunArmorPiece extends SlimefunItem { - private String setID = null; + private NamespacedKey id = null; private final PotionEffect[] effects; public SlimefunArmorPiece(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, PotionEffect[] effects) { @@ -20,10 +22,10 @@ public class SlimefunArmorPiece extends SlimefunItem { this.effects = effects == null ? new PotionEffect[0] : effects; } - public SlimefunArmorPiece(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, PotionEffect[] effects, String setID) { + public SlimefunArmorPiece(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, PotionEffect[] effects, String setId) { this(category, item, recipeType, recipe, effects); - this.setID = setID; + this.id = new NamespacedKey(SlimefunPlugin.instance, setId); } /** @@ -37,11 +39,11 @@ public class SlimefunArmorPiece extends SlimefunItem { } /** - * This returns the armor set ID of this {@link SlimefunArmorPiece}. + * This returns the armor set {@link NamespacedKey} of this {@link SlimefunArmorPiece}. * - * @return The set ID, null if no set ID is found. + * @return The set {@link NamespacedKey}, null if none is found. */ - public String getSetID() { - return setID; + public NamespacedKey getSetId() { + return id; } } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/BeeListener.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/BeeListener.java index c5897f96e..c45531d5a 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/BeeListener.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/BeeListener.java @@ -10,11 +10,9 @@ import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.inventory.ItemStack; import io.github.thebusybiscuit.cscorelib2.inventory.ItemUtils; -import io.github.thebusybiscuit.slimefun4.api.items.HashedArmorpiece; import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile; -import io.github.thebusybiscuit.slimefun4.core.attributes.CustomProtection; +import io.github.thebusybiscuit.slimefun4.core.attributes.ProtectionType; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; -import io.github.thebusybiscuit.slimefun4.implementation.items.armor.SlimefunArmorPiece; /** * The listener for Hazmat Suit's {@link Bee} sting protection. @@ -39,10 +37,9 @@ public class BeeListener implements Listener { PlayerProfile.request(p); return; } - PlayerProfile profile = optional.get(); - HashedArmorpiece[] armors = profile.getArmor(); - if (shouldProtect(armors)) { + PlayerProfile profile = optional.get(); + if (profile.isProtected(ProtectionType.BEES)) { for (ItemStack armor : p.getInventory().getArmorContents()) { ItemUtils.damageItem(armor, 1, false); } @@ -51,35 +48,4 @@ public class BeeListener implements Listener { } } - private boolean shouldProtect(HashedArmorpiece[] armors) { - int armorCount = 0; - boolean first = true; - - String setID = null; - for (HashedArmorpiece armor : armors) { - Optional armorPiece = armor.getItem(); - if (!armorPiece.isPresent()) return false; - - if (armorPiece.get() instanceof CustomProtection) { - CustomProtection protectedArmor = (CustomProtection) armorPiece.get(); - - if (first) { - if (protectedArmor.requireFullSet()) setID = armorPiece.get().getSetID(); - first = false; - } - - for (CustomProtection.ProtectionType protectionType : protectedArmor.getProtectionTypes()) { - if (protectionType == CustomProtection.ProtectionType.BEES) { - if (setID == null) { - return true; - } - armorCount++; - } - } - - } - } - - return armorCount == 4; - } } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/tasks/ArmorTask.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/tasks/ArmorTask.java index 1ac187eca..17351980f 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/tasks/ArmorTask.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/tasks/ArmorTask.java @@ -2,7 +2,6 @@ package io.github.thebusybiscuit.slimefun4.implementation.tasks; import java.util.Collections; import java.util.HashSet; -import java.util.Optional; import java.util.Set; import org.bukkit.Bukkit; @@ -15,7 +14,7 @@ import org.bukkit.potion.PotionEffectType; import io.github.thebusybiscuit.slimefun4.api.items.HashedArmorpiece; import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile; -import io.github.thebusybiscuit.slimefun4.core.attributes.CustomProtection; +import io.github.thebusybiscuit.slimefun4.core.attributes.ProtectionType; import io.github.thebusybiscuit.slimefun4.core.attributes.Radioactive; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunItems; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; @@ -122,18 +121,16 @@ public class ArmorTask implements Runnable { } private void checkForRadiation(Player p, PlayerProfile profile) { - HashedArmorpiece[] armor = profile.getArmor(); - - if (!shouldProtect(armor)) { + if (!profile.isProtected(ProtectionType.RADIATION)) { for (ItemStack item : p.getInventory()) { - if (checkAndApplyRadioactive(p, item)) { + if (checkAndApplyRadiation(p, item)) { break; } } } } - private boolean checkAndApplyRadioactive(Player p, ItemStack item) { + private boolean checkAndApplyRadiation(Player p, ItemStack item) { for (SlimefunItem radioactiveItem : SlimefunPlugin.getRegistry().getRadioactiveItems()) { if (radioactiveItem.isItem(item) && Slimefun.isEnabled(p, radioactiveItem, true)) { // If the item is enabled in the world, then make radioactivity do its job @@ -150,36 +147,4 @@ public class ArmorTask implements Runnable { return false; } - - private boolean shouldProtect(HashedArmorpiece[] armors) { - int armorCount = 0; - boolean first = true; - - String setID = null; - for (HashedArmorpiece armor : armors) { - Optional armorPiece = armor.getItem(); - if (!armorPiece.isPresent()) return false; - - if (armorPiece.get() instanceof CustomProtection) { - CustomProtection protectedArmor = (CustomProtection) armorPiece.get(); - - if (first) { - if (protectedArmor.requireFullSet()) setID = armorPiece.get().getSetID(); - first = false; - } - - for (CustomProtection.ProtectionType protectionType : protectedArmor.getProtectionTypes()) { - if (protectionType == CustomProtection.ProtectionType.RADIATION) { - if (setID == null) { - return true; - } - armorCount++; - } - } - - } - } - - return armorCount == 4; - } }