diff --git a/src/runtime/java/badasintended/megane/runtime/data/block/BlockData.java b/src/runtime/java/badasintended/megane/runtime/data/block/BlockData.java index 91e10ee..e2df9a2 100644 --- a/src/runtime/java/badasintended/megane/runtime/data/block/BlockData.java +++ b/src/runtime/java/badasintended/megane/runtime/data/block/BlockData.java @@ -3,18 +3,28 @@ import java.util.function.Supplier; import badasintended.megane.config.MeganeConfig; +import badasintended.megane.runtime.registry.Registry; import mcp.mobius.waila.api.IServerDataProvider; import net.minecraft.block.entity.BlockEntity; import net.minecraft.nbt.CompoundTag; import net.minecraft.server.network.ServerPlayerEntity; -import net.minecraft.util.registry.Registry; +import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; +import static badasintended.megane.util.MeganeUtils.LOGGER; +import static net.minecraft.util.registry.Registry.BLOCK; + public abstract class BlockData implements IServerDataProvider { + private final Registry registry; private final Supplier baseConfig; public BlockData(Supplier baseConfig) { + this(null, baseConfig); + } + + public BlockData(Registry registry, Supplier baseConfig) { + this.registry = registry; this.baseConfig = baseConfig; } @@ -22,11 +32,21 @@ public BlockData(Supplier baseConfig) { @Override public final void appendServerData(CompoundTag data, ServerPlayerEntity player, World world, BlockEntity blockEntity) { - if (!baseConfig.get().isEnabled() || baseConfig.get().getBlacklist().contains(Registry.BLOCK.getId(blockEntity.getCachedState().getBlock()))) { + if (!baseConfig.get().isEnabled() || baseConfig.get().getBlacklist().contains(BLOCK.getId(blockEntity.getCachedState().getBlock()))) { return; } - append(data, player, world, blockEntity); + try { + append(data, player, world, blockEntity); + } catch (Exception e) { + BlockPos pos = blockEntity.getPos(); + LOGGER.error("Something went wrong when retrieving data for {} at ({}, {}, {})", blockEntity.getClass().getName(), pos.getX(), pos.getY(), pos.getZ()); + LOGGER.error("Stacktrace:", e); + + if (registry != null) { + registry.error(blockEntity); + } + } } } diff --git a/src/runtime/java/badasintended/megane/runtime/data/block/BlockInventoryData.java b/src/runtime/java/badasintended/megane/runtime/data/block/BlockInventoryData.java index 525ddd0..1512cec 100644 --- a/src/runtime/java/badasintended/megane/runtime/data/block/BlockInventoryData.java +++ b/src/runtime/java/badasintended/megane/runtime/data/block/BlockInventoryData.java @@ -23,7 +23,7 @@ public class BlockInventoryData extends BlockData { public BlockInventoryData() { - super(() -> config().inventory); + super(Registrar.INVENTORY, () -> config().inventory); } @Override diff --git a/src/runtime/java/badasintended/megane/runtime/data/block/EnergyData.java b/src/runtime/java/badasintended/megane/runtime/data/block/EnergyData.java index a28956b..31cb11a 100644 --- a/src/runtime/java/badasintended/megane/runtime/data/block/EnergyData.java +++ b/src/runtime/java/badasintended/megane/runtime/data/block/EnergyData.java @@ -17,7 +17,7 @@ public class EnergyData extends BlockData { public EnergyData() { - super(() -> config().energy); + super(Registrar.ENERGY, () -> config().energy); } @Override diff --git a/src/runtime/java/badasintended/megane/runtime/data/block/FluidData.java b/src/runtime/java/badasintended/megane/runtime/data/block/FluidData.java index a632ee5..a63b941 100644 --- a/src/runtime/java/badasintended/megane/runtime/data/block/FluidData.java +++ b/src/runtime/java/badasintended/megane/runtime/data/block/FluidData.java @@ -21,7 +21,7 @@ public class FluidData extends BlockData { public FluidData() { - super(() -> config().fluid); + super(Registrar.FLUID, () -> config().fluid); } @Override diff --git a/src/runtime/java/badasintended/megane/runtime/data/block/ProgressData.java b/src/runtime/java/badasintended/megane/runtime/data/block/ProgressData.java index fa06c2f..79e417c 100644 --- a/src/runtime/java/badasintended/megane/runtime/data/block/ProgressData.java +++ b/src/runtime/java/badasintended/megane/runtime/data/block/ProgressData.java @@ -27,7 +27,7 @@ public class ProgressData extends BlockData { public ProgressData() { - super(() -> config().progress); + super(Registrar.PROGRESS, () -> config().progress); } @Override diff --git a/src/runtime/java/badasintended/megane/runtime/data/entity/EntityData.java b/src/runtime/java/badasintended/megane/runtime/data/entity/EntityData.java index 7b9398b..f8f82ca 100644 --- a/src/runtime/java/badasintended/megane/runtime/data/entity/EntityData.java +++ b/src/runtime/java/badasintended/megane/runtime/data/entity/EntityData.java @@ -3,18 +3,28 @@ import java.util.function.Supplier; import badasintended.megane.config.MeganeConfig; +import badasintended.megane.runtime.registry.Registry; import mcp.mobius.waila.api.IServerDataProvider; import net.minecraft.entity.LivingEntity; import net.minecraft.nbt.CompoundTag; import net.minecraft.server.network.ServerPlayerEntity; -import net.minecraft.util.registry.Registry; +import net.minecraft.util.math.Vec3d; import net.minecraft.world.World; +import static badasintended.megane.util.MeganeUtils.LOGGER; +import static net.minecraft.util.registry.Registry.ENTITY_TYPE; + public abstract class EntityData implements IServerDataProvider { + private final Registry registry; private final Supplier baseConfig; public EntityData(Supplier baseConfig) { + this(null, baseConfig); + } + + public EntityData(Registry registry, Supplier baseConfig) { + this.registry = registry; this.baseConfig = baseConfig; } @@ -22,11 +32,21 @@ public EntityData(Supplier baseConfig) { @Override public final void appendServerData(CompoundTag data, ServerPlayerEntity player, World world, LivingEntity entity) { - if (!baseConfig.get().isEnabled() || baseConfig.get().getBlacklist().contains(Registry.ENTITY_TYPE.getId(entity.getType()))) { + if (!baseConfig.get().isEnabled() || baseConfig.get().getBlacklist().contains(ENTITY_TYPE.getId(entity.getType()))) { return; } - append(data, player, world, entity); + try { + append(data, player, world, entity); + } catch (Exception e) { + Vec3d pos = entity.getPos(); + LOGGER.error("Something went wrong when retrieving data for {} at ({}, {}, {})", entity.getClass().getName(), pos.getX(), pos.getY(), pos.getZ()); + LOGGER.error("Stacktrace:", e); + + if (registry != null) { + registry.error(entity); + } + } } } diff --git a/src/runtime/java/badasintended/megane/runtime/data/entity/EntityInventoryData.java b/src/runtime/java/badasintended/megane/runtime/data/entity/EntityInventoryData.java index 3f60931..097c4d8 100644 --- a/src/runtime/java/badasintended/megane/runtime/data/entity/EntityInventoryData.java +++ b/src/runtime/java/badasintended/megane/runtime/data/entity/EntityInventoryData.java @@ -23,7 +23,7 @@ public class EntityInventoryData extends EntityData { public EntityInventoryData() { - super(() -> config().entityInventory); + super(Registrar.INVENTORY, () -> config().entityInventory); } @Override diff --git a/src/runtime/java/badasintended/megane/runtime/registry/Registry.java b/src/runtime/java/badasintended/megane/runtime/registry/Registry.java index 7e0b69a..b7a3564 100644 --- a/src/runtime/java/badasintended/megane/runtime/registry/Registry.java +++ b/src/runtime/java/badasintended/megane/runtime/registry/Registry.java @@ -1,8 +1,10 @@ package badasintended.megane.runtime.registry; import java.util.Comparator; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; import it.unimi.dsi.fastutil.objects.ObjectArrayList; @@ -16,6 +18,8 @@ public class Registry { private final List> sorter = new ObjectArrayList<>(); + private final Set> error = new HashSet<>(); + public void add(Class key, T val, int priority) { map.computeIfAbsent(key, k -> new ObjectArrayList<>()) .add(new Entry<>(val, priority)); @@ -25,6 +29,13 @@ public void add(Object key, T val) { objMap.put(key, ObjectLists.singleton(val)); } + public void error(Object obj) { + Class clazz = obj.getClass(); + error.add(clazz); + cache.remove(clazz); + map.remove(clazz); + } + @SuppressWarnings("unchecked") public List get(Object obj) { if (objMap.containsKey(obj)) { @@ -36,7 +47,7 @@ public List get(Object obj) { } Class clazz = obj.getClass(); - if (clazz == Object.class) { + if (clazz == Object.class || error.contains(clazz)) { return ObjectLists.emptyList(); }