Skip to content

Commit

Permalink
Migrate to new VFP API, fixes #702
Browse files Browse the repository at this point in the history
  • Loading branch information
Earthcomputer committed Jan 2, 2025
1 parent af0d8f0 commit 79f2a87
Showing 1 changed file with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,25 +184,27 @@ private int doGetProtocolVersion() throws ReflectiveOperationException {
}

private static final class ViaFabricPlus extends AbstractViaVersion {
private final Method viaFabricPlusGetImpl;
private final Method getTargetVersion;
private final Method olderThan;
private final Method itemRegistryDiffKeepItem;
private final Method itemExistsInConnection;
private final Object release1_7_2Version;

private ViaFabricPlus() throws ReflectiveOperationException {
Class<?> protocolTranslator = Class.forName("de.florianmichael.viafabricplus.protocoltranslator.ProtocolTranslator");
Class<?> itemRegistryDiff = Class.forName("de.florianmichael.viafabricplus.fixes.data.ItemRegistryDiff");
Class<?> viaFabricPlus = Class.forName("com.viaversion.viafabricplus.ViaFabricPlus");
Class<?> viaFabricPlusBase = Class.forName("com.viaversion.viafabricplus.api.ViaFabricPlusBase");
Class<?> protocolVersion = Class.forName("com.viaversion.viaversion.api.protocol.version.ProtocolVersion");
getTargetVersion = protocolTranslator.getMethod("getTargetVersion");
olderThan = getTargetVersion.getReturnType().getMethod("olderThan", getTargetVersion.getReturnType());
itemRegistryDiffKeepItem = itemRegistryDiff.getMethod("keepItem", Item.class);
viaFabricPlusGetImpl = viaFabricPlus.getMethod("getImpl");
getTargetVersion = viaFabricPlusBase.getMethod("getTargetVersion");
olderThan = protocolVersion.getMethod("olderThan", protocolVersion);
itemExistsInConnection = viaFabricPlusBase.getMethod("itemExistsInConnection", Item.class);
release1_7_2Version = protocolVersion.getField("v1_7_2").get(null);
}

@Override
public int getProtocolVersion() {
try {
final boolean isOlderThan1_7_2 = (boolean) olderThan.invoke(getTargetVersion.invoke(null), release1_7_2Version);
final boolean isOlderThan1_7_2 = (boolean) olderThan.invoke(getTargetVersion.invoke(getImpl()), release1_7_2Version);
if (isOlderThan1_7_2) {
return V1_7_2;
} else {
Expand All @@ -215,13 +217,21 @@ public int getProtocolVersion() {

@Override
protected Object getCurrentVersion() throws ReflectiveOperationException {
return getTargetVersion.invoke(null);
return getTargetVersion.invoke(getImpl());
}

@Override
public boolean doesItemExist(Item item) {
try {
return (Boolean) itemRegistryDiffKeepItem.invoke(null, item);
return (Boolean) itemExistsInConnection.invoke(getImpl(), item);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}

private Object getImpl() {
try {
return viaFabricPlusGetImpl.invoke(null);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
Expand Down

0 comments on commit 79f2a87

Please sign in to comment.