aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/api/multitileentity/base
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/gregtech/api/multitileentity/base')
-rw-r--r--src/main/java/gregtech/api/multitileentity/base/MultiTileEntity.java182
-rw-r--r--src/main/java/gregtech/api/multitileentity/base/TickableMultiTileEntity.java81
2 files changed, 94 insertions, 169 deletions
diff --git a/src/main/java/gregtech/api/multitileentity/base/MultiTileEntity.java b/src/main/java/gregtech/api/multitileentity/base/MultiTileEntity.java
index 4b2a5aca67..3d98663e6d 100644
--- a/src/main/java/gregtech/api/multitileentity/base/MultiTileEntity.java
+++ b/src/main/java/gregtech/api/multitileentity/base/MultiTileEntity.java
@@ -8,6 +8,8 @@ import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
+import javax.annotation.Nonnull;
+
import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.creativetab.CreativeTabs;
@@ -16,6 +18,7 @@ import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
+import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.Packet;
@@ -28,9 +31,6 @@ import net.minecraft.world.Explosion;
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.IFluidTank;
import com.gtnewhorizons.modularui.common.internal.network.NetworkUtils;
@@ -90,6 +90,7 @@ public abstract class MultiTileEntity extends CoverableTileEntity
protected boolean needsUpdate = false;
protected boolean hasInventoryChanged = false;
protected boolean isPainted = false;
+ @Nonnull
protected ForgeDirection facing = ForgeDirection.WEST; // Default to WEST, so it renders facing Left in the
// inventory
protected byte color;
@@ -203,12 +204,6 @@ public abstract class MultiTileEntity extends CoverableTileEntity
}
@Override
- public ITexture[] getTexture(Block ignoredBlock, ForgeDirection ignoredSide) {
- // We are not going to be using this
- return null;
- }
-
- @Override
public void readFromNBT(NBTTagCompound nbt) {
// Check if it is a World/Chunk-Loading Process calling readFromNBT
if (mteID == GT_Values.W || mteRegistry == GT_Values.W) {
@@ -231,7 +226,7 @@ public abstract class MultiTileEntity extends CoverableTileEntity
if (nbt.hasKey("y")) yCoord = nbt.getInteger("y");
if (nbt.hasKey("z")) zCoord = nbt.getInteger("z");
// read the custom Name.
- if (nbt.hasKey(NBT.DISPAY)) customName = nbt.getCompoundTag(NBT.DISPAY)
+ if (nbt.hasKey(NBT.DISPLAY)) customName = nbt.getCompoundTag(NBT.DISPLAY)
.getString(NBT.CUSTOM_NAME);
// And now everything else.
@@ -249,6 +244,7 @@ public abstract class MultiTileEntity extends CoverableTileEntity
if (nbt.hasKey(NBT.FACING)) facing = ForgeDirection.getOrientation(nbt.getInteger(NBT.FACING));
readCoverNBT(nbt);
+ readTasksNBT(nbt);
readMultiTileNBT(nbt);
if (NetworkUtils.isDedicatedClient()) {
@@ -272,6 +268,8 @@ public abstract class MultiTileEntity extends CoverableTileEntity
/* Do Nothing */
}
+ protected void readTasksNBT(NBTTagCompound nbt) {}
+
@Override
public final void writeToNBT(NBTTagCompound aNBT) {
super.writeToNBT(aNBT);
@@ -281,11 +279,11 @@ public abstract class MultiTileEntity extends CoverableTileEntity
// write the Custom Name
if (GT_Utility.isStringValid(customName)) {
final NBTTagCompound displayNBT;
- if (aNBT.hasKey(NBT.DISPAY)) {
- displayNBT = aNBT.getCompoundTag(NBT.DISPAY);
+ if (aNBT.hasKey(NBT.DISPLAY)) {
+ displayNBT = aNBT.getCompoundTag(NBT.DISPLAY);
} else {
displayNBT = new NBTTagCompound();
- aNBT.setTag(NBT.DISPAY, displayNBT);
+ aNBT.setTag(NBT.DISPLAY, displayNBT);
}
displayNBT.setString(NBT.CUSTOM_NAME, customName);
}
@@ -298,6 +296,7 @@ public abstract class MultiTileEntity extends CoverableTileEntity
aNBT.setInteger(NBT.FACING, facing.ordinal());
writeCoverNBT(aNBT, false);
+ writeTasksNBT(aNBT);
writeMultiTileNBT(aNBT);
} catch (Throwable e) {
GT_FML_LOGGER.error("writeToNBT", e);
@@ -308,10 +307,13 @@ public abstract class MultiTileEntity extends CoverableTileEntity
/* Do Nothing */
}
+ protected void writeTasksNBT(NBTTagCompound aNBT) {}
+
@Override
public NBTTagCompound writeItemNBT(NBTTagCompound aNBT) {
writeCoverNBT(aNBT, true);
if (shouldSaveNBTToItemStack()) {
+ writeTasksNBT(aNBT);
writeMultiTileNBT(aNBT);
}
return aNBT;
@@ -711,6 +713,8 @@ public abstract class MultiTileEntity extends CoverableTileEntity
public boolean onPlaced(ItemStack aStack, EntityPlayer aPlayer, World aWorld, int aX, int aY, int aZ,
ForgeDirection side, float aHitX, float aHitY, float aHitZ) {
facing = getSideForPlayerPlacing(aPlayer, facing, getValidFacings());
+ setOwnerUuid(aPlayer.getUniqueID());
+ setOwnerName(aPlayer.getDisplayName());
onFacingChange();
return true;
}
@@ -749,8 +753,7 @@ public abstract class MultiTileEntity extends CoverableTileEntity
}
if (!getCoverInfoAtSide(side).isGUIClickable()) return false;
- }
- if (isServerSide()) {
+ } else { // server side
if (!privateAccess() || aPlayer.getDisplayName()
.equalsIgnoreCase(getOwnerName())) {
final ItemStack tCurrentItem = aPlayer.inventory.getCurrentItem();
@@ -836,6 +839,11 @@ public abstract class MultiTileEntity extends CoverableTileEntity
if (!getCoverInfoAtSide(side).isGUIClickable()) return false;
+ if (aPlayer.getHeldItem() != null && aPlayer.getHeldItem()
+ .getItem() instanceof ItemBlock) {
+ return false;
+ }
+
return openModularUi(aPlayer, side);
}
}
@@ -1060,7 +1068,7 @@ public abstract class MultiTileEntity extends CoverableTileEntity
}
@Override
- public boolean receiveClientEvent(int aEventID, int aValue) {
+ public boolean receiveClientData(int aEventID, int aValue) {
super.receiveClientEvent(aEventID, aValue);
if (isClientSide()) {
issueTextureUpdate();
@@ -1127,11 +1135,6 @@ public abstract class MultiTileEntity extends CoverableTileEntity
}
@Override
- public boolean hasCustomInventoryName() {
- return false;
- }
-
- @Override
public ArrayList<String> getDebugInfo(EntityPlayer aPlayer, int aLogLevel) {
final ArrayList<String> tList = new ArrayList<>();
if (aLogLevel > 2) {
@@ -1155,82 +1158,6 @@ public abstract class MultiTileEntity extends CoverableTileEntity
}
/**
- * Fluid - A Default implementation of the Fluid Tank behaviour, so that every TileEntity can use this to simplify
- * its Code.
- */
- protected IFluidTank getFluidTankFillable(ForgeDirection side, FluidStack aFluidToFill) {
- return null;
- }
-
- protected IFluidTank getFluidTankDrainable(ForgeDirection side, FluidStack aFluidToDrain) {
- return null;
- }
-
- protected IFluidTank[] getFluidTanks(ForgeDirection side) {
- return GT_Values.emptyFluidTank;
- }
-
- public boolean isLiquidInput(ForgeDirection side) {
- return true;
- }
-
- public boolean isLiquidOutput(ForgeDirection side) {
- return true;
- }
-
- @Override
- public int fill(ForgeDirection aDirection, FluidStack aFluid, boolean aDoFill) {
- if (aFluid == null || aFluid.amount <= 0) return 0;
- final IFluidTank tTank = getFluidTankFillable(aDirection, aFluid);
- return (tTank == null) ? 0 : tTank.fill(aFluid, aDoFill);
- }
-
- @Override
- public FluidStack drain(ForgeDirection aDirection, FluidStack aFluid, boolean aDoDrain) {
- if (aFluid == null || aFluid.amount <= 0) return null;
- final IFluidTank tTank = getFluidTankDrainable(aDirection, aFluid);
- if (tTank == null || tTank.getFluid() == null
- || tTank.getFluidAmount() == 0
- || !tTank.getFluid()
- .isFluidEqual(aFluid))
- return null;
- return tTank.drain(aFluid.amount, aDoDrain);
- }
-
- @Override
- public FluidStack drain(ForgeDirection aDirection, int aAmountToDrain, boolean aDoDrain) {
- if (aAmountToDrain <= 0) return null;
- final IFluidTank tTank = getFluidTankDrainable(aDirection, null);
- if (tTank == null || tTank.getFluid() == null || tTank.getFluidAmount() == 0) return null;
- return tTank.drain(aAmountToDrain, aDoDrain);
- }
-
- @Override
- public boolean canFill(ForgeDirection aDirection, Fluid aFluid) {
- if (aFluid == null) return false;
- final IFluidTank tTank = getFluidTankFillable(aDirection, new FluidStack(aFluid, 0));
- return tTank != null && (tTank.getFluid() == null || tTank.getFluid()
- .getFluid() == aFluid);
- }
-
- @Override
- public boolean canDrain(ForgeDirection aDirection, Fluid aFluid) {
- if (aFluid == null) return false;
- final IFluidTank tTank = getFluidTankDrainable(aDirection, new FluidStack(aFluid, 0));
- return tTank != null && (tTank.getFluid() != null && tTank.getFluid()
- .getFluid() == aFluid);
- }
-
- @Override
- public FluidTankInfo[] getTankInfo(ForgeDirection aDirection) {
- final IFluidTank[] tTanks = getFluidTanks(aDirection);
- if (tTanks == null || tTanks.length <= 0) return GT_Values.emptyFluidTankInfo;
- final FluidTankInfo[] rInfo = new FluidTankInfo[tTanks.length];
- for (int i = 0; i < tTanks.length; i++) rInfo[i] = new FluidTankInfo(tTanks[i]);
- return rInfo;
- }
-
- /**
* Energy - Do nothing by Default
*/
@Override
@@ -1321,17 +1248,6 @@ public abstract class MultiTileEntity extends CoverableTileEntity
/**
* Inventory - Do nothing by default
*/
- @Override
- public void openInventory() {
- System.out.println("Open Inventory");
- /* Do nothing */
- }
-
- @Override
- public void closeInventory() {
- System.out.println("Close Inventory");
- /* Do nothing */
- }
@Override
public boolean hasInventoryBeenModified() {
@@ -1354,56 +1270,6 @@ public abstract class MultiTileEntity extends CoverableTileEntity
}
@Override
- public int[] getAccessibleSlotsFromSide(int ordinalSide) {
- return GT_Values.emptyIntArray;
- }
-
- @Override
- public boolean canInsertItem(int aSlot, ItemStack aStack, int ordinalSide) {
- return false;
- }
-
- @Override
- public boolean canExtractItem(int aSlot, ItemStack aStack, int ordinalSide) {
- return false;
- }
-
- @Override
- public int getSizeInventory() {
- return 0;
- }
-
- @Override
- public ItemStack getStackInSlot(int aSlot) {
- return null;
- }
-
- @Override
- public ItemStack decrStackSize(int aSlot, int aDecrement) {
- return null;
- }
-
- @Override
- public ItemStack getStackInSlotOnClosing(int aSlot) {
- return null;
- }
-
- @Override
- public void setInventorySlotContents(int aSlot, ItemStack aStack) {
- /* Do nothing */
- }
-
- @Override
- public int getInventoryStackLimit() {
- return 0;
- }
-
- @Override
- public boolean isItemValidForSlot(int aSlot, ItemStack aStack) {
- return false;
- }
-
- @Override
public void markInventoryBeenModified() {
hasInventoryChanged = true;
}
diff --git a/src/main/java/gregtech/api/multitileentity/base/TickableMultiTileEntity.java b/src/main/java/gregtech/api/multitileentity/base/TickableMultiTileEntity.java
index d61f32813f..9dcfce6d43 100644
--- a/src/main/java/gregtech/api/multitileentity/base/TickableMultiTileEntity.java
+++ b/src/main/java/gregtech/api/multitileentity/base/TickableMultiTileEntity.java
@@ -2,16 +2,26 @@ package gregtech.api.multitileentity.base;
import static gregtech.GT_Mod.GT_FML_LOGGER;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayerMP;
+import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
+import gregtech.api.enums.GT_Values;
import gregtech.api.multitileentity.interfaces.IMultiTileEntity.IMTE_OnNeighborBlockChange;
+import gregtech.api.task.TaskHost;
+import gregtech.api.task.TickableTask;
import gregtech.api.util.GT_Log;
import gregtech.api.util.GT_Util;
-public abstract class TickableMultiTileEntity extends MultiTileEntity implements IMTE_OnNeighborBlockChange {
+public abstract class TickableMultiTileEntity extends MultiTileEntity implements TaskHost, IMTE_OnNeighborBlockChange {
/** Variable for seeing if the Tick Function is called right now. */
public boolean isRunningTick = false;
@@ -22,11 +32,26 @@ public abstract class TickableMultiTileEntity extends MultiTileEntity implements
/** Variable for updating Data to the Client */
private boolean sendClientData = false;
+ private final Map<String, TickableTask<?>> tasks = new HashMap<>();
+
public TickableMultiTileEntity() {
super(true);
}
@Override
+ public final void registerTask(@Nonnull TickableTask<?> task) {
+ if (tasks.containsKey(task.getName())) {
+ throw new IllegalStateException(String.format("Task with name %s is already registered", task.getName()));
+ }
+ tasks.put(task.getName(), task);
+ }
+
+ @Nullable
+ public TickableTask<?> getTask(@Nonnull String name) {
+ return tasks.get(name);
+ }
+
+ @Override
public final void updateEntity() {
isRunningTick = true;
final boolean isServerSide = isServerSide();
@@ -46,6 +71,9 @@ public abstract class TickableMultiTileEntity extends MultiTileEntity implements
needsUpdate = false;
}
onTick(timer, isServerSide);
+ for (TickableTask<?> task : tasks.values()) {
+ task.update(timer, isServerSide);
+ }
if (isServerSide && timer > 2 && sendClientData) {
sendClientData(null);
}
@@ -73,7 +101,9 @@ public abstract class TickableMultiTileEntity extends MultiTileEntity implements
}
}
- /** The very first Tick happening to this TileEntity */
+ /**
+ * The very first Tick happening to this TileEntity.
+ */
public void onFirstTick(boolean isServerSide) {
if (isServerSide) {
checkDropCover();
@@ -82,19 +112,48 @@ public abstract class TickableMultiTileEntity extends MultiTileEntity implements
}
}
- /** The first part of the Tick. */
- public void onPreTick(long aTick, boolean isServerSide) {}
+ /**
+ * The first part of the Tick, before block update.
+ */
+ public void onPreTick(long tick, boolean isServerSide) {}
- /** The regular Tick. */
- public void onTick(long tick, boolean isServerSide) {
+ /**
+ * The regular Tick. After block update, before sending data to client.
+ */
+ public void onTick(long tick, boolean isServerSide) {}
- }
+ /**
+ * The absolute last part of the Tick, after sending data to client.
+ */
+ public void onPostTick(long tick, boolean isServerSide) {}
- /** The absolute last part of the Tick. */
- public void onPostTick(long aTick, boolean isServerSide) {}
+ /**
+ * Gets called when there is an Exception/Error happening during one of the Tick methods.
+ */
+ public void onTickFailed(long tick, boolean isServerSide) {}
- /** Gets called when there is an Exception happening during one of the Tick Functions. */
- public void onTickFailed(long aTimer, boolean isServerSide) {}
+ @Override
+ protected final void readTasksNBT(NBTTagCompound nbt) {
+ if (nbt.hasKey(GT_Values.NBT.TASKS)) {
+ NBTTagCompound tasksTag = nbt.getCompoundTag(GT_Values.NBT.TASKS);
+ for (TickableTask<?> task : tasks.values()) {
+ if (tasksTag.hasKey(task.getName())) {
+ task.readFromNBT(tasksTag.getCompoundTag(task.getName()));
+ }
+ }
+ }
+ }
+
+ @Override
+ protected final void writeTasksNBT(NBTTagCompound aNBT) {
+ NBTTagCompound tasksTag = new NBTTagCompound();
+ for (TickableTask<?> task : tasks.values()) {
+ NBTTagCompound tag = new NBTTagCompound();
+ task.writeToNBT(tag);
+ tasksTag.setTag(task.getName(), tag);
+ }
+ aNBT.setTag(GT_Values.NBT.TASKS, tasksTag);
+ }
@Override
public void onNeighborBlockChange(World aWorld, Block aBlock) {