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