Skip to content

Instantly share code, notes, and snippets.

@sinnlosername
Created July 2, 2016 00:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sinnlosername/4e1c61ec4218b73f999fff87b2c0b899 to your computer and use it in GitHub Desktop.
Save sinnlosername/4e1c61ec4218b73f999fff87b2c0b899 to your computer and use it in GitHub Desktop.
@EqualsAndHashCode(of = "handle")
@Getter
public class AbstractWrapper {
private final Object handle;
private final Class<?> handleClass;
public AbstractWrapper(Object handle, Class<?> handleClass) {
if (!handleClass.isInstance(handle)) throw new IllegalArgumentException(handle.getClass().getSimpleName()+ " cannot be handled from wrapper with "+handleClass.getSimpleName());
this.handle = handle;
this.handleClass = handleClass;
}
@Override
public String toString() {
return handle.toString();
}
}
public class WrappedNBTTagCompound extends AbstractWrapper {
protected static final Class<?> typeClass = NMSReflection.getNMSClass("NBTTagCompound");
private final HashMap<String, ?> map;
public WrappedNBTTagCompound(Object handle) {
super(handle, typeClass);
map = (HashMap) NMSReflection.getField(handle, "map");
}
public boolean isList(String s) {
return WrappedNBTTagList.typeClass.isInstance(map.get(s));
}
public boolean isCompound(String s) {
return WrappedNBTTagCompound.typeClass.isInstance(map.get(s));
}
public WrappedNBTTagCompound getCompound(String s) {
return new WrappedNBTTagCompound(map.get(s));
}
public WrappedNBTTagList getList(String s) {
return new WrappedNBTTagList(map.get(s));
}
public void remove(String s) {
map.remove(s);
}
public Set<String> getKeys() {
return map.keySet();
}
}
public class WrappedNBTTagList extends AbstractWrapper implements Iterable {
protected static final Class<?> typeClass = NMSReflection.getNMSClass("NBTTagList");
private final List list;
public WrappedNBTTagList(Object handle) {
super(handle, typeClass);
list = (List) NMSReflection.getField(handle, "list");
}
public boolean isList(int i) {
return WrappedNBTTagList.typeClass.isInstance(list.get(i));
}
public boolean isCompound(int i) {
return WrappedNBTTagCompound.typeClass.isInstance(list.get(i));
}
public WrappedNBTTagCompound getCompound(int i) {
return new WrappedNBTTagCompound(list.get(i));
}
public WrappedNBTTagList getList(int i) {
return new WrappedNBTTagList(list.get(i));
}
public void remove(int i) {
list.remove(i);
}
public int size() {
return list.size();
}
@Override
public Iterator iterator() {
return list.iterator();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment