Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix prospecting inconsistencies #2688

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<FluidInfo> FLUID = new ProspectorMode<>("metaitem.prospector.mode.fluid", 1) {
Expand All @@ -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)
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,13 +24,16 @@
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;

import net.minecraft.ChatFormatting;
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;
Expand Down Expand Up @@ -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)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<T> implements IPacket {

protected final Table<ResourceKey<Level>, BlockPos, T> data;

protected SPacketProspect() {
data = HashBasedTable.create();
}

protected SPacketProspect(Table<ResourceKey<Level>, BlockPos, T> data) {
this.data = data;
}

protected SPacketProspect(Collection<ResourceKey<Level>> keys, Collection<BlockPos> positions,
Collection<T> 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<Level> key, Collection<BlockPos> positions, Collection<T> 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<Level> 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);
}
Original file line number Diff line number Diff line change
@@ -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<ProspectorMode.FluidInfo> {

public SPacketProspectBedrockFluid() {
super();
}

public SPacketProspectBedrockFluid(ResourceKey<Level> key, Collection<BlockPos> positions,
Collection<ProspectorMode.FluidInfo> prospected) {
super(key, positions, prospected);
}

public SPacketProspectBedrockFluid(ResourceKey<Level> 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)));
}
}
Original file line number Diff line number Diff line change
@@ -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<ProspectorMode.OreInfo> {

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
}
}
Loading
Loading