Skip to content

Commit

Permalink
Fix data for 1_14_R1, 1_15_R1 and 1_16_R1
Browse files Browse the repository at this point in the history
  • Loading branch information
Lauriichan committed Apr 14, 2021
1 parent e27cf3f commit bb9b1ef
Show file tree
Hide file tree
Showing 15 changed files with 478 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.sourcewriters.minecraft.versiontools.reflection.provider.v1_14_R1;

import net.sourcewriters.minecraft.versiontools.reflection.VersionControl;
import net.sourcewriters.minecraft.versiontools.reflection.provider.v1_14_R1.data.hook.BukkitContainerAdapterHook1_14_R1;

public class VersionControl1_14_R1 extends VersionControl {

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

private VersionControl1_14_R1() {

BukkitContainerAdapterHook1_14_R1.hookEntity();
}

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


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

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package net.sourcewriters.minecraft.versiontools.reflection.provider.v1_14_R1.data;

import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;

import org.bukkit.craftbukkit.v1_14_R1.persistence.CraftPersistentDataContainer;
import org.bukkit.persistence.PersistentDataContainer;

import com.syntaxphoenix.syntaxapi.data.DataAdapterContext;
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 @@ -46,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 @@ -56,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 Expand Up @@ -96,15 +94,12 @@ public boolean remove(IKey key) {

@Override
public IKey[] getKeys() {
return getKeyspaces().stream().map(string -> NamespacedKey.fromString(string)).toArray(IKey[]::new);
return container.getKeys().stream().map(BukkitKey1_14_R1::new).map(WrappedKey::getNamespacedKey).toArray(IKey[]::new);
}

@Override
public Set<String> getKeyspaces() {
if (container instanceof CraftPersistentDataContainer) {
return ((CraftPersistentDataContainer) container).getRaw().keySet();
}
return Collections.emptySet();
return container.getKeys().stream().map(org.bukkit.NamespacedKey::toString).collect(Collectors.toSet());
}

@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_14_R1((PersistentDataContainer) primitive);
case 2:
return (P0) Arrays.stream((PersistentDataContainer[]) primitive).map(SyntaxContainer1_14_R1::new)
.toArray(IDataContainer[]::new);
.toArray(SyntaxContainer1_14_R1[]::new);
case 3:
return (P0) new BukkitContainer1_14_R1((IDataContainer) primitive);
case 4:
return (P0) Arrays.stream((IDataContainer[]) primitive).map(BukkitContainer1_14_R1::new)
.toArray(PersistentDataContainer[]::new);
.toArray(BukkitContainer1_14_R1[]::new);
default:
return (P0) primitive;
}
Expand All @@ -64,11 +64,11 @@ public C0 toComplexWrapped(C1 complex) {
case 1:
return (C0) new SyntaxContainer1_14_R1((PersistentDataContainer) complex);
case 2:
return (C0) Arrays.stream((PersistentDataContainer[]) complex).map(SyntaxContainer1_14_R1::new).toArray(IDataContainer[]::new);
return (C0) Arrays.stream((PersistentDataContainer[]) complex).map(SyntaxContainer1_14_R1::new).toArray(SyntaxContainer1_14_R1[]::new);
case 3:
return (C0) new BukkitContainer1_14_R1((IDataContainer) complex);
case 4:
return (C0) Arrays.stream((IDataContainer[]) complex).map(BukkitContainer1_14_R1::new).toArray(PersistentDataContainer[]::new);
return (C0) Arrays.stream((IDataContainer[]) complex).map(BukkitContainer1_14_R1::new).toArray(BukkitContainer1_14_R1[]::new);
default:
return (C0) complex;
}
Expand All @@ -81,12 +81,12 @@ public P1 toPrimitiveOriginal(P0 primitive) {
return (P1) new BukkitContainer1_14_R1((IDataContainer) primitive);
case 2:
return (P1) Arrays.stream((IDataContainer[]) primitive).map(BukkitContainer1_14_R1::new)
.toArray(PersistentDataContainer[]::new);
.toArray(BukkitContainer1_14_R1[]::new);
case 3:
return (P1) new SyntaxContainer1_14_R1((PersistentDataContainer) primitive);
case 4:
return (P1) Arrays.stream((PersistentDataContainer[]) primitive).map(SyntaxContainer1_14_R1::new)
.toArray(IDataContainer[]::new);
.toArray(SyntaxContainer1_14_R1[]::new);
default:
return (P1) primitive;
}
Expand All @@ -98,11 +98,11 @@ public C1 toComplexOriginal(C0 complex) {
case 1:
return (C1) new BukkitContainer1_14_R1((IDataContainer) complex);
case 2:
return (C1) Arrays.stream((IDataContainer[]) complex).map(BukkitContainer1_14_R1::new).toArray(PersistentDataContainer[]::new);
return (C1) Arrays.stream((IDataContainer[]) complex).map(BukkitContainer1_14_R1::new).toArray(BukkitContainer1_14_R1[]::new);
case 3:
return (C1) new SyntaxContainer1_14_R1((PersistentDataContainer) complex);
case 4:
return (C1) Arrays.stream((PersistentDataContainer[]) complex).map(SyntaxContainer1_14_R1::new).toArray(IDataContainer[]::new);
return (C1) Arrays.stream((PersistentDataContainer[]) complex).map(SyntaxContainer1_14_R1::new).toArray(SyntaxContainer1_14_R1[]::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_14_R1.class;
case 2:
return IDataContainer[].class;
return SyntaxContainer1_14_R1[].class;
case 3:
return PersistentDataContainer.class;
return BukkitContainer1_14_R1.class;
case 4:
return PersistentDataContainer[].class;
return BukkitContainer1_14_R1[].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_14_R1.data.hook;

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

import org.bukkit.craftbukkit.v1_14_R1.entity.CraftEntity;
import org.bukkit.craftbukkit.v1_14_R1.persistence.CraftPersistentDataContainer;
import org.bukkit.craftbukkit.v1_14_R1.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_14_R1.NBTTagCompound;
import net.sourcewriters.minecraft.versiontools.reflection.VersionControl;
import net.sourcewriters.minecraft.versiontools.reflection.data.WrappedContainer;
import net.sourcewriters.minecraft.versiontools.reflection.provider.v1_14_R1.data.BukkitContainer1_14_R1;
import net.sourcewriters.minecraft.versiontools.reflection.provider.v1_14_R1.data.SyntaxContainer1_14_R1;

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

private static final BukkitContainerAdapterHook1_14_R1 HOOK = new BukkitContainerAdapterHook1_14_R1();

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_14_R1() {}

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_14_R1.class);
adapters.remove(SyntaxContainer1_14_R1.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_14_R1.class, type)) {
return (E) buildAdapter(registry, BukkitContainer1_14_R1.class, tag -> fromPrimitiveSyntax(tag));
}
if (Objects.equals(SyntaxContainer1_14_R1.class, type)) {
return (E) buildAdapter(registry, SyntaxContainer1_14_R1.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_14_R1 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_14_R1(container);
}

private SyntaxContainer1_14_R1 fromPrimitiveBukkit(CraftPersistentDataTypeRegistry registry, NBTTagCompound data) {
CraftPersistentDataContainer container = new CraftPersistentDataContainer(registry);
container.putAll(data);
return new SyntaxContainer1_14_R1(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 @@ -68,7 +68,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,6 +1,7 @@
package net.sourcewriters.minecraft.versiontools.reflection.provider.v1_15_R1;

import net.sourcewriters.minecraft.versiontools.reflection.VersionControl;
import net.sourcewriters.minecraft.versiontools.reflection.provider.v1_15_R1.data.hook.BukkitContainerAdapterHook1_15_R1;

public class VersionControl1_15_R1 extends VersionControl {

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

private VersionControl1_15_R1() {

BukkitContainerAdapterHook1_15_R1.hookEntity();
}

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

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

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package net.sourcewriters.minecraft.versiontools.reflection.provider.v1_15_R1.data;

import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;

import org.bukkit.craftbukkit.v1_15_R1.persistence.CraftPersistentDataContainer;
import org.bukkit.persistence.PersistentDataContainer;

import com.syntaxphoenix.syntaxapi.data.DataAdapterContext;
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 @@ -46,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 @@ -56,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 Expand Up @@ -96,15 +94,12 @@ public boolean remove(IKey key) {

@Override
public IKey[] getKeys() {
return getKeyspaces().stream().map(string -> NamespacedKey.fromString(string)).toArray(IKey[]::new);
return container.getKeys().stream().map(BukkitKey1_15_R1::new).map(WrappedKey::getNamespacedKey).toArray(IKey[]::new);
}

@Override
public Set<String> getKeyspaces() {
if (container instanceof CraftPersistentDataContainer) {
return ((CraftPersistentDataContainer) container).getRaw().keySet();
}
return Collections.emptySet();
return container.getKeys().stream().map(org.bukkit.NamespacedKey::toString).collect(Collectors.toSet());
}

@Override
Expand Down
Loading

0 comments on commit bb9b1ef

Please sign in to comment.