diff --git a/src/main/java/me/FusionDev/FusionPixelmon/apis/Time.java b/src/main/java/me/FusionDev/FusionPixelmon/apis/Time.java new file mode 100644 index 0000000..b42dcd1 --- /dev/null +++ b/src/main/java/me/FusionDev/FusionPixelmon/apis/Time.java @@ -0,0 +1,20 @@ +package me.FusionDev.FusionPixelmon.apis; + +public class Time { + + /** + * Asynchronously executes the specified runnable a time 'millis' milliseconds later. + * @param runnable the runnable. + * @param millis the delay in milliseconds before executing. + */ + public static void setTimeout(Runnable runnable, int millis) { + new Thread(() -> { + try { + Thread.sleep(millis); + runnable.run(); + } catch (InterruptedException ex) { + ex.printStackTrace(); + } + }).start(); + } +} diff --git a/src/main/java/me/FusionDev/FusionPixelmon/guis/ArcPlates.java b/src/main/java/me/FusionDev/FusionPixelmon/guis/ArcPlates.java index fb88f62..c63c2ba 100644 --- a/src/main/java/me/FusionDev/FusionPixelmon/guis/ArcPlates.java +++ b/src/main/java/me/FusionDev/FusionPixelmon/guis/ArcPlates.java @@ -8,6 +8,7 @@ import me.FusionDev.FusionPixelmon.apis.FileFactory; import me.FusionDev.FusionPixelmon.FusionPixelmon; import me.FusionDev.FusionPixelmon.apis.Grammar; +import me.FusionDev.FusionPixelmon.apis.Time; import me.FusionDev.FusionPixelmon.data.ArcStorageData; import me.FusionDev.FusionPixelmon.inventory.InvInventory; import me.FusionDev.FusionPixelmon.inventory.InvItem; @@ -51,6 +52,9 @@ public class ArcPlates { private static final int ROWS = 5; private static final int[] BACKGROUND_SLOTS = {0, 1, 9, 10, 19, 27, 28, 36, 37}; + // Cooldown variable to prevent duping + private boolean enabled = true; + /** * Launches the Arc Plates Storage interface for the specified * Player and Pokemon. Only an Arceus Pokemon should be passed @@ -117,54 +121,61 @@ public void launch(Player player, Pokemon pokemon) { else if (selected.getType() instanceof ItemPlate) { ItemPlate selectedItemPlate = (ItemPlate) selected.getType(); - // Left clicking plate in GUI - if (event instanceof ClickInventoryEvent.Primary) { - /* - * Give the pokemon the plate that is left clicked in the GUI. - * If the pokemon is already holding a plate (but different type), put it in storage and give - * the pokemon the new clicked plate. - * If the pokemon is already holding a plate but there is another one of the same type in - * storage, do nothing as player must remove the one in storage first. - * If the pokemon is already holding something which isnt a plate, do nothing as player must - * remove that first. - */ - if (pokemon.getHeldItemAsItemHeld() instanceof NoItem || pokemon.getHeldItemAsItemHeld() instanceof ItemPlate) { - if (pokemon.getHeldItemAsItemHeld() instanceof ItemPlate) { - ItemPlate heldItemPlate = (ItemPlate) pokemon.getHeldItemAsItemHeld(); - if (selectedItemPlate == heldItemPlate) { - player.sendMessage(Text.of(TextColors.RED, "That Plate is already equipped!")); - return; - } else { - ItemType heldItemType = getType(Objects.requireNonNull(heldItemPlate.getRegistryName())); - for (Plate p : Plate.values()) { - if (getType(Objects.requireNonNull(p.plate.getItem().getRegistryName())) == heldItemType) { - if (data.get(p.i) == null) { - data.set(p.i, ItemStack.builder().itemType(heldItemType).build()); - break; - } else { - player.sendMessage(Text.of(TextColors.RED, "Cant unequip " + Grammar.cap(p.name()) + " Plate because there is another in Storage! Please remove the one in Storage first before unequiping.")); - return; + if (enabled) { + // Delay to prevent duping + enabled = false; + Time.setTimeout(() -> enabled = true, 700); + + // Left clicking plate in GUI + if (event instanceof ClickInventoryEvent.Primary) { + /* + * Give the pokemon the plate that is left clicked in the GUI. + * If the pokemon is already holding a plate (but different type), put it in storage and give + * the pokemon the new clicked plate. + * If the pokemon is already holding a plate but there is another one of the same type in + * storage, do nothing as player must remove the one in storage first. + * If the pokemon is already holding something which isnt a plate, do nothing as player must + * remove that first. + */ + if (pokemon.getHeldItemAsItemHeld() instanceof NoItem || pokemon.getHeldItemAsItemHeld() instanceof ItemPlate) { + if (pokemon.getHeldItemAsItemHeld() instanceof ItemPlate) { + ItemPlate heldItemPlate = (ItemPlate) pokemon.getHeldItemAsItemHeld(); + if (selectedItemPlate == heldItemPlate) { + player.sendMessage(Text.of(TextColors.RED, "That Plate is already equipped!")); + return; + } else { + ItemType heldItemType = getType(Objects.requireNonNull(heldItemPlate.getRegistryName())); + for (Plate p : Plate.values()) { + if (getType(Objects.requireNonNull(p.plate.getItem().getRegistryName())) == heldItemType) { + if (data.get(p.i) == null) { + data.set(p.i, ItemStack.builder().itemType(heldItemType).build()); + break; + } else { + player.sendMessage(Text.of(TextColors.RED, "Cant unequip " + Grammar.cap(p.name()) + " Plate because there is another in Storage! Please remove the one in Storage first before unequiping.")); + return; + } } } } } - } - pokemon.setHeldItem(new net.minecraft.item.ItemStack(selectedItemPlate)); - data.set(getIDFromSlot(slot), null); - player.sendMessage(Text.of(TextColors.GREEN, "Plate equipped!")); - } else player.sendMessage(Text.of(TextColors.RED, "Cannot equip Plate because Pokemon is currently holding something!")); - } - // Right clicking plate in GUI - else if (event instanceof ClickInventoryEvent.Secondary) { - /* - * Take the plate that is right clicked in the GUI and give it to the player if there is free - * inventory space. - */ - PlayerInventory playerInv = (PlayerInventory) player.getInventory(); - if (playerInv.getMainGrid().canFit(selected)) { - player.getInventory().offer(selected); - data.set(getIDFromSlot(slot), null); - } else player.sendMessage(Text.of(TextColors.RED, "Your inventory is full!")); + pokemon.setHeldItem(new net.minecraft.item.ItemStack(selectedItemPlate)); + data.set(getIDFromSlot(slot), null); + player.sendMessage(Text.of(TextColors.GREEN, "Plate equipped!")); + } else + player.sendMessage(Text.of(TextColors.RED, "Cannot equip Plate because Pokemon is currently holding something!")); + } + // Right clicking plate in GUI + else if (event instanceof ClickInventoryEvent.Secondary) { + /* + * Take the plate that is right clicked in the GUI and give it to the player if there is free + * inventory space. + */ + PlayerInventory playerInv = (PlayerInventory) player.getInventory(); + if (playerInv.getMainGrid().canFit(selected)) { + player.getInventory().offer(selected); + data.set(getIDFromSlot(slot), null); + } else player.sendMessage(Text.of(TextColors.RED, "Your inventory is full!")); + } } } } diff --git a/src/main/java/me/FusionDev/FusionPixelmon/guis/shops/EvolutionShop.java b/src/main/java/me/FusionDev/FusionPixelmon/guis/shops/EvolutionShop.java index e838c0f..382308e 100644 --- a/src/main/java/me/FusionDev/FusionPixelmon/guis/shops/EvolutionShop.java +++ b/src/main/java/me/FusionDev/FusionPixelmon/guis/shops/EvolutionShop.java @@ -4,6 +4,7 @@ import com.pixelmonmod.pixelmon.api.pokemon.PokemonSpec; import com.pixelmonmod.pixelmon.entities.pixelmon.stats.evolution.Evolution; import com.pixelmonmod.pixelmon.enums.EnumSpecies; +import me.FusionDev.FusionPixelmon.apis.Time; import me.FusionDev.FusionPixelmon.inventory.InvItem; import me.FusionDev.FusionPixelmon.inventory.InvPage; import me.FusionDev.FusionPixelmon.pixelmon.PixelmonAPI; @@ -62,14 +63,7 @@ protected void priceSummaries() { @Override public void purchaseAction(Object value) { PokemonSpecWrapper evolve = (PokemonSpecWrapper) value; - new Thread(() -> { - try { - Thread.sleep(1000); - } catch (InterruptedException ex) { - ex.printStackTrace(); - } - shops.pokemon.evolve(evolve.pokemonSpec); - }).start(); + Time.setTimeout(() -> shops.pokemon.evolve(evolve.pokemonSpec), 1000); } private List getEvolutionsList(Pokemon pokemon) { diff --git a/src/main/java/me/FusionDev/FusionPixelmon/guis/shops/LevelShop.java b/src/main/java/me/FusionDev/FusionPixelmon/guis/shops/LevelShop.java index 33ffbee..8b24ce5 100644 --- a/src/main/java/me/FusionDev/FusionPixelmon/guis/shops/LevelShop.java +++ b/src/main/java/me/FusionDev/FusionPixelmon/guis/shops/LevelShop.java @@ -1,6 +1,7 @@ package me.FusionDev.FusionPixelmon.guis.shops; import com.pixelmonmod.pixelmon.api.enums.ExperienceGainType; +import me.FusionDev.FusionPixelmon.apis.Time; import me.FusionDev.FusionPixelmon.inventory.InvItem; import me.FusionDev.FusionPixelmon.inventory.InvPage; import org.spongepowered.api.data.key.Keys; @@ -118,15 +119,10 @@ protected void priceSummaries() { public void purchaseAction(Object value) { int levels = (int) value; if (levels > 0) { - new Thread(() -> { - try { - Thread.sleep(1000); - } catch (InterruptedException ex) { - ex.printStackTrace(); - } + Time.setTimeout(() -> { for (int i = 1; i <= levels; i++) shops.pokemon.getLevelContainer().awardEXP(shops.pokemon.getExperienceToLevelUp(), ExperienceGainType.BATTLE); - }).start(); + } ,1000); } else shops.pokemon.setLevel(shops.pokemon.getLevel() + levels); } } diff --git a/src/main/java/me/FusionDev/FusionPixelmon/pixelmon/PixelmonEvents.java b/src/main/java/me/FusionDev/FusionPixelmon/pixelmon/PixelmonEvents.java index 4bd61af..4aeea5d 100644 --- a/src/main/java/me/FusionDev/FusionPixelmon/pixelmon/PixelmonEvents.java +++ b/src/main/java/me/FusionDev/FusionPixelmon/pixelmon/PixelmonEvents.java @@ -1,6 +1,7 @@ package me.FusionDev.FusionPixelmon.pixelmon; import com.pixelmonmod.pixelmon.api.events.BeatWildPixelmonEvent; +import me.FusionDev.FusionPixelmon.apis.Time; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraftforge.event.entity.living.LivingFallEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; @@ -24,14 +25,7 @@ public class PixelmonEvents { public void onBeatPokemon(BeatWildPixelmonEvent event) { if (!invulnerableFall.contains(event.player)) { invulnerableFall.add(event.player); - new Thread(() -> { - try { - Thread.sleep(FALL_INVULNERABILITY * 1000); - } catch (InterruptedException ex) { - ex.printStackTrace(); - } - invulnerableFall.remove(event.player); - }).start(); + Time.setTimeout(() -> invulnerableFall.remove(event.player), FALL_INVULNERABILITY * 1000); } }