1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-20 19:55:48 +00:00

Refactor and rename methods

This commit is contained in:
svr333 2020-10-12 00:19:19 +02:00
parent bbf5236bd6
commit 125239f173
2 changed files with 39 additions and 36 deletions

View File

@ -134,12 +134,20 @@ public class Talisman extends SlimefunItem {
return talisman.getMessageSuffix() != null; return talisman.getMessageSuffix() != null;
} }
public static ItemStack checkFor(@Nonnull Event e, @Nonnull SlimefunItemStack stack) { public static boolean tryActivate(@Nonnull Event e, @Nonnull SlimefunItemStack stack) {
return checkFor(e, stack.getItem()); return (tryActivateAndGet(e, stack.getItem()) != null);
}
public static boolean tryActivate(@Nonnull Event e, @Nonnull SlimefunItem item) {
return (tryActivateAndGet(e, item) != null);
}
public static ItemStack tryActivateAndGet(@Nonnull Event e, @Nonnull SlimefunItemStack stack) {
return tryActivateAndGet(e, stack.getItem());
} }
@Nullable @Nullable
public static ItemStack checkFor(@Nonnull Event e, @Nonnull SlimefunItem item) { public static ItemStack tryActivateAndGet(@Nonnull Event e, @Nonnull SlimefunItem item) {
if (!(item instanceof Talisman)) { if (!(item instanceof Talisman)) {
return null; return null;
} }
@ -154,23 +162,25 @@ public class Talisman extends SlimefunItem {
return null; return null;
} }
ItemStack talismanItem = talisman.getItem();
ItemStack enderTalisman = talisman.getEnderVariant();
if (!Slimefun.hasUnlocked(p, talisman, true)) { if (!Slimefun.hasUnlocked(p, talisman, true)) {
return null; return null;
} }
if (SlimefunUtils.containsSimilarItem(p.getInventory(), talismanItem, true)) { ItemStack possibleTalisman = retrieveTalismanFromInventory(p.getInventory(), talisman);
if (possibleTalisman != null) {
activateTalisman(e, p, p.getInventory(), talisman); activateTalisman(e, p, p.getInventory(), talisman);
return retrieveTalismanFromInventory(p.getInventory(), talisman); return possibleTalisman;
}
else if (SlimefunUtils.containsSimilarItem(p.getEnderChest(), enderTalisman, true)) {
activateTalisman(e, p, p.getEnderChest(), talisman);
return retrieveTalismanFromInventory(p.getEnderChest(), talisman);
} }
return null; possibleTalisman = retrieveTalismanFromInventory(p.getEnderChest(), talisman);
if (possibleTalisman != null) {
activateTalisman(e, p, p.getEnderChest(), talisman);
return possibleTalisman;
}
return possibleTalisman;
} }
@Nullable @Nullable

View File

@ -70,24 +70,24 @@ public class TalismanListener implements Listener {
public void onDamageGet(EntityDamageEvent e) { public void onDamageGet(EntityDamageEvent e) {
if (e.getEntity() instanceof Player) { if (e.getEntity() instanceof Player) {
if (e.getCause() == DamageCause.LAVA) { if (e.getCause() == DamageCause.LAVA) {
Talisman.checkFor(e, SlimefunItems.TALISMAN_LAVA); Talisman.tryActivate(e, SlimefunItems.TALISMAN_LAVA);
} }
if (e.getCause() == DamageCause.DROWNING) { if (e.getCause() == DamageCause.DROWNING) {
Talisman.checkFor(e, SlimefunItems.TALISMAN_WATER); Talisman.tryActivate(e, SlimefunItems.TALISMAN_WATER);
} }
if (e.getCause() == DamageCause.FALL) { if (e.getCause() == DamageCause.FALL) {
Talisman.checkFor(e, SlimefunItems.TALISMAN_ANGEL); Talisman.tryActivate(e, SlimefunItems.TALISMAN_ANGEL);
} }
if (e.getCause() == DamageCause.FIRE) { if (e.getCause() == DamageCause.FIRE) {
Talisman.checkFor(e, SlimefunItems.TALISMAN_FIRE); Talisman.tryActivate(e, SlimefunItems.TALISMAN_FIRE);
} }
if (e.getCause() == DamageCause.ENTITY_ATTACK) { if (e.getCause() == DamageCause.ENTITY_ATTACK) {
Talisman.checkFor(e, SlimefunItems.TALISMAN_KNIGHT); Talisman.tryActivate(e, SlimefunItems.TALISMAN_KNIGHT);
Talisman.checkFor(e, SlimefunItems.TALISMAN_WARRIOR); Talisman.tryActivate(e, SlimefunItems.TALISMAN_WARRIOR);
} }
if (e.getCause() == DamageCause.PROJECTILE && e instanceof EntityDamageByEntityEvent) { if (e.getCause() == DamageCause.PROJECTILE && e instanceof EntityDamageByEntityEvent) {
@ -100,8 +100,7 @@ public class TalismanListener implements Listener {
if (e.getDamager() instanceof Projectile && !(e.getDamager() instanceof Trident)) { if (e.getDamager() instanceof Projectile && !(e.getDamager() instanceof Trident)) {
Projectile projectile = (Projectile) e.getDamager(); Projectile projectile = (Projectile) e.getDamager();
ItemStack possibleTalisman = Talisman.checkFor(e, SlimefunItems.TALISMAN_WHIRLWIND); if (Talisman.tryActivate(e, SlimefunItems.TALISMAN_WHIRLWIND)) {
if (possibleTalisman != null) {
Player p = (Player) e.getEntity(); Player p = (Player) e.getEntity();
returnProjectile(p, projectile); returnProjectile(p, projectile);
} }
@ -153,8 +152,7 @@ public class TalismanListener implements Listener {
// We are also excluding entities which can pickup items, this is not perfect // We are also excluding entities which can pickup items, this is not perfect
// but it at least prevents dupes by tossing items to zombies // but it at least prevents dupes by tossing items to zombies
ItemStack possibleTalisman = Talisman.checkFor(e, SlimefunItems.TALISMAN_HUNTER); if (!entity.getCanPickupItems() && Talisman.tryActivate(e, SlimefunItems.TALISMAN_HUNTER)) {
if (!entity.getCanPickupItems() && possibleTalisman != null) {
Collection<ItemStack> extraDrops = getExtraDrops(e.getEntity(), e.getDrops()); Collection<ItemStack> extraDrops = getExtraDrops(e.getEntity(), e.getDrops());
for (ItemStack drop : extraDrops) { for (ItemStack drop : extraDrops) {
@ -203,8 +201,7 @@ public class TalismanListener implements Listener {
@EventHandler @EventHandler
public void onItemBreak(PlayerItemBreakEvent e) { public void onItemBreak(PlayerItemBreakEvent e) {
ItemStack possibleTalisman = Talisman.checkFor(e, SlimefunItems.TALISMAN_ANVIL); if (Talisman.tryActivate(e, SlimefunItems.TALISMAN_ANVIL)) {
if (possibleTalisman != null) {
PlayerInventory inv = e.getPlayer().getInventory(); PlayerInventory inv = e.getPlayer().getInventory();
int slot = inv.getHeldItemSlot(); int slot = inv.getHeldItemSlot();
@ -237,16 +234,15 @@ public class TalismanListener implements Listener {
@EventHandler @EventHandler
public void onSprint(PlayerToggleSprintEvent e) { public void onSprint(PlayerToggleSprintEvent e) {
if (e.isSprinting()) { if (e.isSprinting()) {
Talisman.checkFor(e, SlimefunItems.TALISMAN_TRAVELLER); Talisman.tryActivate(e, SlimefunItems.TALISMAN_TRAVELLER);
} }
} }
@EventHandler @EventHandler
public void onEnchant(EnchantItemEvent e) { public void onEnchant(EnchantItemEvent e) {
Random random = ThreadLocalRandom.current(); Random random = ThreadLocalRandom.current();
ItemStack possibleTalisman = Talisman.checkFor(e, SlimefunItems.TALISMAN_MAGICIAN);
if (possibleTalisman != null) { if (Talisman.tryActivate(e, SlimefunItems.TALISMAN_MAGICIAN)) {
MagicianTalisman talisman = (MagicianTalisman) SlimefunItems.TALISMAN_MAGICIAN.getItem(); MagicianTalisman talisman = (MagicianTalisman) SlimefunItems.TALISMAN_MAGICIAN.getItem();
TalismanEnchantment enchantment = talisman.getRandomEnchantment(e.getItem()); TalismanEnchantment enchantment = talisman.getRandomEnchantment(e.getItem());
@ -254,11 +250,10 @@ public class TalismanListener implements Listener {
e.getEnchantsToAdd().put(enchantment.getEnchantment(), enchantment.getLevel()); e.getEnchantsToAdd().put(enchantment.getEnchantment(), enchantment.getLevel());
} }
} }
ItemStack possibleWizardTalisman = Talisman.checkFor(e, SlimefunItems.TALISMAN_WIZARD);
if (!e.getEnchantsToAdd().containsKey(Enchantment.SILK_TOUCH) if (!e.getEnchantsToAdd().containsKey(Enchantment.SILK_TOUCH)
&& Enchantment.LOOT_BONUS_BLOCKS.canEnchantItem(e.getItem()) && Enchantment.LOOT_BONUS_BLOCKS.canEnchantItem(e.getItem())
&& possibleWizardTalisman != null) { && Talisman.tryActivate(e, SlimefunItems.TALISMAN_WIZARD)) {
Set<Enchantment> enchantments = e.getEnchantsToAdd().keySet(); Set<Enchantment> enchantments = e.getEnchantsToAdd().keySet();
for (Enchantment enchantment : enchantments) { for (Enchantment enchantment : enchantments) {
@ -281,8 +276,7 @@ public class TalismanListener implements Listener {
if (item.getType() != Material.AIR && item.getAmount() > 0 && !item.containsEnchantment(Enchantment.SILK_TOUCH)) { if (item.getType() != Material.AIR && item.getAmount() > 0 && !item.containsEnchantment(Enchantment.SILK_TOUCH)) {
Collection<Item> drops = e.getItems(); Collection<Item> drops = e.getItems();
ItemStack possibleTalisman = Talisman.checkFor(e, SlimefunItems.TALISMAN_MINER); if (Talisman.tryActivate(e, SlimefunItems.TALISMAN_MINER)) {
if (possibleTalisman != null) {
int dropAmount = getAmountWithFortune(type, item.getEnchantmentLevel(Enchantment.LOOT_BONUS_BLOCKS)); int dropAmount = getAmountWithFortune(type, item.getEnchantmentLevel(Enchantment.LOOT_BONUS_BLOCKS));
boolean doubledDrops = false; boolean doubledDrops = false;
@ -307,14 +301,13 @@ public class TalismanListener implements Listener {
@EventHandler @EventHandler
public void onBlockBreak(BlockBreakEvent e) { public void onBlockBreak(BlockBreakEvent e) {
if (e.getBlock().getType().name().endsWith("_ORE")) { if (e.getBlock().getType().name().endsWith("_ORE")) {
Talisman.checkFor(e, SlimefunItems.TALISMAN_CAVEMAN); Talisman.tryActivate(e, SlimefunItems.TALISMAN_CAVEMAN);
} }
} }
@EventHandler @EventHandler
public void onExperienceReceive(PlayerExpChangeEvent e) { public void onExperienceReceive(PlayerExpChangeEvent e) {
ItemStack possibleTalisman = Talisman.checkFor(e, SlimefunItems.TALISMAN_WISE); if (e.getAmount() > 0 && Talisman.tryActivate(e, SlimefunItems.TALISMAN_WISE)) {
if (e.getAmount() > 0 && possibleTalisman != null) {
e.setAmount(e.getAmount() * 2); e.setAmount(e.getAmount() * 2);
} }
} }
@ -323,7 +316,7 @@ public class TalismanListener implements Listener {
public void onPlayerDeath(PlayerDeathEvent e) { public void onPlayerDeath(PlayerDeathEvent e) {
Player player = e.getEntity(); Player player = e.getEntity();
DamageCause dmgCause = player.getLastDamageCause().getCause(); DamageCause dmgCause = player.getLastDamageCause().getCause();
ItemStack talisman = Talisman.checkFor(e, SlimefunItems.TALISMAN_RESURRECTED); ItemStack talisman = Talisman.tryActivateAndGet(e, SlimefunItems.TALISMAN_RESURRECTED);
if (dmgCause == DamageCause.VOID && talisman != null) { if (dmgCause == DamageCause.VOID && talisman != null) {
SlimefunPlugin.runSync(() -> { SlimefunPlugin.runSync(() -> {