From 7c030c736f495c11ad1a96eebbf6ebc7e8ad7902 Mon Sep 17 00:00:00 2001 From: svr333 Date: Thu, 8 Oct 2020 18:21:00 +0200 Subject: [PATCH] Refactor (still build errors) --- .../items/magical/talismans/Talisman.java | 102 ++++++------------ .../listeners/TalismanListener.java | 22 ++-- 2 files changed, 42 insertions(+), 82 deletions(-) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/magical/talismans/Talisman.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/magical/talismans/Talisman.java index 94cbd0765..893a441f5 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/magical/talismans/Talisman.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/magical/talismans/Talisman.java @@ -135,99 +135,61 @@ public class Talisman extends SlimefunItem { return talisman.getMessageSuffix() != null; } - public static boolean checkFor(Event e, SlimefunItemStack stack) { + public static ItemStack checkFor(@Nonnull Event e, @Nonnull SlimefunItemStack stack) { return checkFor(e, stack.getItem()); } - public static boolean checkFor(Event e, SlimefunItem item) { + @Nullable + public static ItemStack checkFor(@Nonnull Event e, @Nonnull SlimefunItem item) { if (!(item instanceof Talisman)) { - return false; + return null; } Talisman talisman = (Talisman) item; if (ThreadLocalRandom.current().nextInt(100) > talisman.getChance()) { - return false; + return null; } Player p = getPlayerByEventType(e); if (p == null || !pass(p, talisman)) { - return false; - } - - ItemStack talismanItem = talisman.getItem(); - - if (SlimefunUtils.containsSimilarItem(p.getInventory(), talismanItem, true)) { - if (Slimefun.hasUnlocked(p, talisman, true)) { - activateTalisman(e, p, p.getInventory(), talisman); - return true; - } - else { - return false; - } - } - else { - ItemStack enderTalisman = talisman.getEnderVariant(); - - if (SlimefunUtils.containsSimilarItem(p.getEnderChest(), enderTalisman, true)) { - if (Slimefun.hasUnlocked(p, talisman, true)) { - activateTalisman(e, p, p.getEnderChest(), talisman); - return true; - } - else { - return false; - } - } - else { - return false; - } - } - } - - @Nullable - public static ItemStack checkForAndGet(@Nonnull SlimefunItemStack stack, @Nonnull Player player) { - return checkForAndGet(stack.getItem(), player); - } - - @Nullable - public static ItemStack checkForAndGet(@Nonnull SlimefunItem sfItem, @Nonnull Player player) { - if (!(sfItem instanceof Talisman)) { return null; } - Talisman talisman = (Talisman) sfItem; - ItemStack talismanItem = talisman.getItem(); ItemStack enderTalisman = talisman.getEnderVariant(); - if (SlimefunUtils.containsSimilarItem(player.getInventory(), talismanItem, true)) { - ItemStack[] contents = player.getInventory().getContents(); + if (!Slimefun.hasUnlocked(p, talisman, true)) { + return null; + } - for (int i = 0; i < contents.length; i++) { - ItemStack item = contents[i]; + if (SlimefunUtils.containsSimilarItem(p.getInventory(), talismanItem, true)) { + activateTalisman(e, p, p.getInventory(), talisman); + return retrieveTalismanFromInventory(p.getInventory(), talisman); + } + else if (SlimefunUtils.containsSimilarItem(p.getEnderChest(), enderTalisman, true)) { + activateTalisman(e, p, p.getEnderChest(), talisman); + return retrieveTalismanFromInventory(p.getEnderChest(), talisman); + } - if (SlimefunUtils.isItemSimilar(item, talisman.getItem(), true, false)) { - return item; - } + return null; + } + + @Nullable + private static ItemStack retrieveTalismanFromInventory(@Nonnull Inventory inv, @Nonnull Talisman talisman) { + ItemStack[] contents = inv.getContents(); + for (int i = 0; i < contents.length; i++) { + ItemStack item = contents[i]; + + if (SlimefunUtils.isItemSimilar(item, talisman.getItem(), true, false)) { + return item; } - - return null; } - else if (SlimefunUtils.containsSimilarItem(player.getEnderChest(), enderTalisman, true)) { - ItemStack[] contents = player.getEnderChest().getContents(); - for (int i = 0; i < contents.length; i++) { - ItemStack item = contents[i]; - - if (SlimefunUtils.isItemSimilar(item, talisman.getItem(), true, false)) { - return item; - } - } - - return null; - } - else { - return null; - } + /* The point of this method is that it only gets called when youre sure there is + * a talisman in the inventory, so that it never returns null. + * However, this method will be nullable since there is an if statement and it needs + * to return something */ + return null; } private static void activateTalisman(Event e, Player p, Inventory inv, Talisman talisman) { diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/TalismanListener.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/TalismanListener.java index 95675bb69..a8da01485 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/TalismanListener.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/TalismanListener.java @@ -314,30 +314,28 @@ public class TalismanListener implements Listener { public void onPlayerDeath(PlayerDeathEvent e) { Player player = e.getEntity(); DamageCause dmgCause = player.getLastDamageCause().getCause(); + ItemStack talisman = Talisman.checkFor(e, SlimefunItems.TALISMAN_RESURRECTED); - if (dmgCause == DamageCause.VOID && Talisman.checkFor(e, SlimefunItems.TALISMAN_RESURRECTED)) { + if (dmgCause == DamageCause.VOID && talisman != null) { SlimefunPlugin.runSync(() -> { - PaperLib.teleportAsync(player, getSafeRespawnLocation(player)); + PaperLib.teleportAsync(player, getSafeRespawnLocation(talisman, player)); }, 2); } } @Nonnull - private Location getSafeRespawnLocation(@Nonnull Player player) { - ItemStack item = Talisman.checkForAndGet(SlimefunItems.TALISMAN_RESURRECTED, player); - SlimefunItem sfItem = ResurrectedTalisman.getByItem(item); + private Location getSafeRespawnLocation(@Nonnull ItemStack item, @Nonnull Player player) { + SlimefunItem sfItem = SlimefunItem.getByItem(item); if (sfItem instanceof ResurrectedTalisman) { ResurrectedTalisman talisman = (ResurrectedTalisman) sfItem; - if (item != null) { - Location savedLoc = talisman.getSavedLocation(item); + Location savedLoc = talisman.getSavedLocation(item); - if (savedLoc != null) { - return savedLoc; - } - } - } + if (savedLoc != null) { + return savedLoc; + } + } Location bedSpawn = player.getBedSpawnLocation(); return (bedSpawn != null) ? bedSpawn : player.getLocation().getWorld().getSpawnLocation();