Skip to content

Commit

Permalink
Merge pull request #19 from Ivan8or/fix-autocraft-dupe
Browse files Browse the repository at this point in the history
Fix autocraft not working in certain cases
  • Loading branch information
Ivan8or committed Jan 7, 2022
2 parents 555737c + 569bb59 commit bbdd029
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 21 deletions.
18 changes: 8 additions & 10 deletions src/main/java/online/umbcraft/libraries/dupes/AutocraftDupe.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ public void onCraftItem(final PrepareItemCraftEvent e) {
}


// increment the number of extra items by 2
// increment the number of extra items
final int currentAmnt = dupeAmnt.getOrDefault(playerUUID, 0);
dupeAmnt.put(playerUUID, currentAmnt + 2);
dupeAmnt.put(playerUUID, currentAmnt+1);


// prolongs the time until the extra items reset to 0 by one second
Expand Down Expand Up @@ -124,29 +124,27 @@ public void onPlayerLeave(final PlayerQuitEvent e) {


// gives the player the extra items once they pick up after performing the dupe
@EventHandler(priority = EventPriority.HIGH)
@EventHandler(priority = EventPriority.LOW)
public void onItemPickup(final EntityPickupItemEvent e) {

// players who didn't do the dupe / other entities are not affected
if (!dupeAmnt.containsKey(e.getEntity().getUniqueId())) {
return;
}

final Player p = (Player) e.getEntity();
final ItemStack toDupe = e.getItem().getItemStack();

final Player p = (Player) e.getEntity();

// set stacksize to 64 if vanilla, get correct size otherwise
final int stacksize = (plugin.getConfig().getBoolean(ConfigPath.AUTOCRAFT_VANILLA.path()))
? 64 : decideAmount(toDupe.getType(), p.getUniqueId());

final ItemStack duped = dupe(toDupe, stacksize);
p.getInventory().addItem(duped);

if (stacksize != 1) { // this is a silly way to do it, but spigot api is poopy
e.getItem().remove();
e.setCancelled(true);
}
//e.getItem().setItemStack(duped);
e.getItem().setItemStack(null);
e.setCancelled(true);
p.getInventory().addItem(duped);

// player only tries to dupe the first item he picks up
dupeAmnt.remove(p.getUniqueId());
Expand Down
67 changes: 56 additions & 11 deletions src/main/java/online/umbcraft/libraries/dupes/Dupe.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public abstract class Dupe {
final protected GoldenDupes plugin;

final static private boolean TOTEMS_EXIST;
final static private boolean USE_LEGACY_TOTEM;
final static private Material TOTEM_MATERIAL;
final static private boolean SHULKERS_EXIST;

final static private EnumSet<Material> shulkerBoxes;
Expand All @@ -30,17 +32,56 @@ public abstract class Dupe {
private static int totemStackSize;

static {
TOTEMS_EXIST = Material.matchMaterial("minecraft:totem_of_undying") != null;
SHULKERS_EXIST = Material.matchMaterial("minecraft:shulker_shell") != null;

shulkerBoxes = EnumSet.noneOf(Material.class);
boolean shulkersExist;
try {
Material.valueOf("SHULKER_SHELL");
shulkersExist = true;
}
catch(IllegalArgumentException e) {
shulkersExist = false;
}

boolean totemsExist;
Material totemMaterial = null;
try {
totemMaterial = Material.valueOf("TOTEM_OF_UNDYING");
totemsExist = true;
}
catch(IllegalArgumentException e) {
totemsExist = false;
}

boolean legacyTotems = false;

if(!totemsExist) {
try {
totemMaterial = Material.valueOf("TOTEM");
legacyTotems = true;
totemsExist = true;
}
catch(IllegalArgumentException e) {
legacyTotems = false;
}
}

TOTEMS_EXIST = totemsExist;
TOTEM_MATERIAL = totemMaterial;

SHULKERS_EXIST = shulkersExist;
USE_LEGACY_TOTEM = legacyTotems;

shulkerBoxes = EnumSet.noneOf(Material.class);

System.out.println("do shulkers exist? "+SHULKERS_EXIST);
System.out.println("do totems exist? "+TOTEMS_EXIST);
System.out.println("legacy totems? "+USE_LEGACY_TOTEM);
// building an EnumSet of all colors of shulker box
if (SHULKERS_EXIST)
if (SHULKERS_EXIST) {
Arrays.stream(Material.values())
.filter(m -> m.name().endsWith("SHULKER_BOX"))
.filter(m -> m.name().contains("SHULKER_BOX"))
.forEach(shulkerBoxes::add);
}
}


Expand All @@ -49,6 +90,7 @@ public Dupe(GoldenDupes plugin) {

}

// loads cofig values regarding item limits
public static void loadConfig(FileConfiguration config) {
dupeNonStacking = config.getBoolean(NON_STACK_DO_DUPE.path());
nonStackingStackSize = config.getInt(NON_STACK_STACKSIZE.path());
Expand All @@ -61,29 +103,32 @@ public static void loadConfig(FileConfiguration config) {
}



// dupes an item
// takes in the item to dupe and the maximum acceptable stack size before considering config limits
public ItemStack dupe(ItemStack toDupe, int amount) {


if (toDupe == null)
return new ItemStack(Material.AIR);

boolean dupe = false;
boolean dupe = true;
int stacksize = amount;
boolean isSize64 = toDupe.getMaxStackSize() == 64;


if (!isSize64) {
dupe = dupeNonStacking;
stacksize = Math.max(stacksize, nonStackingStackSize);
stacksize = Math.max(amount, nonStackingStackSize);
}

if (TOTEMS_EXIST && toDupe.getType() == Material.TOTEM_OF_UNDYING) {
if (TOTEMS_EXIST && toDupe.getType() == TOTEM_MATERIAL) {
dupe = dupeTotems;
stacksize = Math.max(stacksize, totemStackSize);
stacksize = Math.min(amount, totemStackSize);
}

if (SHULKERS_EXIST && shulkerBoxes.contains(toDupe.getType())) {
dupe = dupeShulkers;
stacksize = Math.max(stacksize, shulkerStackSize);
stacksize = Math.min(amount, shulkerStackSize);
}

if (!dupe)
Expand Down

0 comments on commit bbdd029

Please sign in to comment.