fix: empty string caused channel crash && check if custom name exist before getting

This commit is contained in:
Fox_white committed 2023-08-08 02:03:08 +08:00
1 parent b019050f32
commit f1f0940b1c
2 files changed
+15 -3

No files matched your search

@@ -2,6 +2,8 @@ package com.glodblock.github.network;
import static com.glodblock.github.common.item.ItemWirelessUltraTerminal.getGuis;
import java.util.Objects;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.DimensionManager;
@@ -97,15 +99,23 @@ public class CPacketRenamer implements IMessage {
public static class Handler implements IMessageHandler<CPacketRenamer, IMessage> {
private String _getName(ICustomNameObject obj) {
// "‎" is a zero size character, sent because simple channel does not support empty string
return obj.hasCustomName() ? obj.getCustomName() : "\u200E";
}
private String getName(TileEntity tile, ForgeDirection side) {
if (tile instanceof TileCableBus) {
return ((ICustomNameObject) ((TileCableBus) tile).getPart(side)).getCustomName();
return _getName((ICustomNameObject) ((TileCableBus) tile).getPart(side));
} else {
return ((ICustomNameObject) tile).getCustomName();
return _getName((ICustomNameObject) tile);
}
}
private void setName(TileEntity tile, ForgeDirection side, String text) {
if (Objects.equals(text, "\u200E")) {
text = "";
}
if (tile instanceof TileCableBus) {
((ICustomNameObject) ((TileCableBus) tile).getPart(side)).setCustomName(text);
} else {
@@ -1,5 +1,7 @@
package com.glodblock.github.network;
import java.util.Objects;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
@@ -53,7 +55,7 @@ public class SPacketStringUpdate implements IMessage {
public IMessage onMessage(SPacketStringUpdate message, MessageContext ctx) {
final GuiScreen gs = Minecraft.getMinecraft().currentScreen;
if (gs instanceof GuiRenamer) {
((GuiRenamer) gs).postUpdate(message.text);
((GuiRenamer) gs).postUpdate(Objects.equals(message.text, "\u200E") ? "" : message.text);
}
return null;
}