diff options
Diffstat (limited to 'src/main/java/gregtech/api/metatileentity')
46 files changed, 22971 insertions, 0 deletions
diff --git a/src/main/java/gregtech/api/metatileentity/BaseMetaPipeEntity.java b/src/main/java/gregtech/api/metatileentity/BaseMetaPipeEntity.java new file mode 100644 index 0000000000..1dc0e34d53 --- /dev/null +++ b/src/main/java/gregtech/api/metatileentity/BaseMetaPipeEntity.java @@ -0,0 +1,1416 @@ +package gregtech.api.metatileentity; + +import static gregtech.GT_Mod.GT_FML_LOGGER; +import static gregtech.api.enums.GT_Values.NW; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.UUID; + +import net.minecraft.block.Block; +import net.minecraft.entity.Entity; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.init.Items; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.AxisAlignedBB; +import net.minecraft.util.EnumChatFormatting; +import net.minecraft.world.World; +import net.minecraftforge.common.util.ForgeDirection; +import net.minecraftforge.fluids.Fluid; +import net.minecraftforge.fluids.FluidStack; +import net.minecraftforge.fluids.FluidTankInfo; +import net.minecraftforge.fluids.IFluidHandler; + +import gregtech.api.GregTech_API; +import gregtech.api.enums.GT_Values; +import gregtech.api.enums.SoundResource; +import gregtech.api.enums.Textures; +import gregtech.api.graphs.Lock; +import gregtech.api.graphs.Node; +import gregtech.api.graphs.paths.NodePath; +import gregtech.api.interfaces.ITexture; +import gregtech.api.interfaces.metatileentity.IConnectable; +import gregtech.api.interfaces.metatileentity.IMetaTileEntity; +import gregtech.api.interfaces.tileentity.IDebugableTileEntity; +import gregtech.api.interfaces.tileentity.IGregTechTileEntity; +import gregtech.api.interfaces.tileentity.IPipeRenderedTileEntity; +import gregtech.api.net.GT_Packet_TileEntity; +import gregtech.api.objects.GT_ItemStack; +import gregtech.api.util.GT_CoverBehaviorBase; +import gregtech.api.util.GT_Log; +import gregtech.api.util.GT_ModHandler; +import gregtech.api.util.GT_OreDictUnificator; +import gregtech.api.util.GT_Utility; +import gregtech.common.covers.CoverInfo; +import mcp.mobius.waila.api.IWailaConfigHandler; +import mcp.mobius.waila.api.IWailaDataAccessor; + +/** + * NEVER INCLUDE THIS FILE IN YOUR MOD!!! + * <p/> + * This is the main TileEntity for EVERYTHING. + */ +public class BaseMetaPipeEntity extends CommonMetaTileEntity + implements IGregTechTileEntity, IPipeRenderedTileEntity, IDebugableTileEntity { + + public byte mConnections = IConnectable.NO_CONNECTION; + protected MetaPipeEntity mMetaTileEntity; + private final int[] mTimeStatistics = new int[GregTech_API.TICKS_FOR_LAG_AVERAGING]; + private boolean hasTimeStatisticsStarted; + private boolean mWorkUpdate = false, mWorks = true; + private byte mColor = 0, oColor = 0, oStrongRedstone = 0, oRedstoneData = 63, oTextureData = 0, oUpdateData = 0, + mLagWarningCount = 0; + private int oX = 0, oY = 0, oZ = 0, mTimeStatisticsIndex = 0; + protected Node node; + protected NodePath nodePath; + + public Node getNode() { + return node; + } + + public void setNode(Node node) { + this.node = node; + } + + public NodePath getNodePath() { + return nodePath; + } + + public void setNodePath(NodePath nodePath) { + this.nodePath = nodePath; + } + + public void addToLock(TileEntity tileEntity, ForgeDirection side) { + if (node != null) { + final Lock lock = node.locks[side.ordinal()]; + if (lock != null) { + lock.addTileEntity(tileEntity); + } + } else if (nodePath != null) { + nodePath.lock.addTileEntity(tileEntity); + } + } + + public void removeFromLock(TileEntity tileEntity, ForgeDirection side) { + if (node != null) { + final Lock lock = node.locks[side.ordinal()]; + if (lock != null) { + lock.removeTileEntity(tileEntity); + } + } else if (nodePath != null) { + nodePath.lock.removeTileEntity(tileEntity); + } + } + + public void reloadLocks() { + final IMetaTileEntity meta = getMetaTileEntity(); + if (meta instanceof MetaPipeEntity) { + ((MetaPipeEntity) meta).reloadLocks(); + } + } + + public BaseMetaPipeEntity() {} + + @Override + public void writeToNBT(NBTTagCompound aNBT) { + try { + super.writeToNBT(aNBT); + } catch (Throwable e) { + GT_FML_LOGGER.error("Encountered CRITICAL ERROR while saving MetaTileEntity", e); + } + try { + aNBT.setInteger("mID", mID); + writeCoverNBT(aNBT, false); + aNBT.setByte("mConnections", mConnections); + aNBT.setByte("mColor", mColor); + aNBT.setBoolean("mWorks", !mWorks); + } catch (Throwable e) { + GT_FML_LOGGER.error("Encountered CRITICAL ERROR while saving MetaTileEntity", e); + } + saveMetaTileNBT(aNBT); + } + + @Override + public void readFromNBT(NBTTagCompound aNBT) { + super.readFromNBT(aNBT); + setInitialValuesAsNBT(aNBT, (short) 0); + } + + @Override + public void setInitialValuesAsNBT(NBTTagCompound aNBT, short aID) { + if (aNBT == null) { + if (aID > 0) mID = aID; + else mID = mID > 0 ? mID : 0; + if (mID != 0) createNewMetatileEntity(mID); + } else { + if (aID <= 0) mID = (short) aNBT.getInteger("mID"); + else mID = aID; + mConnections = aNBT.getByte("mConnections"); + mColor = aNBT.getByte("mColor"); + mWorks = !aNBT.getBoolean("mWorks"); + + if (mSidedRedstone.length != 6) mSidedRedstone = new byte[] { 0, 0, 0, 0, 0, 0 }; + + readCoverNBT(aNBT); + loadMetaTileNBT(aNBT); + } + } + + @Override + public void updateEntity() { + super.updateEntity(); + + if (!hasValidMetaTileEntity()) { + if (mMetaTileEntity == null) return; + mMetaTileEntity.setBaseMetaTileEntity(this); + } + + long tTime; + if (hasTimeStatisticsStarted) { + tTime = System.nanoTime(); + } else { + tTime = 0; + } + try { + if (hasValidMetaTileEntity()) { + if (mTickTimer++ == 0) { + oX = xCoord; + oY = yCoord; + oZ = zCoord; + if (isServerSide()) checkDropCover(); + else { + requestCoverDataIfNeeded(); + } + worldObj.markTileEntityChunkModified(xCoord, yCoord, zCoord, this); + mMetaTileEntity.onFirstTick(this); + if (!hasValidMetaTileEntity()) return; + } + + if (isClientSide()) { + if (mColor != oColor) { + mMetaTileEntity.onColorChangeClient(oColor = mColor); + issueTextureUpdate(); + } + + if (mNeedsUpdate) { + worldObj.markBlockForUpdate(xCoord, yCoord, zCoord); + mNeedsUpdate = false; + } + } + if (isServerSide() && mTickTimer > 10) { + if (!doCoverThings()) return; + + final byte oldConnections = mConnections; + // Mask-out connection direction bits to keep only Foam related connections + mConnections = (byte) (mMetaTileEntity.mConnections | (mConnections & ~IConnectable.CONNECTED_ALL)); + // If foam not hardened, tries roll chance to harden + if ((mConnections & IConnectable.HAS_FOAM) == IConnectable.HAS_FRESHFOAM + && getRandomNumber(1000) == 0) { + mConnections = (byte) ((mConnections & ~IConnectable.HAS_FRESHFOAM) + | IConnectable.HAS_HARDENEDFOAM); + } + if (mTickTimer > 12 && oldConnections != mConnections) + GregTech_API.causeCableUpdate(worldObj, xCoord, yCoord, zCoord); + } + mMetaTileEntity.onPreTick(this, mTickTimer); + if (!hasValidMetaTileEntity()) return; + if (isServerSide()) { + if (mTickTimer == 10) { + updateCoverBehavior(); + issueBlockUpdate(); + joinEnet(); + } + + if (xCoord != oX || yCoord != oY || zCoord != oZ) { + oX = xCoord; + oY = yCoord; + oZ = zCoord; + issueClientUpdate(); + clearTileEntityBuffer(); + } + } + + mMetaTileEntity.onPostTick(this, mTickTimer); + if (!hasValidMetaTileEntity()) return; + + if (isServerSide()) { + if (mTickTimer % 10 == 0) { + sendClientData(); + } + + if (mTickTimer > 10) { + if (mConnections != oTextureData) sendBlockEvent((byte) 0, oTextureData = mConnections); + byte tData = mMetaTileEntity.getUpdateData(); + if (tData != oUpdateData) sendBlockEvent((byte) 1, oUpdateData = tData); + if (mColor != oColor) sendBlockEvent((byte) 2, oColor = mColor); + tData = (byte) (((mSidedRedstone[0] > 0) ? 1 : 0) | ((mSidedRedstone[1] > 0) ? 2 : 0) + | ((mSidedRedstone[2] > 0) ? 4 : 0) + | ((mSidedRedstone[3] > 0) ? 8 : 0) + | ((mSidedRedstone[4] > 0) ? 16 : 0) + | ((mSidedRedstone[5] > 0) ? 32 : 0)); + if (tData != oRedstoneData) sendBlockEvent((byte) 3, oRedstoneData = tData); + } + + if (mNeedsBlockUpdate) { + updateNeighbours(mStrongRedstone, oStrongRedstone); + oStrongRedstone = mStrongRedstone; + mNeedsBlockUpdate = false; + } + } + } + } catch (Throwable e) { + e.printStackTrace(); + e.printStackTrace(GT_Log.err); + } + + if (isServerSide() && hasTimeStatisticsStarted && hasValidMetaTileEntity()) { + tTime = System.nanoTime() - tTime; + mTimeStatisticsIndex = (mTimeStatisticsIndex + 1) % mTimeStatistics.length; + mTimeStatistics[mTimeStatisticsIndex] = (int) tTime; + if (tTime > 0 && tTime > (GregTech_API.MILLISECOND_THRESHOLD_UNTIL_LAG_WARNING * 1000000L) + && mTickTimer > 1000 + && getMetaTileEntity().doTickProfilingMessageDuringThisTick() + && mLagWarningCount++ < 10) + GT_FML_LOGGER.warn( + "WARNING: Possible Lag Source at [" + xCoord + + "," + + yCoord + + "," + + zCoord + + "] in Dimension " + + worldObj.provider.dimensionId + + " with " + + tTime + + " ns caused by an instance of " + + getMetaTileEntity().getClass()); + } + + mWorkUpdate = mInventoryChanged = false; + } + + private void sendClientData() { + if (mSendClientData) { + NW.sendPacketToAllPlayersInRange( + worldObj, + new GT_Packet_TileEntity( + xCoord, + (short) yCoord, + zCoord, + mID, + getCoverInfoAtSide(ForgeDirection.DOWN).getCoverID(), + getCoverInfoAtSide(ForgeDirection.UP).getCoverID(), + getCoverInfoAtSide(ForgeDirection.NORTH).getCoverID(), + getCoverInfoAtSide(ForgeDirection.SOUTH).getCoverID(), + getCoverInfoAtSide(ForgeDirection.WEST).getCoverID(), + getCoverInfoAtSide(ForgeDirection.EAST).getCoverID(), + oTextureData = mConnections, + oUpdateData = hasValidMetaTileEntity() ? mMetaTileEntity.getUpdateData() : 0, + oRedstoneData = (byte) (((mSidedRedstone[0] > 0) ? 1 : 0) | ((mSidedRedstone[1] > 0) ? 2 : 0) + | ((mSidedRedstone[2] > 0) ? 4 : 0) + | ((mSidedRedstone[3] > 0) ? 8 : 0) + | ((mSidedRedstone[4] > 0) ? 16 : 0) + | ((mSidedRedstone[5] > 0) ? 32 : 0)), + oColor = mColor), + xCoord, + zCoord); + mSendClientData = false; + } + sendCoverDataIfNeeded(); + } + + public final void receiveMetaTileEntityData(short aID, int aCover0, int aCover1, int aCover2, int aCover3, + int aCover4, int aCover5, byte aTextureData, byte aUpdateData, byte aRedstoneData, byte aColorData) { + issueTextureUpdate(); + if (aID > 0 && mID != aID) { + mID = aID; + createNewMetatileEntity(mID); + } + + setCoverIDAtSide(ForgeDirection.DOWN, aCover0); + setCoverIDAtSide(ForgeDirection.UP, aCover1); + setCoverIDAtSide(ForgeDirection.NORTH, aCover2); + setCoverIDAtSide(ForgeDirection.SOUTH, aCover3); + setCoverIDAtSide(ForgeDirection.WEST, aCover4); + setCoverIDAtSide(ForgeDirection.EAST, aCover5); + + receiveClientEvent(GregTechTileClientEvents.CHANGE_COMMON_DATA, aTextureData); + receiveClientEvent(GregTechTileClientEvents.CHANGE_CUSTOM_DATA, aUpdateData); + receiveClientEvent(GregTechTileClientEvents.CHANGE_COLOR, aColorData); + receiveClientEvent(GregTechTileClientEvents.CHANGE_REDSTONE_OUTPUT, aRedstoneData); + } + + @Override + public boolean receiveClientEvent(int aEventID, int aValue) { + super.receiveClientEvent(aEventID, aValue); + + if (hasValidMetaTileEntity()) { + try { + mMetaTileEntity.receiveClientEvent((byte) aEventID, (byte) aValue); + } catch (Throwable e) { + GT_FML_LOGGER.error("Encountered Exception while receiving Data from the Server", e); + } + } + + if (isClientSide()) { + issueTextureUpdate(); + switch (aEventID) { + case GregTechTileClientEvents.CHANGE_COMMON_DATA -> mConnections = (byte) aValue; + case GregTechTileClientEvents.CHANGE_CUSTOM_DATA -> { + if (hasValidMetaTileEntity()) mMetaTileEntity.onValueUpdate((byte) aValue); + } + case GregTechTileClientEvents.CHANGE_COLOR -> { + if (aValue > 16 || aValue < 0) aValue = 0; + mColor = (byte) aValue; + } + case GregTechTileClientEvents.CHANGE_REDSTONE_OUTPUT -> { + mSidedRedstone[0] = (byte) ((aValue & 1) == 1 ? 15 : 0); + mSidedRedstone[1] = (byte) ((aValue & 2) == 2 ? 15 : 0); + mSidedRedstone[2] = (byte) ((aValue & 4) == 4 ? 15 : 0); + mSidedRedstone[3] = (byte) ((aValue & 8) == 8 ? 15 : 0); + mSidedRedstone[4] = (byte) ((aValue & 16) == 16 ? 15 : 0); + mSidedRedstone[5] = (byte) ((aValue & 32) == 32 ? 15 : 0); + } + case GregTechTileClientEvents.DO_SOUND -> { + if (hasValidMetaTileEntity() && mTickTimer > 20) + mMetaTileEntity.doSound((byte) aValue, xCoord + 0.5, yCoord + 0.5, zCoord + 0.5); + } + case GregTechTileClientEvents.START_SOUND_LOOP -> { + if (hasValidMetaTileEntity() && mTickTimer > 20) + mMetaTileEntity.startSoundLoop((byte) aValue, xCoord + 0.5, yCoord + 0.5, zCoord + 0.5); + } + case GregTechTileClientEvents.STOP_SOUND_LOOP -> { + if (hasValidMetaTileEntity() && mTickTimer > 20) + mMetaTileEntity.stopSoundLoop((byte) aValue, xCoord + 0.5, yCoord + 0.5, zCoord + 0.5); + } + } + } + return true; + } + + @Override + public ArrayList<String> getDebugInfo(EntityPlayer aPlayer, int aLogLevel) { + final ArrayList<String> tList = new ArrayList<>(); + if (aLogLevel > 3) { + tList.add( + "Meta-ID: " + EnumChatFormatting.BLUE + + mID + + EnumChatFormatting.RESET + + (hasValidMetaTileEntity() ? EnumChatFormatting.GREEN + " valid" + EnumChatFormatting.RESET + : EnumChatFormatting.RED + " invalid" + EnumChatFormatting.RESET) + + (mMetaTileEntity == null + ? EnumChatFormatting.RED + " MetaTileEntity == null!" + EnumChatFormatting.RESET + : " ")); + } + if (aLogLevel > 1) { + if (hasTimeStatisticsStarted) { + double tAverageTime = 0; + double tWorstTime = 0; + int amountOfZero = 0; + for (int tTime : mTimeStatistics) { + tAverageTime += tTime; + if (tTime > tWorstTime) { + tWorstTime = tTime; + } + if (tTime == 0) { + amountOfZero += 1; + } + } + // tick time zero means it has not been updated yet + int samples = mTimeStatistics.length - amountOfZero; + if (samples > 0) { + tList.add( + "Average CPU-load of ~" + (tAverageTime / samples) + + "ns since " + + samples + + " ticks with worst time of " + + tWorstTime + + "ns."); + } + } else { + startTimeStatistics(); + tList.add("Just started tick time statistics."); + } + if (mLagWarningCount > 0) { + tList.add( + "Caused " + (mLagWarningCount >= 10 ? "more than 10" : mLagWarningCount) + + " Lag Spike Warnings (anything taking longer than " + + GregTech_API.MILLISECOND_THRESHOLD_UNTIL_LAG_WARNING + + "ms) on the Server."); + } + if (mMetaTileEntity != null) { + tList.add( + "Is" + (mMetaTileEntity.isAccessAllowed(aPlayer) ? " " + : EnumChatFormatting.RED + " not " + EnumChatFormatting.RESET) + "accessible for you"); + } + } + if (joinedIc2Enet) tList.add("Joined IC2 ENet"); + + return mMetaTileEntity != null ? mMetaTileEntity.getSpecialDebugInfo(this, aPlayer, aLogLevel, tList) + : new ArrayList<>(); + } + + @Override + public boolean isGivingInformation() { + if (canAccessData()) return mMetaTileEntity.isGivingInformation(); + return false; + } + + @Override + public ForgeDirection getBackFacing() { + return getFrontFacing().getOpposite(); + } + + @Override + public ForgeDirection getFrontFacing() { + return ForgeDirection.UNKNOWN; + } + + @Override + public void setFrontFacing(ForgeDirection aFacing) { + doEnetUpdate(); + } + + @Override + public int getSizeInventory() { + if (canAccessData()) return mMetaTileEntity.getSizeInventory(); + return 0; + } + + @Override + public Item |
