Significant performance boost for Coal Gen (#1184)

Needs testing.
This commit is contained in:
Brennan Ward 2018-09-27 15:17:23 -04:00
parent 7b44cec1dc
commit f585d814ce
6 changed files with 155 additions and 114 deletions

View file

@ -14,7 +14,7 @@ buildscript {
apply plugin: 'net.minecraftforge.gradle.forge' apply plugin: 'net.minecraftforge.gradle.forge'
apply plugin: 'idea' apply plugin: 'idea'
version = "1.12.2-r141" version = "1.12.2-r142_dev"
group = "de.ellpeck.actuallyadditions" group = "de.ellpeck.actuallyadditions"
archivesBaseName = "ActuallyAdditions" archivesBaseName = "ActuallyAdditions"

View file

@ -39,6 +39,10 @@ public class CustomEnergyStorage extends EnergyStorage{
return toReturn; return toReturn;
} }
public void addEnergyRaw(int energy){
this.energy = Math.min(this.energy + energy, this.capacity);
}
@Override @Override
public int receiveEnergy(int maxReceive, boolean simulate){ public int receiveEnergy(int maxReceive, boolean simulate){
if(!this.canReceive()){ if(!this.canReceive()){

View file

@ -12,8 +12,7 @@ package de.ellpeck.actuallyadditions.mod.tile;
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions; import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
import de.ellpeck.actuallyadditions.mod.config.values.ConfigIntValues; import de.ellpeck.actuallyadditions.mod.config.values.ConfigIntValues;
import de.ellpeck.actuallyadditions.mod.network.PacketHandler; import de.ellpeck.actuallyadditions.mod.util.VanillaPacketDispatcher;
import de.ellpeck.actuallyadditions.mod.network.PacketServerToClient;
import de.ellpeck.actuallyadditions.mod.util.WorldUtil; import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
import net.minecraft.block.state.IBlockState; import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
@ -33,7 +32,6 @@ import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.IEnergyStorage; import net.minecraftforge.energy.IEnergyStorage;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler; import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler; import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.fml.common.registry.GameRegistry; import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.items.CapabilityItemHandler; import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.IItemHandler;
@ -161,6 +159,8 @@ public abstract class TileEntityBase extends TileEntity implements ITickable{
} }
public final void sendUpdate(){ public final void sendUpdate(){
if(world != null && !world.isRemote) VanillaPacketDispatcher.dispatchTEToNearbyPlayers(this);
/*
if(this.world != null && !this.world.isRemote){ if(this.world != null && !this.world.isRemote){
NBTTagCompound compound = new NBTTagCompound(); NBTTagCompound compound = new NBTTagCompound();
this.writeSyncableNBT(compound, NBTType.SYNC); this.writeSyncableNBT(compound, NBTType.SYNC);
@ -170,8 +170,8 @@ public abstract class TileEntityBase extends TileEntity implements ITickable{
data.setInteger("X", this.pos.getX()); data.setInteger("X", this.pos.getX());
data.setInteger("Y", this.pos.getY()); data.setInteger("Y", this.pos.getY());
data.setInteger("Z", this.pos.getZ()); data.setInteger("Z", this.pos.getZ());
PacketHandler.theNetwork.sendToAllAround(new PacketServerToClient(data, PacketHandler.TILE_ENTITY_HANDLER), new NetworkRegistry.TargetPoint(this.world.provider.getDimension(), this.getPos().getX(), this.getPos().getY(), this.getPos().getZ(), 64)); PacketHandler.theNetwork.sendToAllTracking(new PacketServerToClient(data, PacketHandler.TILE_ENTITY_HANDLER), new TargetPoint(this.world.provider.getDimension(), this.getPos().getX(), this.getPos().getY(), this.getPos().getZ(), 0));
} }*/
} }
public void writeSyncableNBT(NBTTagCompound compound, NBTType type){ public void writeSyncableNBT(NBTTagCompound compound, NBTType type){
@ -227,11 +227,14 @@ public abstract class TileEntityBase extends TileEntity implements ITickable{
return 0; return 0;
} }
private boolean shareEnergy = this instanceof ISharingEnergyProvider;
private boolean shareFluid = this instanceof ISharingFluidHandler;
public void updateEntity(){ public void updateEntity(){
this.ticksElapsed++; this.ticksElapsed++;
if(!this.world.isRemote){ if(!this.world.isRemote){
if(this instanceof ISharingEnergyProvider){ if(shareEnergy){
ISharingEnergyProvider provider = (ISharingEnergyProvider)this; ISharingEnergyProvider provider = (ISharingEnergyProvider)this;
if(provider.doesShareEnergy()){ if(provider.doesShareEnergy()){
int total = provider.getEnergyToSplitShare(); int total = provider.getEnergyToSplitShare();
@ -253,7 +256,7 @@ public abstract class TileEntityBase extends TileEntity implements ITickable{
} }
} }
if(this instanceof ISharingFluidHandler){ if(shareFluid){
ISharingFluidHandler handler = (ISharingFluidHandler)this; ISharingFluidHandler handler = (ISharingFluidHandler)this;
if(handler.doesShareFluid()){ if(handler.doesShareFluid()){
int total = handler.getMaxFluidAmountToSplitShare(); int total = handler.getMaxFluidAmountToSplitShare();

View file

@ -12,6 +12,7 @@ package de.ellpeck.actuallyadditions.mod.tile;
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor; import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor;
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IRemover; import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IRemover;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
@ -23,124 +24,131 @@ import net.minecraftforge.fml.relauncher.SideOnly;
public class TileEntityCoalGenerator extends TileEntityInventoryBase implements ISharingEnergyProvider { public class TileEntityCoalGenerator extends TileEntityInventoryBase implements ISharingEnergyProvider {
public static final int PRODUCE = 30; public static final int PRODUCE = 30;
public final CustomEnergyStorage storage = new CustomEnergyStorage(60000, 0, 80); public final CustomEnergyStorage storage = new CustomEnergyStorage(60000, 0, 80);
public int maxBurnTime; public int maxBurnTime;
public int currentBurnTime; public int currentBurnTime;
private int lastEnergy; private int lastEnergy;
private int lastBurnTime; private int lastBurnTime;
private int lastCurrentBurnTime; private int lastCurrentBurnTime;
private int lastCompare; private int lastCompare;
private ItemStack curStack = ItemStack.EMPTY;
private int curBurn = -1;
public TileEntityCoalGenerator() { public TileEntityCoalGenerator() {
super(1, "coalGenerator"); super(1, "coalGenerator");
} }
@SideOnly(Side.CLIENT) @SideOnly(Side.CLIENT)
public int getEnergyScaled(int i) { public int getEnergyScaled(int i) {
return this.storage.getEnergyStored() * i / this.storage.getMaxEnergyStored(); return this.storage.getEnergyStored() * i / this.storage.getMaxEnergyStored();
} }
@SideOnly(Side.CLIENT) @SideOnly(Side.CLIENT)
public int getBurningScaled(int i) { public int getBurningScaled(int i) {
return this.currentBurnTime * i / this.maxBurnTime; return this.currentBurnTime * i / this.maxBurnTime;
} }
@Override @Override
public void writeSyncableNBT(NBTTagCompound compound, NBTType type) { public void writeSyncableNBT(NBTTagCompound compound, NBTType type) {
if (type != NBTType.SAVE_BLOCK) { if (type != NBTType.SAVE_BLOCK) {
compound.setInteger("BurnTime", this.currentBurnTime); compound.setInteger("BurnTime", this.currentBurnTime);
compound.setInteger("MaxBurnTime", this.maxBurnTime); compound.setInteger("MaxBurnTime", this.maxBurnTime);
} }
this.storage.writeToNBT(compound); this.storage.writeToNBT(compound);
super.writeSyncableNBT(compound, type); super.writeSyncableNBT(compound, type);
} }
@Override @Override
public void readSyncableNBT(NBTTagCompound compound, NBTType type) { public void readSyncableNBT(NBTTagCompound compound, NBTType type) {
if (type != NBTType.SAVE_BLOCK) { if (type != NBTType.SAVE_BLOCK) {
this.currentBurnTime = compound.getInteger("BurnTime"); this.currentBurnTime = compound.getInteger("BurnTime");
this.maxBurnTime = compound.getInteger("MaxBurnTime"); this.maxBurnTime = compound.getInteger("MaxBurnTime");
} }
this.storage.readFromNBT(compound); this.storage.readFromNBT(compound);
super.readSyncableNBT(compound, type); super.readSyncableNBT(compound, type);
} }
@Override @Override
public void updateEntity() { public void updateEntity() {
super.updateEntity(); super.updateEntity();
if (!this.world.isRemote) { if (!this.world.isRemote) {
boolean flag = this.currentBurnTime > 0; boolean flag = this.currentBurnTime > 0;
if (this.currentBurnTime > 0) { if (this.currentBurnTime > 0) {
this.currentBurnTime--; this.currentBurnTime--;
this.storage.receiveEnergyInternal(PRODUCE, false); this.storage.addEnergyRaw(PRODUCE);
} }
ItemStack stack = inv.getStackInSlot(0); ItemStack stack = inv.getStackInSlot(0);
int burn = TileEntityFurnace.getItemBurnTime(stack); if (!stack.isEmpty() && stack != curStack) {
if (!this.isRedstonePowered && this.currentBurnTime <= 0 && burn > 0 && this.storage.getEnergyStored() < this.storage.getMaxEnergyStored()) { curStack = stack;
this.maxBurnTime = burn; curBurn = TileEntityFurnace.getItemBurnTime(stack);
this.currentBurnTime = burn; } else if (stack.isEmpty()) {
ItemStack copy = stack.copy(); curStack = ItemStack.EMPTY;
stack.shrink(1); curBurn = -1;
if (stack.isEmpty()) inv.setStackInSlot(0, copy.getItem().getContainerItem(copy)); }
}
if (flag != this.currentBurnTime > 0 || this.lastCompare != this.getComparatorStrength()) { if (!this.isRedstonePowered && this.currentBurnTime <= 0 && curBurn > 0 && this.storage.getEnergyStored() < this.storage.getMaxEnergyStored()) {
this.lastCompare = this.getComparatorStrength(); this.maxBurnTime = curBurn;
this.markDirty(); this.currentBurnTime = curBurn;
} inv.setStackInSlot(0, StackUtil.shrinkForContainer(stack, 1));
}
if ((this.storage.getEnergyStored() != this.lastEnergy || this.currentBurnTime != this.lastCurrentBurnTime || this.lastBurnTime != this.maxBurnTime) && this.sendUpdateWithInterval()) { if (flag != this.currentBurnTime > 0 || this.lastCompare != this.getComparatorStrength()) {
this.lastEnergy = this.storage.getEnergyStored(); this.lastCompare = this.getComparatorStrength();
this.lastCurrentBurnTime = this.currentBurnTime; this.markDirty();
this.lastBurnTime = this.currentBurnTime; }
}
}
}
@Override if ((this.storage.getEnergyStored() != this.lastEnergy || this.currentBurnTime != this.lastCurrentBurnTime || this.lastBurnTime != this.maxBurnTime) && this.sendUpdateWithInterval()) {
public int getComparatorStrength() { this.lastEnergy = this.storage.getEnergyStored();
float calc = ((float) this.storage.getEnergyStored() / (float) this.storage.getMaxEnergyStored()) * 15F; this.lastCurrentBurnTime = this.currentBurnTime;
return (int) calc; this.lastBurnTime = this.currentBurnTime;
} }
}
}
@Override @Override
public IAcceptor getAcceptor() { public int getComparatorStrength() {
return (slot, stack, automation) -> TileEntityFurnace.getItemBurnTime(stack) > 0; float calc = ((float) this.storage.getEnergyStored() / (float) this.storage.getMaxEnergyStored()) * 15F;
} return (int) calc;
}
@Override @Override
public IRemover getRemover() { public IAcceptor getAcceptor() {
return (slot, automation) -> { return (slot, stack, automation) -> TileEntityFurnace.getItemBurnTime(stack) > 0;
if (!automation) return true; }
return TileEntityFurnace.getItemBurnTime(this.inv.getStackInSlot(0)) <= 0;
};
}
@Override @Override
public int getEnergyToSplitShare() { public IRemover getRemover() {
return this.storage.getEnergyStored(); return (slot, automation) -> {
} if (!automation) return true;
return TileEntityFurnace.getItemBurnTime(this.inv.getStackInSlot(0)) <= 0;
};
}
@Override @Override
public boolean doesShareEnergy() { public int getEnergyToSplitShare() {
return true; return this.storage.getEnergyStored();
} }
@Override @Override
public EnumFacing[] getEnergyShareSides() { public boolean doesShareEnergy() {
return EnumFacing.values(); return true;
} }
@Override @Override
public boolean canShareTo(TileEntity tile) { public EnumFacing[] getEnergyShareSides() {
return true; return EnumFacing.values();
} }
@Override @Override
public IEnergyStorage getEnergyStorage(EnumFacing facing) { public boolean canShareTo(TileEntity tile) {
return this.storage; return true;
} }
@Override
public IEnergyStorage getEnergyStorage(EnumFacing facing) {
return this.storage;
}
} }

View file

@ -0,0 +1,28 @@
package de.ellpeck.actuallyadditions.mod.util;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.server.management.PlayerChunkMapEntry;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
public final class VanillaPacketDispatcher {
//Don't call from the client.
public static void dispatchTEToNearbyPlayers(TileEntity tile) {
WorldServer world = (WorldServer) tile.getWorld();
PlayerChunkMapEntry entry = world.getPlayerChunkMap().getEntry(tile.getPos().getX() >> 4, tile.getPos().getZ() >> 4);
if (entry == null) return;
for (EntityPlayerMP player : entry.getWatchingPlayers())
((EntityPlayerMP) player).connection.sendPacket(tile.getUpdatePacket());
}
public static void dispatchTEToNearbyPlayers(World world, BlockPos pos) {
TileEntity tile = world.getTileEntity(pos);
if (tile != null) dispatchTEToNearbyPlayers(tile);
}
}

View file

@ -121,11 +121,10 @@ public final class WorldUtil {
public static void doEnergyInteraction(TileEntity tileFrom, TileEntity tileTo, EnumFacing sideTo, int maxTransfer) { public static void doEnergyInteraction(TileEntity tileFrom, TileEntity tileTo, EnumFacing sideTo, int maxTransfer) {
if (maxTransfer > 0) { if (maxTransfer > 0) {
EnumFacing opp = sideTo == null ? null : sideTo.getOpposite(); EnumFacing opp = sideTo == null ? null : sideTo.getOpposite();
if (tileFrom.hasCapability(CapabilityEnergy.ENERGY, sideTo) && tileTo.hasCapability(CapabilityEnergy.ENERGY, opp)) {
IEnergyStorage handlerFrom = tileFrom.getCapability(CapabilityEnergy.ENERGY, sideTo); IEnergyStorage handlerFrom = tileFrom.getCapability(CapabilityEnergy.ENERGY, sideTo);
IEnergyStorage handlerTo = tileTo.getCapability(CapabilityEnergy.ENERGY, opp); IEnergyStorage handlerTo = tileTo.getCapability(CapabilityEnergy.ENERGY, opp);
if (handlerFrom != null && handlerTo != null) { if (handlerFrom != null && handlerTo != null) {
if(handlerFrom.getEnergyStored() == 0) return;
int drain = handlerFrom.extractEnergy(maxTransfer, true); int drain = handlerFrom.extractEnergy(maxTransfer, true);
if (drain > 0) { if (drain > 0) {
int filled = handlerTo.receiveEnergy(drain, false); int filled = handlerTo.receiveEnergy(drain, false);
@ -133,7 +132,6 @@ public final class WorldUtil {
return; return;
} }
} }
}
} }
} }