Skip to content

Commit bbdd029

Browse files
authored
Merge pull request #19 from Ivan8or/fix-autocraft-dupe
Fix autocraft not working in certain cases
2 parents 555737c + 569bb59 commit bbdd029

File tree

2 files changed

+64
-21
lines changed

2 files changed

+64
-21
lines changed

src/main/java/online/umbcraft/libraries/dupes/AutocraftDupe.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ public void onCraftItem(final PrepareItemCraftEvent e) {
7171
}
7272

7373

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

7878

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

125125

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

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

135-
final Player p = (Player) e.getEntity();
136135
final ItemStack toDupe = e.getItem().getItemStack();
137-
136+
final Player p = (Player) e.getEntity();
138137

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

143142
final ItemStack duped = dupe(toDupe, stacksize);
144-
p.getInventory().addItem(duped);
145143

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

151149
// player only tries to dupe the first item he picks up
152150
dupeAmnt.remove(p.getUniqueId());

src/main/java/online/umbcraft/libraries/dupes/Dupe.java

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public abstract class Dupe {
1616
final protected GoldenDupes plugin;
1717

1818
final static private boolean TOTEMS_EXIST;
19+
final static private boolean USE_LEGACY_TOTEM;
20+
final static private Material TOTEM_MATERIAL;
1921
final static private boolean SHULKERS_EXIST;
2022

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

3234
static {
33-
TOTEMS_EXIST = Material.matchMaterial("minecraft:totem_of_undying") != null;
34-
SHULKERS_EXIST = Material.matchMaterial("minecraft:shulker_shell") != null;
3535

36-
shulkerBoxes = EnumSet.noneOf(Material.class);
36+
boolean shulkersExist;
37+
try {
38+
Material.valueOf("SHULKER_SHELL");
39+
shulkersExist = true;
40+
}
41+
catch(IllegalArgumentException e) {
42+
shulkersExist = false;
43+
}
44+
45+
boolean totemsExist;
46+
Material totemMaterial = null;
47+
try {
48+
totemMaterial = Material.valueOf("TOTEM_OF_UNDYING");
49+
totemsExist = true;
50+
}
51+
catch(IllegalArgumentException e) {
52+
totemsExist = false;
53+
}
54+
55+
boolean legacyTotems = false;
56+
57+
if(!totemsExist) {
58+
try {
59+
totemMaterial = Material.valueOf("TOTEM");
60+
legacyTotems = true;
61+
totemsExist = true;
62+
}
63+
catch(IllegalArgumentException e) {
64+
legacyTotems = false;
65+
}
66+
}
3767

68+
TOTEMS_EXIST = totemsExist;
69+
TOTEM_MATERIAL = totemMaterial;
3870

71+
SHULKERS_EXIST = shulkersExist;
72+
USE_LEGACY_TOTEM = legacyTotems;
73+
74+
shulkerBoxes = EnumSet.noneOf(Material.class);
75+
76+
System.out.println("do shulkers exist? "+SHULKERS_EXIST);
77+
System.out.println("do totems exist? "+TOTEMS_EXIST);
78+
System.out.println("legacy totems? "+USE_LEGACY_TOTEM);
3979
// building an EnumSet of all colors of shulker box
40-
if (SHULKERS_EXIST)
80+
if (SHULKERS_EXIST) {
4181
Arrays.stream(Material.values())
42-
.filter(m -> m.name().endsWith("SHULKER_BOX"))
82+
.filter(m -> m.name().contains("SHULKER_BOX"))
4383
.forEach(shulkerBoxes::add);
84+
}
4485
}
4586

4687

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

5091
}
5192

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

63105

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

111+
66112
if (toDupe == null)
67113
return new ItemStack(Material.AIR);
68114

69-
boolean dupe = false;
115+
boolean dupe = true;
70116
int stacksize = amount;
71117
boolean isSize64 = toDupe.getMaxStackSize() == 64;
72118

73-
74119
if (!isSize64) {
75120
dupe = dupeNonStacking;
76-
stacksize = Math.max(stacksize, nonStackingStackSize);
121+
stacksize = Math.max(amount, nonStackingStackSize);
77122
}
78123

79-
if (TOTEMS_EXIST && toDupe.getType() == Material.TOTEM_OF_UNDYING) {
124+
if (TOTEMS_EXIST && toDupe.getType() == TOTEM_MATERIAL) {
80125
dupe = dupeTotems;
81-
stacksize = Math.max(stacksize, totemStackSize);
126+
stacksize = Math.min(amount, totemStackSize);
82127
}
83128

84129
if (SHULKERS_EXIST && shulkerBoxes.contains(toDupe.getType())) {
85130
dupe = dupeShulkers;
86-
stacksize = Math.max(stacksize, shulkerStackSize);
131+
stacksize = Math.min(amount, shulkerStackSize);
87132
}
88133

89134
if (!dupe)

0 commit comments

Comments
 (0)