diff --git a/src/main/java/com/gregtechceu/gtceu/api/gui/misc/ProspectorMode.java b/src/main/java/com/gregtechceu/gtceu/api/gui/misc/ProspectorMode.java index 53d7b8d990..6ce17cbeed 100644 --- a/src/main/java/com/gregtechceu/gtceu/api/gui/misc/ProspectorMode.java +++ b/src/main/java/com/gregtechceu/gtceu/api/gui/misc/ProspectorMode.java @@ -7,6 +7,7 @@ import com.gregtechceu.gtceu.api.data.tag.TagPrefix; import com.gregtechceu.gtceu.api.data.tag.TagUtil; import com.gregtechceu.gtceu.api.data.worldgen.bedrockfluid.BedrockFluidVeinSavedData; +import com.gregtechceu.gtceu.api.data.worldgen.bedrockfluid.FluidVeinWorldEntry; import com.gregtechceu.gtceu.api.data.worldgen.bedrockore.BedrockOreVeinSavedData; import com.gregtechceu.gtceu.api.gui.texture.ProspectingTexture; import com.gregtechceu.gtceu.common.data.GTMaterials; @@ -186,6 +187,15 @@ public CompoundTag toNbt() { tag.putInt("yield", yield); return tag; } + + public static FluidInfo fromVeinWorldEntry(@NotNull FluidVeinWorldEntry savedData) { + if (savedData.getDefinition() == null) { + return null; + } + return new FluidInfo(savedData.getDefinition().getStoredFluid().get(), + savedData.getFluidYield(), + 100 * savedData.getOperationsRemaining() / BedrockFluidVeinSavedData.MAXIMUM_VEIN_OPERATIONS); + } } public static ProspectorMode FLUID = new ProspectorMode<>("metaitem.prospector.mode.fluid", 1) { @@ -196,11 +206,8 @@ public void scan(FluidInfo[][][] storage, LevelChunk chunk) { var fluidVein = BedrockFluidVeinSavedData.getOrCreate(serverLevel) .getFluidVeinWorldEntry(chunk.getPos().x, chunk.getPos().z); if (fluidVein.getDefinition() != null) { - var left = 100 * fluidVein.getOperationsRemaining() / - BedrockFluidVeinSavedData.MAXIMUM_VEIN_OPERATIONS; storage[0][0] = new FluidInfo[] { - new FluidInfo(fluidVein.getDefinition().getStoredFluid().get(), fluidVein.getFluidYield(), - left), + FluidInfo.fromVeinWorldEntry(fluidVein) }; } } diff --git a/src/main/java/com/gregtechceu/gtceu/api/gui/widget/ProspectingMapWidget.java b/src/main/java/com/gregtechceu/gtceu/api/gui/widget/ProspectingMapWidget.java index 46a46a2d70..654b4f2e30 100644 --- a/src/main/java/com/gregtechceu/gtceu/api/gui/widget/ProspectingMapWidget.java +++ b/src/main/java/com/gregtechceu/gtceu/api/gui/widget/ProspectingMapWidget.java @@ -154,7 +154,9 @@ public void detectAndSendChanges() { int oz = row - chunkRadius + 1; var chunk = world.getChunk(playerChunkX + ox, playerChunkZ + oz); - ServerCache.instance.prospectAllInChunk(world.dimension(), chunk.getPos(), (ServerPlayer) player); + if (mode == ProspectorMode.ORE) { + ServerCache.instance.prospectAllInChunk(world.dimension(), chunk.getPos(), (ServerPlayer) player); + } PacketProspecting packet = new PacketProspecting(playerChunkX + ox, playerChunkZ + oz, this.mode); mode.scan(packet.data, chunk); writeUpdateInfo(-1, packet::writePacketData); diff --git a/src/main/java/com/gregtechceu/gtceu/common/item/PortableScannerBehavior.java b/src/main/java/com/gregtechceu/gtceu/common/item/PortableScannerBehavior.java index 3d0b3c16a4..ee09f99565 100644 --- a/src/main/java/com/gregtechceu/gtceu/common/item/PortableScannerBehavior.java +++ b/src/main/java/com/gregtechceu/gtceu/common/item/PortableScannerBehavior.java @@ -9,6 +9,7 @@ import com.gregtechceu.gtceu.api.capability.IWorkable; import com.gregtechceu.gtceu.api.capability.forge.GTCapability; import com.gregtechceu.gtceu.api.data.worldgen.bedrockfluid.BedrockFluidVeinSavedData; +import com.gregtechceu.gtceu.api.gui.misc.ProspectorMode; import com.gregtechceu.gtceu.api.item.component.IAddInformation; import com.gregtechceu.gtceu.api.item.component.IInteractionItem; import com.gregtechceu.gtceu.api.machine.IMachineBlockEntity; @@ -23,6 +24,8 @@ import com.gregtechceu.gtceu.common.capability.EnvironmentalHazardSavedData; import com.gregtechceu.gtceu.common.capability.LocalizedHazardSavedData; import com.gregtechceu.gtceu.common.data.GTSoundEntries; +import com.gregtechceu.gtceu.common.network.GTNetwork; +import com.gregtechceu.gtceu.common.network.packets.prospecting.SPacketProspectBedrockFluid; import com.gregtechceu.gtceu.utils.FormattingUtil; import com.gregtechceu.gtceu.utils.GTUtil; @@ -30,6 +33,7 @@ import net.minecraft.core.BlockPos; import net.minecraft.network.chat.Component; import net.minecraft.server.level.ServerLevel; +import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.InteractionResultHolder; @@ -389,19 +393,26 @@ else if (machine instanceof IDataInfoProvider) if (level instanceof ServerLevel serverLevel) { list.add(Component.translatable("behavior.portable_scanner.divider")); var veinData = BedrockFluidVeinSavedData.getOrCreate(serverLevel); - Fluid fluid = veinData.getFluidInChunk(pos.getX() >> 4, pos.getZ() >> 4); + int chunkX = pos.getX() >> 4; + int chunkZ = pos.getZ() >> 4; + Fluid fluid = veinData.getFluidInChunk(chunkX, chunkZ); if (fluid != null) { FluidStack stack = new FluidStack(fluid, - veinData.getOperationsRemaining(pos.getX() >> 4, pos.getZ() >> 4)); + veinData.getOperationsRemaining(chunkX, chunkZ)); double fluidPercent = stack.getAmount() * 100.0 / BedrockFluidVeinSavedData.MAXIMUM_VEIN_OPERATIONS; + var fluidInfo = ProspectorMode.FluidInfo + .fromVeinWorldEntry(veinData.getFluidVeinWorldEntry(chunkX, chunkZ)); + var packet = new SPacketProspectBedrockFluid(level.dimension(), pos, fluidInfo); + GTNetwork.NETWORK.sendToPlayer(packet, (ServerPlayer) player); + if (player.isCreative()) { list.add(Component.translatable("behavior.portable_scanner.bedrock_fluid.amount", Component.translatable(stack.getTranslationKey()) .withStyle(ChatFormatting.GOLD), Component.translatable(String.valueOf( - veinData.getFluidYield(pos.getX() >> 4, pos.getZ() >> 4))) + veinData.getFluidYield(chunkX, chunkZ))) .withStyle(ChatFormatting.GOLD), Component.translatable(String.valueOf(fluidPercent)) .withStyle(ChatFormatting.YELLOW))); diff --git a/src/main/java/com/gregtechceu/gtceu/common/network/GTNetwork.java b/src/main/java/com/gregtechceu/gtceu/common/network/GTNetwork.java index aa3473fd2e..9018e4c8b1 100644 --- a/src/main/java/com/gregtechceu/gtceu/common/network/GTNetwork.java +++ b/src/main/java/com/gregtechceu/gtceu/common/network/GTNetwork.java @@ -6,6 +6,9 @@ import com.gregtechceu.gtceu.common.network.packets.hazard.SPacketRemoveHazardZone; import com.gregtechceu.gtceu.common.network.packets.hazard.SPacketSyncHazardZoneStrength; import com.gregtechceu.gtceu.common.network.packets.hazard.SPacketSyncLevelHazards; +import com.gregtechceu.gtceu.common.network.packets.prospecting.SPacketProspectBedrockFluid; +import com.gregtechceu.gtceu.common.network.packets.prospecting.SPacketProspectBedrockOre; +import com.gregtechceu.gtceu.common.network.packets.prospecting.SPacketProspectOre; import com.lowdragmc.lowdraglib.networking.INetworking; import com.lowdragmc.lowdraglib.networking.forge.LDLNetworkingImpl; @@ -24,7 +27,9 @@ public static void init() { NETWORK.registerS2C(SPacketRemoveHazardZone.class); NETWORK.registerS2C(SPacketSyncHazardZoneStrength.class); NETWORK.registerS2C(SPacketSyncLevelHazards.class); - NETWORK.registerS2C(SPacketOreProspect.class); + NETWORK.registerS2C(SPacketProspectOre.class); + NETWORK.registerS2C(SPacketProspectBedrockFluid.class); + NETWORK.registerS2C(SPacketProspectBedrockOre.class); NETWORK.registerS2C(SPacketSendWorldID.class); NETWORK.registerBoth(SCPacketShareProspection.class); diff --git a/src/main/java/com/gregtechceu/gtceu/common/network/packets/SPacketOreProspect.java b/src/main/java/com/gregtechceu/gtceu/common/network/packets/SPacketOreProspect.java deleted file mode 100644 index 2692190589..0000000000 --- a/src/main/java/com/gregtechceu/gtceu/common/network/packets/SPacketOreProspect.java +++ /dev/null @@ -1,93 +0,0 @@ -package com.gregtechceu.gtceu.common.network.packets; - -import com.gregtechceu.gtceu.api.data.worldgen.ores.GeneratedVeinMetadata; -import com.gregtechceu.gtceu.integration.map.cache.GridPos; -import com.gregtechceu.gtceu.integration.map.cache.client.GTClientCache; - -import com.lowdragmc.lowdraglib.networking.IHandlerContext; -import com.lowdragmc.lowdraglib.networking.IPacket; - -import net.minecraft.core.registries.Registries; -import net.minecraft.network.FriendlyByteBuf; -import net.minecraft.resources.ResourceKey; -import net.minecraft.world.level.Level; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class SPacketOreProspect implements IPacket { - - private final List> dimList; - private final List gridXList; - private final List gridZList; - private final List nameList; - - public SPacketOreProspect() { - this.dimList = new ArrayList<>(); - this.gridXList = new ArrayList<>(); - this.gridZList = new ArrayList<>(); - this.nameList = new ArrayList<>(); - } - - public SPacketOreProspect(ResourceKey dim, int gridX, int gridZ, GeneratedVeinMetadata vein) { - this.dimList = Collections.singletonList(dim); - this.gridXList = Collections.singletonList(gridX); - this.gridZList = Collections.singletonList(gridZ); - this.nameList = Collections.singletonList(vein); - } - - public SPacketOreProspect(List> dimList, List gridXList, List gridZList, - List nameList) { - this.dimList = dimList; - this.gridXList = gridXList; - this.gridZList = gridZList; - this.nameList = nameList; - } - - public SPacketOreProspect(ResourceKey dim, List veins) { - this(); - for (GeneratedVeinMetadata vein : veins) { - dimList.add(dim); - gridXList.add(GridPos.blockToGridCoords(vein.center().getX())); - gridZList.add(GridPos.blockToGridCoords(vein.center().getZ())); - nameList.add(vein); - } - } - - @Override - public void execute(IHandlerContext handler) { - if (handler.isClient()) { - int newVeins = 0; - for (int i = 0; i < dimList.size(); i++) { - if (GTClientCache.instance.addVein(dimList.get(i), gridXList.get(i), gridZList.get(i), - nameList.get(i))) { - newVeins++; - } - } - GTClientCache.instance.notifyNewVeins(newVeins); - } - } - - @Override - public void encode(FriendlyByteBuf packetBuffer) { - packetBuffer.writeInt(dimList.size()); - for (int i = 0; i < dimList.size(); i++) { - packetBuffer.writeResourceKey(dimList.get(i)); - packetBuffer.writeInt(gridXList.get(i)); - packetBuffer.writeInt(gridZList.get(i)); - nameList.get(i).writeToPacket(packetBuffer); - } - } - - @Override - public void decode(FriendlyByteBuf packetBuffer) { - int size = packetBuffer.readInt(); - for (int i = 0; i < size; i++) { - dimList.add(packetBuffer.readResourceKey(Registries.DIMENSION)); - gridXList.add(packetBuffer.readInt()); - gridZList.add(packetBuffer.readInt()); - nameList.add(GeneratedVeinMetadata.readFromPacket(packetBuffer)); - } - } -} diff --git a/src/main/java/com/gregtechceu/gtceu/common/network/packets/prospecting/SPacketProspect.java b/src/main/java/com/gregtechceu/gtceu/common/network/packets/prospecting/SPacketProspect.java new file mode 100644 index 0000000000..bbdda38f3d --- /dev/null +++ b/src/main/java/com/gregtechceu/gtceu/common/network/packets/prospecting/SPacketProspect.java @@ -0,0 +1,87 @@ +package com.gregtechceu.gtceu.common.network.packets.prospecting; + +import com.lowdragmc.lowdraglib.networking.IHandlerContext; +import com.lowdragmc.lowdraglib.networking.IPacket; + +import net.minecraft.core.BlockPos; +import net.minecraft.core.registries.Registries; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.resources.ResourceKey; +import net.minecraft.world.level.Level; + +import com.google.common.collect.HashBasedTable; +import com.google.common.collect.Table; + +import java.util.Collection; + +public abstract class SPacketProspect implements IPacket { + + protected final Table, BlockPos, T> data; + + protected SPacketProspect() { + data = HashBasedTable.create(); + } + + protected SPacketProspect(Table, BlockPos, T> data) { + this.data = data; + } + + protected SPacketProspect(Collection> keys, Collection positions, + Collection prospected) { + this(); + var keyIterator = keys.iterator(); + var posIterator = positions.iterator(); + var prospectedIterator = prospected.iterator(); + while (keyIterator.hasNext()) { + data.put(keyIterator.next(), posIterator.next(), prospectedIterator.next()); + } + } + + protected SPacketProspect(ResourceKey key, Collection positions, Collection prospected) { + data = HashBasedTable.create(1, prospected.size()); + var posIterator = positions.iterator(); + var prospectedIterator = prospected.iterator(); + while (posIterator.hasNext()) { + data.put(key, posIterator.next(), prospectedIterator.next()); + } + } + + protected SPacketProspect(ResourceKey key, BlockPos position, T prospected) { + data = HashBasedTable.create(1, 1); + data.put(key, position, prospected); + } + + public abstract void encodeData(FriendlyByteBuf buf, T data); + + public abstract T decodeData(FriendlyByteBuf buf); + + @Override + public void encode(FriendlyByteBuf buf) { + buf.writeInt(data.rowKeySet().size()); + data.rowMap().forEach((key, entry) -> { + buf.writeResourceKey(key); + buf.writeInt(entry.size()); + entry.forEach(((blockPos, t) -> { + buf.writeBlockPos(blockPos); + encodeData(buf, t); + })); + }); + } + + @Override + public void decode(FriendlyByteBuf buf) { + var rowCount = buf.readInt(); + for (int i = 0; i < rowCount; i++) { + var rowKey = buf.readResourceKey(Registries.DIMENSION); + var entryCount = buf.readInt(); + for (int j = 0; j < entryCount; j++) { + var blockPos = buf.readBlockPos(); + var t = decodeData(buf); + data.put(rowKey, blockPos, t); + } + } + } + + @Override + public abstract void execute(IHandlerContext handler); +} diff --git a/src/main/java/com/gregtechceu/gtceu/common/network/packets/prospecting/SPacketProspectBedrockFluid.java b/src/main/java/com/gregtechceu/gtceu/common/network/packets/prospecting/SPacketProspectBedrockFluid.java new file mode 100644 index 0000000000..92f06679e1 --- /dev/null +++ b/src/main/java/com/gregtechceu/gtceu/common/network/packets/prospecting/SPacketProspectBedrockFluid.java @@ -0,0 +1,46 @@ +package com.gregtechceu.gtceu.common.network.packets.prospecting; + +import com.gregtechceu.gtceu.api.gui.misc.ProspectorMode; +import com.gregtechceu.gtceu.integration.map.cache.client.GTClientCache; + +import com.lowdragmc.lowdraglib.networking.IHandlerContext; + +import net.minecraft.core.BlockPos; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.resources.ResourceKey; +import net.minecraft.world.level.Level; + +import java.util.Collection; + +public class SPacketProspectBedrockFluid extends SPacketProspect { + + public SPacketProspectBedrockFluid() { + super(); + } + + public SPacketProspectBedrockFluid(ResourceKey key, Collection positions, + Collection prospected) { + super(key, positions, prospected); + } + + public SPacketProspectBedrockFluid(ResourceKey key, BlockPos pos, ProspectorMode.FluidInfo vein) { + super(key, pos, vein); + } + + @Override + public void encodeData(FriendlyByteBuf buf, ProspectorMode.FluidInfo data) { + ProspectorMode.FLUID.serialize(data, buf); + } + + @Override + public ProspectorMode.FluidInfo decodeData(FriendlyByteBuf buf) { + return ProspectorMode.FLUID.deserialize(buf); + } + + @Override + public void execute(IHandlerContext handler) { + data.rowMap().forEach((level, fluids) -> fluids + .forEach((blockPos, fluid) -> GTClientCache.instance.addFluid(level, + blockPos.getX() >> 4, blockPos.getZ() >> 4, fluid))); + } +} diff --git a/src/main/java/com/gregtechceu/gtceu/common/network/packets/prospecting/SPacketProspectBedrockOre.java b/src/main/java/com/gregtechceu/gtceu/common/network/packets/prospecting/SPacketProspectBedrockOre.java new file mode 100644 index 0000000000..27b52e5f26 --- /dev/null +++ b/src/main/java/com/gregtechceu/gtceu/common/network/packets/prospecting/SPacketProspectBedrockOre.java @@ -0,0 +1,29 @@ +package com.gregtechceu.gtceu.common.network.packets.prospecting; + +import com.gregtechceu.gtceu.api.gui.misc.ProspectorMode; + +import com.lowdragmc.lowdraglib.networking.IHandlerContext; + +import net.minecraft.network.FriendlyByteBuf; + +public class SPacketProspectBedrockOre extends SPacketProspect { + + public SPacketProspectBedrockOre() { + super(); + } + + @Override + public void encodeData(FriendlyByteBuf buf, ProspectorMode.OreInfo data) { + ProspectorMode.BEDROCK_ORE.serialize(data, buf); + } + + @Override + public ProspectorMode.OreInfo decodeData(FriendlyByteBuf buf) { + return ProspectorMode.BEDROCK_ORE.deserialize(buf); + } + + @Override + public void execute(IHandlerContext handler) { + // todo: add cache for bedrock ore veins + } +} diff --git a/src/main/java/com/gregtechceu/gtceu/common/network/packets/prospecting/SPacketProspectOre.java b/src/main/java/com/gregtechceu/gtceu/common/network/packets/prospecting/SPacketProspectOre.java new file mode 100644 index 0000000000..1cdc440af9 --- /dev/null +++ b/src/main/java/com/gregtechceu/gtceu/common/network/packets/prospecting/SPacketProspectOre.java @@ -0,0 +1,40 @@ +package com.gregtechceu.gtceu.common.network.packets.prospecting; + +import com.gregtechceu.gtceu.api.data.worldgen.ores.GeneratedVeinMetadata; +import com.gregtechceu.gtceu.integration.map.cache.client.GTClientCache; + +import com.lowdragmc.lowdraglib.networking.IHandlerContext; + +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.resources.ResourceKey; +import net.minecraft.world.level.Level; + +import java.util.Collection; + +public class SPacketProspectOre extends SPacketProspect { + + public SPacketProspectOre() { + super(); + } + + public SPacketProspectOre(ResourceKey key, Collection veins) { + super(key, veins.stream().map(GeneratedVeinMetadata::center).toList(), veins); + } + + @Override + public void encodeData(FriendlyByteBuf buf, GeneratedVeinMetadata data) { + data.writeToPacket(buf); + } + + @Override + public GeneratedVeinMetadata decodeData(FriendlyByteBuf buf) { + return GeneratedVeinMetadata.readFromPacket(buf); + } + + @Override + public void execute(IHandlerContext handler) { + data.rowMap().forEach((level, ores) -> ores + .forEach((blockPos, vein) -> GTClientCache.instance.addVein(level, + blockPos.getX() >> 4, blockPos.getZ() >> 4, vein))); + } +} diff --git a/src/main/java/com/gregtechceu/gtceu/integration/map/cache/server/ServerCache.java b/src/main/java/com/gregtechceu/gtceu/integration/map/cache/server/ServerCache.java index a64ff50af8..59844e8a63 100644 --- a/src/main/java/com/gregtechceu/gtceu/integration/map/cache/server/ServerCache.java +++ b/src/main/java/com/gregtechceu/gtceu/integration/map/cache/server/ServerCache.java @@ -6,7 +6,7 @@ import com.gregtechceu.gtceu.api.data.worldgen.ores.GeneratedVeinMetadata; import com.gregtechceu.gtceu.api.registry.GTRegistries; import com.gregtechceu.gtceu.common.network.GTNetwork; -import com.gregtechceu.gtceu.common.network.packets.SPacketOreProspect; +import com.gregtechceu.gtceu.common.network.packets.prospecting.SPacketProspectOre; import com.gregtechceu.gtceu.config.ConfigHolder; import com.gregtechceu.gtceu.integration.map.cache.DimensionCache; import com.gregtechceu.gtceu.integration.map.cache.WorldCache; @@ -80,7 +80,7 @@ public void prospectBySurfaceRockMaterial(ResourceKey dim, final Material foundVeins.add(nearbyVein); } } - GTNetwork.NETWORK.sendToPlayer(new SPacketOreProspect(dim, foundVeins), player); + GTNetwork.NETWORK.sendToPlayer(new SPacketProspectOre(dim, foundVeins), player); } public void prospectByOreMaterial(ResourceKey dim, Material material, BlockPos origin, ServerPlayer player, @@ -93,7 +93,7 @@ public void prospectByOreMaterial(ResourceKey dim, Material material, Blo foundVeins.add(nearbyVein); } } - GTNetwork.NETWORK.sendToPlayer(new SPacketOreProspect(dim, foundVeins), player); + GTNetwork.NETWORK.sendToPlayer(new SPacketProspectOre(dim, foundVeins), player); } public void prospectByDepositName(ResourceKey dim, String depositName, BlockPos origin, ServerPlayer player, @@ -106,12 +106,12 @@ public void prospectByDepositName(ResourceKey dim, String depositName, Bl foundVeins.add(nearbyVein); } } - GTNetwork.NETWORK.sendToPlayer(new SPacketOreProspect(dim, foundVeins), player); + GTNetwork.NETWORK.sendToPlayer(new SPacketProspectOre(dim, foundVeins), player); } public void prospectAllInChunk(ResourceKey dim, ChunkPos pos, ServerPlayer player) { if (cache.containsKey(dim)) { - GTNetwork.NETWORK.sendToPlayer(new SPacketOreProspect(dim, cache.get(dim).getVeinsInChunk(pos)), player); + GTNetwork.NETWORK.sendToPlayer(new SPacketProspectOre(dim, cache.get(dim).getVeinsInChunk(pos)), player); } }