Skip to content

Commit

Permalink
Merge pull request #2 from BrendonCurmi/ISSUE1
Browse files Browse the repository at this point in the history
Fix ArcPlates fast click dupe glitch
  • Loading branch information
BrendonCurmi authored Apr 10, 2020
2 parents 9292de5 + f17b720 commit 43f8890
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 67 deletions.
20 changes: 20 additions & 0 deletions src/main/java/me/FusionDev/FusionPixelmon/apis/Time.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
99 changes: 55 additions & 44 deletions src/main/java/me/FusionDev/FusionPixelmon/guis/ArcPlates.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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!"));
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<PokemonSpec> getEvolutionsList(Pokemon pokemon) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
}

Expand Down

0 comments on commit 43f8890

Please sign in to comment.