Skip to content

Commit

Permalink
Fix IllegalAccessException due to reflection usage for 1.14
Browse files Browse the repository at this point in the history
Closes WesJD#30
  • Loading branch information
WesJD committed Dec 2, 2019
1 parent 7dbcfc2 commit 1250483
Show file tree
Hide file tree
Showing 14 changed files with 40 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Wrapper1_10_R1 implements VersionWrapper {
* {@inheritDoc}
*/
@Override
public int getNextContainerId(Player player) {
public int getNextContainerId(Player player, Object container) {
return toNMS(player).nextContainerCounter();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Wrapper1_11_R1 implements VersionWrapper {
* {@inheritDoc}
*/
@Override
public int getNextContainerId(Player player) {
public int getNextContainerId(Player player, Object container) {
return toNMS(player).nextContainerCounter();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Wrapper1_12_R1 implements VersionWrapper {
* {@inheritDoc}
*/
@Override
public int getNextContainerId(Player player) {
public int getNextContainerId(Player player, Object container) {
return toNMS(player).nextContainerCounter();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class Wrapper1_13_R1 implements VersionWrapper {
* {@inheritDoc}
*/
@Override
public int getNextContainerId(Player player) {
public int getNextContainerId(Player player, Object container) {
return toNMS(player).nextContainerCounter();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class Wrapper1_13_R2 implements VersionWrapper {
* {@inheritDoc}
*/
@Override
public int getNextContainerId(Player player) {
public int getNextContainerId(Player player, Object container) {
return toNMS(player).nextContainerCounter();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ public void e() {
this.levelCost.set(0);
}

public int getContainerId() {
return windowId;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,24 @@
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

public class Wrapper1_14_R1 implements VersionWrapper {

private final boolean IS_ONE_FOURTEEN = Bukkit.getBukkitVersion().contains("1.14.4");

private int getRealNextContainerId(Player player) {
return toNMS(player).nextContainerCounter();
}

/**
* {@inheritDoc}
*/
@Override
public int getNextContainerId(Player player) {
return toNMS(player).nextContainerCounter();
public int getNextContainerId(Player player, Object container) {
if(IS_ONE_FOURTEEN) {
return ((AnvilContainer1_14_4_R1) container).getContainerId();
} else {
return ((AnvilContainer) container).getContainerId();
}
}

/**
Expand Down Expand Up @@ -67,17 +74,7 @@ public void setActiveContainer(Player player, Object container) {
*/
@Override
public void setActiveContainerId(Object container, int containerId) {
try {
final Field field = Container.class.getField("windowId");

final Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);

field.set(container, containerId);
} catch(Exception ex) {
throw new RuntimeException(ex);
}
//noop
}

/**
Expand All @@ -101,10 +98,11 @@ public Inventory toBukkitInventory(Object container) {
*/
@Override
public Object newContainerAnvil(Player player) {
if (Bukkit.getBukkitVersion().contains("1.14.4")) {
return new AnvilContainer1_14_4_R1(player, getNextContainerId(player));
if (IS_ONE_FOURTEEN) {
return new AnvilContainer1_14_4_R1(player, getRealNextContainerId(player));
} else {
return new Wrapper1_14_R1.AnvilContainer(player);
}
return new Wrapper1_14_R1.AnvilContainer(player);
}

/**
Expand All @@ -123,7 +121,7 @@ private EntityPlayer toNMS(Player player) {
private class AnvilContainer extends ContainerAnvil {

public AnvilContainer(Player player) {
super(Wrapper1_14_R1.this.getNextContainerId(player), ((CraftPlayer) player).getHandle().inventory,
super(getRealNextContainerId(player), ((CraftPlayer) player).getHandle().inventory,
ContainerAccess.at(((CraftWorld) player.getWorld()).getHandle(), new BlockPosition(0, 0, 0)));
this.checkReachable = false;
setTitle(new ChatMessage("Repair & Name"));
Expand All @@ -135,6 +133,10 @@ public void e() {
this.levelCost.a(0);
}

public int getContainerId() {
return windowId;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Wrapper1_8_R1 implements VersionWrapper {
* {@inheritDoc}
*/
@Override
public int getNextContainerId(Player player) {
public int getNextContainerId(Player player, Object container) {
return toNMS(player).nextContainerCounter();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Wrapper1_8_R2 implements VersionWrapper {
* {@inheritDoc}
*/
@Override
public int getNextContainerId(Player player) {
public int getNextContainerId(Player player, Object container) {
return toNMS(player).nextContainerCounter();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Wrapper1_8_R3 implements VersionWrapper {
* {@inheritDoc}
*/
@Override
public int getNextContainerId(Player player) {
public int getNextContainerId(Player player, Object container) {
return toNMS(player).nextContainerCounter();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Wrapper1_9_R1 implements VersionWrapper {
* {@inheritDoc}
*/
@Override
public int getNextContainerId(Player player) {
public int getNextContainerId(Player player, Object container) {
return toNMS(player).nextContainerCounter();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Wrapper1_9_R2 implements VersionWrapper {
* {@inheritDoc}
*/
@Override
public int getNextContainerId(Player player) {
public int getNextContainerId(Player player, Object container) {
return toNMS(player).nextContainerCounter();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ public interface VersionWrapper {
/**
* Gets the next available NMS container id for the player
* @param player The player to get the next container id of
* @return The next available NMS container id0
* @param container The container that a new id is being generated for
* @return The next available NMS container id
*/
int getNextContainerId(Player player);
int getNextContainerId(Player player, Object container);

/**
* Closes the current inventory for the player
Expand Down
2 changes: 1 addition & 1 deletion api/src/main/java/net/wesjd/anvilgui/AnvilGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private void openInventory() {
inventory = WRAPPER.toBukkitInventory(container);
inventory.setItem(Slot.INPUT_LEFT, this.insert);

containerId = WRAPPER.getNextContainerId(player);
containerId = WRAPPER.getNextContainerId(player, container);
WRAPPER.sendPacketOpenWindow(player, containerId);
WRAPPER.setActiveContainer(player, container);
WRAPPER.setActiveContainerId(container, containerId);
Expand Down

0 comments on commit 1250483

Please sign in to comment.