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: 'idea'
version = "1.12.2-r141"
version = "1.12.2-r142_dev"
group = "de.ellpeck.actuallyadditions"
archivesBaseName = "ActuallyAdditions"

View file

@ -39,6 +39,10 @@ public class CustomEnergyStorage extends EnergyStorage{
return toReturn;
}
public void addEnergyRaw(int energy){
this.energy = Math.min(this.energy + energy, this.capacity);
}
@Override
public int receiveEnergy(int maxReceive, boolean simulate){
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.config.values.ConfigIntValues;
import de.ellpeck.actuallyadditions.mod.network.PacketHandler;
import de.ellpeck.actuallyadditions.mod.network.PacketServerToClient;
import de.ellpeck.actuallyadditions.mod.util.VanillaPacketDispatcher;
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
@ -33,7 +32,6 @@ import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.IEnergyStorage;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
@ -161,6 +159,8 @@ public abstract class TileEntityBase extends TileEntity implements ITickable{
}
public final void sendUpdate(){
if(world != null && !world.isRemote) VanillaPacketDispatcher.dispatchTEToNearbyPlayers(this);
/*
if(this.world != null && !this.world.isRemote){
NBTTagCompound compound = new NBTTagCompound();
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("Y", this.pos.getY());
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){
@ -227,11 +227,14 @@ public abstract class TileEntityBase extends TileEntity implements ITickable{
return 0;
}
private boolean shareEnergy = this instanceof ISharingEnergyProvider;
private boolean shareFluid = this instanceof ISharingFluidHandler;
public void updateEntity(){
this.ticksElapsed++;
if(!this.world.isRemote){
if(this instanceof ISharingEnergyProvider){
if(shareEnergy){
ISharingEnergyProvider provider = (ISharingEnergyProvider)this;
if(provider.doesShareEnergy()){
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;
if(handler.doesShareFluid()){
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.IRemover;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
@ -31,6 +32,8 @@ public class TileEntityCoalGenerator extends TileEntityInventoryBase implements
private int lastBurnTime;
private int lastCurrentBurnTime;
private int lastCompare;
private ItemStack curStack = ItemStack.EMPTY;
private int curBurn = -1;
public TileEntityCoalGenerator() {
super(1, "coalGenerator");
@ -74,17 +77,22 @@ public class TileEntityCoalGenerator extends TileEntityInventoryBase implements
if (this.currentBurnTime > 0) {
this.currentBurnTime--;
this.storage.receiveEnergyInternal(PRODUCE, false);
this.storage.addEnergyRaw(PRODUCE);
}
ItemStack stack = inv.getStackInSlot(0);
int burn = TileEntityFurnace.getItemBurnTime(stack);
if (!this.isRedstonePowered && this.currentBurnTime <= 0 && burn > 0 && this.storage.getEnergyStored() < this.storage.getMaxEnergyStored()) {
this.maxBurnTime = burn;
this.currentBurnTime = burn;
ItemStack copy = stack.copy();
stack.shrink(1);
if (stack.isEmpty()) inv.setStackInSlot(0, copy.getItem().getContainerItem(copy));
if (!stack.isEmpty() && stack != curStack) {
curStack = stack;
curBurn = TileEntityFurnace.getItemBurnTime(stack);
} else if (stack.isEmpty()) {
curStack = ItemStack.EMPTY;
curBurn = -1;
}
if (!this.isRedstonePowered && this.currentBurnTime <= 0 && curBurn > 0 && this.storage.getEnergyStored() < this.storage.getMaxEnergyStored()) {
this.maxBurnTime = curBurn;
this.currentBurnTime = curBurn;
inv.setStackInSlot(0, StackUtil.shrinkForContainer(stack, 1));
}
if (flag != this.currentBurnTime > 0 || this.lastCompare != this.getComparatorStrength()) {

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) {
if (maxTransfer > 0) {
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 handlerTo = tileTo.getCapability(CapabilityEnergy.ENERGY, opp);
if (handlerFrom != null && handlerTo != null) {
if(handlerFrom.getEnergyStored() == 0) return;
int drain = handlerFrom.extractEnergy(maxTransfer, true);
if (drain > 0) {
int filled = handlerTo.receiveEnergy(drain, false);
@ -135,7 +134,6 @@ public final class WorldUtil {
}
}
}
}
public static void doFluidInteraction(TileEntity tileFrom, TileEntity tileTo, EnumFacing sideTo, int maxTransfer) {
if (maxTransfer > 0) {