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

Refactored Talismans a bit

This commit is contained in:
TheBusyBiscuit 2020-05-10 10:31:28 +02:00
parent 71d13869a1
commit eacbd2c5c1
3 changed files with 80 additions and 50 deletions

View File

@ -26,6 +26,7 @@
#### Changes #### Changes
* Little performance improvements * Little performance improvements
* Bandages, Rags and Splints will no longer be consumed if your health is full and you are not on fire * Bandages, Rags and Splints will no longer be consumed if your health is full and you are not on fire
* Player Profiles (researches and stuff) are now loaded completely asynchronously
#### Fixes #### Fixes
* Fixed #1824 * Fixed #1824
@ -36,6 +37,7 @@
* Fixed Electric Smeltery not prioritisting recipes * Fixed Electric Smeltery not prioritisting recipes
* Fixed #1851 * Fixed #1851
* Fixed #1891 * Fixed #1891
* Fixed #1893
## Release Candidate 11 (25 Apr 2020) ## Release Candidate 11 (25 Apr 2020)

View File

@ -21,13 +21,8 @@ class EnderTalisman extends Talisman {
private static final LockedCategory ENDER_TALISMANS_CATEGORY = new LockedCategory(new NamespacedKey(SlimefunPlugin.instance, "ender_talismans"), new CustomItem(SlimefunItems.ENDER_TALISMAN, "&7Talismans - &aTier II"), 3, Talisman.TALISMANS_CATEGORY.getKey()); private static final LockedCategory ENDER_TALISMANS_CATEGORY = new LockedCategory(new NamespacedKey(SlimefunPlugin.instance, "ender_talismans"), new CustomItem(SlimefunItems.ENDER_TALISMAN, "&7Talismans - &aTier II"), 3, Talisman.TALISMANS_CATEGORY.getKey());
public EnderTalisman(Talisman parent) { public EnderTalisman(Talisman parent, SlimefunItemStack item) {
super(ENDER_TALISMANS_CATEGORY, parent.upgrade(), new ItemStack[] { SlimefunItems.ENDER_LUMP_3, null, SlimefunItems.ENDER_LUMP_3, null, parent.getItem(), null, SlimefunItems.ENDER_LUMP_3, null, SlimefunItems.ENDER_LUMP_3 }, parent.isConsumable(), parent.isEventCancelled(), parent.getSuffix(), parent.getChance(), parent.getEffects()); super(ENDER_TALISMANS_CATEGORY, item, new ItemStack[] { SlimefunItems.ENDER_LUMP_3, null, SlimefunItems.ENDER_LUMP_3, null, parent.getItem(), null, SlimefunItems.ENDER_LUMP_3, null, SlimefunItems.ENDER_LUMP_3 }, parent.isConsumable(), parent.isEventCancelled(), parent.getMessageSuffix(), parent.getChance(), parent.getEffects());
}
@Override
public SlimefunItemStack upgrade() {
throw new UnsupportedOperationException();
} }
@Override @Override

View File

@ -2,6 +2,7 @@ package io.github.thebusybiscuit.slimefun4.implementation.items.magical.talisman
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
@ -32,6 +33,8 @@ public class Talisman extends SlimefunItem {
protected static final Category TALISMANS_CATEGORY = new Category(new NamespacedKey(SlimefunPlugin.instance, "talismans"), new CustomItem(SlimefunItems.TALISMAN, "&7Talismans - &aTier I"), 2); protected static final Category TALISMANS_CATEGORY = new Category(new NamespacedKey(SlimefunPlugin.instance, "talismans"), new CustomItem(SlimefunItems.TALISMAN, "&7Talismans - &aTier I"), 2);
private final SlimefunItemStack enderTalisman;
protected final String suffix; protected final String suffix;
protected final boolean consumable; protected final boolean consumable;
protected final boolean cancel; protected final boolean cancel;
@ -58,43 +61,51 @@ public class Talisman extends SlimefunItem {
this.suffix = messageSuffix; this.suffix = messageSuffix;
this.effects = effects; this.effects = effects;
this.chance = chance; this.chance = chance;
}
public String getSuffix() { if (!(this instanceof EnderTalisman)) {
return suffix; String name = "&5Ender " + ChatColor.stripColor(getItem().getItemMeta().getDisplayName());
List<String> lore = new ArrayList<>();
lore.add("&7&oEnder Infused");
lore.add("");
for (String line : getItem().getItemMeta().getLore()) {
lore.add(line);
}
enderTalisman = new SlimefunItemStack("ENDER_" + getID(), getItem().getType(), name, lore.toArray(new String[0]));
}
else {
enderTalisman = null;
}
} }
public boolean isConsumable() { public boolean isConsumable() {
return consumable; return consumable;
} }
public boolean isEventCancelled() { public int getChance() {
return cancel; return chance;
} }
public PotionEffect[] getEffects() { public PotionEffect[] getEffects() {
return effects; return effects;
} }
public int getChance() { protected String getMessageSuffix() {
return chance; return suffix;
} }
public SlimefunItemStack upgrade() { protected boolean isEventCancelled() {
List<String> lore = new ArrayList<>(); return cancel;
lore.add("&7&oEnder Infused"); }
lore.add("");
for (String line : getItem().getItemMeta().getLore()) { private SlimefunItemStack getEnderVariant() {
lore.add(line); return enderTalisman;
}
return new SlimefunItemStack("ENDER_" + getID(), getItem().getType(), "&5Ender " + ChatColor.stripColor(getItem().getItemMeta().getDisplayName()), lore.toArray(new String[lore.size()]));
} }
@Override @Override
public void postRegister() { public void postRegister() {
EnderTalisman talisman = new EnderTalisman(this); EnderTalisman talisman = new EnderTalisman(this, getEnderVariant());
talisman.register(addon); talisman.register(addon);
} }
@ -105,16 +116,16 @@ public class Talisman extends SlimefunItem {
} }
protected void createEnderTalisman() { protected void createEnderTalisman() {
EnderTalisman talisman = (EnderTalisman) SlimefunItem.getByItem(upgrade()); EnderTalisman talisman = (EnderTalisman) SlimefunItem.getByItem(getEnderVariant());
Research research = Research.getByID(112); Optional<Research> research = Research.getResearch(new NamespacedKey(SlimefunPlugin.instance, "ender_talismans"));
if (talisman != null && research != null) { if (talisman != null && research.isPresent()) {
talisman.setResearch(research); talisman.setResearch(research.get());
} }
} }
private static boolean hasMessage(Talisman talisman) { private static boolean hasMessage(Talisman talisman) {
return !("").equalsIgnoreCase(talisman.getSuffix()); return !("").equalsIgnoreCase(talisman.getMessageSuffix());
} }
public static boolean checkFor(Event e, SlimefunItemStack stack) { public static boolean checkFor(Event e, SlimefunItemStack stack) {
@ -137,25 +148,37 @@ public class Talisman extends SlimefunItem {
return false; return false;
} }
if (p.getInventory().containsAtLeast(talisman.getItem(), 1)) { ItemStack talismanItem = talisman.getItem();
if (Slimefun.hasUnlocked(p, talisman.getItem(), true)) {
activateTalisman(e, p, p.getInventory(), talisman); if (p.getInventory().containsAtLeast(talismanItem, 1)) {
if (Slimefun.hasUnlocked(p, talismanItem, true)) {
activateTalisman(e, p, p.getInventory(), talisman, talismanItem);
return true; return true;
} }
else return false; else {
} return false;
else if (p.getEnderChest().containsAtLeast(talisman.upgrade(), 1)) { }
if (Slimefun.hasUnlocked(p, talisman.upgrade(), true)) { }
activateTalisman(e, p, p.getEnderChest(), talisman); else {
return true; ItemStack enderTalisman = talisman.getEnderVariant();
if (p.getEnderChest().containsAtLeast(enderTalisman, 1)) {
if (Slimefun.hasUnlocked(p, enderTalisman, true)) {
activateTalisman(e, p, p.getEnderChest(), talisman, enderTalisman);
return true;
}
else {
return false;
}
}
else {
return false;
} }
else return false;
} }
else return false;
} }
private static void activateTalisman(Event e, Player p, Inventory inv, Talisman talisman) { private static void activateTalisman(Event e, Player p, Inventory inv, Talisman talisman, ItemStack talismanItem) {
consumeItem(inv, talisman); consumeItem(inv, talisman, talismanItem);
applyTalismanEffects(p, talisman); applyTalismanEffects(p, talisman);
cancelEvent(e, talisman); cancelEvent(e, talisman);
sendMessage(p, talisman); sendMessage(p, talisman);
@ -175,22 +198,32 @@ public class Talisman extends SlimefunItem {
private static void sendMessage(Player p, Talisman talisman) { private static void sendMessage(Player p, Talisman talisman) {
if (hasMessage(talisman)) { if (hasMessage(talisman)) {
SlimefunPlugin.getLocal().sendMessage(p, "messages.talisman." + talisman.getSuffix(), true); SlimefunPlugin.getLocal().sendMessage(p, "messages.talisman." + talisman.getMessageSuffix(), true);
} }
} }
private static void consumeItem(Inventory inv, Talisman talisman) { private static void consumeItem(Inventory inv, Talisman talisman, ItemStack talismanItem) {
if (talisman.isConsumable()) { if (talisman.isConsumable()) {
inv.removeItem(talisman.getItem()); inv.removeItem(talismanItem);
} }
} }
private static Player getPlayerByEventType(Event e) { private static Player getPlayerByEventType(Event e) {
if (e instanceof EntityDeathEvent) return ((EntityDeathEvent) e).getEntity().getKiller(); if (e instanceof EntityDeathEvent) {
else if (e instanceof BlockBreakEvent) return ((BlockBreakEvent) e).getPlayer(); return ((EntityDeathEvent) e).getEntity().getKiller();
else if (e instanceof PlayerEvent) return ((PlayerEvent) e).getPlayer(); }
else if (e instanceof EntityEvent) return (Player) ((EntityEvent) e).getEntity(); else if (e instanceof BlockBreakEvent) {
else if (e instanceof EnchantItemEvent) return ((EnchantItemEvent) e).getEnchanter(); return ((BlockBreakEvent) e).getPlayer();
}
else if (e instanceof PlayerEvent) {
return ((PlayerEvent) e).getPlayer();
}
else if (e instanceof EntityEvent) {
return (Player) ((EntityEvent) e).getEntity();
}
else if (e instanceof EnchantItemEvent) {
return ((EnchantItemEvent) e).getEnchanter();
}
return null; return null;
} }