Skip to content

Commit

Permalink
wrap server data provider with try-catch
Browse files Browse the repository at this point in the history
  • Loading branch information
deirn committed Jun 3, 2021
1 parent f485fe7 commit 52a27a9
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,50 @@
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<BlockEntity> {

private final Registry<?> registry;
private final Supplier<MeganeConfig.Base> baseConfig;

public BlockData(Supplier<MeganeConfig.Base> baseConfig) {
this(null, baseConfig);
}

public BlockData(Registry<?> registry, Supplier<MeganeConfig.Base> baseConfig) {
this.registry = registry;
this.baseConfig = baseConfig;
}

abstract void append(CompoundTag data, ServerPlayerEntity player, World world, BlockEntity blockEntity);

@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);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
public class BlockInventoryData extends BlockData {

public BlockInventoryData() {
super(() -> config().inventory);
super(Registrar.INVENTORY, () -> config().inventory);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
public class EnergyData extends BlockData {

public EnergyData() {
super(() -> config().energy);
super(Registrar.ENERGY, () -> config().energy);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
public class FluidData extends BlockData {

public FluidData() {
super(() -> config().fluid);
super(Registrar.FLUID, () -> config().fluid);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
public class ProgressData extends BlockData {

public ProgressData() {
super(() -> config().progress);
super(Registrar.PROGRESS, () -> config().progress);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,50 @@
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<LivingEntity> {

private final Registry<?> registry;
private final Supplier<MeganeConfig.Base> baseConfig;

public EntityData(Supplier<MeganeConfig.Base> baseConfig) {
this(null, baseConfig);
}

public EntityData(Registry<?> registry, Supplier<MeganeConfig.Base> baseConfig) {
this.registry = registry;
this.baseConfig = baseConfig;
}

abstract void append(CompoundTag data, ServerPlayerEntity player, World world, LivingEntity entity);

@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);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
public class EntityInventoryData extends EntityData {

public EntityInventoryData() {
super(() -> config().entityInventory);
super(Registrar.INVENTORY, () -> config().entityInventory);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,6 +18,8 @@ public class Registry<T> {

private final List<Entry<?>> sorter = new ObjectArrayList<>();

private final Set<Class<?>> error = new HashSet<>();

public void add(Class<?> key, T val, int priority) {
map.computeIfAbsent(key, k -> new ObjectArrayList<>())
.add(new Entry<>(val, priority));
Expand All @@ -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<T> get(Object obj) {
if (objMap.containsKey(obj)) {
Expand All @@ -36,7 +47,7 @@ public List<T> get(Object obj) {
}

Class<?> clazz = obj.getClass();
if (clazz == Object.class) {
if (clazz == Object.class || error.contains(clazz)) {
return ObjectLists.emptyList();
}

Expand Down

0 comments on commit 52a27a9

Please sign in to comment.