Skip to content

Commit

Permalink
Fix 1_16_R2 and rename 1_16_R3 hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Lauriichan committed Apr 14, 2021
1 parent 83a1380 commit 7d75a72
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.sourcewriters.minecraft.versiontools.reflection.provider.v1_16_R2;

import net.sourcewriters.minecraft.versiontools.reflection.VersionControl;
import net.sourcewriters.minecraft.versiontools.reflection.provider.v1_16_R2.data.hook.BukkitContainerAdapterHook1_16_R2;

public class VersionControl1_16_R2 extends VersionControl {

Expand All @@ -18,7 +19,7 @@ public static VersionControl1_16_R2 init() {
private final BukkitConversion1_16_R2 bukkitConversion = new BukkitConversion1_16_R2(this);

private VersionControl1_16_R2() {

BukkitContainerAdapterHook1_16_R2.hookEntity();
}

@Override
Expand Down Expand Up @@ -50,5 +51,11 @@ public PacketHandler1_16_R2 getPacketHandler() {
public BukkitConversion1_16_R2 getBukkitConversion() {
return bukkitConversion;
}

@Override
public void shutdown() {
dataProvider.getDefaultDistributor().shutdown();
BukkitContainerAdapterHook1_16_R2.unhookAll();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.syntaxphoenix.syntaxapi.data.DataType;
import com.syntaxphoenix.syntaxapi.data.IDataContainer;
import com.syntaxphoenix.syntaxapi.utils.key.IKey;
import com.syntaxphoenix.syntaxapi.utils.key.NamespacedKey;

import net.sourcewriters.minecraft.versiontools.reflection.data.WrapType;
import net.sourcewriters.minecraft.versiontools.reflection.data.WrappedContainer;
Expand Down Expand Up @@ -45,7 +44,7 @@ public boolean has(IKey key) {

@Override
public boolean has(String key, DataType<?, ?> type) {
return has(NamespacedKey.fromString(key), type);
return has(syntaxKey(key), type);
}

@Override
Expand All @@ -55,7 +54,7 @@ public boolean has(IKey key, DataType<?, ?> type) {

@Override
public <C> C get(String key, DataType<?, C> type) {
return get(NamespacedKey.fromString(key), type);
return get(syntaxKey(key), type);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ public P0 toPrimitiveWrapped(P1 primitive) {
return (P0) new SyntaxContainer1_16_R2((PersistentDataContainer) primitive);
case 2:
return (P0) Arrays.stream((PersistentDataContainer[]) primitive).map(SyntaxContainer1_16_R2::new)
.toArray(IDataContainer[]::new);
.toArray(SyntaxContainer1_16_R2[]::new);
case 3:
return (P0) new BukkitContainer1_16_R2((IDataContainer) primitive);
case 4:
return (P0) Arrays.stream((IDataContainer[]) primitive).map(BukkitContainer1_16_R2::new)
.toArray(PersistentDataContainer[]::new);
.toArray(BukkitContainer1_16_R2[]::new);
default:
return (P0) primitive;
}
Expand All @@ -64,11 +64,11 @@ public C0 toComplexWrapped(C1 complex) {
case 1:
return (C0) new SyntaxContainer1_16_R2((PersistentDataContainer) complex);
case 2:
return (C0) Arrays.stream((PersistentDataContainer[]) complex).map(SyntaxContainer1_16_R2::new).toArray(IDataContainer[]::new);
return (C0) Arrays.stream((PersistentDataContainer[]) complex).map(SyntaxContainer1_16_R2::new).toArray(BukkitContainer1_16_R2[]::new);
case 3:
return (C0) new BukkitContainer1_16_R2((IDataContainer) complex);
case 4:
return (C0) Arrays.stream((IDataContainer[]) complex).map(BukkitContainer1_16_R2::new).toArray(PersistentDataContainer[]::new);
return (C0) Arrays.stream((IDataContainer[]) complex).map(BukkitContainer1_16_R2::new).toArray(BukkitContainer1_16_R2[]::new);
default:
return (C0) complex;
}
Expand All @@ -81,12 +81,12 @@ public P1 toPrimitiveOriginal(P0 primitive) {
return (P1) new BukkitContainer1_16_R2((IDataContainer) primitive);
case 2:
return (P1) Arrays.stream((IDataContainer[]) primitive).map(BukkitContainer1_16_R2::new)
.toArray(PersistentDataContainer[]::new);
.toArray(BukkitContainer1_16_R2[]::new);
case 3:
return (P1) new SyntaxContainer1_16_R2((PersistentDataContainer) primitive);
case 4:
return (P1) Arrays.stream((PersistentDataContainer[]) primitive).map(SyntaxContainer1_16_R2::new)
.toArray(IDataContainer[]::new);
.toArray(BukkitContainer1_16_R2[]::new);
default:
return (P1) primitive;
}
Expand All @@ -98,11 +98,11 @@ public C1 toComplexOriginal(C0 complex) {
case 1:
return (C1) new BukkitContainer1_16_R2((IDataContainer) complex);
case 2:
return (C1) Arrays.stream((IDataContainer[]) complex).map(BukkitContainer1_16_R2::new).toArray(PersistentDataContainer[]::new);
return (C1) Arrays.stream((IDataContainer[]) complex).map(BukkitContainer1_16_R2::new).toArray(BukkitContainer1_16_R2[]::new);
case 3:
return (C1) new SyntaxContainer1_16_R2((PersistentDataContainer) complex);
case 4:
return (C1) Arrays.stream((PersistentDataContainer[]) complex).map(SyntaxContainer1_16_R2::new).toArray(IDataContainer[]::new);
return (C1) Arrays.stream((PersistentDataContainer[]) complex).map(SyntaxContainer1_16_R2::new).toArray(SyntaxContainer1_16_R2[]::new);
default:
return (C1) complex;
}
Expand All @@ -111,13 +111,13 @@ public C1 toComplexOriginal(C0 complex) {
protected static Class<?> internalWrap(Class<?> clazz, int state) {
switch (state) {
case 1:
return IDataContainer.class;
return SyntaxContainer1_16_R2.class;
case 2:
return IDataContainer[].class;
return SyntaxContainer1_16_R2[].class;
case 3:
return PersistentDataContainer.class;
return BukkitContainer1_16_R2.class;
case 4:
return PersistentDataContainer[].class;
return BukkitContainer1_16_R2[].class;
default:
return clazz;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package net.sourcewriters.minecraft.versiontools.reflection.provider.v1_16_R2.data.hook;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;

import org.bukkit.craftbukkit.v1_16_R2.entity.CraftEntity;
import org.bukkit.craftbukkit.v1_16_R2.persistence.CraftPersistentDataContainer;
import org.bukkit.craftbukkit.v1_16_R2.persistence.CraftPersistentDataTypeRegistry;
import org.bukkit.persistence.PersistentDataContainer;

import com.syntaxphoenix.syntaxapi.data.IDataContainer;
import com.syntaxphoenix.syntaxapi.data.container.nbt.NbtContainer;
import com.syntaxphoenix.syntaxapi.nbt.NbtCompound;
import com.syntaxphoenix.syntaxapi.reflection.AbstractReflect;
import com.syntaxphoenix.syntaxapi.reflection.Reflect;

import net.minecraft.server.v1_16_R2.NBTTagCompound;
import net.sourcewriters.minecraft.versiontools.reflection.VersionControl;
import net.sourcewriters.minecraft.versiontools.reflection.data.WrappedContainer;
import net.sourcewriters.minecraft.versiontools.reflection.provider.v1_16_R2.data.BukkitContainer1_16_R2;
import net.sourcewriters.minecraft.versiontools.reflection.provider.v1_16_R2.data.SyntaxContainer1_16_R2;

@SuppressWarnings({
"rawtypes",
"unchecked"
})
public final class BukkitContainerAdapterHook1_16_R2 {

private static final BukkitContainerAdapterHook1_16_R2 HOOK = new BukkitContainerAdapterHook1_16_R2();

private final AbstractReflect registryRef = new Reflect(CraftPersistentDataTypeRegistry.class)
.searchMethod("create", "createAdapter", Class.class, Class.class, Function.class, Function.class)
.searchField("adapters", "adapters")
.searchField("function", "CREATE_ADAPTER");
private final AbstractReflect entityRef = new Reflect(CraftEntity.class).searchField("registry", "DATA_TYPE_REGISTRY");

private BukkitContainerAdapterHook1_16_R2() {}

private final HashMap<CraftPersistentDataTypeRegistry, Function> map = new HashMap<>();

private CraftPersistentDataTypeRegistry getEntityRegistry() {
return (CraftPersistentDataTypeRegistry) entityRef.getFieldValue("registry");
}

private void uninjectAll() {
for (CraftPersistentDataTypeRegistry registry : map.keySet()) {
Map adapters = (Map) registryRef.getFieldValue("adapters", registry);
adapters.remove(BukkitContainer1_16_R2.class);
adapters.remove(SyntaxContainer1_16_R2.class);
registryRef.setFieldValue(registry, "function", map.get(registry));
}
map.clear();
}

private void inject(CraftPersistentDataTypeRegistry registry) {
if (map.containsKey(registry)) {
return;
}
map.put(registry, (Function) registryRef.getFieldValue("function", registry));
Function function = clazz -> createAdapter(registry, registryRef.getMethod("create").getReturnType(), (Class) clazz);
registryRef.setFieldValue(registry, "function", function);
}

private <E> E createAdapter(CraftPersistentDataTypeRegistry registry, Class<E> adapterType, Class type) {
if (Objects.equals(BukkitContainer1_16_R2.class, type)) {
return (E) buildAdapter(registry, BukkitContainer1_16_R2.class, tag -> fromPrimitiveSyntax(tag));
}
if (Objects.equals(SyntaxContainer1_16_R2.class, type)) {
return (E) buildAdapter(registry, SyntaxContainer1_16_R2.class, tag -> fromPrimitiveBukkit(registry, tag));
}
return (E) map.get(registry).apply(type);
}

private <C extends WrappedContainer> Object buildAdapter(Object handle, Class<C> type, Function<NBTTagCompound, C> function) {
return registryRef.run(handle, "create", type, NBTTagCompound.class, (Function<C, NBTTagCompound>) input -> toPrimitive(input),
function);
}

private NBTTagCompound toPrimitive(WrappedContainer input) {
Object handle = findFinalContainer(input).getHandle();
if (handle instanceof PersistentDataContainer) {
if (handle instanceof CraftPersistentDataContainer) {
return ((CraftPersistentDataContainer) handle).toTagCompound();
}
throw new IllegalArgumentException(
"Expected 'CraftPersistentDataContainer' got '" + handle.getClass().getSimpleName() + " instead'!");
}
if (handle instanceof IDataContainer) {
if (handle instanceof NbtContainer) {
return (NBTTagCompound) VersionControl.get().getBukkitConversion().toMinecraftCompound(((NbtContainer) handle).asNbt());
}
throw new IllegalArgumentException(
"Expected 'CraftPersistentDataContainer' got '" + handle.getClass().getSimpleName() + " instead'!");
}
throw new IllegalArgumentException("Unknown WrappedContainer implementation!");
}

private BukkitContainer1_16_R2 fromPrimitiveSyntax(NBTTagCompound data) {
VersionControl control = VersionControl.get();
NbtContainer container = new NbtContainer(control.getDataProvider().getRegistry());
NbtCompound compound = control.getBukkitConversion().fromMinecraftCompound(data);
container.fromNbt(compound);
return new BukkitContainer1_16_R2(container);
}

private SyntaxContainer1_16_R2 fromPrimitiveBukkit(CraftPersistentDataTypeRegistry registry, NBTTagCompound data) {
CraftPersistentDataContainer container = new CraftPersistentDataContainer(registry);
container.putAll(data);
return new SyntaxContainer1_16_R2(container);
}

private WrappedContainer findFinalContainer(WrappedContainer container) {
WrappedContainer output = container;
while (output.getHandle() instanceof WrappedContainer) {
output = (WrappedContainer) output.getHandle();
}
return output;
}

public static void unhookAll() {
HOOK.uninjectAll();
}

public static void hookEntity() {
HOOK.inject(HOOK.getEntityRegistry());
}

public static void hook(CraftPersistentDataTypeRegistry registry) {
HOOK.inject(registry);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public WrappedContainer getDataAdapter() {

@Override
public void setSkin(Skin skin) {
if (skin == null || getSkin().equals(skin)) {
if (skin == null) {
return;
}
dataAdapter.set("skin", skin, SkinDataType.INSTANCE);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.sourcewriters.minecraft.versiontools.reflection.provider.v1_16_R3;

import net.sourcewriters.minecraft.versiontools.reflection.VersionControl;
import net.sourcewriters.minecraft.versiontools.reflection.provider.v1_16_R3.data.hook.BukkitContainerAdapterHook;
import net.sourcewriters.minecraft.versiontools.reflection.provider.v1_16_R3.data.hook.BukkitContainerAdapterHook1_16_R3;

public class VersionControl1_16_R3 extends VersionControl {

Expand All @@ -19,7 +19,7 @@ public static VersionControl1_16_R3 init() {
private final BukkitConversion1_16_R3 bukkitConversion = new BukkitConversion1_16_R3(this);

private VersionControl1_16_R3() {
BukkitContainerAdapterHook.hookEntity();
BukkitContainerAdapterHook1_16_R3.hookEntity();
}

@Override
Expand Down Expand Up @@ -55,7 +55,7 @@ public BukkitConversion1_16_R3 getBukkitConversion() {
@Override
public void shutdown() {
dataProvider.getDefaultDistributor().shutdown();
BukkitContainerAdapterHook.unhookAll();
BukkitContainerAdapterHook1_16_R3.unhookAll();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@
"rawtypes",
"unchecked"
})
public final class BukkitContainerAdapterHook {
public final class BukkitContainerAdapterHook1_16_R3 {

private static final BukkitContainerAdapterHook HOOK = new BukkitContainerAdapterHook();
private static final BukkitContainerAdapterHook1_16_R3 HOOK = new BukkitContainerAdapterHook1_16_R3();

private final AbstractReflect registryRef = new Reflect(CraftPersistentDataTypeRegistry.class)
.searchMethod("create", "createAdapter", Class.class, Class.class, Function.class, Function.class)
.searchField("adapters", "adapters")
.searchField("function", "CREATE_ADAPTER");
private final AbstractReflect entityRef = new Reflect(CraftEntity.class).searchField("registry", "DATA_TYPE_REGISTRY");

private BukkitContainerAdapterHook() {}
private BukkitContainerAdapterHook1_16_R3() {}

private final HashMap<CraftPersistentDataTypeRegistry, Function> map = new HashMap<>();

Expand Down

0 comments on commit 7d75a72

Please sign in to comment.