use of com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutUpdateSignHandle in project BKCommonLib by bergerhealer.
the class BlockStateChangePacketHandler_1_8_to_1_9_2 method enable.
@Override
public void enable() {
register(PacketType.OUT_UPDATE_SIGN, (player, commonPacket, listener) -> {
final PacketPlayOutUpdateSignHandle packet = PacketPlayOutUpdateSignHandle.createHandle(commonPacket.getHandle());
// Stores decoded String lines that it was before, for easier comparison
// Only set if the deferred metadata supplier is ever called
final AtomicReference<String[]> linesBeforeRef = new AtomicReference<>();
// Initialize the metadata once called
final DeferredSupplier<CommonTagCompound> metadataSupplier = DeferredSupplier.of(() -> {
ChatText[] lines = packet.getLines();
CommonTagCompound metadata = new CommonTagCompound();
String[] linesBefore = new String[4];
for (int n = 0; n < 4; n++) {
metadata.putValue(LINE_META_KEYS[n], linesBefore[n] = lines[n].getJson());
}
linesBeforeRef.set(linesBefore);
return metadata;
});
// Errors are handled upstream
if (!listener.onBlockChange(player, BlockStateChange.deferred(packet.getPosition(), BlockStateType.SIGN, metadataSupplier, () -> true))) {
return false;
}
// Only if getMetadata() was ever even called
if (metadataSupplier.isInitialized()) {
// Check if the lines in metadata changed compared to the lines in the packet
// If they are, write them all back (re-serialize them as chat components)
// Don't touch lines that weren't modified to retain any original json formatting
CommonTagCompound metadata = metadataSupplier.get();
String[] linesBefore = linesBeforeRef.get();
ChatText[] newLines = packet.getLines().clone();
boolean changed = false;
for (int n = 0; n < 4; n++) {
String newLine = metadata.getValue(LINE_META_KEYS[n], "");
if (!linesBefore[n].equals(newLine)) {
newLines[n] = ChatText.fromJson(newLine);
changed = true;
}
}
if (changed) {
packet.setLines(newLines);
}
}
return true;
});
}
Aggregations