Removed book swaps in favor of interface

This commit is contained in:
Arthur Uzulin committed 2019-09-30 03:38:14 +10:00
1 parent 41777b6e57
commit 4e7741f2a1
8 files changed
+82 -96

No files matched your search

@@ -25,7 +25,7 @@ public class LinkedSectionData extends SectionData {
this.pages.addAll(section.pages);
}
this.icon.location = this.source.getResourceLocation(this.icon.file, true);
this.icon.load(source);
}
public void addSection(SectionData data) {
@@ -1,26 +1,21 @@
package slimeknights.mantle.client.book.data;
import net.minecraft.client.renderer.texture.AtlasTexture;
import net.minecraft.resources.IResource;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import slimeknights.mantle.client.book.BookLoader;
import slimeknights.mantle.client.book.data.content.ContentError;
import slimeknights.mantle.client.book.data.content.PageContent;
import slimeknights.mantle.client.book.data.element.BlockData;
import slimeknights.mantle.client.book.data.element.DataLocation;
import slimeknights.mantle.client.book.data.element.ItemStackData;
import slimeknights.mantle.client.book.data.element.IDataElement;
import slimeknights.mantle.client.book.repository.BookRepository;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
@OnlyIn(Dist.CLIENT)
public class PageData implements IDataItem {
private static final transient ArrayList<ValueHotswap> hotswaps = new ArrayList<>();
public String name = null;
public String type = "";
public String data = "";
@@ -91,101 +86,49 @@ public class PageData implements IDataItem {
this.content.source = this.source;
for (Field f : this.content.getClass().getFields()) {
if (Modifier.isTransient(f.getModifiers()) || Modifier.isStatic(f.getModifiers()) || Modifier
.isFinal(f.getModifiers())) {
continue;
}
try {
if (f.get(this.content) == null) {
continue;
}
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
for (ValueHotswap swap : hotswaps) {
Class<?> c = f.getType();
if (c.isArray() && c.getComponentType().isAssignableFrom(swap.t)) {
try {
f.setAccessible(true);
Object[] o = (Object[]) f.get(this.content);
for (Object ob : o) {
swap.swap(this.source, ob);
}
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
}
else if (swap.t.isAssignableFrom(c)) {
try {
f.setAccessible(true);
Object o = f.get(this.content);
swap.swap(this.source, o);
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
processField(f);
}
}
public static void addSwap(Class<?> t, Class<? extends ValueHotswap> swap) {
try {
ValueHotswap hotswap = swap.newInstance();
hotswap.t = t;
private void processField(Field f) {
f.setAccessible(true);
hotswaps.add(hotswap);
if (Modifier.isTransient(f.getModifiers()) || Modifier.isStatic(f.getModifiers()) || Modifier
.isFinal(f.getModifiers())) {
return;
}
catch (InstantiationException | IllegalAccessException e) {
try {
Object o = f.get(content);
if (o != null) {
processObject(o, 0);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
private void processObject(Object o, final int depth) {
if(depth > 4 || o == null)
return;
Class<?> c = o.getClass();
boolean isArray = c.isArray();
if(!isArray) {
if(IDataElement.class.isAssignableFrom(c)) {
((IDataElement)o).load(source);
}
return;
}
for(int i = 0; i < Array.getLength(o); i++){
processObject(Array.get(o, i), depth + 1);
}
}
public String getTitle() {
String title = this.parent.parent.strings.get(this.parent.name + "." + this.name);
return title == null ? this.name : title;
}
static {
addSwap(DataLocation.class, new ValueHotswap<DataLocation>() {
@Override
public void swap(BookRepository source, DataLocation object) {
object.location = object.file == "$BLOCK_ATLAS" ? AtlasTexture.LOCATION_BLOCKS_TEXTURE : source
.getResourceLocation(object.file, true);
}
}.getClass());
addSwap(ItemStackData.class, new ValueHotswap<ItemStackData>() {
@Override
public void swap(BookRepository source, ItemStackData object) {
object.source = source;
object.itemListLocation = source.getResourceLocation(object.itemList);
if (object.itemListLocation != null) {
object.id = "->itemList";
}
}
}.getClass());
addSwap(BlockData.class, new ValueHotswap<BlockData>() {
@Override
public void swap(BookRepository source, BlockData object) {
if (object.endPos == null) {
object.endPos = object.pos;
}
}
}.getClass());
}
public static abstract class ValueHotswap<T> {
protected Class<?> t;
public abstract void swap(BookRepository source, T object);
}
}
@@ -79,7 +79,7 @@ public class SectionData implements IDataItem {
page.load();
}
this.icon.location = this.source.getResourceLocation(this.icon.file, true);
this.icon.load(source);
}
/**
@@ -1,14 +1,22 @@
package slimeknights.mantle.client.book.data.element;
import com.google.gson.JsonObject;
import slimeknights.mantle.client.book.repository.BookRepository;
import java.util.Map;
public class BlockData {
public class BlockData implements IDataElement {
public int[] pos;
public int[] endPos;
public String block;
public JsonObject nbt;
public Map<String, String> state;
@Override
public void load(BookRepository source) {
if(endPos == null) {
endPos = pos;
}
}
}
@@ -1,9 +1,16 @@
package slimeknights.mantle.client.book.data.element;
import net.minecraft.client.renderer.texture.AtlasTexture;
import net.minecraft.util.ResourceLocation;
import slimeknights.mantle.client.book.repository.BookRepository;
public class DataLocation {
public class DataLocation implements IDataElement {
public String file;
public transient ResourceLocation location;
@Override
public void load(BookRepository source) {
location = "$BLOCK_ATLAS".equals(file) ? AtlasTexture.LOCATION_BLOCKS_TEXTURE : source.getResourceLocation(file, true);
}
}
@@ -0,0 +1,7 @@
package slimeknights.mantle.client.book.data.element;
import slimeknights.mantle.client.book.repository.BookRepository;
public interface IDataElement {
void load(BookRepository source);
}
@@ -3,6 +3,7 @@ package slimeknights.mantle.client.book.data.element;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import slimeknights.mantle.client.book.repository.BookRepository;
@OnlyIn(Dist.CLIENT)
public class ImageData extends DataLocation {
@@ -57,4 +58,13 @@ public class ImageData extends DataLocation {
MISSING.uw = 32;
MISSING.vh = 32;
}
@Override
public void load(BookRepository source) {
super.load(source);
if(item != null) {
item.load(source);
}
}
}
@@ -16,7 +16,7 @@ import net.minecraftforge.registries.ForgeRegistries;
import slimeknights.mantle.client.book.BookLoader;
import slimeknights.mantle.client.book.repository.BookRepository;
public class ItemStackData {
public class ItemStackData implements IDataElement {
public String itemList = null;
public transient BookRepository source;
@@ -106,6 +106,17 @@ public class ItemStackData {
return s.replaceAll("\"(\\w+)\"\\s*:", "$1: ");
}
@Override
public void load(BookRepository source) {
this.source = source;
itemListLocation = source.getResourceLocation(itemList);
if (itemListLocation != null) {
id = "->itemList";
}
}
private static class ItemsList {
public ItemStackData[] items = new ItemStackData[0];