Fix crafting CPU name comparision and implement ITextComponent container sync.

This commit is contained in:
Sebastian Hartte
2020-06-19 00:08:17 +02:00
parent 3faa3db2b6
commit f2a9d8c806
6 changed files with 116 additions and 72 deletions
@@ -137,9 +137,8 @@ public class GuiCraftConfirm extends AEBaseGui<ContainerCraftConfirm> {
String btnTextText = GuiText.CraftingCPU.getLocal() + ": " + GuiText.Automatic.getLocal(); String btnTextText = GuiText.CraftingCPU.getLocal() + ": " + GuiText.Automatic.getLocal();
if (this.container.getSelectedCpu() >= 0)// && status.selectedCpu < status.cpus.size() ) if (this.container.getSelectedCpu() >= 0)// && status.selectedCpu < status.cpus.size() )
{ {
if (this.container.getName().length() > 0) { if (this.container.getName() != null) {
final String name = this.container.getName().substring(0, final String name = this.container.getName().getStringTruncated(20);
Math.min(20, this.container.getName().length()));
btnTextText = GuiText.CraftingCPU.getLocal() + ": " + name; btnTextText = GuiText.CraftingCPU.getLocal() + ": " + name;
} else { } else {
btnTextText = GuiText.CraftingCPU.getLocal() + ": #" + this.container.getSelectedCpu(); btnTextText = GuiText.CraftingCPU.getLocal() + ": #" + this.container.getSelectedCpu();
@@ -70,8 +70,8 @@ public class GuiCraftingStatus extends GuiCraftingCPU<ContainerCraftingStatus> {
if (this.container.selectedCpu >= 0)// && status.selectedCpu < status.cpus.size() ) if (this.container.selectedCpu >= 0)// && status.selectedCpu < status.cpus.size() )
{ {
if (this.container.myName.length() > 0) { if (this.container.myName != null) {
final String name = this.container.myName.substring(0, Math.min(20, this.container.myName.length())); final String name = this.container.myName.getStringTruncated(20);
btnTextText = GuiText.CPUs.getLocal() + ": " + name; btnTextText = GuiText.CPUs.getLocal() + ": " + name;
} else { } else {
btnTextText = GuiText.CPUs.getLocal() + ": #" + this.container.selectedCpu; btnTextText = GuiText.CPUs.getLocal() + ": #" + this.container.selectedCpu;
@@ -18,12 +18,14 @@
package appeng.container.guisync; package appeng.container.guisync;
import java.io.IOException; import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.EnumSet; import java.util.Objects;
import net.minecraft.entity.player.ServerPlayerEntity; import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.inventory.container.IContainerListener; import net.minecraft.inventory.container.IContainerListener;
import net.minecraft.util.text.ITextComponent;
import appeng.container.AEBaseContainer; import appeng.container.AEBaseContainer;
import appeng.core.AELog; import appeng.core.AELog;
@@ -31,18 +33,33 @@ import appeng.core.sync.network.NetworkHandler;
import appeng.core.sync.packets.PacketProgressBar; import appeng.core.sync.packets.PacketProgressBar;
import appeng.core.sync.packets.PacketValueConfig; import appeng.core.sync.packets.PacketValueConfig;
/**
* This class is responsible for synchronizing Container-fields from server to
* client.
*/
public class SyncData { public class SyncData {
private final AEBaseContainer source; private final AEBaseContainer source;
private final Field field; private final Field field;
private final Class<?> fieldType;
private final int channel; private final int channel;
private final MethodHandle getter;
private final MethodHandle setter;
private Object clientVersion; private Object clientVersion;
public SyncData(final AEBaseContainer container, final Field field, final GuiSync annotation) { public SyncData(final AEBaseContainer container, final Field field, final GuiSync annotation) {
this.clientVersion = null; this.clientVersion = null;
this.source = container; this.source = container;
this.field = field;
this.channel = annotation.value(); this.channel = annotation.value();
this.field = field;
this.fieldType = field.getType();
try {
this.getter = MethodHandles.publicLookup().unreflectGetter(field);
this.setter = MethodHandles.publicLookup().unreflectSetter(field);
} catch (IllegalAccessException e) {
throw new RuntimeException(
"Failed to get accessor for field " + field + ". Did you forget to make it public?");
}
} }
public int getChannel() { public int getChannel() {
@@ -50,96 +67,119 @@ public class SyncData {
} }
public void tick(final IContainerListener c) { public void tick(final IContainerListener c) {
try { try {
final Object val = this.field.get(this.source); final Object val = this.getter.invoke(source);
if (val != null && this.clientVersion == null) { if (!Objects.equals(val, this.clientVersion)) {
this.send(c, val);
} else if (!val.equals(this.clientVersion)) {
this.send(c, val); this.send(c, val);
} }
} catch (final IllegalArgumentException | IllegalAccessException e) { } catch (Throwable e) {
AELog.debug(e); AELog.debug(e);
} }
} }
private void send(final IContainerListener o, final Object val) { private void send(final IContainerListener o, Object val) {
if (val instanceof String) { if (fieldType.isAssignableFrom(ITextComponent.class)) {
if (o instanceof ServerPlayerEntity) {
String json = "";
if (val != null) {
json = ITextComponent.Serializer.toJson((ITextComponent) val);
}
NetworkHandler.instance().sendTo(new PacketValueConfig("SyncDat." + this.channel, json),
(ServerPlayerEntity) o);
}
}
// Types other than ITextComponent must be non-null
if (val == null) {
return;
}
if (fieldType.equals(String.class)) {
if (o instanceof ServerPlayerEntity) { if (o instanceof ServerPlayerEntity) {
NetworkHandler.instance().sendTo(new PacketValueConfig("SyncDat." + this.channel, (String) val), NetworkHandler.instance().sendTo(new PacketValueConfig("SyncDat." + this.channel, (String) val),
(ServerPlayerEntity) o); (ServerPlayerEntity) o);
} }
} else if (this.field.getType().isEnum()) { } else if (this.fieldType.isEnum()) {
o.sendWindowProperty(this.source, this.channel, ((Enum) val).ordinal()); o.sendWindowProperty(this.source, this.channel, ((Enum<?>) val).ordinal());
} else if (val instanceof Long || val.getClass() == long.class) { } else if (val instanceof Long) {
if (o instanceof ServerPlayerEntity) { if (o instanceof ServerPlayerEntity) {
NetworkHandler.instance().sendTo(new PacketProgressBar(this.channel, (Long) val), NetworkHandler.instance().sendTo(new PacketProgressBar(this.channel, (Long) val),
(ServerPlayerEntity) o); (ServerPlayerEntity) o);
} }
} else if (val instanceof Boolean || val.getClass() == boolean.class) { } else if (fieldType.equals(Boolean.class) || fieldType.equals(boolean.class)) {
o.sendWindowProperty(this.source, this.channel, ((Boolean) val) ? 1 : 0); o.sendWindowProperty(this.source, this.channel, ((Boolean) val) ? 1 : 0);
} else { } else if (fieldType.equals(Integer.class) || fieldType.equals(int.class)) {
o.sendWindowProperty(this.source, this.channel, (Integer) val); o.sendWindowProperty(this.source, this.channel, (Integer) val);
} else {
throw new IllegalStateException("Unknown field type: " + fieldType);
} }
this.clientVersion = val; this.clientVersion = val;
} }
public void update(final Object val) { public void update(Object val) {
try { try {
final Object oldValue = this.field.get(this.source); final Object oldValue = this.getter.invoke(source);
if (val instanceof String) { if (val instanceof String) {
this.updateString(oldValue, (String) val); if (this.fieldType.isAssignableFrom(ITextComponent.class)) {
String json = (String) val;
ITextComponent text = null;
if (!json.isEmpty()) {
text = ITextComponent.Serializer.fromJson((String) val);
}
this.updateTextComponent(text);
} else {
this.updateString((String) val);
}
} else { } else {
this.updateValue(oldValue, (Long) val); this.updateValue(oldValue, (Long) val);
} }
} catch (final IllegalArgumentException e) { } catch (Throwable e) {
AELog.debug(e);
} catch (final IllegalAccessException e) {
AELog.debug(e); AELog.debug(e);
} }
} }
private void updateString(final Object oldValue, final String val) { private void updateString(final String val) {
try { try {
this.field.set(this.source, val); this.setter.invoke(source, val);
} catch (final IllegalArgumentException e) { } catch (Throwable e) {
AELog.debug(e); AELog.debug(e);
} catch (final IllegalAccessException e) { }
}
private void updateTextComponent(final ITextComponent val) {
try {
this.setter.invoke(source, val);
} catch (Throwable e) {
AELog.debug(e); AELog.debug(e);
} }
} }
private void updateValue(final Object oldValue, final long val) { private void updateValue(final Object oldValue, final long val) {
try { try {
if (this.field.getType().isEnum()) { if (this.fieldType.isEnum()) {
final EnumSet<? extends Enum> valList = EnumSet.allOf((Class<? extends Enum>) this.field.getType()); Object e = this.fieldType.getEnumConstants()[(int) val];
for (final Enum e : valList) { this.setter.invoke(source, e);
if (e.ordinal() == val) {
this.field.set(this.source, e);
break;
}
}
} else { } else {
if (this.field.getType().equals(int.class)) { if (this.fieldType.equals(int.class)) {
this.field.set(this.source, (int) val); this.setter.invoke(source, (int) val);
} else if (this.field.getType().equals(long.class)) { } else if (this.fieldType.equals(long.class)) {
this.field.set(this.source, val); this.setter.invoke(source, val);
} else if (this.field.getType().equals(boolean.class)) { } else if (this.fieldType.equals(boolean.class)) {
this.field.set(this.source, val == 1); this.setter.invoke(source, val == 1);
} else if (this.field.getType().equals(Integer.class)) { } else if (this.fieldType.equals(Integer.class)) {
this.field.set(this.source, (int) val); this.setter.invoke(source, (int) val);
} else if (this.field.getType().equals(Long.class)) { } else if (this.fieldType.equals(Long.class)) {
this.field.set(this.source, val); this.setter.invoke(source, val);
} else if (this.field.getType().equals(Boolean.class)) { } else if (this.fieldType.equals(Boolean.class)) {
this.field.set(this.source, val == 1); this.setter.invoke(source, val == 1);
} }
} }
this.source.onUpdate(this.field.getName(), oldValue, this.field.get(this.source)); this.source.onUpdate(this.field.getName(), oldValue, this.getter.invoke(source));
} catch (final IllegalArgumentException e) { } catch (Throwable e) {
AELog.debug(e);
} catch (final IllegalAccessException e) {
AELog.debug(e); AELog.debug(e);
} }
} }
@@ -24,6 +24,7 @@ import java.util.Collections;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
@@ -33,6 +34,7 @@ import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.inventory.container.ContainerType; import net.minecraft.inventory.container.ContainerType;
import net.minecraft.inventory.container.IContainerListener; import net.minecraft.inventory.container.IContainerListener;
import net.minecraft.network.PacketBuffer; import net.minecraft.network.PacketBuffer;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent; import net.minecraft.util.text.StringTextComponent;
import net.minecraft.world.World; import net.minecraft.world.World;
@@ -99,7 +101,7 @@ public class ContainerCraftConfirm extends AEBaseContainer {
@GuiSync(6) @GuiSync(6)
public boolean noCPU = true; public boolean noCPU = true;
@GuiSync(7) @GuiSync(7)
public String myName = ""; public ITextComponent myName;
public ContainerCraftConfirm(int id, PlayerInventory ip, ITerminalHost te) { public ContainerCraftConfirm(int id, PlayerInventory ip, ITerminalHost te) {
super(TYPE, id, ip, te); super(TYPE, id, ip, te);
@@ -121,11 +123,12 @@ public class ContainerCraftConfirm extends AEBaseContainer {
if (this.getSelectedCpu() == -1) { if (this.getSelectedCpu() == -1) {
this.setCpuAvailableBytes(0); this.setCpuAvailableBytes(0);
this.setCpuCoProcessors(0); this.setCpuCoProcessors(0);
this.setName(""); this.setName(null);
} else { } else {
this.setName(this.cpus.get(this.getSelectedCpu()).getName().getString()); CraftingCPURecord cpu = this.cpus.get(this.getSelectedCpu());
this.setCpuAvailableBytes(this.cpus.get(this.getSelectedCpu()).getSize()); this.setName(cpu.getName());
this.setCpuCoProcessors(this.cpus.get(this.getSelectedCpu()).getProcessors()); this.setCpuAvailableBytes(cpu.getSize());
this.setCpuCoProcessors(cpu.getProcessors());
} }
} }
@@ -145,6 +148,7 @@ public class ContainerCraftConfirm extends AEBaseContainer {
for (final CraftingCPURecord ccr : this.cpus) { for (final CraftingCPURecord ccr : this.cpus) {
if (ccr.getCpu() == c) { if (ccr.getCpu() == c) {
found = true; found = true;
break;
} }
} }
@@ -280,11 +284,12 @@ public class ContainerCraftConfirm extends AEBaseContainer {
this.setSelectedCpu(-1); this.setSelectedCpu(-1);
this.setCpuAvailableBytes(0); this.setCpuAvailableBytes(0);
this.setCpuCoProcessors(0); this.setCpuCoProcessors(0);
this.setName(""); this.setName(null);
} else if (this.getSelectedCpu() != -1) { } else if (this.getSelectedCpu() != -1) {
this.setName(this.cpus.get(this.getSelectedCpu()).getName().getString()); CraftingCPURecord cpu = this.cpus.get(this.getSelectedCpu());
this.setCpuAvailableBytes(this.cpus.get(this.getSelectedCpu()).getSize()); this.setName(cpu.getName());
this.setCpuCoProcessors(this.cpus.get(this.getSelectedCpu()).getProcessors()); this.setCpuAvailableBytes(cpu.getSize());
this.setCpuCoProcessors(cpu.getProcessors());
} }
} }
@@ -386,11 +391,11 @@ public class ContainerCraftConfirm extends AEBaseContainer {
this.selectedCpu = selectedCpu; this.selectedCpu = selectedCpu;
} }
public String getName() { public ITextComponent getName() {
return this.myName; return this.myName;
} }
private void setName(@Nonnull final String myName) { private void setName(@Nullable final ITextComponent myName) {
this.myName = myName; this.myName = myName;
} }
@@ -28,6 +28,7 @@ import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory; import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.container.ContainerType; import net.minecraft.inventory.container.ContainerType;
import net.minecraft.network.PacketBuffer; import net.minecraft.network.PacketBuffer;
import net.minecraft.util.text.ITextComponent;
import appeng.api.config.SecurityPermissions; import appeng.api.config.SecurityPermissions;
import appeng.api.networking.crafting.ICraftingCPU; import appeng.api.networking.crafting.ICraftingCPU;
@@ -58,7 +59,7 @@ public class ContainerCraftingStatus extends ContainerCraftingCPU {
@GuiSync(6) @GuiSync(6)
public boolean noCPU = true; public boolean noCPU = true;
@GuiSync(7) @GuiSync(7)
public String myName = ""; public ITextComponent myName;
public ContainerCraftingStatus(int id, final PlayerInventory ip, final ITerminalHost te) { public ContainerCraftingStatus(int id, final PlayerInventory ip, final ITerminalHost te) {
super(TYPE, id, ip, te); super(TYPE, id, ip, te);
@@ -117,9 +118,9 @@ public class ContainerCraftingStatus extends ContainerCraftingCPU {
if (this.selectedCpu >= this.cpus.size()) { if (this.selectedCpu >= this.cpus.size()) {
this.selectedCpu = -1; this.selectedCpu = -1;
this.myName = ""; this.myName = null;
} else if (this.selectedCpu != -1) { } else if (this.selectedCpu != -1) {
this.myName = this.cpus.get(this.selectedCpu).getName().getString(); this.myName = this.cpus.get(this.selectedCpu).getName();
} }
if (this.selectedCpu == -1 && this.cpus.size() > 0) { if (this.selectedCpu == -1 && this.cpus.size() > 0) {
@@ -153,10 +154,10 @@ public class ContainerCraftingStatus extends ContainerCraftingCPU {
} }
if (this.selectedCpu == -1) { if (this.selectedCpu == -1) {
this.myName = ""; this.myName = null;
this.setCPU(null); this.setCPU(null);
} else { } else {
this.myName = this.cpus.get(this.selectedCpu).getName().getString(); this.myName = this.cpus.get(this.selectedCpu).getName();
this.setCPU(this.cpus.get(this.selectedCpu).getCpu()); this.setCPU(this.cpus.get(this.selectedCpu).getCpu());
} }
} }
@@ -61,7 +61,6 @@ public class PacketValueConfig extends AppEngPacket {
public PacketValueConfig(final PacketBuffer stream) { public PacketValueConfig(final PacketBuffer stream) {
this.Name = stream.readString(); this.Name = stream.readString();
this.Value = stream.readString(); this.Value = stream.readString();
// dis.close();
} }
// api // api
@@ -202,7 +201,7 @@ public class PacketValueConfig extends AppEngPacket {
} else if (this.Name.equals("CraftingStatus") && this.Value.equals("Clear")) { } else if (this.Name.equals("CraftingStatus") && this.Value.equals("Clear")) {
final Screen gs = Minecraft.getInstance().currentScreen; final Screen gs = Minecraft.getInstance().currentScreen;
if (gs instanceof GuiCraftingCPU) { if (gs instanceof GuiCraftingCPU) {
((GuiCraftingCPU) gs).clearItems(); ((GuiCraftingCPU<?>) gs).clearItems();
} }
} else if (c instanceof IConfigurableObject) { } else if (c instanceof IConfigurableObject) {
final IConfigManager cm = ((IConfigurableObject) c).getConfigManager(); final IConfigManager cm = ((IConfigurableObject) c).getConfigManager();