Fix for interactions with stacker plugins.

This commit is contained in:
Brianna O'Keefe 2019-01-29 17:05:01 -05:00
parent 5944ebfc9b
commit 211a6898ad
2 changed files with 34 additions and 2 deletions

View File

@ -4,7 +4,7 @@ stages:
variables:
name: "EpicHoppers"
path: "/builds/Songoda/$name"
version: "3.4.4"
version: "3.4.5"
build:
stage: build

View File

@ -15,6 +15,8 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.metadata.FixedMetadataValue;
import xyz.wildseries.wildstacker.api.WildStackerAPI;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
@ -24,8 +26,26 @@ public class ModuleSuction implements Module {
private boolean wildStacker = Bukkit.getPluginManager().isPluginEnabled("WildStacker");
private Class<?> clazzItemStack, clazzItem, clazzCraftItemStack;
private Method methodGetItem, methodAsNMSCopy;
private Field fieldMaxStackSize;
public ModuleSuction(int amount) {
this.amount = amount;
try {
String ver = Bukkit.getServer().getClass().getPackage().getName().substring(23);
clazzCraftItemStack = Class.forName("org.bukkit.craftbukkit." + ver + ".inventory.CraftItemStack");
clazzItemStack = Class.forName("net.minecraft.server." + ver + ".ItemStack");
clazzItem = Class.forName("net.minecraft.server." + ver + ".Item");
methodAsNMSCopy = clazzCraftItemStack.getMethod("asNMSCopy", ItemStack.class);
methodGetItem = clazzItemStack.getDeclaredMethod("getItem");
fieldMaxStackSize = clazzItem.getDeclaredField("maxStackSize");
fieldMaxStackSize.setAccessible(true);
} catch (ReflectiveOperationException e) {
e.printStackTrace();
}
}
@Override
@ -60,8 +80,10 @@ public class ModuleSuction implements Module {
if (wildStacker)
hopItem.setAmount(WildStackerAPI.getItemAmount((Item) entity));
ItemStack item = ((Item) entity).getItemStack();
ItemStack item = setMax(((Item) entity).getItemStack(), 0, true);
if (item == null) continue;
if (!canMove(hopperBlock.getInventory(), item)) {
continue;
}
@ -104,4 +126,14 @@ public class ModuleSuction implements Module {
}
return false;
}
public ItemStack setMax(ItemStack item, int max, boolean reset) {
try {
Object objItemStack = methodGetItem.invoke(methodAsNMSCopy.invoke(null, item));
fieldMaxStackSize.set(objItemStack, reset ? new ItemStack(item.getType()).getMaxStackSize() : max);
} catch (ReflectiveOperationException e) {
e.printStackTrace();
}
return item;
}
}