begin work

This commit is contained in:
GlodBlock committed 2022-05-10 17:57:38 +08:00
commit db2ad29b52
124 files changed
+11799

No files matched your search

@@ -0,0 +1,295 @@
package com.glodblock.github.common.tile;
import appeng.api.config.Actionable;
import appeng.api.networking.GridFlags;
import appeng.api.networking.crafting.ICraftingGrid;
import appeng.api.networking.energy.IEnergyGrid;
import appeng.api.networking.events.*;
import appeng.api.networking.security.BaseActionSource;
import appeng.api.networking.security.MachineSource;
import appeng.api.networking.storage.IBaseMonitor;
import appeng.api.networking.storage.IStorageGrid;
import appeng.api.storage.*;
import appeng.api.storage.data.IAEFluidStack;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IAEStack;
import appeng.api.storage.data.IItemList;
import appeng.helpers.IPriorityHost;
import appeng.me.GridAccessException;
import appeng.me.cache.CraftingGridCache;
import appeng.me.storage.MEInventoryHandler;
import appeng.tile.grid.AENetworkTile;
import appeng.util.Platform;
import com.glodblock.github.common.item.ItemFluidDrop;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class TileFluidDiscretizer extends AENetworkTile implements IPriorityHost, ICellContainer {
private final BaseActionSource ownActionSource = new MachineSource(this);
private final FluidDiscretizingInventory fluidDropInv = new FluidDiscretizingInventory();
private final FluidCraftingInventory fluidCraftInv = new FluidCraftingInventory();
private boolean prevActiveState = false;
public TileFluidDiscretizer() {
super();
getProxy().setIdlePowerUsage(3D);
getProxy().setFlags(GridFlags.REQUIRE_CHANNEL);
}
@Override
public boolean canBeRotated() {
return false;
//based
}
@Override
@SuppressWarnings("rawtypes")
public List<IMEInventoryHandler> getCellArray(StorageChannel channel) {
/*conflict = !checkDiscreatizer();
if (conflict) return Collections.emptyList();*/
if (getProxy().isActive()) {
if (channel == StorageChannel.ITEMS) {
return Collections.singletonList(fluidDropInv.invHandler);
} else if (channel == StorageChannel.FLUIDS) {
return Collections.singletonList(fluidCraftInv.invHandler);
}
}
return Collections.emptyList();
}
@Override
public int getPriority() {
return Integer.MAX_VALUE;
}
@Override
public void setPriority(int newValue) {
//do nothing
}
@Override
public void blinkCell(int slot) {
//do nothing
}
@Override
public void gridChanged() {
IMEMonitor<IAEFluidStack> fluidGrid = getFluidGrid();
if (fluidGrid != null) {
fluidGrid.addListener(fluidDropInv, fluidGrid);
}
}
@Override
public void saveChanges(IMEInventory cellInventory) {
markDirty();
}
private IMEMonitor<IAEFluidStack> getFluidGrid() {
try {
return getProxy().getGrid().<IStorageGrid>getCache(IStorageGrid.class).getFluidInventory();
} catch (GridAccessException e) {
return null;
}
}
private IEnergyGrid getEnergyGrid() {
try {
return getProxy().getGrid().getCache(IEnergyGrid.class);
} catch (GridAccessException e) {
return null;
}
}
private void updateState() {
boolean isActive = getProxy().isActive();
if (isActive != prevActiveState) {
prevActiveState = isActive;
try {
getProxy().getGrid().postEvent(new MENetworkCellArrayUpdate());
} catch (GridAccessException e) {
// NO-OP
}
}
}
@MENetworkEventSubscribe
public void onPowerUpdate(MENetworkPowerStatusChange event) {
updateState();
}
@MENetworkEventSubscribe
public void onChannelUpdate(MENetworkChannelsChanged event) {
updateState();
}
@MENetworkEventSubscribe
public void onStorageUpdate(MENetworkStorageEvent event) {
updateState();
}
private boolean checkDiscreatizer() {
int amount = getProxy().getNode().getGrid().getMachines(this.getClass()).size();
return amount <= 1;
}
private class FluidDiscretizingInventory implements IMEInventory<IAEItemStack>, IMEMonitorHandlerReceiver<IAEFluidStack> {
private final MEInventoryHandler<IAEItemStack> invHandler = new MEInventoryHandler<>(this, getChannel());
private List<IAEItemStack> itemCache = null;
FluidDiscretizingInventory() {
invHandler.setPriority(Integer.MAX_VALUE);
}
@Override
public IAEItemStack injectItems(IAEItemStack request, Actionable type, BaseActionSource src) {
/*if (conflict) return request;*/
IAEFluidStack fluidStack = ItemFluidDrop.getAeFluidStack(request);
if (fluidStack == null) {
return request;
}
IMEMonitor<IAEFluidStack> fluidGrid = getFluidGrid();
if (fluidGrid == null) {
return request;
}
IEnergyGrid energyGrid = getEnergyGrid();
if (energyGrid == null) {
return request;
}
if (type == Actionable.SIMULATE) {
return ItemFluidDrop.newAeStack(fluidGrid.injectItems(fluidStack.copy(), Actionable.SIMULATE, src));
}
else {
return ItemFluidDrop.newAeStack(Platform.poweredInsert(energyGrid, fluidGrid, fluidStack, ownActionSource));
}
}
@Override
public IAEItemStack extractItems(IAEItemStack request, Actionable mode, BaseActionSource src) {
/*if (conflict) return null;*/
IAEFluidStack fluidStack = ItemFluidDrop.getAeFluidStack(request);
if (fluidStack == null) {
return null;
}
IMEMonitor<IAEFluidStack> fluidGrid = getFluidGrid();
if (fluidGrid == null) {
return null;
}
IEnergyGrid energyGrid = getEnergyGrid();
if (energyGrid == null) {
return null;
}
if (mode == Actionable.SIMULATE) {
return ItemFluidDrop.newAeStack(fluidGrid.extractItems(fluidStack.copy(), Actionable.SIMULATE, src));
}
else {
return ItemFluidDrop.newAeStack(Platform.poweredExtraction(energyGrid, fluidGrid, fluidStack, ownActionSource));
}
}
@Override
public IItemList<IAEItemStack> getAvailableItems(IItemList<IAEItemStack> out) {
/*if (conflict) return out;*/
if (itemCache == null) {
itemCache = new ArrayList<>();
IMEMonitor<IAEFluidStack> fluidGrid = getFluidGrid();
if (fluidGrid != null) {
for (IAEFluidStack fluid : fluidGrid.getStorageList()) {
IAEItemStack stack = ItemFluidDrop.newAeStack(fluid);
if (stack != null) {
itemCache.add(stack);
}
}
}
}
for (IAEItemStack stack : itemCache) {
out.addStorage(stack);
}
return out;
}
@Override
public StorageChannel getChannel() {
return StorageChannel.ITEMS;
}
@Override
public boolean isValid(Object verificationToken) {
IMEMonitor<IAEFluidStack> fluidGrid = getFluidGrid();
return fluidGrid != null && fluidGrid == verificationToken /*&& !conflict*/;
}
@Override
public void postChange(IBaseMonitor<IAEFluidStack> monitor, Iterable<IAEFluidStack> change, BaseActionSource actionSource) {
itemCache = null;
try {
List<IAEItemStack> mappedChanges = new ArrayList<>();
for (IAEFluidStack fluidStack : change) {
IAEItemStack itemStack = ItemFluidDrop.newAeStack(fluidStack);
if (itemStack != null) {
mappedChanges.add(itemStack);
}
}
getProxy().getGrid().<IStorageGrid>getCache(IStorageGrid.class)
.postAlterationOfStoredItems(getChannel(), mappedChanges, ownActionSource);
} catch (GridAccessException e) {
// NO-OP
}
}
@Override
public void onListUpdate() {
// NO-OP
}
}
private class FluidCraftingInventory implements IMEInventory<IAEFluidStack> {
private final MEInventoryHandler<IAEFluidStack> invHandler = new MEInventoryHandler<>(this, getChannel());
FluidCraftingInventory() {
invHandler.setPriority(Integer.MAX_VALUE);
}
@Override
@SuppressWarnings("rawtypes")
public IAEFluidStack injectItems(IAEFluidStack input, Actionable type, BaseActionSource src) {
/*if (conflict) return null;*/
ICraftingGrid craftingGrid;
try {
craftingGrid = getProxy().getGrid().getCache(ICraftingGrid.class);
} catch (GridAccessException e) {
return null;
}
if (craftingGrid instanceof CraftingGridCache) {
IAEStack remaining = ((CraftingGridCache)craftingGrid).injectItems(
ItemFluidDrop.newAeStack(input), type, ownActionSource);
if (remaining instanceof IAEItemStack) {
return ItemFluidDrop.getAeFluidStack((IAEItemStack) remaining);
}
}
return null;
}
@Override
public IAEFluidStack extractItems(IAEFluidStack request, Actionable mode, BaseActionSource src) {
return null;
}
@Override
public IItemList<IAEFluidStack> getAvailableItems(IItemList<IAEFluidStack> out) {
return out;
}
@Override
public StorageChannel getChannel() {
return StorageChannel.FLUIDS;
}
}
}
@@ -0,0 +1,81 @@
package com.glodblock.github.common.tile;
import appeng.api.config.Actionable;
import appeng.api.networking.energy.IEnergyGrid;
import appeng.api.networking.security.BaseActionSource;
import appeng.api.networking.security.MachineSource;
import appeng.api.networking.storage.IStorageGrid;
import appeng.api.storage.IMEMonitor;
import appeng.api.storage.data.IAEFluidStack;
import appeng.me.GridAccessException;
import appeng.tile.misc.TileInterface;
import appeng.util.Platform;
import appeng.util.item.AEFluidStack;
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;
public class TileFluidInterface extends TileInterface implements IFluidHandler
{
private final BaseActionSource ownActionSource = new MachineSource(this);
private IMEMonitor<IAEFluidStack> getFluidGrid() {
try {
return getProxy().getGrid().<IStorageGrid>getCache(IStorageGrid.class).getFluidInventory();
} catch (GridAccessException e) {
return null;
}
}
private IEnergyGrid getEnergyGrid() {
try {
return getProxy().getGrid().getCache(IEnergyGrid.class);
} catch (GridAccessException e) {
return null;
}
}
@Override
public int fill(ForgeDirection from, FluidStack resource, boolean doFill) {
IMEMonitor<IAEFluidStack> fluidGrid = getFluidGrid();
IEnergyGrid energyGrid = getEnergyGrid();
if (energyGrid == null || fluidGrid == null || resource == null)
return 0;
int ori = resource.amount;
IAEFluidStack remove;
if (doFill) {
remove = Platform.poweredInsert(energyGrid, fluidGrid, AEFluidStack.create(resource), ownActionSource);
} else {
remove = fluidGrid.injectItems(AEFluidStack.create(resource), Actionable.SIMULATE, ownActionSource);
}
return remove == null ? ori : (int) (ori - remove.getStackSize());
}
@Override
public FluidStack drain(ForgeDirection from, FluidStack resource, boolean doDrain) {
return null;
}
@Override
public FluidStack drain(ForgeDirection from, int maxDrain, boolean doDrain) {
return null;
}
@Override
public boolean canFill(ForgeDirection from, Fluid fluid) {
return true;
}
@Override
public boolean canDrain(ForgeDirection from, Fluid fluid) {
return false;
}
@Override
public FluidTankInfo[] getTankInfo(ForgeDirection from) {
return new FluidTankInfo[]{new FluidTankInfo(null, 0)};
}
}
@@ -0,0 +1,102 @@
package com.glodblock.github.common.tile;
import appeng.api.networking.GridFlags;
import appeng.api.networking.IGridNode;
import appeng.api.networking.energy.IEnergyGrid;
import appeng.api.networking.security.BaseActionSource;
import appeng.api.networking.security.MachineSource;
import appeng.api.networking.storage.IStorageGrid;
import appeng.api.networking.ticking.IGridTickable;
import appeng.api.networking.ticking.TickRateModulation;
import appeng.api.networking.ticking.TickingRequest;
import appeng.api.storage.IMEMonitor;
import appeng.api.storage.data.IAEFluidStack;
import appeng.helpers.Reflected;
import appeng.me.GridAccessException;
import appeng.tile.TileEvent;
import appeng.tile.events.TileEventType;
import appeng.tile.grid.AENetworkTile;
import appeng.tile.inventory.AppEngInternalInventory;
import appeng.tile.inventory.IAEAppEngInventory;
import appeng.tile.inventory.InvOperation;
import appeng.util.Platform;
import appeng.util.item.AEFluidStack;
import com.glodblock.github.common.item.ItemFluidPacket;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fluids.FluidStack;
public class TileFluidPacketDecoder extends AENetworkTile implements IGridTickable, IAEAppEngInventory {
private final AppEngInternalInventory inventory = new AppEngInternalInventory(this, 1);
private final BaseActionSource ownActionSource = new MachineSource(this);
@Reflected
public TileFluidPacketDecoder() {
getProxy().setIdlePowerUsage(1D);
getProxy().setFlags(GridFlags.REQUIRE_CHANNEL);
}
public IInventory getInventory() {
return inventory;
}
@Override
public boolean canBeRotated() {
return false;
}
@Override
public TickingRequest getTickingRequest(IGridNode node) {
return new TickingRequest(5, 120, false, true);
}
@Override
public TickRateModulation tickingRequest(IGridNode node, int ticksSinceLastCall) {
ItemStack stack = inventory.getStackInSlot(0);
if (stack == null || !(stack.getItem() instanceof ItemFluidPacket)) {
return TickRateModulation.SLEEP;
}
FluidStack fluid = ItemFluidPacket.getFluidStack(stack);
if (fluid == null || fluid.amount <= 0) {
inventory.setInventorySlotContents(0, null);
return TickRateModulation.SLEEP;
}
IAEFluidStack aeFluid = AEFluidStack.create(fluid);
IEnergyGrid energyGrid = node.getGrid().getCache(IEnergyGrid.class);
IMEMonitor<IAEFluidStack> fluidGrid = node.getGrid().<IStorageGrid>getCache(IStorageGrid.class).getFluidInventory();
IAEFluidStack remaining = Platform.poweredInsert(energyGrid, fluidGrid, aeFluid, ownActionSource);
if (remaining != null) {
if (remaining.getStackSize() == aeFluid.getStackSize()) {
return TickRateModulation.SLOWER;
}
inventory.setInventorySlotContents(0, ItemFluidPacket.newStack(remaining.getFluidStack()));
return TickRateModulation.FASTER;
} else {
inventory.setInventorySlotContents(0, null);
return TickRateModulation.SLEEP;
}
}
@Override
public void onChangeInventory(IInventory inv, int slot, InvOperation mc, ItemStack removedStack, ItemStack newStack) {
try {
getProxy().getTick().alertDevice(getProxy().getNode());
} catch (GridAccessException e) {
// NO-OP
}
}
@TileEvent( TileEventType.WORLD_NBT_WRITE )
public NBTTagCompound writeToNBTEvent(NBTTagCompound data) {
inventory.writeToNBT(data, "Inventory");
return data;
}
@TileEvent( TileEventType.WORLD_NBT_READ )
public void readFromNBTEvent(NBTTagCompound data) {
inventory.readFromNBT(data, "Inventory");
}
}
@@ -0,0 +1,77 @@
package com.glodblock.github.common.tile;
import appeng.api.storage.StorageChannel;
import appeng.api.storage.data.IAEItemStack;
import appeng.tile.AEBaseTile;
import appeng.tile.TileEvent;
import appeng.tile.events.TileEventType;
import appeng.tile.inventory.AppEngInternalInventory;
import appeng.tile.inventory.IAEAppEngInventory;
import appeng.tile.inventory.InvOperation;
import com.glodblock.github.inventory.AeStackInventory;
import com.glodblock.github.inventory.AeStackInventoryImpl;
import com.glodblock.github.util.BlockPos;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import java.util.List;
public class TileFluidPatternEncoder extends AEBaseTile implements IAEAppEngInventory {
private final AppEngInternalInventory patternInv = new AppEngInternalInventory(this, 2);
private final AeStackInventoryImpl<IAEItemStack> crafting = new AeStackInventoryImpl<>(StorageChannel.ITEMS, 9, this);
private final AeStackInventoryImpl<IAEItemStack> output = new AeStackInventoryImpl<>(StorageChannel.ITEMS, 3, this);
public IInventory getInventory() {
return patternInv;
}
public AeStackInventory<IAEItemStack> getCraftingSlots() {
return crafting;
}
public AeStackInventory<IAEItemStack> getOutputSlots() {
return output;
}
@Override
public boolean canBeRotated() {
return false;
}
@Override
public void getDrops(final World w, final int x, final int y, final int z, final List<ItemStack> drops ) {
getDrops(w, new BlockPos(x, y, z), drops);
}
public void getDrops(World world, BlockPos pos, List<ItemStack> drops) {
for (ItemStack stack : patternInv) {
if (stack != null) {
drops.add(stack);
}
}
}
@TileEvent( TileEventType.WORLD_NBT_READ )
public void readFromNBTEvent(NBTTagCompound data) {
patternInv.readFromNBT(data, "Inventory");
crafting.readFromNbt(data, "CraftingSlots");
output.readFromNbt(data, "OutputSlots");
}
@TileEvent( TileEventType.WORLD_NBT_WRITE )
public NBTTagCompound writeToNBTEvent(NBTTagCompound data) {
patternInv.writeToNBT(data, "Inventory");
crafting.writeToNbt(data, "CraftingSlots");
output.writeToNbt(data, "OutputSlots");
return data;
}
@Override
public void onChangeInventory(IInventory inv, int slot, InvOperation mc, ItemStack removedStack, ItemStack newStack) {
}
}