Merge pull request #1531 from MellowArpeggiation/master

ByteBuf string reader/writer + performance improvements
This commit is contained in:
HbmMods 2024-06-07 18:55:15 +02:00 committed by GitHub
commit 49ddcd6f85
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 212 additions and 120 deletions

View File

@ -10,7 +10,9 @@ import com.hbm.blocks.IPersistentInfoProvider;
import com.hbm.blocks.ITooltipProvider;
import com.hbm.blocks.ModBlocks;
import com.hbm.lib.RefStrings;
import com.hbm.tileentity.INBTPacketReceiver;
import com.hbm.packet.BufPacket;
import com.hbm.packet.PacketDispatcher;
import com.hbm.tileentity.IBufPacketReceiver;
import com.hbm.tileentity.IPersistentNBT;
import com.hbm.tileentity.TileEntityLoadedBase;
import com.hbm.util.BobMathUtil;
@ -20,8 +22,10 @@ import com.hbm.util.fauxpointtwelve.BlockPos;
import api.hbm.energymk2.IEnergyProviderMK2;
import api.hbm.energymk2.IEnergyReceiverMK2;
import cpw.mods.fml.client.registry.RenderingRegistry;
import cpw.mods.fml.common.network.NetworkRegistry.TargetPoint;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import io.netty.buffer.ByteBuf;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
@ -146,7 +150,7 @@ public class MachineCapacitor extends BlockContainer implements ILookOverlay, IP
player.addExhaustion(0.025F);
}
public static class TileEntityCapacitor extends TileEntityLoadedBase implements IEnergyProviderMK2, IEnergyReceiverMK2, INBTPacketReceiver, IPersistentNBT {
public static class TileEntityCapacitor extends TileEntityLoadedBase implements IEnergyProviderMK2, IEnergyReceiverMK2, IBufPacketReceiver, IPersistentNBT {
public long power;
protected long maxPower;
@ -190,20 +194,31 @@ public class MachineCapacitor extends BlockContainer implements ILookOverlay, IP
this.tryProvide(worldObj, pos.getX(), pos.getY(), pos.getZ(), last);
}
this.trySubscribe(worldObj, xCoord + opp.offsetX, yCoord+ opp.offsetY, zCoord + opp.offsetZ, opp);
NBTTagCompound data = new NBTTagCompound();
data.setLong("power", power);
data.setLong("maxPower", maxPower);
data.setLong("rec", powerReceived);
data.setLong("sent", powerSent);
INBTPacketReceiver.networkPack(this, data, 15);
this.trySubscribe(worldObj, xCoord + opp.offsetX, yCoord + opp.offsetY, zCoord + opp.offsetZ, opp);
PacketDispatcher.wrapper.sendToAllAround(new BufPacket(xCoord, yCoord, zCoord, this), new TargetPoint(this.worldObj.provider.dimensionId, xCoord, yCoord, zCoord, 15));
this.powerSent = 0;
this.powerReceived = 0;
}
}
@Override
public void serialize(ByteBuf buf) {
buf.writeLong(power);
buf.writeLong(maxPower);
buf.writeLong(powerReceived);
buf.writeLong(powerSent);
}
@Override
public void deserialize(ByteBuf buf) {
power = buf.readLong();
maxPower = buf.readLong();
powerReceived = buf.readLong();
powerSent = buf.readLong();
}
@Override
public long transferPower(long power) {
if(power + this.getPower() <= this.getMaxPower()) {
@ -224,14 +239,6 @@ public class MachineCapacitor extends BlockContainer implements ILookOverlay, IP
this.setPower(this.getPower() - power);
}
@Override
public void networkUnpack(NBTTagCompound nbt) {
this.power = nbt.getLong("power");
this.maxPower = nbt.getLong("maxPower");
this.powerReceived = nbt.getLong("rec");
this.powerSent = nbt.getLong("sent");
}
@Override
public long getPower() {
return power;

View File

@ -2,8 +2,10 @@ package com.hbm.module;
import java.util.List;
import com.hbm.util.BufferUtil;
import com.hbm.util.ItemStackUtil;
import io.netty.buffer.ByteBuf;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
@ -151,4 +153,16 @@ public class ModulePatternMatcher {
}
}
}
public void serialize(ByteBuf buf) {
for(int i = 0; i < modes.length; i++) {
BufferUtil.writeString(buf, modes[i]);
}
}
public void deserialize(ByteBuf buf) {
for(int i = 0; i < modes.length; i++) {
modes[i] = BufferUtil.readString(buf);
}
}
}

View File

@ -1,15 +1,19 @@
package com.hbm.tileentity.machine;
import com.hbm.blocks.machine.BlockHadronPower;
import com.hbm.tileentity.INBTPacketReceiver;
import com.hbm.packet.BufPacket;
import com.hbm.packet.PacketDispatcher;
import com.hbm.tileentity.IBufPacketReceiver;
import com.hbm.tileentity.TileEntityLoadedBase;
import api.hbm.energymk2.IEnergyReceiverMK2;
import cpw.mods.fml.common.network.NetworkRegistry.TargetPoint;
import io.netty.buffer.ByteBuf;
import net.minecraft.block.Block;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.util.ForgeDirection;
public class TileEntityHadronPower extends TileEntityLoadedBase implements IEnergyReceiverMK2, INBTPacketReceiver {
public class TileEntityHadronPower extends TileEntityLoadedBase implements IEnergyReceiverMK2, IBufPacketReceiver {
public long power;
@ -26,15 +30,18 @@ public class TileEntityHadronPower extends TileEntityLoadedBase implements IEner
this.trySubscribe(worldObj, xCoord + dir.offsetX, yCoord + dir.offsetY, zCoord + dir.offsetZ, dir);
}
NBTTagCompound data = new NBTTagCompound();
data.setLong("power", power);
INBTPacketReceiver.networkPack(this, data, 15);
PacketDispatcher.wrapper.sendToAllAround(new BufPacket(xCoord, yCoord, zCoord, this), new TargetPoint(this.worldObj.provider.dimensionId, xCoord, yCoord, zCoord, 15));
}
}
@Override
public void networkUnpack(NBTTagCompound nbt) {
this.power = nbt.getLong("power");
public void serialize(ByteBuf buf) {
buf.writeLong(power);
}
@Override
public void deserialize(ByteBuf buf) {
power = buf.readLong();
}
@Override

View File

@ -29,6 +29,7 @@ import api.hbm.energymk2.IEnergyReceiverMK2;
import api.hbm.fluid.IFluidStandardTransceiver;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import io.netty.buffer.ByteBuf;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
@ -152,17 +153,7 @@ public class TileEntityMachineChemplant extends TileEntityMachineBase implements
process();
}
NBTTagCompound data = new NBTTagCompound();
data.setLong("power", this.power);
data.setInteger("progress", this.progress);
data.setInteger("maxProgress", this.maxProgress);
data.setBoolean("isProgressing", isProgressing);
for(int i = 0; i < tanks.length; i++) {
tanks[i].writeToNBT(data, "t" + i);
}
this.networkPack(data, 150);
this.networkPackNT(150);
} else {
if(isProgressing && this.worldObj.getTotalWorldTime() % 3 == 0) {
@ -197,26 +188,36 @@ public class TileEntityMachineChemplant extends TileEntityMachineBase implements
}
}
}
@Override
public void serialize(ByteBuf buf) {
super.serialize(buf);
buf.writeLong(power);
buf.writeInt(progress);
buf.writeInt(maxProgress);
buf.writeBoolean(isProgressing);
for(int i = 0; i < tanks.length; i++)
tanks[i].serialize(buf);
}
@Override
public void deserialize(ByteBuf buf) {
super.deserialize(buf);
power = buf.readLong();
progress = buf.readInt();
maxProgress = buf.readInt();
isProgressing = buf.readBoolean();
for(int i = 0; i < tanks.length; i++)
tanks[i].deserialize(buf);
}
@Override
public AudioWrapper createAudioLoop() {
return MainRegistry.proxy.getLoopedSound("hbm:block.chemplantOperate", xCoord, yCoord, zCoord, 1.0F, 10F, 1.0F);
}
@Override
public void networkUnpack(NBTTagCompound nbt) {
super.networkUnpack(nbt);
this.power = nbt.getLong("power");
this.progress = nbt.getInteger("progress");
this.maxProgress = nbt.getInteger("maxProgress");
this.isProgressing = nbt.getBoolean("isProgressing");
for(int i = 0; i < tanks.length; i++) {
tanks[i].readFromNBT(nbt, "t" + i);
}
}
@Override
public void onChunkUnload() {
@ -501,7 +502,7 @@ public class TileEntityMachineChemplant extends TileEntityMachineBase implements
@Override
public long getMaxPower() {
return this.maxPower;
return maxPower;
}
@Override
@ -588,12 +589,12 @@ public class TileEntityMachineChemplant extends TileEntityMachineBase implements
public void provideInfo(UpgradeType type, int level, List<String> info, boolean extendedInfo) {
info.add(IUpgradeInfoProvider.getStandardLabel(ModBlocks.machine_chemplant));
if(type == UpgradeType.SPEED) {
info.add(EnumChatFormatting.GREEN + I18nUtil.resolveKey(this.KEY_DELAY, "-" + (level * 25) + "%"));
info.add(EnumChatFormatting.RED + I18nUtil.resolveKey(this.KEY_CONSUMPTION, "+" + (level * 300) + "%"));
info.add(EnumChatFormatting.GREEN + I18nUtil.resolveKey(KEY_DELAY, "-" + (level * 25) + "%"));
info.add(EnumChatFormatting.RED + I18nUtil.resolveKey(KEY_CONSUMPTION, "+" + (level * 300) + "%"));
}
if(type == UpgradeType.POWER) {
info.add(EnumChatFormatting.GREEN + I18nUtil.resolveKey(this.KEY_CONSUMPTION, "-" + (level * 30) + "%"));
info.add(EnumChatFormatting.RED + I18nUtil.resolveKey(this.KEY_DELAY, "+" + (level * 5) + "%"));
info.add(EnumChatFormatting.GREEN + I18nUtil.resolveKey(KEY_CONSUMPTION, "-" + (level * 30) + "%"));
info.add(EnumChatFormatting.RED + I18nUtil.resolveKey(KEY_DELAY, "+" + (level * 5) + "%"));
}
if(type == UpgradeType.OVERDRIVE) {
info.add((BobMathUtil.getBlink() ? EnumChatFormatting.RED : EnumChatFormatting.DARK_GRAY) + "YES");

View File

@ -5,19 +5,23 @@ import com.hbm.inventory.fluid.Fluids;
import com.hbm.inventory.fluid.tank.FluidTank;
import com.hbm.inventory.recipes.FractionRecipes;
import com.hbm.lib.Library;
import com.hbm.tileentity.INBTPacketReceiver;
import com.hbm.packet.BufPacket;
import com.hbm.packet.PacketDispatcher;
import com.hbm.tileentity.IBufPacketReceiver;
import com.hbm.tileentity.TileEntityLoadedBase;
import com.hbm.util.Tuple.Pair;
import com.hbm.util.fauxpointtwelve.DirPos;
import api.hbm.fluid.IFluidStandardTransceiver;
import cpw.mods.fml.common.network.NetworkRegistry.TargetPoint;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import io.netty.buffer.ByteBuf;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
public class TileEntityMachineFractionTower extends TileEntityLoadedBase implements INBTPacketReceiver, IFluidStandardTransceiver {
public class TileEntityMachineFractionTower extends TileEntityLoadedBase implements IBufPacketReceiver, IFluidStandardTransceiver {
public FluidTank[] tanks;
@ -64,20 +68,21 @@ public class TileEntityMachineFractionTower extends TileEntityLoadedBase impleme
fractionate();
this.sendFluid();
NBTTagCompound data = new NBTTagCompound();
for(int i = 0; i < 3; i++)
tanks[i].writeToNBT(data, "tank" + i);
INBTPacketReceiver.networkPack(this, data, 50);
PacketDispatcher.wrapper.sendToAllAround(new BufPacket(xCoord, yCoord, zCoord, this), new TargetPoint(this.worldObj.provider.dimensionId, xCoord, yCoord, zCoord, 50));
}
}
@Override
public void networkUnpack(NBTTagCompound nbt) {
public void serialize(ByteBuf buf) {
for(int i = 0; i < 3; i++)
tanks[i].readFromNBT(nbt, "tank" + i);
tanks[i].serialize(buf);
}
@Override
public void deserialize(ByteBuf buf) {
for(int i = 0; i < 3; i++)
tanks[i].deserialize(buf);
}
private void updateConnections() {

View File

@ -22,6 +22,7 @@ import com.hbm.util.fauxpointtwelve.DirPos;
import cpw.mods.fml.common.Optional;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import io.netty.buffer.ByteBuf;
import li.cil.oc.api.machine.Arguments;
import li.cil.oc.api.machine.Callback;
import li.cil.oc.api.machine.Context;
@ -108,7 +109,6 @@ public class TileEntityBarrel extends TileEntityMachineBase implements IFluidAcc
tank.setType(0, 1, slots);
tank.loadTank(2, 3, slots);
tank.unloadTank(4, 5, slots);
tank.updateTank(xCoord, yCoord, zCoord, worldObj.provider.dimensionId);
this.sendingBrake = true;
tank.setFill(transmitFluidFairly(worldObj, tank, this, tank.getFill(), this.mode == 0 || this.mode == 1, this.mode == 1 || this.mode == 2, getConPos()));
@ -121,11 +121,23 @@ public class TileEntityBarrel extends TileEntityMachineBase implements IFluidAcc
checkFluidInteraction();
}
NBTTagCompound data = new NBTTagCompound();
data.setShort("mode", mode);
this.networkPack(data, 50);
this.networkPackNT(50);
}
}
@Override
public void serialize(ByteBuf buf) {
super.serialize(buf);
buf.writeShort(mode);
tank.serialize(buf);
}
@Override
public void deserialize(ByteBuf buf) {
super.deserialize(buf);
mode = buf.readShort();
tank.deserialize(buf);
}
protected DirPos[] getConPos() {
return new DirPos[] {
@ -140,8 +152,8 @@ public class TileEntityBarrel extends TileEntityMachineBase implements IFluidAcc
protected static int transmitFluidFairly(World world, FluidTank tank, IFluidConnector that, int fill, boolean connect, boolean send, DirPos[] connections) {
Set<IPipeNet> nets = new HashSet();
Set<IFluidConnector> consumers = new HashSet();
Set<IPipeNet> nets = new HashSet<>();
Set<IFluidConnector> consumers = new HashSet<>();
FluidType type = tank.getTankType();
int pressure = tank.getPressure();
@ -166,13 +178,13 @@ public class TileEntityBarrel extends TileEntityMachineBase implements IFluidAcc
consumers.remove(that);
if(fill > 0 && send) {
List<IFluidConnector> con = new ArrayList();
List<IFluidConnector> con = new ArrayList<>();
con.addAll(consumers);
con.removeIf(x -> x == null || !(x instanceof TileEntity) || ((TileEntity)x).isInvalid());
if(PipeNet.trackingInstances == null) {
PipeNet.trackingInstances = new ArrayList();
PipeNet.trackingInstances = new ArrayList<>();
}
PipeNet.trackingInstances.clear();
@ -265,12 +277,6 @@ public class TileEntityBarrel extends TileEntityMachineBase implements IFluidAcc
}
}
}
public void networkUnpack(NBTTagCompound data) {
super.networkUnpack(data);
mode = data.getShort("mode");
}
@Override
public void setFillForSync(int fill, int index) {

View File

@ -34,6 +34,7 @@ import cpw.mods.fml.common.Optional;
import cpw.mods.fml.common.network.NetworkRegistry.TargetPoint;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import io.netty.buffer.ByteBuf;
import li.cil.oc.api.machine.Arguments;
import li.cil.oc.api.machine.Callback;
import li.cil.oc.api.machine.Context;
@ -161,11 +162,7 @@ public class TileEntityMachineFluidTank extends TileEntityMachineBase implements
tank.unloadTank(4, 5, slots);
NBTTagCompound data = new NBTTagCompound();
data.setShort("mode", mode);
data.setBoolean("hasExploded", hasExploded);
this.tank.writeToNBT(data, "t");
this.networkPack(data, 150);
this.networkPackNT(150);
}
ForgeDirection dir = ForgeDirection.getOrientation(this.getBlockMetadata() - 10);
@ -177,6 +174,22 @@ public class TileEntityMachineFluidTank extends TileEntityMachineBase implements
props.isOnLadder = true;
}
}
@Override
public void serialize(ByteBuf buf) {
super.serialize(buf);
buf.writeShort(mode);
buf.writeBoolean(hasExploded);
tank.serialize(buf);
}
@Override
public void deserialize(ByteBuf buf) {
super.deserialize(buf);
mode = buf.readShort();
hasExploded = buf.readBoolean();
tank.deserialize(buf);
}
/** called when the tank breaks due to hazardous materials or external force, can be used to quickly void part of the tank or spawn a mushroom cloud */
public void explode() {
@ -269,12 +282,6 @@ public class TileEntityMachineFluidTank extends TileEntityMachineBase implements
};
}
public void networkUnpack(NBTTagCompound data) {
this.mode = data.getShort("mode");
this.hasExploded = data.getBoolean("hasExploded");
this.tank.readFromNBT(data, "t");
}
public void handleButtonPacket(int value, int meta) {
mode = (short) ((mode + 1) % modes);
this.markChanged();
@ -462,7 +469,7 @@ public class TileEntityMachineFluidTank extends TileEntityMachineBase implements
return this.hasExploded;
}
List<AStack> repair = new ArrayList();
List<AStack> repair = new ArrayList<>();
@Override
public List<AStack> getRepairMaterials() {

View File

@ -10,6 +10,7 @@ import com.hbm.module.ModulePatternMatcher;
import com.hbm.tileentity.IGUIProvider;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import io.netty.buffer.ByteBuf;
import net.minecraft.block.Block;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.entity.player.EntityPlayer;
@ -154,13 +155,24 @@ public class TileEntityCraneExtractor extends TileEntityCraneBase implements IGU
}
}
}
NBTTagCompound data = new NBTTagCompound();
data.setBoolean("isWhitelist", isWhitelist);
this.matcher.writeToNBT(data);
this.networkPack(data, 15);
this.networkPackNT(15);
}
}
@Override
public void serialize(ByteBuf buf) {
super.serialize(buf);
buf.writeBoolean(isWhitelist);
this.matcher.serialize(buf);
}
@Override
public void deserialize(ByteBuf buf) {
super.deserialize(buf);
isWhitelist = buf.readBoolean();
this.matcher.deserialize(buf);
}
public static int[] masquerade(ISidedInventory sided, int side) {
@ -171,14 +183,6 @@ public class TileEntityCraneExtractor extends TileEntityCraneBase implements IGU
return sided.getAccessibleSlotsFromSide(side);
}
public void networkUnpack(NBTTagCompound nbt) {
super.networkUnpack(nbt);
this.isWhitelist = nbt.getBoolean("isWhitelist");
this.matcher.modes = new String[this.matcher.modes.length];
this.matcher.readFromNBT(nbt);
}
public boolean matchesFilter(ItemStack stack) {
for(int i = 0; i < 9; i++) {

View File

@ -3,9 +3,13 @@ package com.hbm.tileentity.network;
import java.util.List;
import com.hbm.entity.item.EntityDeliveryDrone;
import com.hbm.tileentity.INBTPacketReceiver;
import com.hbm.packet.BufPacket;
import com.hbm.packet.PacketDispatcher;
import com.hbm.tileentity.IBufPacketReceiver;
import com.hbm.util.fauxpointtwelve.BlockPos;
import cpw.mods.fml.common.network.NetworkRegistry.TargetPoint;
import io.netty.buffer.ByteBuf;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
@ -13,7 +17,7 @@ import net.minecraft.util.MathHelper;
import net.minecraft.util.Vec3;
import net.minecraftforge.common.util.ForgeDirection;
public class TileEntityDroneWaypoint extends TileEntity implements INBTPacketReceiver, IDroneLinkable {
public class TileEntityDroneWaypoint extends TileEntity implements IBufPacketReceiver, IDroneLinkable {
public int height = 5;
public int nextX = -1;
@ -35,11 +39,8 @@ public class TileEntityDroneWaypoint extends TileEntity implements INBTPacketRec
}
}
}
NBTTagCompound data = new NBTTagCompound();
data.setInteger("height", height);
data.setIntArray("pos", new int[] {nextX, nextY, nextZ});
INBTPacketReceiver.networkPack(this, data, 15);
PacketDispatcher.wrapper.sendToAllAround(new BufPacket(xCoord, yCoord, zCoord, this), new TargetPoint(this.worldObj.provider.dimensionId, xCoord, yCoord, zCoord, 15));
} else {
if(nextY != -1 && worldObj.getTotalWorldTime() % 2 == 0) {
@ -52,6 +53,22 @@ public class TileEntityDroneWaypoint extends TileEntity implements INBTPacketRec
}
}
@Override
public void serialize(ByteBuf buf) {
buf.writeInt(height);
buf.writeInt(nextX);
buf.writeInt(nextY);
buf.writeInt(nextZ);
}
@Override
public void deserialize(ByteBuf buf) {
height = buf.readInt();
nextX = buf.readInt();
nextY = buf.readInt();
nextZ = buf.readInt();
}
@Override
public BlockPos getPoint() {
ForgeDirection dir = ForgeDirection.getOrientation(this.getBlockMetadata());
@ -65,15 +82,6 @@ public class TileEntityDroneWaypoint extends TileEntity implements INBTPacketRec
this.nextZ = z;
this.markDirty();
}
@Override
public void networkUnpack(NBTTagCompound nbt) {
this.height = nbt.getInteger("height");
int[] pos = nbt.getIntArray("pos");
this.nextX = pos[0];
this.nextY = pos[1];
this.nextZ = pos[2];
}
public void addHeight(int h) {
height += h;

View File

@ -0,0 +1,33 @@
package com.hbm.util;
import java.nio.charset.Charset;
import io.netty.buffer.ByteBuf;
public class BufferUtil {
private static final Charset CHARSET = Charset.forName("UTF-8");
// Writes a string to a byte buffer by encoding the length and raw bytes
public static final void writeString(ByteBuf buf, String value) {
if(value == null) {
buf.writeInt(-1);
return;
}
buf.writeInt(value.length());
buf.writeBytes(value.getBytes(CHARSET));
}
// Reads a string from a byte buffer via the written length and raw bytes
public static final String readString(ByteBuf buf) {
final int count = buf.readInt();
if(count < 0) return null;
final byte[] bytes = new byte[count];
buf.readBytes(bytes);
return new String(bytes, CHARSET);
}
}