Skip to content

Commit

Permalink
Add endpoints to packet
Browse files Browse the repository at this point in the history
  • Loading branch information
yuesha-yc committed Jan 10, 2025
1 parent c24c14e commit 741f598
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -322,21 +322,27 @@ protected void pushData() {
public void writeNetwork(FriendlyByteBuf pb) {
pb.writeRegistryIdUnsafe(ForgeRegistries.BLOCKS, blk);
pb.writeBlockPos(pos);
pb.writeInt(priority);
pb.writeFloat(capacity);
pb.writeFloat(avgIntake);
pb.writeFloat(avgOutput);
pb.writeBoolean(canCostMore);
pb.writeFloat(maxIntake);
pb.writeFloat(maxOutput);
}

public static HeatEndpoint readNetwork(FriendlyByteBuf pb) {
HeatEndpoint dat = new HeatEndpoint(
pb.readRegistryIdUnsafe(ForgeRegistries.BLOCKS),
pb.readBlockPos(),
pb.readInt(),
pb.readFloat()
);
dat.avgIntake = pb.readFloat();
dat.avgOutput = pb.readFloat();
dat.canCostMore = pb.readBoolean();
dat.maxIntake = pb.readFloat();
dat.maxOutput = pb.readFloat();
return dat;
}

Expand All @@ -351,17 +357,6 @@ public void save(CompoundTag nbt, boolean isPacket) {
//nbt.putLong("pos", pos.asLong());
//nbt.putString("block", RegistryUtils.getRegistryName(blk).toString());
}
public void loadNetwork(CompoundTag nbt, boolean isPacket) {
heat = nbt.getFloat("net_power");
pos = BlockPos.of(nbt.getLong("pos"));
blk = RegistryUtils.getBlock(new ResourceLocation(nbt.getString("block")));
}

public void saveNetwork(CompoundTag nbt, boolean isPacket) {
nbt.putFloat("net_power", heat);
nbt.putLong("pos", pos.asLong());
nbt.putString("block", RegistryUtils.getRegistryName(blk).toString());
}
@Override
public HeatNetwork getNetwork() {
return network;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.player.Inventory;
Expand All @@ -43,17 +44,7 @@
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.common.util.LazyOptional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Set;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;

Expand Down Expand Up @@ -512,4 +503,9 @@ public List<HeatEndpoint> getEndpoints(){
return eplist;
}

public void setEndpoints(Collection<HeatEndpoint> endpoints) {
this.endpoints.clear();
for(HeatEndpoint i : endpoints)
this.endpoints.add(LazyOptional.of(()->i));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,47 @@
import com.teammoeg.frostedheart.FHMain;
import com.teammoeg.frostedheart.base.network.FHMessage;
import com.teammoeg.frostedheart.util.FHUtils;
import com.teammoeg.frostedheart.util.io.SerializeUtil;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.network.NetworkEvent;

import java.util.Collection;
import java.util.function.Supplier;

/**
* Sends all endpoints and total output and intake of a HeatNetwork to the client.
*/
public class HeatNetworkResponseS2CPacket implements FHMessage {
BlockPos pos;
private final float totalEndpointOutput;
private final float totalEndpointIntake;
private final Collection<HeatEndpoint> endpoints;


public HeatNetworkResponseS2CPacket(BlockPos pos, HeatNetwork network) {
this.pos = pos;
this.totalEndpointOutput = network.getTotalEndpointOutput();
this.totalEndpointIntake = network.getTotalEndpointIntake();
this.endpoints = network.getEndpoints();
}

public HeatNetworkResponseS2CPacket(FriendlyByteBuf buffer) {
pos = buffer.readBlockPos();
totalEndpointOutput = buffer.readFloat();
totalEndpointIntake = buffer.readFloat();
endpoints = SerializeUtil.readList(buffer, HeatEndpoint::readNetwork);
}

@Override
public void encode(FriendlyByteBuf buffer) {
buffer.writeBlockPos(pos);
buffer.writeFloat(totalEndpointOutput);
buffer.writeFloat(totalEndpointIntake);
SerializeUtil.writeList(buffer, endpoints, HeatEndpoint::writeNetwork);
}

@Override
Expand All @@ -45,9 +55,12 @@ public void handle(Supplier<NetworkEvent.Context> context) {
if (be instanceof HeatNetworkProvider hp) {
FHMain.LOGGER.debug("Server data received. Updating client HeatNetwork data.");
HeatNetwork network = hp.getNetwork();
network.setTotalEndpointOutput(totalEndpointOutput);
network.setTotalEndpointIntake(totalEndpointIntake);
FHMain.LOGGER.debug("Update Complete: " + pos + " " + totalEndpointOutput + " " + totalEndpointIntake);
if (network != null) {
network.setTotalEndpointOutput(totalEndpointOutput);
network.setTotalEndpointIntake(totalEndpointIntake);
network.setEndpoints(endpoints);
FHMain.LOGGER.debug("Update Complete: " + pos + " " + totalEndpointOutput + " " + totalEndpointIntake);
}
}
});
context.get().setPacketHandled(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class CreativeHeaterBlockEntity extends HeatManagerBlockEntity {
protected ScrollValueBehaviour generatedHeat;
public CreativeHeaterBlockEntity(BlockEntityType<? extends BlockEntity> type, BlockPos pos, BlockState state) {
super(type, pos, state);
endpoint = new HeatEndpoint(-1, MAX_HEAT * 4, MAX_HEAT, 0);
endpoint = new HeatEndpoint(-1, MAX_HEAT * 4, DEFAULT_HEAT, 0);
}

@Override
Expand Down

0 comments on commit 741f598

Please sign in to comment.