Skip to content

Commit

Permalink
fix method mappings for 1.12
Browse files Browse the repository at this point in the history
  • Loading branch information
ds58 committed May 6, 2019
1 parent d9efb6a commit a038cbd
Showing 1 changed file with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,29 @@
import net.minecraft.server.v1_12_R1.Container;
import net.minecraft.server.v1_12_R1.ItemStack;
import net.minecraft.server.v1_12_R1.NBTTagCompound;
import net.minecraft.server.v1_12_R1.Slot;
import org.bukkit.craftbukkit.v1_12_R1.entity.CraftPlayer;
import org.bukkit.entity.Player;

import java.lang.reflect.Field;
import java.util.List;

public class ContainerCleaner implements IContainerCleaner {

private final PConfig config;
private final IProtocolConstants protocolConstants;

private static boolean is_1_12_2;

static {
try {
Container.class.getField("slots");
is_1_12_2 = true;
} catch (NoSuchFieldException e) {
is_1_12_2 = false;
}
}

public ContainerCleaner(PConfig config, IProtocolConstants protocolConstants) {
this.config = config;
this.protocolConstants = protocolConstants;
Expand All @@ -26,7 +41,23 @@ public void clean(Player player) {
CraftPlayer craftPlayer = (CraftPlayer) player;
Container container = craftPlayer.getHandle().activeContainer;

for (int slot = 0; slot < container.slots.size(); slot++) {
int containerSlotsSize = 0;

if (is_1_12_2) {
containerSlotsSize = container.slots.size();
} else {
try {
Field slotsField = Container.class.getField("c");
List<Slot> slots = (List<Slot>) slotsField.get(container);
containerSlotsSize = slots.size();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}

for (int slot = 0; slot < containerSlotsSize; slot++) {
ItemStack itemStack = container.getSlot(slot).getItem();

if (itemStack == null || !itemStack.hasTag()) continue;
Expand Down

0 comments on commit a038cbd

Please sign in to comment.