mirror of
https://github.com/StarWishsama/Slimefun4.git
synced 2024-09-20 19:55:48 +00:00
Refactor (still build errors)
This commit is contained in:
parent
82716dd808
commit
7c030c736f
@ -135,99 +135,61 @@ public class Talisman extends SlimefunItem {
|
|||||||
return talisman.getMessageSuffix() != null;
|
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());
|
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)) {
|
if (!(item instanceof Talisman)) {
|
||||||
return false;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
Talisman talisman = (Talisman) item;
|
Talisman talisman = (Talisman) item;
|
||||||
if (ThreadLocalRandom.current().nextInt(100) > talisman.getChance()) {
|
if (ThreadLocalRandom.current().nextInt(100) > talisman.getChance()) {
|
||||||
return false;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
Player p = getPlayerByEventType(e);
|
Player p = getPlayerByEventType(e);
|
||||||
if (p == null || !pass(p, talisman)) {
|
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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
Talisman talisman = (Talisman) sfItem;
|
|
||||||
|
|
||||||
ItemStack talismanItem = talisman.getItem();
|
ItemStack talismanItem = talisman.getItem();
|
||||||
ItemStack enderTalisman = talisman.getEnderVariant();
|
ItemStack enderTalisman = talisman.getEnderVariant();
|
||||||
|
|
||||||
if (SlimefunUtils.containsSimilarItem(player.getInventory(), talismanItem, true)) {
|
if (!Slimefun.hasUnlocked(p, talisman, true)) {
|
||||||
ItemStack[] contents = player.getInventory().getContents();
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < contents.length; i++) {
|
if (SlimefunUtils.containsSimilarItem(p.getInventory(), talismanItem, true)) {
|
||||||
ItemStack item = contents[i];
|
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 null;
|
||||||
return item;
|
}
|
||||||
}
|
|
||||||
|
@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++) {
|
/* The point of this method is that it only gets called when youre sure there is
|
||||||
ItemStack item = contents[i];
|
* 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
|
||||||
if (SlimefunUtils.isItemSimilar(item, talisman.getItem(), true, false)) {
|
* to return something */
|
||||||
return item;
|
return null;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void activateTalisman(Event e, Player p, Inventory inv, Talisman talisman) {
|
private static void activateTalisman(Event e, Player p, Inventory inv, Talisman talisman) {
|
||||||
|
@ -314,30 +314,28 @@ 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);
|
||||||
|
|
||||||
if (dmgCause == DamageCause.VOID && Talisman.checkFor(e, SlimefunItems.TALISMAN_RESURRECTED)) {
|
if (dmgCause == DamageCause.VOID && talisman != null) {
|
||||||
SlimefunPlugin.runSync(() -> {
|
SlimefunPlugin.runSync(() -> {
|
||||||
PaperLib.teleportAsync(player, getSafeRespawnLocation(player));
|
PaperLib.teleportAsync(player, getSafeRespawnLocation(talisman, player));
|
||||||
}, 2);
|
}, 2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
private Location getSafeRespawnLocation(@Nonnull Player player) {
|
private Location getSafeRespawnLocation(@Nonnull ItemStack item, @Nonnull Player player) {
|
||||||
ItemStack item = Talisman.checkForAndGet(SlimefunItems.TALISMAN_RESURRECTED, player);
|
SlimefunItem sfItem = SlimefunItem.getByItem(item);
|
||||||
SlimefunItem sfItem = ResurrectedTalisman.getByItem(item);
|
|
||||||
|
|
||||||
if (sfItem instanceof ResurrectedTalisman) {
|
if (sfItem instanceof ResurrectedTalisman) {
|
||||||
ResurrectedTalisman talisman = (ResurrectedTalisman) sfItem;
|
ResurrectedTalisman talisman = (ResurrectedTalisman) sfItem;
|
||||||
|
|
||||||
if (item != null) {
|
Location savedLoc = talisman.getSavedLocation(item);
|
||||||
Location savedLoc = talisman.getSavedLocation(item);
|
|
||||||
|
|
||||||
if (savedLoc != null) {
|
if (savedLoc != null) {
|
||||||
return savedLoc;
|
return savedLoc;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Location bedSpawn = player.getBedSpawnLocation();
|
Location bedSpawn = player.getBedSpawnLocation();
|
||||||
return (bedSpawn != null) ? bedSpawn : player.getLocation().getWorld().getSpawnLocation();
|
return (bedSpawn != null) ? bedSpawn : player.getLocation().getWorld().getSpawnLocation();
|
||||||
|
Loading…
Reference in New Issue
Block a user