Skip to content

Commit

Permalink
catch Throwable and send a message to player
Browse files Browse the repository at this point in the history
  • Loading branch information
deirn committed Jun 3, 2021
1 parent 52a27a9 commit 23279d6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/base/java/badasintended/megane/util/MeganeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public final class MeganeUtils {
private static final int[] EMPTY_INT_ARRAY = new int[0];

public static final String MODID = "megane";
public static final String ISSUE_URL = "https://github.com/badasintended/megane/issues";
public static final Logger LOGGER = LogManager.getLogger(MODID);

public static final int CONFIG_VERSION = 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,26 @@
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.ClickEvent;
import net.minecraft.text.HoverEvent;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.Util;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

import static badasintended.megane.util.MeganeUtils.ISSUE_URL;
import static badasintended.megane.util.MeganeUtils.LOGGER;
import static net.minecraft.util.registry.Registry.BLOCK;

public abstract class BlockData implements IServerDataProvider<BlockEntity> {

private static final Text ERROR_TEXT = new LiteralText("Something went wrong when retrieving data for this block").styled(style -> style
.withColor(Formatting.RED)
.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new LiteralText("Click me to open an issue at GitHub")))
.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, ISSUE_URL)));

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

Expand All @@ -38,10 +50,11 @@ public final void appendServerData(CompoundTag data, ServerPlayerEntity player,

try {
append(data, player, world, blockEntity);
} catch (Exception e) {
} catch (Throwable t) {
BlockPos pos = blockEntity.getPos();
player.sendSystemMessage(ERROR_TEXT, Util.NIL_UUID);
LOGGER.error("Something went wrong when retrieving data for {} at ({}, {}, {})", blockEntity.getClass().getName(), pos.getX(), pos.getY(), pos.getZ());
LOGGER.error("Stacktrace:", e);
LOGGER.error("Stacktrace:", t);

if (registry != null) {
registry.error(blockEntity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,26 @@
import net.minecraft.entity.LivingEntity;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.ClickEvent;
import net.minecraft.text.HoverEvent;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.Util;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;

import static badasintended.megane.util.MeganeUtils.ISSUE_URL;
import static badasintended.megane.util.MeganeUtils.LOGGER;
import static net.minecraft.util.registry.Registry.ENTITY_TYPE;

public abstract class EntityData implements IServerDataProvider<LivingEntity> {

private static final Text ERROR_TEXT = new LiteralText("Something went wrong when retrieving data for this entity").styled(style -> style
.withColor(Formatting.RED)
.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new LiteralText("Click me to open an issue at GitHub")))
.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, ISSUE_URL)));

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

Expand All @@ -38,10 +50,11 @@ public final void appendServerData(CompoundTag data, ServerPlayerEntity player,

try {
append(data, player, world, entity);
} catch (Exception e) {
} catch (Throwable t) {
Vec3d pos = entity.getPos();
player.sendSystemMessage(ERROR_TEXT, Util.NIL_UUID);
LOGGER.error("Something went wrong when retrieving data for {} at ({}, {}, {})", entity.getClass().getName(), pos.getX(), pos.getY(), pos.getZ());
LOGGER.error("Stacktrace:", e);
LOGGER.error("Stacktrace:", t);

if (registry != null) {
registry.error(entity);
Expand Down

0 comments on commit 23279d6

Please sign in to comment.