Made every block be able to save the tile entities around it for future use

This commit is contained in:
Ellpeck 2016-09-09 18:40:09 +02:00
parent 2fea0d3c6d
commit 37d1bc95f9
12 changed files with 102 additions and 96 deletions

View file

@ -90,16 +90,6 @@ public class BlockInputter extends BlockContainerBase{
return EnumRarity.EPIC; return EnumRarity.EPIC;
} }
@Override
public void neighborsChangedCustom(World world, BlockPos pos){
super.neighborsChangedCustom(world, pos);
TileEntity tile = world.getTileEntity(pos);
if(tile instanceof TileEntityInputter){
((TileEntityInputter)tile).initVars();
}
}
public static class TheItemBlock extends ItemBlockBase{ public static class TheItemBlock extends ItemBlockBase{
private long lastSysTime; private long lastSysTime;

View file

@ -30,17 +30,6 @@ public class BlockItemViewer extends BlockContainerBase{
this.setSoundType(SoundType.STONE); this.setSoundType(SoundType.STONE);
} }
@Override
public void neighborsChangedCustom(World world, BlockPos pos){
super.neighborsChangedCustom(world, pos);
TileEntity tile = world.getTileEntity(pos);
if(tile instanceof TileEntityItemViewer){
((TileEntityItemViewer)tile).saveConnectedRelay();
}
}
@Override @Override
public TileEntity createNewTileEntity(World worldIn, int meta){ public TileEntity createNewTileEntity(World worldIn, int meta){
return new TileEntityItemViewer(); return new TileEntityItemViewer();

View file

@ -112,16 +112,6 @@ public class BlockLaserRelay extends BlockContainerBase{
return false; return false;
} }
@Override
public void neighborsChangedCustom(World world, BlockPos pos){
super.neighborsChangedCustom(world, pos);
TileEntity tile = world.getTileEntity(pos);
if(tile instanceof TileEntityLaserRelay){
((TileEntityLaserRelay)tile).saveAllHandlersAround();
}
}
@Override @Override
public TileEntity createNewTileEntity(World world, int i){ public TileEntity createNewTileEntity(World world, int i){
switch(this.type){ switch(this.type){

View file

@ -13,6 +13,7 @@ package de.ellpeck.actuallyadditions.mod.blocks.base;
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions; import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityBase; import de.ellpeck.actuallyadditions.mod.tile.TileEntityBase;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityInventoryBase; import de.ellpeck.actuallyadditions.mod.tile.TileEntityInventoryBase;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityLaserRelay;
import de.ellpeck.actuallyadditions.mod.util.ItemUtil; import de.ellpeck.actuallyadditions.mod.util.ItemUtil;
import de.ellpeck.actuallyadditions.mod.util.Util; import de.ellpeck.actuallyadditions.mod.util.Util;
import net.minecraft.block.Block; import net.minecraft.block.Block;
@ -156,6 +157,14 @@ public abstract class BlockContainerBase extends BlockContainer implements ItemB
public void neighborsChangedCustom(World world, BlockPos pos){ public void neighborsChangedCustom(World world, BlockPos pos){
this.updateRedstoneState(world, pos); this.updateRedstoneState(world, pos);
TileEntity tile = world.getTileEntity(pos);
if(tile instanceof TileEntityBase){
TileEntityBase base = (TileEntityBase)tile;
if(base.shouldSaveHandlersAround()){
base.saveAllHandlersAround();
}
}
} }
@Override @Override

View file

@ -42,6 +42,9 @@ public abstract class TileEntityBase extends TileEntity implements ITickable{
public boolean isPulseMode; public boolean isPulseMode;
protected int ticksElapsed; protected int ticksElapsed;
protected TileEntity[] tilesAround = new TileEntity[6];
protected boolean hasCheckedHandlersAround;
public TileEntityBase(String name){ public TileEntityBase(String name){
this.name = name; this.name = name;
} }
@ -236,8 +239,12 @@ public abstract class TileEntityBase extends TileEntity implements ITickable{
if(amount <= 0){ if(amount <= 0){
amount = total; amount = total;
} }
for(EnumFacing side : sides){ for(EnumFacing side : sides){
WorldUtil.doEnergyInteraction(this, side, amount); TileEntity tile = this.tilesAround[side.ordinal()];
if(tile != null){
WorldUtil.doEnergyInteraction(this, tile, side, amount);
}
} }
} }
} }
@ -254,15 +261,37 @@ public abstract class TileEntityBase extends TileEntity implements ITickable{
if(amount <= 0){ if(amount <= 0){
amount = total; amount = total;
} }
for(EnumFacing side : sides){ for(EnumFacing side : sides){
WorldUtil.doFluidInteraction(this, side, amount); TileEntity tile = this.tilesAround[side.ordinal()];
if(tile != null){
WorldUtil.doFluidInteraction(this, tile, side, amount);
}
} }
} }
} }
} }
if(!this.hasCheckedHandlersAround){
if(this.shouldSaveHandlersAround()){
this.saveAllHandlersAround();
}
this.hasCheckedHandlersAround = true;
}
} }
} }
public void saveAllHandlersAround(){
for(EnumFacing side : EnumFacing.values()){
this.tilesAround[side.ordinal()] = this.worldObj.getTileEntity(this.pos.offset(side));
}
}
public boolean shouldSaveHandlersAround(){
return this instanceof ISharingEnergyProvider || this instanceof ISharingFluidHandler;
}
public void setRedstonePowered(boolean powered){ public void setRedstonePowered(boolean powered){
this.isRedstonePowered = powered; this.isRedstonePowered = powered;
this.markDirty(); this.markDirty();

View file

@ -48,8 +48,6 @@ public class TileEntityInputter extends TileEntityInventoryBase implements IButt
public FilterSettings leftFilter = new FilterSettings(PULL_FILTER_START, PULL_FILTER_START+12, true, true, false, 0, -1000); public FilterSettings leftFilter = new FilterSettings(PULL_FILTER_START, PULL_FILTER_START+12, true, true, false, 0, -1000);
public FilterSettings rightFilter = new FilterSettings(PUT_FILTER_START, PUT_FILTER_START+12, true, true, false, 0, -2000); public FilterSettings rightFilter = new FilterSettings(PUT_FILTER_START, PUT_FILTER_START+12, true, true, false, 0, -2000);
private boolean hasCheckedTilesAround;
public TileEntityInputter(int slots, String name){ public TileEntityInputter(int slots, String name){
super(slots, name); super(slots, name);
} }
@ -313,10 +311,16 @@ public class TileEntityInputter extends TileEntityInventoryBase implements IButt
return !this.isAdvanced || (output ? this.rightFilter : this.leftFilter).check(stack, this.slots); return !this.isAdvanced || (output ? this.rightFilter : this.leftFilter).check(stack, this.slots);
} }
@Override
public boolean shouldSaveHandlersAround(){
return true;
}
/** /**
* Sets all of the relevant variables * Sets all of the relevant variables
*/ */
public void initVars(){ @Override
public void saveAllHandlersAround(){
if(this.sideToPull != -1){ if(this.sideToPull != -1){
EnumFacing side = WorldUtil.getDirectionBySidesInOrder(this.sideToPull); EnumFacing side = WorldUtil.getDirectionBySidesInOrder(this.sideToPull);
this.placeToPull = this.worldObj.getTileEntity(this.pos.offset(side)); this.placeToPull = this.worldObj.getTileEntity(this.pos.offset(side));
@ -399,7 +403,7 @@ public class TileEntityInputter extends TileEntityInventoryBase implements IButt
} }
this.markDirty(); this.markDirty();
this.initVars(); this.saveAllHandlersAround();
} }
@Override @Override
@ -438,10 +442,6 @@ public class TileEntityInputter extends TileEntityInventoryBase implements IButt
public void updateEntity(){ public void updateEntity(){
super.updateEntity(); super.updateEntity();
if(!this.worldObj.isRemote){ if(!this.worldObj.isRemote){
if(!this.hasCheckedTilesAround){
this.initVars();
this.hasCheckedTilesAround = true;
}
//Is Block not powered by Redstone? //Is Block not powered by Redstone?
if(!this.isRedstonePowered){ if(!this.isRedstonePowered){

View file

@ -26,7 +26,6 @@ import java.util.List;
public class TileEntityItemViewer extends TileEntityInventoryBase{ public class TileEntityItemViewer extends TileEntityInventoryBase{
public TileEntityLaserRelayItem connectedRelay; public TileEntityLaserRelayItem connectedRelay;
private boolean hasCheckedRelayOnLoad;
public TileEntityItemViewer(){ public TileEntityItemViewer(){
super(0, "itemViewer"); super(0, "itemViewer");
@ -35,11 +34,6 @@ public class TileEntityItemViewer extends TileEntityInventoryBase{
@Override @Override
public void updateEntity(){ public void updateEntity(){
super.updateEntity(); super.updateEntity();
if(!this.worldObj.isRemote && !this.hasCheckedRelayOnLoad){
this.saveConnectedRelay();
this.hasCheckedRelayOnLoad = true;
}
} }
private List<GenericItemHandlerInfo> getItemHandlerInfos(){ private List<GenericItemHandlerInfo> getItemHandlerInfos(){
@ -75,7 +69,13 @@ public class TileEntityItemViewer extends TileEntityInventoryBase{
return null; return null;
} }
public void saveConnectedRelay(){ @Override
public boolean shouldSaveHandlersAround(){
return true;
}
@Override
public void saveAllHandlersAround(){
TileEntityLaserRelayItem tileFound = null; TileEntityLaserRelayItem tileFound = null;
if(this.worldObj != null){ //Why is that even possible..? if(this.worldObj != null){ //Why is that even possible..?
for(int i = 0; i <= 5; i++){ for(int i = 0; i <= 5; i++){

View file

@ -42,8 +42,6 @@ public abstract class TileEntityLaserRelay extends TileEntityBase{
private Set<ConnectionPair> tempConnectionStorage; private Set<ConnectionPair> tempConnectionStorage;
private boolean hasCheckedHandlersAround;
public TileEntityLaserRelay(String name, LaserType type){ public TileEntityLaserRelay(String name, LaserType type){
super(name); super(name);
this.type = type; this.type = type;
@ -90,14 +88,6 @@ public abstract class TileEntityLaserRelay extends TileEntityBase{
if(this.worldObj.isRemote){ if(this.worldObj.isRemote){
this.renderParticles(); this.renderParticles();
} }
else if(!this.hasCheckedHandlersAround){
this.saveAllHandlersAround();
this.hasCheckedHandlersAround = true;
}
}
public void saveAllHandlersAround(){
} }
@SideOnly(Side.CLIENT) @SideOnly(Side.CLIENT)

View file

@ -74,6 +74,11 @@ public class TileEntityLaserRelayEnergy extends TileEntityLaserRelay implements
return true; return true;
} }
@Override
public boolean shouldSaveHandlersAround(){
return true;
}
@Override @Override
public void saveAllHandlersAround(){ public void saveAllHandlersAround(){
this.receiversAround.clear(); this.receiversAround.clear();

View file

@ -39,6 +39,11 @@ public class TileEntityLaserRelayFluids extends TileEntityLaserRelay implements
super("laserRelayFluids", LaserType.FLUID); super("laserRelayFluids", LaserType.FLUID);
} }
@Override
public boolean shouldSaveHandlersAround(){
return true;
}
@Override @Override
public void saveAllHandlersAround(){ public void saveAllHandlersAround(){
this.receiversAround.clear(); this.receiversAround.clear();

View file

@ -43,6 +43,11 @@ public class TileEntityLaserRelayItem extends TileEntityLaserRelay{
return true; return true;
} }
@Override
public boolean shouldSaveHandlersAround(){
return true;
}
@Override @Override
public void saveAllHandlersAround(){ public void saveAllHandlersAround(){
this.handlersAround.clear(); this.handlersAround.clear();

View file

@ -88,55 +88,49 @@ public final class WorldUtil{
return null; return null;
} }
public static void doEnergyInteraction(TileEntity tileFrom, EnumFacing sideTo, int maxTransfer){ public static void doEnergyInteraction(TileEntity tileFrom, TileEntity tileTo, EnumFacing sideTo, int maxTransfer){
if(maxTransfer > 0){ if(maxTransfer > 0){
TileEntity tileTo = tileFrom.getWorld().getTileEntity(tileFrom.getPos().offset(sideTo)); if(tileFrom instanceof IEnergyProvider && tileTo instanceof IEnergyReceiver){
if(tileTo != null){ IEnergyReceiver handlerTo = (IEnergyReceiver)tileTo;
if(tileFrom instanceof IEnergyProvider && tileTo instanceof IEnergyReceiver){ IEnergyProvider handlerFrom = (IEnergyProvider)tileFrom;
IEnergyReceiver handlerTo = (IEnergyReceiver)tileTo;
IEnergyProvider handlerFrom = (IEnergyProvider)tileFrom;
int drain = handlerFrom.extractEnergy(sideTo, maxTransfer, true); int drain = handlerFrom.extractEnergy(sideTo, maxTransfer, true);
if(drain > 0){ if(drain > 0){
if(handlerTo.canConnectEnergy(sideTo.getOpposite())){ if(handlerTo.canConnectEnergy(sideTo.getOpposite())){
int filled = handlerTo.receiveEnergy(sideTo.getOpposite(), drain, false); int filled = handlerTo.receiveEnergy(sideTo.getOpposite(), drain, false);
handlerFrom.extractEnergy(sideTo, filled, false); handlerFrom.extractEnergy(sideTo, filled, false);
}
} }
} }
else if(ActuallyAdditions.teslaLoaded){ }
TeslaUtil.doWrappedTeslaRFInteraction(tileFrom, tileTo, sideTo, maxTransfer); else if(ActuallyAdditions.teslaLoaded){
} TeslaUtil.doWrappedTeslaRFInteraction(tileFrom, tileTo, sideTo, maxTransfer);
} }
} }
} }
public static void doFluidInteraction(TileEntity tileFrom, EnumFacing sideTo, int maxTransfer){ public static void doFluidInteraction(TileEntity tileFrom, TileEntity tileTo, EnumFacing sideTo, int maxTransfer){
if(maxTransfer > 0){ if(maxTransfer > 0){
TileEntity tileTo = tileFrom.getWorld().getTileEntity(tileFrom.getPos().offset(sideTo)); //Push and pull with old fluid system
if(tileTo != null){ if(tileFrom instanceof net.minecraftforge.fluids.IFluidHandler && tileTo instanceof net.minecraftforge.fluids.IFluidHandler){
//Push and pull with old fluid system net.minecraftforge.fluids.IFluidHandler handlerTo = (net.minecraftforge.fluids.IFluidHandler)tileTo;
if(tileFrom instanceof net.minecraftforge.fluids.IFluidHandler && tileTo instanceof net.minecraftforge.fluids.IFluidHandler){ net.minecraftforge.fluids.IFluidHandler handlerFrom = (net.minecraftforge.fluids.IFluidHandler)tileFrom;
net.minecraftforge.fluids.IFluidHandler handlerTo = (net.minecraftforge.fluids.IFluidHandler)tileTo; FluidStack drain = handlerFrom.drain(sideTo, maxTransfer, false);
net.minecraftforge.fluids.IFluidHandler handlerFrom = (net.minecraftforge.fluids.IFluidHandler)tileFrom; if(drain != null){
FluidStack drain = handlerFrom.drain(sideTo, maxTransfer, false); if(handlerTo.canFill(sideTo.getOpposite(), drain.getFluid())){
if(drain != null){ int filled = handlerTo.fill(sideTo.getOpposite(), drain.copy(), true);
if(handlerTo.canFill(sideTo.getOpposite(), drain.getFluid())){ handlerFrom.drain(sideTo, filled, true);
int filled = handlerTo.fill(sideTo.getOpposite(), drain.copy(), true);
handlerFrom.drain(sideTo, filled, true);
}
} }
} }
//Push and pull with new fluid system }
else{ //Push and pull with new fluid system
if(tileFrom.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, sideTo) && tileTo.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, sideTo.getOpposite())){ else{
IFluidHandler handlerFrom = tileFrom.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, sideTo); if(tileFrom.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, sideTo) && tileTo.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, sideTo.getOpposite())){
IFluidHandler handlerTo = tileTo.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, sideTo.getOpposite()); IFluidHandler handlerFrom = tileFrom.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, sideTo);
FluidStack drain = handlerFrom.drain(maxTransfer, false); IFluidHandler handlerTo = tileTo.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, sideTo.getOpposite());
if(drain != null){ FluidStack drain = handlerFrom.drain(maxTransfer, false);
int filled = handlerTo.fill(drain.copy(), true); if(drain != null){
handlerFrom.drain(filled, true); int filled = handlerTo.fill(drain.copy(), true);
} handlerFrom.drain(filled, true);
} }
} }
} }