diff options
Diffstat (limited to 'src/main/java/gregtech/api/multitileentity/machine')
| -rw-r--r-- | src/main/java/gregtech/api/multitileentity/machine/MultiTileBasicMachine.java | 821 |
1 files changed, 312 insertions, 509 deletions
diff --git a/src/main/java/gregtech/api/multitileentity/machine/MultiTileBasicMachine.java b/src/main/java/gregtech/api/multitileentity/machine/MultiTileBasicMachine.java index 0b6d4f587d..19c052b96a 100644 --- a/src/main/java/gregtech/api/multitileentity/machine/MultiTileBasicMachine.java +++ b/src/main/java/gregtech/api/multitileentity/machine/MultiTileBasicMachine.java @@ -1,18 +1,19 @@ package gregtech.api.multitileentity.machine; -import static com.google.common.primitives.Ints.saturatedCast; -import static gregtech.api.enums.GT_Values.B; -import static gregtech.api.enums.GT_Values.VN; +import static gregtech.api.enums.GT_Values.*; +import static gregtech.api.enums.TickTime.MINUTE; import java.io.IOException; import java.util.ArrayList; -import java.util.Arrays; +import java.util.Objects; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import net.minecraft.client.Minecraft; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.nbt.NBTTagList; import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntityFurnace; import net.minecraft.util.EnumChatFormatting; @@ -20,26 +21,32 @@ import net.minecraft.util.ResourceLocation; import net.minecraft.util.StatCollector; import net.minecraftforge.common.util.ForgeDirection; import net.minecraftforge.fluids.FluidStack; -import net.minecraftforge.fluids.IFluidTank; + +import org.jetbrains.annotations.ApiStatus.OverrideOnly; import com.gtnewhorizons.modularui.api.forge.IItemHandlerModifiable; import com.gtnewhorizons.modularui.api.forge.ItemStackHandler; +import com.gtnewhorizons.modularui.api.screen.UIBuildContext; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import gregtech.api.enums.GT_Values; import gregtech.api.enums.GT_Values.NBT; +import gregtech.api.enums.InventoryType; import gregtech.api.enums.Mods; import gregtech.api.enums.SoundResource; import gregtech.api.enums.Textures; import gregtech.api.enums.Textures.BlockIcons.CustomIcon; import gregtech.api.enums.TickTime; -import gregtech.api.fluid.FluidTankGT; +import gregtech.api.enums.VoidingMode; +import gregtech.api.gui.GUIHost; +import gregtech.api.gui.GUIProvider; import gregtech.api.interfaces.ITexture; -import gregtech.api.logic.PollutionLogic; +import gregtech.api.logic.FluidInventoryLogic; +import gregtech.api.logic.ItemInventoryLogic; +import gregtech.api.logic.MuTEProcessingLogic; +import gregtech.api.logic.NullPowerLogic; import gregtech.api.logic.PowerLogic; -import gregtech.api.logic.ProcessingLogic; -import gregtech.api.logic.interfaces.PollutionLogicHost; import gregtech.api.logic.interfaces.PowerLogicHost; import gregtech.api.logic.interfaces.ProcessingLogicHost; import gregtech.api.metatileentity.GregTechTileClientEvents; @@ -47,13 +54,14 @@ import gregtech.api.multitileentity.MultiTileEntityRegistry; import gregtech.api.multitileentity.base.TickableMultiTileEntity; import gregtech.api.multitileentity.interfaces.IMultiTileMachine; import gregtech.api.net.GT_Packet_MultiTileEntity; -import gregtech.api.recipe.check.CheckRecipeResult; import gregtech.api.render.TextureFactory; +import gregtech.api.task.tasks.ProcessingTask; import gregtech.api.util.GT_Utility; import gregtech.client.GT_SoundLoop; -import gregtech.common.GT_Pollution; +import gregtech.common.gui.MachineGUIProvider; -public abstract class MultiTileBasicMachine extends TickableMultiTileEntity implements IMultiTileMachine { +public abstract class MultiTileBasicMachine<P extends MuTEProcessingLogic<P>> extends TickableMultiTileEntity + implements IMultiTileMachine, ProcessingLogicHost<P>, PowerLogicHost, GUIHost { protected static final int ACTIVE = B[0]; protected static final int TICKS_BETWEEN_RECIPE_CHECKS = 5 * TickTime.SECOND; @@ -70,22 +78,10 @@ public abstract class MultiTileBasicMachine extends TickableMultiTileEntity impl protected int maxParallel = 1; protected boolean active = false; - protected long storedEnergy = 0; - protected long voltage = 0; - protected long amperage = 2; - protected long eut = 0; protected int tier = 0; - protected long maxProgressTime = 0; - protected long progressTime = 0; protected long burnTime = 0; protected long totalBurnTime = 0; - protected FluidTankGT[] inputTanks = GT_Values.emptyFluidTankGT; - protected FluidTankGT[] outputTanks = GT_Values.emptyFluidTankGT; - protected FluidStack[] fluidsToOutput = GT_Values.emptyFluidStack; - protected ItemStack[] itemsToOutput = GT_Values.emptyItemStackArray; - protected IItemHandlerModifiable inputInventory = EMPTY_INVENTORY; - protected IItemHandlerModifiable outputInventory = EMPTY_INVENTORY; protected boolean outputInventoryChanged = false; protected boolean powerShutDown = false; protected boolean wasEnabled = false; @@ -93,13 +89,30 @@ public abstract class MultiTileBasicMachine extends TickableMultiTileEntity impl protected boolean isElectric = true; protected boolean isSteam = false; protected boolean acceptsFuel = false; - protected boolean isWireless = false; + protected byte soundEvent = 0; protected int soundEventValue = 0; + protected ItemInventoryLogic itemInput; + protected ItemInventoryLogic itemOutput; + protected FluidInventoryLogic fluidInput; + protected FluidInventoryLogic fluidOutput; + + protected P processingLogic; + @Nonnull + protected VoidingMode voidingMode = VoidingMode.VOID_NONE; + protected boolean processingUpdate = false; + @Nonnull + protected PowerLogic power = createPowerLogic(); + @Nonnull + protected GUIProvider<?> guiProvider = createGUIProvider(); @SideOnly(Side.CLIENT) protected GT_SoundLoop activitySoundLoop; + public MultiTileBasicMachine() { + new ProcessingTask<>(this); + } + @Override public String getTileEntityName() { return "gt.multitileentity.machine.basic"; @@ -116,80 +129,34 @@ public abstract class MultiTileBasicMachine extends TickableMultiTileEntity impl nbt.setBoolean(NBT.ACTIVE, active); } - if (inputInventory != null && inputInventory.getSlots() > 0) { - writeInventory(nbt, inputInventory, NBT.INV_INPUT_LIST); - } - - if (outputInventory != null && outputInventory.getSlots() > 0) { - writeInventory(nbt, outputInventory, NBT.INV_OUTPUT_LIST); - } + saveItemLogic(nbt); + saveFluidLogic(nbt); - for (int i = 0; i < inputTanks.length; i++) { - inputTanks[i].writeToNBT(nbt, NBT.TANK_IN + i); - } - - for (int i = 0; i < outputTanks.length; i++) { - outputTanks[i].writeToNBT(nbt, NBT.TANK_OUT + i); - } - - if (fluidsToOutput != null && fluidsToOutput.length > 0) { - writeFluids(nbt, fluidsToOutput, NBT.FLUID_OUT); - } - - if (itemsToOutput != null) { - saveItemsToOutput(nbt); + if (processingLogic != null) { + NBTTagCompound processingLogicNBT = processingLogic.saveToNBT(); + nbt.setTag("processingLogic", processingLogicNBT); } nbt.setInteger(NBT.TIER, tier); - nbt.setLong(NBT.EUT_CONSUMPTION, eut); nbt.setLong(NBT.BURN_TIME_LEFT, burnTime); nbt.setLong(NBT.TOTAL_BURN_TIME, totalBurnTime); nbt.setBoolean(NBT.ALLOWED_WORK, canWork); nbt.setBoolean(NBT.ACTIVE, active); + power.saveToNBT(nbt); } - protected void writeFluids(NBTTagCompound nbt, FluidStack[] fluids, String fluidListTag) { - if (fluids != null && fluids.length > 0) { - final NBTTagList tList = new NBTTagList(); - for (final FluidStack tFluid : fluids) { - if (tFluid != null) { - final NBTTagCompound tag = new NBTTagCompound(); - tFluid.writeToNBT(tag); - tList.appendTag(tag); - } - } - nbt.setTag(fluidListTag, tList); - } - } - - protected void writeInventory(NBTTagCompound nbt, IItemHandlerModifiable inv, String invListTag) { - if (inv != null && inv.getSlots() > 0) { - final NBTTagList tList = new NBTTagList(); - for (int slot = 0; slot < inv.getSlots(); slot++) { - final ItemStack tStack = inv.getStackInSlot(slot); - if (tStack != null) { - final NBTTagCompound tag = new NBTTagCompound(); - tag.setByte("s", (byte) slot); - tStack.writeToNBT(tag); - tList.appendTag(tag); - } - } - nbt.setTag(invListTag, tList); - } + protected void saveItemLogic(NBTTagCompound nbt) { + NBTTagCompound nbtListInput = itemInput.saveToNBT(); + nbt.setTag(NBT.INV_INPUT_LIST, nbtListInput); + NBTTagCompound nbtListOutput = itemOutput.saveToNBT(); + nbt.setTag(NBT.INV_OUTPUT_LIST, nbtListOutput); } - protected void saveItemsToOutput(NBTTagCompound aNBT) { - final NBTTagList nbtList = new NBTTagList(); - for (int slot = 0; slot < itemsToOutput.length; slot++) { - final ItemStack itemStack = itemsToOutput[slot]; - if (itemStack != null) { - final NBTTagCompound tag = new NBTTagCompound(); - tag.setByte("s", (byte) slot); - itemStack.writeToNBT(tag); - nbtList.appendTag(tag); - } - } - aNBT.setTag(NBT.ITEM_OUT, nbtList); + protected void saveFluidLogic(NBTTagCompound nbt) { + NBTTagCompound fluidInputNBT = fluidInput.saveToNBT(); + nbt.setTag(NBT.TANK_IN, fluidInputNBT); + NBTTagCompound fluidOutputNBT = fluidOutput.saveToNBT(); + nbt.setTag(NBT.TANK_OUT, fluidOutputNBT); } @Override @@ -203,63 +170,48 @@ public abstract class MultiTileBasicMachine extends TickableMultiTileEntity impl active = nbt.getBoolean(NBT.ACTIVE); } - /* Inventories */ - inputInventory = new ItemStackHandler(Math.max(nbt.getInteger(NBT.INV_INPUT_SIZE), 0)); - outputInventory = new ItemStackHandler(Math.max(nbt.getInteger(NBT.INV_OUTPUT_SIZE), 0)); - loadInventory(nbt, inputInventory, NBT.INV_INPUT_LIST); - loadInventory(nbt, outputInventory, NBT.INV_OUTPUT_LIST); - - /* Tanks */ - long capacity = 1000; - if (nbt.hasKey(NBT.TANK_CAPACITY)) { - capacity = saturatedCast(nbt.getLong(NBT.TANK_CAPACITY)); - } - - inputTanks = new FluidTankGT[getFluidInputCount()]; - outputTanks = new FluidTankGT[getFluidOutputCount()]; - fluidsToOutput = new FluidStack[getFluidOutputCount()]; - - // TODO: See if we need the adjustable map here `.setCapacity(mRecipes, mParallel * 2L)` in place of the - // `setCapacityMultiplier` - for (int i = 0; i < inputTanks.length; i++) { - inputTanks[i] = new FluidTankGT(capacity).setCapacityMultiplier(maxParallel * 2L) - .readFromNBT(nbt, NBT.TANK_IN + i); - } - for (int i = 0; i < outputTanks.length; i++) { - outputTanks[i] = new FluidTankGT(capacity).setCapacityMultiplier(maxParallel * 2L) - .readFromNBT(nbt, NBT.TANK_OUT + i); - } + loadItemLogic(nbt); + loadFluidLogic(nbt); - for (int i = 0; i < fluidsToOutput.length; i++) { - fluidsToOutput[i] = FluidStack.loadFluidStackFromNBT(nbt.getCompoundTag(NBT.FLUID_OUT + "." + i)); + if (nbt.hasKey("processingLogic")) { + P processingLogic = getProcessingLogic(); + processingLogic.loadFromNBT(Objects.requireNonNull(nbt.getCompoundTag("processingLogic"))); } - loadItemsToOutput(nbt); - tier = nbt.getInteger(NBT.TIER); - eut = nbt.getLong(NBT.EUT_CONSUMPTION); burnTime = nbt.getLong(NBT.BURN_TIME_LEFT); totalBurnTime = nbt.getLong(NBT.TOTAL_BURN_TIME); canWork = nbt.getBoolean(NBT.ALLOWED_WORK); active = nbt.getBoolean(NBT.ACTIVE); + power.loadFromNBT(nbt); } - protected void loadInventory(NBTTagCompound aNBT, IItemHandlerModifiable inv, String invListTag) { - final NBTTagList tList = aNBT.getTagList(invListTag, 10); - for (int i = 0; i < tList.tagCount(); i++) { - final NBTTagCompound tNBT = tList.getCompoundTagAt(i); - final int tSlot = tNBT.getShort("s"); - if (tSlot >= 0 && tSlot < inv.getSlots()) inv.setStackInSlot(tSlot, GT_Utility.loadItem(tNBT)); + protected void loadItemLogic(NBTTagCompound nbt) { + itemInput = new ItemInventoryLogic(nbt.getInteger(NBT.INV_OUTPUT_SIZE), tier); + itemOutput = new ItemInventoryLogic(nbt.getInteger(NBT.INV_OUTPUT_SIZE), tier); + if (nbt.hasKey(NBT.INV_INPUT_LIST)) { + itemInput.loadFromNBT(nbt.getCompoundTag(NBT.INV_INPUT_LIST)); + } + if (nbt.hasKey(NBT.INV_OUTPUT_LIST)) { + itemOutput.loadFromNBT(nbt.getCompoundTag(NBT.INV_OUTPUT_LIST)); } } - protected void loadItemsToOutput(NBTTagCompound aNBT) { - final NBTTagList tList = aNBT.getTagList(NBT.ITEM_OUT, 10); - itemsToOutput = new ItemStack[tList.tagCount()]; - for (int i = 0; i < tList.tagCount(); i++) { - final NBTTagCompound tNBT = tList.getCompoundTagAt(i); - final int tSlot = tNBT.getByte("s"); - if (tSlot >= 0 && tSlot < itemsToOutput.length) itemsToOutput[tSlot] = GT_Utility.loadItem(tNBT); + protected void loadFluidLogic(NBTTagCompound nbt) { + fluidInput = new FluidInventoryLogic(16, 10000, tier); + fluidOutput = new FluidInventoryLogic(16, 10000, tier); + fluidInput.loadFromNBT(nbt.getCompoundTag(NBT.TANK_IN)); + fluidOutput.loadFromNBT(nbt.getCompoundTag(NBT.TANK_OUT)); + } + + public boolean checkTexture(String modID, String resourcePath) { + try { + Minecraft.getMinecraft() + .getResourceManager() + .getResource(new ResourceLocation(modID, resourcePath)); + return true; + } catch (IOException ignored) { + return false; } } @@ -268,17 +220,10 @@ public abstract class MultiTileBasicMachine extends TickableMultiTileEntity impl super.loadTextures(folder); for (StatusTextures textureName : StatusTextures.TEXTURES) { ITexture texture = null; - try { - Minecraft.getMinecraft() - .getResourceManager() - .getResource( - new ResourceLocation( - Mods.GregTech.ID, - "textures/blocks/multitileentity/" + folder + "/" + textureName.getName() + ".png")); - } catch (IOException ignored) { + String texturePath = "textures/blocks/multitileentity/" + folder + "/" + textureName.getName() + ".png"; + if (!checkTexture(Mods.GregTech.ID, texturePath)) { texture = TextureFactory.of(Textures.BlockIcons.VOID); - } - if (texture == null) { + } else { if (textureName.hasGlow()) { texture = TextureFactory.builder() .addIcon(new CustomIcon("multitileentity/" + folder + "/" + textureName.getName())) @@ -357,79 +302,6 @@ public abstract class MultiTileBasicMachine extends TickableMultiTileEntity impl @Override public void setLightValue(byte aLightValue) {} - @Override - public String getInventoryName() { - final String name = getCustomName(); - if (name != null) return name; - final MultiTileEntityRegistry tRegistry = MultiTileEntityRegistry.getRegistry(getMultiTileEntityRegistryID()); - return tRegistry == null ? getClass().getName() : tRegistry.getLocal(getMultiTileEntityID()); - } - - @Override - public boolean isUseableByPlayer(EntityPlayer aPlayer) { - return playerOwnsThis(aPlayer, false) && mTickTimer > 40 - && getTileEntityOffset(0, 0, 0) == this - && aPlayer.getDistanceSq(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5) < 64 - && allowInteraction(aPlayer); - } - - @Override - public boolean isLiquidInput(ForgeDirection side) { - return side != facing; - } - - @Override - public boolean isLiquidOutput(ForgeDirection side) { - return side != facing; - } - - @Override - protected IFluidTank[] getFluidTanks(ForgeDirection side) { - final boolean fluidInput = isLiquidInput(side); - final boolean fluidOutput = isLiquidOutput(side); - - if (fluidInput && fluidOutput) { - final IFluidTank[] rTanks = new IFluidTank[inputTanks.length + outputTanks.length]; - System.arraycopy(inputTanks, 0, rTanks, 0, inputTanks.length); - System.arraycopy(outputTanks, 0, rTanks, inputTanks.length, outputTanks.length); - return rTanks; - } else if (fluidInput) { - return inputTanks; - } else if (fluidOutput) { - return outputTanks; - } - return GT_Values.emptyFluidTank; - } - - @Override - public IFluidTank getFluidTankFillable(ForgeDirection side, FluidStack aFluidToFill) { - return getFluidTankFillable(facing, side, aFluidToFill); - } - - public IFluidTank getFluidTankFillable(ForgeDirection sideSource, ForgeDirection sideDestination, - FluidStack fluidToFill) { - if (sideSource.compareTo(sideDestination) != 0) return null; - for (FluidTankGT tankGT : inputTanks) if (tankGT.contains(fluidToFill)) return tankGT; - // if (!mRecipes.containsInput(aFluidToFill, this, slot(mRecipes.mInputItemsCount + - // mRecipes.mOutputItemsCount))) return null; - for (FluidTankGT fluidTankGT : inputTanks) if (fluidTankGT.isEmpty()) return fluidTankGT; - return null; - } - - @Override - protected IFluidTank getFluidTankDrainable(ForgeDirection side, FluidStack aFluidToDrain) { - return getFluidTankDrainable(facing, side, aFluidToDrain); - } - - protected IFluidTank getFluidTankDrainable(ForgeDirection sideSource, ForgeDirection sideDestination, - FluidStack fluidToDrain) { - if (sideSource.compareTo(sideDestination) != 0) return null; - for (FluidTankGT fluidTankGT : outputTanks) - if (fluidToDrain == null ? fluidTankGT.has() : fluidTankGT.contains(fluidToDrain)) return fluidTankGT; - - return null; - } - /* * Inventory */ @@ -453,16 +325,6 @@ public abstract class MultiTileBasicMachine extends TickableMultiTileEntity impl hasInventoryChanged = true; } - @Override - public boolean isItemValidForSlot(int aSlot, ItemStack aStack) { - return true; - } - - @Override - public int getInventoryStackLimit() { - return 64; - } - // #region Machine @Override @@ -480,28 +342,26 @@ public abstract class MultiTileBasicMachine extends TickableMultiTileEntity impl * @param tick The current tick of the machine */ protected void runMachine(long tick) { - if (acceptsFuel() && isActive()) { - if (!consumeFuel()) { - stopMachine(true); - return; - } + if (acceptsFuel() && isActive() && !consumeFuel()) { + stopMachine(true); + return; } if (hasThingsToDo()) { markDirty(); runningTick(tick); - } else { - if (tick % TICKS_BETWEEN_RECIPE_CHECKS == 0 || hasWorkJustBeenEnabled() || hasInventoryBeenModified()) { - if (isAllowedToWork()) { - wasEnabled = false; - if (checkRecipe()) { - setActive(true); - setSound(GregTechTileClientEvents.START_SOUND_LOOP, PROCESS_START_SOUND_INDEX); - updateSlots(); - markDirty(); - issueClientUpdate(); - } - } + return; + } + + if (tick % TICKS_BETWEEN_RECIPE_CHECKS == 0 || hasWorkJustBeenEnabled() + || hasInventoryBeenModified() && isAllowedToWork()) { + wasEnabled = false; + if (checkRecipe()) { + setActive(true); + setSound(GregTechTileClientEvents.START_SOUND_LOOP, PROCESS_START_SOUND_INDEX); + updateSlots(); + markDirty(); + issueClientUpdate(); } } } @@ -512,81 +372,25 @@ public abstract class MultiTileBasicMachine extends TickableMultiTileEntity impl * @param tick The current tick of the machine */ protected void runningTick(long tick) { - if (this instanceof PowerLogicHost) { - consumeEnergy(); - } - - if (maxProgressTime > 0 && ++progressTime >= maxProgressTime) { - progressTime = 0; - maxProgressTime = 0; - outputItems(); - outputFluids(); - if (isAllowedToWork()) { - if (!checkRecipe()) { - setActive(false); - issueClientUpdate(); - } - } - updateSlots(); - } - - if (this instanceof PollutionLogicHost && tick % POLLUTION_TICK == 0) { - doPollution(); - } - emitEnergy(); + consumeEnergy(); } /** * Runs only on server side */ protected boolean checkRecipe() { - if (!(this instanceof ProcessingLogicHost)) { - return false; - } - ProcessingLogic logic = ((ProcessingLogicHost) this).getProcessingLogic(); - logic.clear(); - CheckRecipeResult result = logic.setInputItems(getInputItems()) - .setInputFluids(getInputFluids()) - .setCurrentOutputItems( - outputInventory.getStacks() - .toArray(new ItemStack[0])) - .process(); - setDuration(logic.getDuration()); - setEut(logic.getCalculatedEut()); - setItemOutputs(logic.getOutputItems()); - setFluidOutputs(logic.getOutputFluids()); - return result.wasSuccessful(); - } - - /** - * Runs only on server side - */ - protected void doPollution() { - PollutionLogic logic = ((PollutionLogicHost) this).getPollutionLogic(); - - if (logic == null) { - return; - } - - GT_Pollution.addPollution(getWorld(), getXCoord() >> 4, getZCoord() >> 4, logic.getPollutionAmount()); + return false; } /** * Runs only on server side */ - protected void emitEnergy() {} - - /** - * Runs only on server side - */ protected void consumeEnergy() { - PowerLogic logic = ((PowerLogicHost) this).getPowerLogic(ForgeDirection.UNKNOWN); + PowerLogic logic = getPowerLogic(); - if (logic == null) { - return; - } + P processing = getProcessingLogic(); - if (!logic.removeEnergyUnsafe(eut)) { + if (!logic.removeEnergyUnsafe(processing.getCalculatedEut())) { stopMachine(true); } } @@ -603,9 +407,8 @@ public abstract class MultiTileBasicMachine extends TickableMultiTileEntity impl } public void startSoundLoop(byte aIndex, double aX, double aY, double aZ) { - if (aIndex == PROCESS_START_SOUND_INDEX) { - if (getProcessStartSound() != null) - GT_Utility.doSoundAtClient(getProcessStartSound(), getTimeBetweenProcessSounds(), 1.0F, aX, aY, aZ); + if (aIndex == PROCESS_START_SOUND_INDEX && getProcessStartSound() != null) { + GT_Utility.doSoundAtClient(getProcessStartSound(), getTimeBetweenProcessSounds(), 1.0F, aX, aY, aZ); } } @@ -619,18 +422,18 @@ public abstract class MultiTileBasicMachine extends TickableMultiTileEntity impl @SideOnly(Side.CLIENT) protected void doActivitySound(ResourceLocation activitySound) { - if (isActive() && activitySound != null) { - if (activitySoundLoop == null) { - activitySoundLoop = new GT_SoundLoop(activitySound, this, false, true); - Minecraft.getMinecraft() - .getSoundHandler() - .playSound(activitySoundLoop); - } - } else { - if (activitySoundLoop != null) { - activitySoundLoop = null; - } + if (isActive() && activitySound != null && activitySoundLoop == null) { + activitySoundLoop = new GT_SoundLoop(activitySound, this, false, true); + Minecraft.getMinecraft() + .getSoundHandler() + .playSound(activitySoundLoop); + return; } + + if (activitySoundLoop != null) { + activitySoundLoop = null; + } + } @SideOnly(Side.CLIENT) @@ -639,105 +442,69 @@ public abstract class MultiTileBasicMachine extends TickableMultiTileEntity impl } protected ItemStack[] getInputItems() { - return inputInventory.getStacks() - .toArray(new ItemStack[0]); + return itemInput.getStoredItems(); } protected FluidStack[] getInputFluids() { - return Arrays.stream(inputTanks) - .map(FluidTankGT::get) - .toArray(FluidStack[]::new); - } - - protected void outputItems() { - outputItems(itemsToOutput); - itemsToOutput = null; - } - - protected void outputItems(ItemStack... itemsToOutput) { - outputItems(outputInventory, itemsToOutput); + return fluidInput.getStoredFluids(); } - protected void outputItems(IItemHandlerModifiable inventory, ItemStack... itemsToOutput) { - if (itemsToOutput == null || inventory == null) { - return; - } - for (ItemStack item : itemsToOutput) { - int index = 0; - while (item != null && item.stackSize > 0 && index < inventory.getSlots()) { - item = inventory.insertItem(index++, item.copy(), false); - } - } - } - - protected void outputFluids() { - outputFluids(fluidsToOutput); - fluidsToOutput = null; - } - - protected void outputFluids(FluidStack... fluidsToOutput) { - outputFluids(outputTanks, fluidsToOutput); - } - - protected void outputFluids(FluidTankGT[] tankArray, FluidStack... fluidsToOutput) { - if (fluidsToOutput == null) { - return; - } - for (FluidStack fluid : fluidsToOutput) { - tryToFillTanks(fluid, tankArray); - } - } - - protected void tryToFillTanks(FluidStack fluid, FluidTankGT... tanks) { - for (FluidTankGT tank : tanks) { - if (tank.canFillAll(fluid)) { - fluid.amount -= tank.add(fluid.amount, fluid); - } - } - } - - public long getProgress() { - return progressTime; + @Override + public int getProgress() { + P processing = getProcessingLogic(); + return processing.getProgress(); } - public long getMaxProgress() { - return maxProgressTime; + @Override + public int getMaxProgress() { + P processing = getProcessingLogic(); + return processing.getDuration(); } - public boolean increaseProgress(int aProgressAmountInTicks) { - progressTime += aProgressAmountInTicks; + @Override + public boolean increaseProgress(int progressAmount) { + P processing = getProcessingLogic(); + processing.increaseProgress(progressAmount); return true; } + @Override public boolean hasThingsToDo() { return getMaxProgress() > 0; } + @Override public boolean hasWorkJustBeenEnabled() { return wasEnabled; } + @Override public void enableWorking() { wasEnabled = true; canWork = true; } + @Override public void disableWorking() { canWork = false; } + @Override public boolean wasShutdown() { return powerShutDown; } + @Override public boolean isAllowedToWork() { return canWork; } + @Override public boolean isActive() { return active; } + @Override public void setActive(boolean active) { this.active = active; } @@ -766,37 +533,18 @@ public abstract class MultiTileBasicMachine extends TickableMultiTileEntity impl this.acceptsFuel = acceptsFuel; } - protected boolean isWireless() { - return isWireless; - } - - protected void setWireless(boolean isWireless) { - this.isWireless = isWireless; - } - - protected boolean drainEut(long eut) { - return decreaseStoredEnergyUnits(eut, false); - } - - protected boolean generateEut(long eut) { - return increaseStoredEnergyUnits(eut, true); - } - - protected boolean isGenerator() { - return false; - } - protected boolean consumeFuel() { + if (isElectric() || isSteam()) return false; if (isActive() && burnTime <= 0) { - for (int i = 0; i < inputInventory.getSlots(); i++) { - if (inputInventory.getStackInSlot(i) != null) { - int checkBurnTime = TileEntityFurnace.getItemBurnTime(inputInventory.getStackInSlot(i)) / 10; - if (checkBurnTime <= 0) continue; - inputInventory.getStackInSlot(i).stackSize--; - burnTime = checkBurnTime; - totalBurnTime = checkBurnTime; - break; - } + for (int i = 0; i < itemInput.getSlots(); i++) { + ItemStack item = itemInput.getItemInSlot(i); + if (item == null) continue; + int checkBurnTime = TileEntityFurnace.getItemBurnTime(item) / 10; + if (checkBurnTime <= 0) continue; + item.stackSize--; + burnTime = checkBurnTime; + totalBurnTime = checkBurnTime; + break; } updateSlots(); } @@ -806,8 +554,7 @@ public abstract class MultiTileBasicMachine extends TickableMultiTileEntity impl totalBurnTime = 0; return false; } - - return true; + return false; } @Override @@ -826,38 +573,36 @@ public abstract class MultiTileBasicMachine extends TickableMultiTileEntity impl list.add("Fuel: " + EnumChatFormatting.GOLD + burnTime + "/" + totalBurnTime); } - if (this instanceof PowerLogicHost powerLogicHost) { - PowerLogic logic = powerLogicHost.getPowerLogic(facing); - if (isElectric) { - list.add( - StatCollector.translateToLocal("GT5U.multiblock.energy") + ": " - + EnumChatFormatting.GREEN - + GT_Utility.formatNumbers(logic.getStoredEnergy()) - + EnumChatFormatting.RESET - + " EU / " - + EnumChatFormatting.YELLOW - + GT_Utility.formatNumbers(logic.getCapacity()) - + EnumChatFormatting.RESET - + " EU"); - list.add( - StatCollector.translateToLocal("GT5U.multiblock.usage") + ": " - + EnumChatFormatting.RED - + GT_Utility.formatNumbers(eut) - + EnumChatFormatting.RESET - + " EU/t"); - list.add( - StatCollector.translateToLocal("GT5U.multiblock.mei") + ": " - + EnumChatFormatting.YELLOW - + GT_Utility.formatNumbers(logic.getVoltage()) - + EnumChatFormatting.RESET - // TODO: Put ampere getter here, once that's variable - + " EU/t(*2A) " - + StatCollector.translateToLocal("GT5U.machines.tier") - + ": " - + EnumChatFormatting.YELLOW - + VN[GT_Utility.getTier(logic.getVoltage())] - + EnumChatFormatting.RESET); - } + PowerLogic logic = getPowerLogic(); + if (isElectric) { + list.add( + StatCollector.translateToLocal("GT5U.multiblock.energy") + ": " + + EnumChatFormatting.GREEN + + GT_Utility.formatNumbers(logic.getStoredEnergy()) + + EnumChatFormatting.RESET + + " EU / " + + EnumChatFormatting.YELLOW + + GT_Utility.formatNumbers(logic.getCapacity()) + + EnumChatFormatting.RESET + + " EU"); + list.add( + StatCollector.translateToLocal("GT5U.multiblock.usage") + ": " + + EnumChatFormatting.RED + + GT_Utility.formatNumbers(getProcessingLogic().getCalculatedEut()) + + EnumChatFormatting.RESET + + " EU/t"); + list.add( + StatCollector.translateToLocal("GT5U.multiblock.mei") + ": " + + EnumChatFormatting.YELLOW + + GT_Utility.formatNumbers(logic.getVoltage()) + + EnumChatFormatting.RESET + // TODO: Put ampere getter here, once that's variable + + " EU/t(*2A) " + + StatCollector.translateToLocal("GT5U.machines.tier") + + ": " + + EnumChatFormatting.YELLOW + + VN[GT_Utility.getTier(logic.getVoltage())] + + EnumChatFormatting.RESET); } addProgressStringToScanner(player, logLevel, list); @@ -873,6 +618,9 @@ public abstract class MultiTileBasicMachine extends TickableMultiTileEntity impl } protected void addProgressStringToScanner(EntityPlayer player, int logLevel, ArrayList<String> list) { + P processing = getProcessingLogic(); + int progressTime = processing.getProgress(); + int maxProgressTime = processing.getDuration(); list.add( StatCollector.translateToLocal("GT5U.multiblock.Progress") + ": " + EnumChatFormatting.GREEN @@ -886,7 +634,6 @@ public abstract class MultiTileBasicMachine extends TickableMultiTileEntity impl } protected void stopMachine(boolean powerShutDown) { - progressTime = 0; setActive(false); disableWorking(); if (powerShutDown) { @@ -896,50 +643,10 @@ public abstract class MultiTileBasicMachine extends TickableMultiTileEntity impl } protected void updateSlots() { - for (int i = 0; i < inputInventory.getSlots(); i++) { - ItemStack item = inputInventory.getStackInSlot(i); - if (item != null && item.stackSize <= 0) { - inputInventory.setStackInSlot(i, null); - } - } - - for (FluidTankGT inputTank : inputTanks) { - if (inputTank == null) { - continue; - } - - if (inputTank.get() != null && inputTank.get().amount <= 0) { - inputTank.setEmpty(); - continue; - } - - FluidStack afterRecipe = inputTank.get(); - FluidStack beforeRecipe = inputTank.get(Integer.MAX_VALUE); - if (afterRecipe == null || beforeRecipe == null) { - continue; - } - int difference = beforeRecipe.amount - afterRecipe.amount; - inputTank.remove(difference); - } - } - - /** - * Must always be a positive. If the multi generates Eu/t isGenerator() should be overridden to true - */ - protected void setEut(long eut) { - if (eut < 0) { - eut = -eut; - } - - this.eut = eut; - } - - protected void setDuration(long duration) { - if (duration < 0) { - duration = -duration; - } - - maxProgressTime = duration; + itemInput.update(false); + itemOutput.update(false); + fluidInput.update(); + fluidOutput.update(); } @Override @@ -956,53 +663,149 @@ public abstract class MultiTileBasicMachine extends TickableMultiTileEntity impl setActive((booleans & ACTIVE) == ACTIVE); } - protected boolean hasItemInput() { + public boolean hasItemInput() { return true; } - protected boolean hasItemOutput() { + public boolean hasItemOutput() { return true; } - protected boolean hasFluidInput() { + public boolean hasFluidInput() { return true; } - protected boolean hasFluidOutput() { |
