diff --git a/build.gradle b/build.gradle index 93372fc1c..03ed7715d 100644 --- a/build.gradle +++ b/build.gradle @@ -23,7 +23,7 @@ plugins { id("com.github.breadmoirai.github-release") version("2.4.1") id("com.matthewprenger.cursegradle") version("1.4.0") id("com.modrinth.minotaur").version("2.+") - id("com.kotori316.plugin.cf").version("2.2") + id("com.kotori316.plugin.cf").version("2.3") } // Only edit below this line, the above code adds and enables the necessary things for Forge to be setup. @@ -565,6 +565,7 @@ tasks.register("registerVersion", CallVersionFunctionTask.class) { functionEndpoint = readVersionFunctionEndpoint(project) gameVersion = project.findProperty("minecraftVersion") platform = "forge" + platformVersion = project.findProperty("forgeVersion") as String modName = "quarryplus" changelog = getShortChangelog(false) homepage = "https://www.curseforge.com/minecraft/mc-mods/additional-enchanted-miner" diff --git a/src/main/scala/com/yogpc/qp/machines/advpump/TileAdvPump.java b/src/main/scala/com/yogpc/qp/machines/advpump/TileAdvPump.java index 775e636f1..ac4ff84ec 100644 --- a/src/main/scala/com/yogpc/qp/machines/advpump/TileAdvPump.java +++ b/src/main/scala/com/yogpc/qp/machines/advpump/TileAdvPump.java @@ -62,25 +62,36 @@ public TileAdvPump(BlockPos pos, BlockState state) { @Override public void saveNbtData(CompoundTag nbt) { nbt.put("storage", storage.toNbt()); + toClientTag(nbt); + nbt.put("moduleInventory", moduleInventory.serializeNBT()); + } + + @Override + public CompoundTag toClientTag(CompoundTag nbt) { nbt.putInt("y", y); nbt.put("enchantments", enchantmentEfficiency.toNbt()); nbt.putBoolean("finished", finished); nbt.putBoolean("deleteFluid", deleteFluid); nbt.putBoolean("placeFrame", placeFrame); - nbt.put("moduleInventory", moduleInventory.serializeNBT()); + return nbt; } @Override public void load(CompoundTag nbt) { super.load(nbt); storage.readNbt(nbt.getCompound("storage")); + fromClientTag(nbt); + moduleInventory.deserializeNBT(nbt.getCompound("moduleInventory")); + isBlockModuleLoaded = false; + } + + @Override + public void fromClientTag(CompoundTag nbt) { y = nbt.getInt("y"); setEnchantment(EnchantmentEfficiency.fromNbt(nbt.getCompound("enchantments"))); finished = nbt.getBoolean("finished"); deleteFluid = nbt.getBoolean("deleteFluid"); placeFrame = nbt.getBoolean("placeFrame"); - moduleInventory.deserializeNBT(nbt.getCompound("moduleInventory")); - isBlockModuleLoaded = false; } public static void tick(Level world, BlockPos pos, BlockState state, TileAdvPump pump) { @@ -231,16 +242,6 @@ public List getDebugLogs() { ).map(Component::literal)).toList(); } - @Override - public void fromClientTag(CompoundTag tag) { - load(tag); - } - - @Override - public CompoundTag toClientTag(CompoundTag tag) { - return saveWithoutMetadata(); - } - public void sync() { if (level != null && !level.isClientSide) PacketHandler.sendToClient(new ClientSyncMessage(this), level); diff --git a/src/main/scala/com/yogpc/qp/machines/marker/FlexMarkerMessage.java b/src/main/scala/com/yogpc/qp/machines/marker/FlexMarkerMessage.java index a7841d279..d5d8b37eb 100644 --- a/src/main/scala/com/yogpc/qp/machines/marker/FlexMarkerMessage.java +++ b/src/main/scala/com/yogpc/qp/machines/marker/FlexMarkerMessage.java @@ -1,8 +1,8 @@ package com.yogpc.qp.machines.marker; +import com.yogpc.qp.packet.ClientSyncMessage; import com.yogpc.qp.packet.IMessage; import com.yogpc.qp.packet.PacketHandler; -import com.yogpc.qp.packet.TileMessage; import com.yogpc.qp.utils.MapMulti; import net.minecraft.core.BlockPos; import net.minecraft.core.registries.Registries; @@ -51,7 +51,7 @@ public static void onReceive(FlexMarkerMessage message, Supplier { m.move(message.movable, message.amount); - PacketHandler.sendToClient(new TileMessage(m), Objects.requireNonNull(m.getLevel())); + PacketHandler.sendToClient(new ClientSyncMessage(m), Objects.requireNonNull(m.getLevel())); })); } } diff --git a/src/main/scala/com/yogpc/qp/machines/marker/Marker16Message.java b/src/main/scala/com/yogpc/qp/machines/marker/Marker16Message.java index ed95259d8..0147080e2 100644 --- a/src/main/scala/com/yogpc/qp/machines/marker/Marker16Message.java +++ b/src/main/scala/com/yogpc/qp/machines/marker/Marker16Message.java @@ -1,8 +1,8 @@ package com.yogpc.qp.machines.marker; +import com.yogpc.qp.packet.ClientSyncMessage; import com.yogpc.qp.packet.IMessage; import com.yogpc.qp.packet.PacketHandler; -import com.yogpc.qp.packet.TileMessage; import com.yogpc.qp.utils.MapMulti; import net.minecraft.core.BlockPos; import net.minecraft.core.registries.Registries; @@ -56,7 +56,7 @@ public static void onReceive(Marker16Message message, Supplier { m.changeSize(message.amount, message.yMax, message.yMin); - PacketHandler.sendToClient(new TileMessage(m), Objects.requireNonNull(m.getLevel())); + PacketHandler.sendToClient(new ClientSyncMessage(m), Objects.requireNonNull(m.getLevel())); })); } diff --git a/src/main/scala/com/yogpc/qp/machines/marker/Tile16Marker.java b/src/main/scala/com/yogpc/qp/machines/marker/Tile16Marker.java index ac2bd2e04..2e1cac8ed 100644 --- a/src/main/scala/com/yogpc/qp/machines/marker/Tile16Marker.java +++ b/src/main/scala/com/yogpc/qp/machines/marker/Tile16Marker.java @@ -4,6 +4,7 @@ import com.yogpc.qp.machines.Area; import com.yogpc.qp.machines.CheckerLog; import com.yogpc.qp.machines.QuarryMarker; +import com.yogpc.qp.packet.ClientSync; import com.yogpc.qp.render.Box; import com.yogpc.qp.render.RenderMarker; import net.minecraft.core.BlockPos; @@ -23,7 +24,7 @@ import java.util.Optional; import java.util.stream.Stream; -public class Tile16Marker extends BlockEntity implements QuarryMarker, CheckerLog { +public class Tile16Marker extends BlockEntity implements QuarryMarker, CheckerLog, ClientSync { private BlockPos min = BlockPos.ZERO; private BlockPos max = BlockPos.ZERO; @Nullable @@ -124,4 +125,16 @@ public List removeAndGetItems() { level.removeBlock(getBlockPos(), false); return List.of(new ItemStack(getBlockState().getBlock())); } + + @Override + public void fromClientTag(CompoundTag tag) { + load(tag); + } + + @Override + public CompoundTag toClientTag(CompoundTag tag) { + saveAdditional(tag); + return tag; + } + } diff --git a/src/main/scala/com/yogpc/qp/machines/marker/TileFlexMarker.java b/src/main/scala/com/yogpc/qp/machines/marker/TileFlexMarker.java index 99c1acf3d..46fd9d7ba 100644 --- a/src/main/scala/com/yogpc/qp/machines/marker/TileFlexMarker.java +++ b/src/main/scala/com/yogpc/qp/machines/marker/TileFlexMarker.java @@ -5,6 +5,7 @@ import com.yogpc.qp.machines.Area; import com.yogpc.qp.machines.CheckerLog; import com.yogpc.qp.machines.QuarryMarker; +import com.yogpc.qp.packet.ClientSync; import com.yogpc.qp.render.Box; import com.yogpc.qp.render.RenderMarker; import net.minecraft.core.BlockPos; @@ -24,7 +25,7 @@ import java.util.function.UnaryOperator; import java.util.stream.Stream; -public class TileFlexMarker extends BlockEntity implements QuarryMarker, CheckerLog { +public class TileFlexMarker extends BlockEntity implements QuarryMarker, CheckerLog, ClientSync { private BlockPos min; private BlockPos max; @@ -154,6 +155,17 @@ public List removeAndGetItems() { return List.of(new ItemStack(getBlockState().getBlock())); } + @Override + public void fromClientTag(CompoundTag tag) { + load(tag); + } + + @Override + public CompoundTag toClientTag(CompoundTag tag) { + saveAdditional(tag); + return tag; + } + public enum Movable { UP(facing -> Direction.UP), LEFT(Direction::getCounterClockWise), diff --git a/src/main/scala/com/yogpc/qp/machines/miningwell/MiningWellTile.java b/src/main/scala/com/yogpc/qp/machines/miningwell/MiningWellTile.java index 607629875..6dc848ee0 100644 --- a/src/main/scala/com/yogpc/qp/machines/miningwell/MiningWellTile.java +++ b/src/main/scala/com/yogpc/qp/machines/miningwell/MiningWellTile.java @@ -3,6 +3,7 @@ import com.yogpc.qp.Holder; import com.yogpc.qp.QuarryPlus; import com.yogpc.qp.machines.*; +import com.yogpc.qp.packet.ClientSync; import net.minecraft.core.BlockPos; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.chat.Component; @@ -26,7 +27,7 @@ import java.util.List; import java.util.stream.Stream; -public class MiningWellTile extends PowerTile implements CheckerLog, MachineStorage.HasStorage, EnchantmentLevel.HasEnchantments { +public class MiningWellTile extends PowerTile implements CheckerLog, MachineStorage.HasStorage, EnchantmentLevel.HasEnchantments, ClientSync { private final MachineStorage storage = new MachineStorage(); private final ItemConverter itemConverter = ItemConverter.defaultConverter(); public int digMinY = 0; @@ -96,20 +97,31 @@ public void tick() { @Override public void saveNbtData(CompoundTag nbt) { nbt.put("storage", storage.toNbt()); - nbt.putInt("digMinY", digMinY); - nbt.putInt("waitingTick", interval); - nbt.putBoolean("finished", finished); - nbt.putInt("efficiencyLevel", efficiencyLevel); + toClientTag(nbt); + } + + @Override + public CompoundTag toClientTag(CompoundTag tag) { + tag.putInt("digMinY", digMinY); + tag.putInt("waitingTick", interval); + tag.putBoolean("finished", finished); + tag.putInt("efficiencyLevel", efficiencyLevel); + return tag; } @Override public void load(CompoundTag nbt) { super.load(nbt); storage.readNbt(nbt.getCompound("storage")); - digMinY = nbt.getInt("digMinY"); - interval = nbt.getInt("waitingTick"); - finished = nbt.getBoolean("finished"); - efficiencyLevel = nbt.getInt("efficiencyLevel"); + fromClientTag(nbt); + } + + @Override + public void fromClientTag(CompoundTag tag) { + digMinY = tag.getInt("digMinY"); + interval = tag.getInt("waitingTick"); + finished = tag.getBoolean("finished"); + efficiencyLevel = tag.getInt("efficiencyLevel"); } @Override diff --git a/src/main/scala/com/yogpc/qp/machines/misc/CreativeGeneratorMenu.java b/src/main/scala/com/yogpc/qp/machines/misc/CreativeGeneratorMenu.java index 88a60621c..083541422 100644 --- a/src/main/scala/com/yogpc/qp/machines/misc/CreativeGeneratorMenu.java +++ b/src/main/scala/com/yogpc/qp/machines/misc/CreativeGeneratorMenu.java @@ -2,17 +2,19 @@ import com.yogpc.qp.Holder; import com.yogpc.qp.QuarryPlus; +import com.yogpc.qp.packet.ClientSyncMessage; import com.yogpc.qp.packet.PacketHandler; -import com.yogpc.qp.packet.TileMessage; import net.minecraft.core.BlockPos; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.inventory.Slot; import net.minecraft.world.item.ItemStack; +import org.jetbrains.annotations.NotNull; public class CreativeGeneratorMenu extends AbstractContainerMenu { public static final String GUI_ID = QuarryPlus.modID + ":gui_" + CreativeGeneratorBlock.NAME; + @NotNull final CreativeGeneratorTile tile; public CreativeGeneratorMenu(int id, Player player, BlockPos pos) { @@ -28,7 +30,7 @@ public CreativeGeneratorMenu(int id, Player player, BlockPos pos) { this.addSlot(new Slot(player.getInventory(), l, 8 + l * 18, 142)); } if (!player.level().isClientSide && tile != null) { - PacketHandler.sendToClientPlayer(new TileMessage(tile), (ServerPlayer) player); + PacketHandler.sendToClientPlayer(new ClientSyncMessage(tile), (ServerPlayer) player); } } diff --git a/src/main/scala/com/yogpc/qp/machines/misc/CreativeGeneratorScreen.java b/src/main/scala/com/yogpc/qp/machines/misc/CreativeGeneratorScreen.java index 3336019f0..578258b4c 100644 --- a/src/main/scala/com/yogpc/qp/machines/misc/CreativeGeneratorScreen.java +++ b/src/main/scala/com/yogpc/qp/machines/misc/CreativeGeneratorScreen.java @@ -4,7 +4,6 @@ import com.yogpc.qp.QuarryPlus; import com.yogpc.qp.machines.PowerTile; import com.yogpc.qp.packet.PacketHandler; -import com.yogpc.qp.packet.TileMessage; import net.minecraft.client.gui.GuiGraphics; import net.minecraft.client.gui.components.EditBox; import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen; @@ -73,7 +72,7 @@ private void changeEnergy(String s) { long e = Long.parseLong(s); if (e >= 0) { getMenu().tile.sendEnergy = e; - PacketHandler.sendToServer(new TileMessage(getMenu().tile)); + PacketHandler.sendToServer(new CreativeGeneratorSyncMessage(getMenu().tile)); } } catch (NumberFormatException ignore) { } diff --git a/src/main/scala/com/yogpc/qp/machines/misc/CreativeGeneratorSyncMessage.java b/src/main/scala/com/yogpc/qp/machines/misc/CreativeGeneratorSyncMessage.java new file mode 100644 index 000000000..b2de780be --- /dev/null +++ b/src/main/scala/com/yogpc/qp/machines/misc/CreativeGeneratorSyncMessage.java @@ -0,0 +1,55 @@ +package com.yogpc.qp.machines.misc; + +import com.yogpc.qp.Holder; +import com.yogpc.qp.packet.IMessage; +import com.yogpc.qp.packet.PacketHandler; +import net.minecraft.core.BlockPos; +import net.minecraft.core.Registry; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.resources.ResourceKey; +import net.minecraft.world.level.Level; +import net.minecraftforge.network.NetworkEvent; + +import java.util.function.Supplier; + +/** + * To server only + */ +public final class CreativeGeneratorSyncMessage implements IMessage { + + private final BlockPos pos; + private final ResourceKey dim; + private final long sendEnergy; + + CreativeGeneratorSyncMessage(BlockPos pos, ResourceKey dim, long sendEnergy) { + this.pos = pos; + this.dim = dim; + this.sendEnergy = sendEnergy; + } + + CreativeGeneratorSyncMessage(CreativeGeneratorTile tile) { + this(tile.getBlockPos(), PacketHandler.getDimension(tile), tile.sendEnergy); + } + + public CreativeGeneratorSyncMessage(FriendlyByteBuf buf) { + this( + buf.readBlockPos(), + ResourceKey.create(Registry.DIMENSION_REGISTRY, buf.readResourceLocation()), + buf.readLong() + ); + } + + @Override + public void write(FriendlyByteBuf buf) { + buf.writeBlockPos(pos).writeResourceLocation(dim.location()); + buf.writeLong(sendEnergy); + } + + public static void onReceive(CreativeGeneratorSyncMessage message, Supplier supplier) { + var world = PacketHandler.getWorld(supplier.get(), message.pos, message.dim); + supplier.get().enqueueWork(() -> + world.flatMap(l -> l.getBlockEntity(message.pos, Holder.CREATIVE_GENERATOR_TYPE)) + .ifPresent(t -> t.sendEnergy = message.sendEnergy) + ); + } +} diff --git a/src/main/scala/com/yogpc/qp/machines/misc/CreativeGeneratorTile.java b/src/main/scala/com/yogpc/qp/machines/misc/CreativeGeneratorTile.java index e746d3469..97dad1fc7 100644 --- a/src/main/scala/com/yogpc/qp/machines/misc/CreativeGeneratorTile.java +++ b/src/main/scala/com/yogpc/qp/machines/misc/CreativeGeneratorTile.java @@ -2,6 +2,7 @@ import com.yogpc.qp.Holder; import com.yogpc.qp.machines.PowerTile; +import com.yogpc.qp.packet.ClientSync; import com.yogpc.qp.utils.MapMulti; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; @@ -16,7 +17,7 @@ import java.util.Arrays; -public class CreativeGeneratorTile extends PowerTile implements MenuProvider { +public class CreativeGeneratorTile extends PowerTile implements MenuProvider, ClientSync { long sendEnergy = ONE_FE * 100000L; public CreativeGeneratorTile(BlockPos pos, BlockState state) { @@ -51,4 +52,15 @@ public Component getDisplayName() { public AbstractContainerMenu createMenu(int id, Inventory inventory, Player player) { return new CreativeGeneratorMenu(id, player, getBlockPos()); } + + @Override + public void fromClientTag(CompoundTag tag) { + load(tag); + } + + @Override + public CompoundTag toClientTag(CompoundTag tag) { + saveAdditional(tag); + return tag; + } } diff --git a/src/main/scala/com/yogpc/qp/machines/misc/YAccessor.java b/src/main/scala/com/yogpc/qp/machines/misc/YAccessor.java index fc2fa9172..ca6b1bfe8 100644 --- a/src/main/scala/com/yogpc/qp/machines/misc/YAccessor.java +++ b/src/main/scala/com/yogpc/qp/machines/misc/YAccessor.java @@ -4,13 +4,14 @@ import com.yogpc.qp.machines.advquarry.TileAdvQuarry; import com.yogpc.qp.machines.miningwell.MiningWellTile; import com.yogpc.qp.machines.quarry.TileQuarry; +import com.yogpc.qp.packet.ClientSync; import com.yogpc.qp.packet.IMessage; import com.yogpc.qp.packet.LevelMessage; import net.minecraft.world.entity.player.Player; import net.minecraft.world.level.block.entity.BlockEntity; import org.jetbrains.annotations.Nullable; -public abstract class YAccessor { +public abstract class YAccessor { public abstract int getDigMinY(); public abstract void setDigMinY(int newValue); @@ -21,8 +22,10 @@ public abstract class YAccessor { abstract boolean stillValid(Player player); + abstract T getEntity(); + @Nullable - public static YAccessor get(@Nullable BlockEntity entity) { + public static YAccessor get(@Nullable BlockEntity entity) { if (entity instanceof TileQuarry quarry) return new QuarryYAccessor(quarry); else if (entity instanceof MiningWellTile miningWell) @@ -34,7 +37,7 @@ else if (entity instanceof TileAdvQuarry quarry) } } -class QuarryYAccessor extends YAccessor { +class QuarryYAccessor extends YAccessor { private final TileQuarry quarry; QuarryYAccessor(TileQuarry quarry) { @@ -66,9 +69,14 @@ int getLimitTop() { boolean stillValid(Player player) { return PowerTile.stillValid(quarry, player); } + + @Override + TileQuarry getEntity() { + return quarry; + } } -class MiningWellYAccessor extends YAccessor { +class MiningWellYAccessor extends YAccessor { private final MiningWellTile miningWell; MiningWellYAccessor(MiningWellTile miningWell) { @@ -100,9 +108,14 @@ int getLimitTop() { boolean stillValid(Player player) { return PowerTile.stillValid(miningWell, player); } + + @Override + MiningWellTile getEntity() { + return miningWell; + } } -class AdvQuarryYAccessor extends YAccessor { +class AdvQuarryYAccessor extends YAccessor { private final TileAdvQuarry quarry; AdvQuarryYAccessor(TileAdvQuarry quarry) { @@ -134,4 +147,9 @@ int getLimitTop() { boolean stillValid(Player player) { return PowerTile.stillValid(quarry, player); } + + @Override + TileAdvQuarry getEntity() { + return quarry; + } } diff --git a/src/main/scala/com/yogpc/qp/machines/misc/YSetterContainer.java b/src/main/scala/com/yogpc/qp/machines/misc/YSetterContainer.java index 9d719d83b..a85bb3643 100644 --- a/src/main/scala/com/yogpc/qp/machines/misc/YSetterContainer.java +++ b/src/main/scala/com/yogpc/qp/machines/misc/YSetterContainer.java @@ -12,7 +12,7 @@ public class YSetterContainer extends AbstractContainerMenu { public static final String GUI_ID = QuarryPlus.modID + ":gui_y_setter"; @Nullable - final YAccessor yAccessor; + final YAccessor yAccessor; public YSetterContainer(int syncId, Player player, BlockPos pos) { super(Holder.Y_SETTER_MENU_TYPE, syncId); diff --git a/src/main/scala/com/yogpc/qp/machines/misc/YSetterItem.java b/src/main/scala/com/yogpc/qp/machines/misc/YSetterItem.java index 27f6c56c5..3cca291e2 100644 --- a/src/main/scala/com/yogpc/qp/machines/misc/YSetterItem.java +++ b/src/main/scala/com/yogpc/qp/machines/misc/YSetterItem.java @@ -2,8 +2,8 @@ import com.yogpc.qp.QuarryPlus; import com.yogpc.qp.machines.QPItem; +import com.yogpc.qp.packet.ClientSyncMessage; import com.yogpc.qp.packet.PacketHandler; -import com.yogpc.qp.packet.TileMessage; import net.minecraft.core.BlockPos; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; @@ -31,10 +31,11 @@ public InteractionResult onItemUseFirst(ItemStack stack, UseOnContext context) { var level = context.getLevel(); var player = context.getPlayer(); var tile = level.getBlockEntity(context.getClickedPos()); - if (YAccessor.get(tile) != null) { + YAccessor accessor = YAccessor.get(tile); + if (accessor != null) { if (!level.isClientSide) { if (player instanceof ServerPlayer p) { - PacketHandler.sendToClientPlayer(new TileMessage(tile), p); + PacketHandler.sendToClientPlayer(new ClientSyncMessage(accessor.getEntity()), p); NetworkHooks.openScreen(p, new YSetterScreenHandler(tile.getBlockPos(), tile.getBlockState().getBlock()), context.getClickedPos()); } diff --git a/src/main/scala/com/yogpc/qp/machines/placer/PlacerTile.java b/src/main/scala/com/yogpc/qp/machines/placer/PlacerTile.java index c9f5940ee..03ecc8e4c 100644 --- a/src/main/scala/com/yogpc/qp/machines/placer/PlacerTile.java +++ b/src/main/scala/com/yogpc/qp/machines/placer/PlacerTile.java @@ -5,8 +5,9 @@ import com.yogpc.qp.machines.CheckerLog; import com.yogpc.qp.machines.InvUtils; import com.yogpc.qp.machines.QuarryFakePlayer; +import com.yogpc.qp.packet.ClientSync; +import com.yogpc.qp.packet.ClientSyncMessage; import com.yogpc.qp.packet.PacketHandler; -import com.yogpc.qp.packet.TileMessage; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.core.NonNullList; @@ -53,6 +54,7 @@ public class PlacerTile extends BlockEntity implements Container, CheckerLog, + ClientSync, MenuProvider { public static final String KEY_ITEM = "items"; public static final String KEY_LAST_PLACED = "last_placed"; @@ -187,7 +189,7 @@ public int getLastPlacedIndex() { void sendPacket() { if (level != null && !level.isClientSide) - PacketHandler.sendToClient(new TileMessage(this), level); + PacketHandler.sendToClient(new ClientSyncMessage(this), level); } private static ItemStack getSilkPickaxe() { @@ -201,8 +203,7 @@ private static ItemStack getSilkPickaxe() { @Override protected void saveAdditional(CompoundTag compound) { compound.put(KEY_ITEM, ContainerHelper.saveAllItems(new CompoundTag(), inventory)); - compound.putInt(KEY_LAST_PLACED, lastPlacedIndex); - compound.putString(KEY_RS_MODE, redstoneMode.name()); + toClientTag(compound); super.saveAdditional(compound); } @@ -210,6 +211,16 @@ protected void saveAdditional(CompoundTag compound) { public void load(CompoundTag compound) { super.load(compound); ContainerHelper.loadAllItems(compound.getCompound(KEY_ITEM), inventory); + fromClientTag(compound); + } + + @Override + public CompoundTag getUpdateTag() { + return this.serializeNBT(); + } + + @Override + public void fromClientTag(CompoundTag compound) { lastPlacedIndex = compound.getInt(KEY_LAST_PLACED); try { redstoneMode = RedstoneMode.valueOf(compound.getString(KEY_RS_MODE)); @@ -220,8 +231,10 @@ public void load(CompoundTag compound) { } @Override - public CompoundTag getUpdateTag() { - return this.serializeNBT(); + public CompoundTag toClientTag(CompoundTag compound) { + compound.putInt(KEY_LAST_PLACED, lastPlacedIndex); + compound.putString(KEY_RS_MODE, redstoneMode.name()); + return compound; } // -------------------- Capability -------------------- diff --git a/src/main/scala/com/yogpc/qp/machines/quarry/QuarryBlock.java b/src/main/scala/com/yogpc/qp/machines/quarry/QuarryBlock.java index b9f9453f0..55384721a 100644 --- a/src/main/scala/com/yogpc/qp/machines/quarry/QuarryBlock.java +++ b/src/main/scala/com/yogpc/qp/machines/quarry/QuarryBlock.java @@ -8,8 +8,6 @@ import com.yogpc.qp.machines.module.ContainerQuarryModule; import com.yogpc.qp.machines.module.EnergyModuleItem; import com.yogpc.qp.machines.module.ModuleLootFunction; -import com.yogpc.qp.packet.PacketHandler; -import com.yogpc.qp.packet.TileMessage; import com.yogpc.qp.utils.CombinedBlockEntityTicker; import com.yogpc.qp.utils.MapMulti; import com.yogpc.qp.utils.QuarryChunkLoadUtil; @@ -96,7 +94,7 @@ public void setPlacedBy(Level level, BlockPos pos, BlockState state, @Nullable L quarry.updateModules(); var preForced = QuarryChunkLoadUtil.makeChunkLoaded(level, pos, quarry.enabled); quarry.setChunkPreLoaded(preForced); - PacketHandler.sendToClient(new TileMessage(quarry), level); + quarry.sync(); } } } diff --git a/src/main/scala/com/yogpc/qp/machines/quarry/SFQuarryBlock.java b/src/main/scala/com/yogpc/qp/machines/quarry/SFQuarryBlock.java index 72b378819..46e7be393 100644 --- a/src/main/scala/com/yogpc/qp/machines/quarry/SFQuarryBlock.java +++ b/src/main/scala/com/yogpc/qp/machines/quarry/SFQuarryBlock.java @@ -7,8 +7,6 @@ import com.yogpc.qp.machines.MachineStorage; import com.yogpc.qp.machines.PowerTile; import com.yogpc.qp.machines.QPBlock; -import com.yogpc.qp.packet.PacketHandler; -import com.yogpc.qp.packet.TileMessage; import com.yogpc.qp.utils.CombinedBlockEntityTicker; import com.yogpc.qp.utils.QuarryChunkLoadUtil; import net.minecraft.core.BlockPos; @@ -86,7 +84,7 @@ public void setPlacedBy(Level level, BlockPos pos, BlockState state, @Nullable L } var preForced = QuarryChunkLoadUtil.makeChunkLoaded(level, pos, quarry.enabled); quarry.setChunkPreLoaded(preForced); - PacketHandler.sendToClient(new TileMessage(quarry), level); + quarry.sync(); } } } diff --git a/src/main/scala/com/yogpc/qp/machines/quarry/TileQuarry.java b/src/main/scala/com/yogpc/qp/machines/quarry/TileQuarry.java index f177325d4..d428c146d 100644 --- a/src/main/scala/com/yogpc/qp/machines/quarry/TileQuarry.java +++ b/src/main/scala/com/yogpc/qp/machines/quarry/TileQuarry.java @@ -77,19 +77,8 @@ public void saveNbtData(CompoundTag nbt) { nbt.put("target", Target.toNbt(target)); } nbt.putString("state", state.name()); - if (area != null) - nbt.put("area", area.toNBT()); - { - var enchantments = new CompoundTag(); - this.enchantments.forEach(e -> - enchantments.putInt(Objects.requireNonNull(e.enchantmentID(), "Invalid enchantment. " + e.enchantment()).toString(), e.level())); - nbt.put("enchantments", enchantments); - } - nbt.putDouble("headX", headX); - nbt.putDouble("headY", headY); - nbt.putDouble("headZ", headZ); nbt.put("storage", storage.toNbt()); - nbt.putInt("digMinY", digMinY); + toClientTag(nbt); nbt.put("moduleInventory", moduleInventory.serializeNBT()); } @@ -98,42 +87,43 @@ public void load(CompoundTag nbt) { super.load(nbt); target = nbt.contains("target") ? Target.fromNbt(nbt.getCompound("target")) : null; state = QuarryState.valueOf(nbt.getString("state")); - area = Area.fromNBT(nbt.getCompound("area")).orElse(null); - { - var enchantments = nbt.getCompound("enchantments"); - setEnchantments(enchantments.getAllKeys().stream() - .mapMulti(MapMulti.getEntry(ForgeRegistries.ENCHANTMENTS, enchantments::getInt)) - .map(EnchantmentLevel::new) - .sorted(EnchantmentLevel.QUARRY_ENCHANTMENT_COMPARATOR) - .toList()); - } - headX = nbt.getDouble("headX"); - headY = nbt.getDouble("headY"); - headZ = nbt.getDouble("headZ"); storage.readNbt(nbt.getCompound("storage")); - digMinY = nbt.getInt("digMinY"); + fromClientTag(nbt); moduleInventory.deserializeNBT(nbt.getCompound("moduleInventory")); init = true; } @Override public CompoundTag toClientTag(CompoundTag tag) { - if (area != null) + if (area != null) { tag.put("area", area.toNBT()); + } + var enchantments = new CompoundTag(); + this.enchantments.forEach(e -> + enchantments.putInt(Objects.requireNonNull(e.enchantmentID(), "Invalid enchantment. " + e.enchantment()).toString(), e.level())); + tag.put("enchantments", enchantments); tag.putString("state", state.name()); tag.putDouble("headX", headX); tag.putDouble("headY", headY); tag.putDouble("headZ", headZ); + tag.putInt("digMinY", digMinY); return tag; } @Override public void fromClientTag(CompoundTag tag) { area = Area.fromNBT(tag.getCompound("area")).orElse(null); + var enchantments = tag.getCompound("enchantments"); + setEnchantments(enchantments.getAllKeys().stream() + .mapMulti(MapMulti.getEntry(ForgeRegistries.ENCHANTMENTS, enchantments::getInt)) + .map(EnchantmentLevel::new) + .sorted(EnchantmentLevel.QUARRY_ENCHANTMENT_COMPARATOR) + .toList()); state = QuarryState.valueOf(tag.getString("state")); headX = tag.getDouble("headX"); headY = tag.getDouble("headY"); headZ = tag.getDouble("headZ"); + digMinY = tag.getInt("digMinY"); } @Override diff --git a/src/main/scala/com/yogpc/qp/machines/workbench/TileWorkbench.java b/src/main/scala/com/yogpc/qp/machines/workbench/TileWorkbench.java index 2be8fc3a7..99b56c1ce 100644 --- a/src/main/scala/com/yogpc/qp/machines/workbench/TileWorkbench.java +++ b/src/main/scala/com/yogpc/qp/machines/workbench/TileWorkbench.java @@ -5,8 +5,9 @@ import com.yogpc.qp.machines.CheckerLog; import com.yogpc.qp.machines.InvUtils; import com.yogpc.qp.machines.PowerTile; +import com.yogpc.qp.packet.ClientSync; +import com.yogpc.qp.packet.ClientSyncMessage; import com.yogpc.qp.packet.PacketHandler; -import com.yogpc.qp.packet.TileMessage; import com.yogpc.qp.utils.MapMulti; import net.minecraft.ChatFormatting; import net.minecraft.core.BlockPos; @@ -42,7 +43,7 @@ import java.util.List; import java.util.stream.Stream; -public class TileWorkbench extends PowerTile implements Container, MenuProvider, CheckerLog { +public class TileWorkbench extends PowerTile implements Container, MenuProvider, CheckerLog, ClientSync { final List ingredientInventory = NonNullList.withSize(27, ItemStack.EMPTY); final List selectionInventory = NonNullList.withSize(18, ItemStack.EMPTY); public List recipesList = Collections.emptyList(); @@ -79,7 +80,7 @@ public void tick() { addEnergy(5 * ONE_FE, false); } if (!openPlayers.isEmpty()) { - PacketHandler.sendToClient(new TileMessage(this), level); + PacketHandler.sendToClient(new ClientSyncMessage(this), level); } } @@ -114,6 +115,17 @@ public void load(CompoundTag nbt) { }; } + @Override + public void fromClientTag(CompoundTag nbt) { + setEnergy(nbt.getLong("energy"), false); + setMaxEnergy(nbt.getLong("maxEnergy")); + var recipeLocation = new ResourceLocation(nbt.getString("recipe")); + initRecipeTask = () -> { + updateRecipeOutputs(); + setCurrentRecipe(recipeLocation); + }; + } + @Override public void saveNbtData(CompoundTag nbt) { ListTag items = new ListTag(); @@ -132,6 +144,14 @@ public void saveNbtData(CompoundTag nbt) { nbt.putString("recipe", currentRecipe.getId().toString()); } + @Override + public CompoundTag toClientTag(CompoundTag nbt) { + nbt.putLong("energy", getEnergy()); + nbt.putLong("maxEnergy", getMaxEnergy()); + nbt.putString("recipe", currentRecipe.getId().toString()); + return nbt; + } + // Implementation of Inventory. @Override public int getContainerSize() { @@ -232,7 +252,7 @@ private void updateRecipeOutputs() { logUsage(); } } - PacketHandler.sendToClient(new TileMessage(this), level); + PacketHandler.sendToClient(new ClientSyncMessage(this), level); } } diff --git a/src/main/scala/com/yogpc/qp/packet/ClientSyncMessage.java b/src/main/scala/com/yogpc/qp/packet/ClientSyncMessage.java index 458f498f0..b00e5cc33 100644 --- a/src/main/scala/com/yogpc/qp/packet/ClientSyncMessage.java +++ b/src/main/scala/com/yogpc/qp/packet/ClientSyncMessage.java @@ -7,6 +7,7 @@ import net.minecraft.resources.ResourceKey; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraftforge.fml.LogicalSide; import net.minecraftforge.network.NetworkEvent; import java.util.function.Supplier; @@ -44,6 +45,9 @@ public void write(FriendlyByteBuf buf) { public static void onReceive(ClientSyncMessage message, Supplier supplier) { var world = PacketHandler.getWorld(supplier.get(), message.pos, message.dim); + if (supplier.get().getDirection().getReceptionSide() != LogicalSide.CLIENT) { + throw new IllegalStateException("Message was sent to unexpected side."); + } supplier.get().enqueueWork(() -> world.map(l -> l.getBlockEntity(message.pos)) .flatMap(optCast(ClientSync.class)) diff --git a/src/main/scala/com/yogpc/qp/packet/PacketHandler.java b/src/main/scala/com/yogpc/qp/packet/PacketHandler.java index 88b6c4f11..6b410b77b 100644 --- a/src/main/scala/com/yogpc/qp/packet/PacketHandler.java +++ b/src/main/scala/com/yogpc/qp/packet/PacketHandler.java @@ -11,6 +11,7 @@ import com.yogpc.qp.machines.marker.Marker16Message; import com.yogpc.qp.machines.mini_quarry.MiniListSyncMessage; import com.yogpc.qp.machines.mini_quarry.MiniRequestListMessage; +import com.yogpc.qp.machines.misc.CreativeGeneratorSyncMessage; import com.yogpc.qp.machines.mover.MoverMessage; import com.yogpc.qp.machines.placer.RemotePlacerMessage; import net.minecraft.client.Minecraft; @@ -48,7 +49,6 @@ public class PacketHandler { public static void init() { AtomicInteger id = new AtomicInteger(1); - INSTANCE.registerMessage(id.getAndIncrement(), TileMessage.class, TileMessage::write, TileMessage::new, setHandled(TileMessage::onReceive)); INSTANCE.registerMessage(id.getAndIncrement(), ClientSyncMessage.class, ClientSyncMessage::write, ClientSyncMessage::new, setHandled(ClientSyncMessage::onReceive)); INSTANCE.registerMessage(id.getAndIncrement(), Marker16Message.class, Marker16Message::write, Marker16Message::new, setHandled(Marker16Message::onReceive)); INSTANCE.registerMessage(id.getAndIncrement(), FlexMarkerMessage.class, FlexMarkerMessage::write, FlexMarkerMessage::new, setHandled(FlexMarkerMessage::onReceive)); @@ -64,6 +64,7 @@ public static void init() { INSTANCE.registerMessage(id.getAndIncrement(), AdvQuarryInitialMessage.class, AdvQuarryInitialMessage::write, AdvQuarryInitialMessage::new, setHandled(AdvQuarryInitialMessage::onReceive)); INSTANCE.registerMessage(id.getAndIncrement(), AdvQuarryInitialMessage.Ask.class, AdvQuarryInitialMessage.Ask::write, AdvQuarryInitialMessage.Ask::new, setHandled(AdvQuarryInitialMessage.Ask::onReceive)); INSTANCE.registerMessage(id.getAndIncrement(), AdvPumpMessage.class, AdvPumpMessage::write, AdvPumpMessage::new, setHandled(AdvPumpMessage::onReceive)); + INSTANCE.registerMessage(id.getAndIncrement(), CreativeGeneratorSyncMessage.class, CreativeGeneratorSyncMessage::write, CreativeGeneratorSyncMessage::new, setHandled(CreativeGeneratorSyncMessage::onReceive)); } public static void sendToClient(@NotNull IMessage message, @NotNull Level world) { diff --git a/src/main/scala/com/yogpc/qp/packet/TileMessage.java b/src/main/scala/com/yogpc/qp/packet/TileMessage.java deleted file mode 100644 index 542a38a92..000000000 --- a/src/main/scala/com/yogpc/qp/packet/TileMessage.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.yogpc.qp.packet; - -import net.minecraft.core.BlockPos; -import net.minecraft.core.registries.Registries; -import net.minecraft.nbt.CompoundTag; -import net.minecraft.network.FriendlyByteBuf; -import net.minecraft.resources.ResourceKey; -import net.minecraft.world.level.Level; -import net.minecraft.world.level.block.entity.BlockEntity; -import net.minecraftforge.network.NetworkEvent; - -import java.util.function.Supplier; - -/** - * To both client and server. - */ -public final class TileMessage implements IMessage { - private final CompoundTag tag; - private final BlockPos pos; - private final ResourceKey dim; - - public TileMessage(BlockPos pos, ResourceKey dim, CompoundTag tag) { - this.tag = tag; - this.pos = pos; - this.dim = dim; - } - - public TileMessage(BlockEntity entity) { - this(entity.getBlockPos(), PacketHandler.getDimension(entity), entity.saveWithoutMetadata()); - } - - public TileMessage(FriendlyByteBuf buf) { - this(buf.readBlockPos(), ResourceKey.create(Registries.DIMENSION, buf.readResourceLocation()), buf.readNbt()); - } - - @Override - public void write(FriendlyByteBuf buf) { - buf.writeBlockPos(this.pos); - buf.writeResourceLocation(this.dim.location()); - buf.writeNbt(this.tag); - } - - public static void onReceive(TileMessage message, Supplier supplier) { - var world = PacketHandler.getWorld(supplier.get(), message.pos, message.dim); - supplier.get().enqueueWork(() -> world.map(l -> l.getBlockEntity(message.pos)).ifPresent(t -> t.load(message.tag))); - } - -} diff --git a/temp_changelog.md b/temp_changelog.md index 7d2035b30..0ce5fc155 100644 --- a/temp_changelog.md +++ b/temp_changelog.md @@ -1,7 +1,6 @@ ## Version LATEST for 1.20.1 -* Add GUI for Adv Pump -* Change internal logic of chaining break of dummy blocks +* Fix a bug of item duplication ## Version for 1.20.1 @@ -21,6 +20,8 @@ * Limit the number of chaining of Dummy Block. #278 * Improve logging of machine. #275 * Fix a crash due to some mods' compatibility. #279 +* Add GUI for Adv Pump +* Change internal logic of chaining break of dummy blocks ## Version 1192.2.3