Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Base
- package exokem.exkva.objects.machines;
- import exokem.exkva.tools.ExkvaEnergyStorage;
- import net.minecraft.block.state.IBlockState;
- import net.minecraft.item.ItemStack;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraft.util.EnumFacing;
- import net.minecraft.util.ITickable;
- import net.minecraft.util.math.BlockPos;
- import net.minecraft.world.World;
- import net.minecraftforge.common.capabilities.Capability;
- import net.minecraftforge.energy.CapabilityEnergy;
- import net.minecraftforge.items.CapabilityItemHandler;
- import net.minecraftforge.items.ItemStackHandler;
- import javax.annotation.Nullable;
- public class TileEntityMachineBase extends TileEntity implements ITickable {
- public static int MAX_STORED_POWER;
- public static int RF_PER_TICK;
- public static int RF_PER_TICK_INPUT;
- protected ExkvaEnergyStorage energyStorage;
- protected ItemStackHandler inventory;
- protected boolean active;
- protected int activeTime;
- protected boolean hasEnergy;
- public TileEntityMachineBase(int maxPow, int rfpt, int rfin) {
- MAX_STORED_POWER = maxPow;
- RF_PER_TICK = rfpt;
- RF_PER_TICK_INPUT = rfin;
- energyStorage = new ExkvaEnergyStorage(maxPow, rfin);
- hasEnergy = true;
- }
- public TileEntityMachineBase() {
- hasEnergy = false;
- }
- //----- Access -----//
- public int getStoredEnergy() {
- if(hasEnergy) {
- return energyStorage.getEnergyStored();
- } return 0;
- }
- public int getActiveTime() {
- return activeTime;
- }
- public ItemStackHandler getInventory() {
- return inventory;
- }
- public void setStoredEnergy(int data) {
- if(hasEnergy) {
- this.energyStorage.setEnergy(data);
- }
- }
- public void setActiveTime(int data) {
- this.activeTime = data;
- }
- public boolean isActive() {
- return active || activeTime > 0;
- }
- //----- Logic -----//
- @Override
- public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newState) {
- return (oldState.getBlock() != newState.getBlock());
- }
- @Override
- public void update() {
- }
- protected ItemStack[] getResult() {
- return null;
- }
- protected boolean canActivate() {
- return false;
- }
- protected void produce() {
- }
- //----- NBT -----//
- @Override
- public void readFromNBT(NBTTagCompound compound) {
- super.readFromNBT(compound);
- inventory.deserializeNBT(compound.getCompoundTag("inventory"));
- activeTime = compound.getInteger("active_time");
- active = compound.getBoolean("active");
- if(hasEnergy) {
- energyStorage.setEnergy(compound.getInteger("stored_energy"));
- }
- }
- @Override
- public NBTTagCompound writeToNBT(NBTTagCompound compound) {
- compound = super.writeToNBT(compound);
- compound.setTag("inventory", inventory.serializeNBT());
- compound.setInteger("active_time", activeTime);
- compound.setBoolean("active", active);
- if(hasEnergy) {
- compound.setInteger("stored_energy", energyStorage.getEnergyStored());
- }
- return compound;
- }
- //----- Capability -----//
- @Override
- public boolean hasCapability(Capability<?> capability, @Nullable EnumFacing facing) {
- if(capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
- return true;
- }
- if(capability == CapabilityEnergy.ENERGY && hasEnergy) {
- return true;
- }
- return super.hasCapability(capability, facing);
- }
- @Override
- public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
- if(capability == CapabilityEnergy.ENERGY && hasEnergy) {
- return CapabilityEnergy.ENERGY.cast(energyStorage);
- }
- return super.getCapability(capability, facing);
- }
- }
- //Extension
- package exokem.exkva.objects.machines;
- import exokem.exkva.common.Exkva;
- import exokem.exkva.common.ExkvaBlocks;
- import exokem.exkva.recipe.RecipeItemConverter;
- import exokem.exkva.recipe.RecipeMatterAtomizer;
- import net.minecraft.client.Minecraft;
- import net.minecraft.item.ItemStack;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraft.util.EnumFacing;
- import net.minecraft.util.math.BlockPos;
- import net.minecraftforge.common.capabilities.Capability;
- import net.minecraftforge.energy.CapabilityEnergy;
- import net.minecraftforge.items.CapabilityItemHandler;
- import net.minecraftforge.items.IItemHandler;
- import net.minecraftforge.items.ItemHandlerHelper;
- import net.minecraftforge.items.ItemStackHandler;
- import net.minecraftforge.items.wrapper.RangedWrapper;
- import javax.annotation.Nullable;
- import static exokem.exkva.objects.machines.MachineBase.FACING;
- public class TileEntityItemConverter extends TileEntityMachineBase {
- protected final ItemStackHandler inventory = new ItemStackHandler(4) {
- public ItemStack insertItem(int index, ItemStack stack, boolean simulate) {
- if(isItemValid(index, stack)) {
- return super.insertItem(index, stack, simulate);
- }
- return stack;
- }
- public boolean isItemValid(int index, ItemStack stack) {
- if(index == 1 || index == 3) {
- return false;
- } else {
- return index >= 0;
- }
- }
- };
- public TileEntityItemConverter() {
- super();
- super.inventory = this.inventory;
- this.hasEnergy = false;
- }
- //----- Constants -----//
- private static final int IN = 0;
- private static final int ACTIVE = 1;
- private static final int UPGRADE = 2;
- private static final int OUT = 3;
- private static final int PROCESS_TIME = 50;
- //----- Data -----//
- private RecipeItemConverter lastRecipe;
- private int gainMultiplier = 1;
- private int gainAmount = 0;
- public int getProcessTime() {
- return PROCESS_TIME;
- }
- public void setGainMultiplier(int m) {
- this.gainMultiplier = m;
- }
- public void checkAndApplyAdjacencies() {
- int x = pos.getX(); int y = pos.getY(); int z = pos.getZ();
- if(world.getBlockState(new BlockPos(x + 1, y, z)).getBlock() == ExkvaBlocks.passiveCollector) {
- this.activeTime += ExkvaBlocks.passiveCollector.getData();
- }
- if(world.getBlockState(new BlockPos(x - 1, y, z)).getBlock() == ExkvaBlocks.passiveCollector) {
- this.activeTime += ExkvaBlocks.passiveCollector.getData();
- }
- if(world.getBlockState(new BlockPos(x, y + 1, z)).getBlock() == ExkvaBlocks.passiveCollector) {
- this.activeTime += ExkvaBlocks.passiveCollector.getData();
- }
- if(world.getBlockState(new BlockPos(x, y - 1, z)).getBlock() == ExkvaBlocks.passiveCollector) {
- this.activeTime += ExkvaBlocks.passiveCollector.getData();
- }
- if(world.getBlockState(new BlockPos(x, y, z + 1)).getBlock() == ExkvaBlocks.passiveCollector) {
- this.activeTime += ExkvaBlocks.passiveCollector.getData();
- }
- if(world.getBlockState(new BlockPos(x, y, z - 1)).getBlock() == ExkvaBlocks.passiveCollector) {
- this.activeTime += ExkvaBlocks.passiveCollector.getData();
- }
- }
- //----- Logic -----//
- @Override
- public void update() {
- if(world.isRemote) {
- return;
- }
- if(!inventory.getStackInSlot(IN).isEmpty()) {
- transfer();
- }
- if(!isActive() && canActivate()) {
- this.active = true;
- world.setBlockState(pos, world.getBlockState(pos).withProperty(MachineItemConverter.ACTIVE, true));
- }
- if(lastRecipe != null) {
- ItemStack[] in = { inventory.getStackInSlot(ACTIVE) };
- if(!lastRecipe.matches(in)) {
- this.activeTime = 0;
- this.active = false;
- world.setBlockState(pos, world.getBlockState(pos).withProperty(MachineItemConverter.ACTIVE, false));
- }
- }
- if(this.isActive()) {
- checkAndApplyAdjacencies();
- this.activeTime *= this.gainMultiplier;
- if(this.activeTime == lastRecipe.getData()) {
- this.produce();
- this.activeTime = 0;
- this.active = false;
- world.setBlockState(pos, world.getBlockState(pos).withProperty(MachineItemConverter.ACTIVE, false));
- }
- } else {
- this.active = false;
- this.activeTime = 0;
- }
- markDirty();
- }
- @Override
- protected void produce() {
- if(inventory.getStackInSlot(OUT).isEmpty()) {
- inventory.setStackInSlot(OUT, getResult(inventory.getStackInSlot(ACTIVE))[0]);
- } else if(ItemHandlerHelper.canItemStacksStack(inventory.getStackInSlot(OUT), getResult(inventory.getStackInSlot(ACTIVE))[0])) {
- inventory.getStackInSlot(OUT).grow(1);
- }
- inventory.getStackInSlot(ACTIVE).shrink(1);
- }
- @Override
- protected boolean canActivate() {
- return (inventory.getStackInSlot(OUT).isEmpty() || ItemHandlerHelper.canItemStacksStack(inventory.getStackInSlot(OUT), lastRecipe.getOutput()[0])) && !inventory.getStackInSlot(ACTIVE).isEmpty();
- }
- private void transfer() {
- ItemStack in = inventory.getStackInSlot(IN);
- ItemStack ac = inventory.getStackInSlot(ACTIVE);
- if(!getResult(in)[0].isEmpty()) {
- if(ac.isEmpty()) {
- inventory.setStackInSlot(ACTIVE, in);
- inventory.setStackInSlot(IN, ItemStack.EMPTY);
- } else if(ItemHandlerHelper.canItemStacksStack(ac, in)) {
- ac.grow(in.getCount());
- inventory.setStackInSlot(IN, ItemStack.EMPTY);
- }
- }
- }
- private ItemStack[] getResult(ItemStack is) {
- ItemStack[] in = { new ItemStack(is.getItem(), 1) };
- if(lastRecipe != null) {
- if (lastRecipe.matches(in)) {
- return lastRecipe.getOutput();
- }
- }
- for(RecipeItemConverter recipe : Exkva.ITEM_CONVERTER_RECIPES) {
- if(recipe.matches(in)) {
- lastRecipe = recipe;
- return recipe.getOutput();
- }
- }
- return new ItemStack[] { ItemStack.EMPTY };
- }
- //----- Capability -----//
- // RangedWrapper TOP = new RangedWrapper(inventory, IN, IN + 1);
- // RangedWrapper LEFT = new RangedWrapper(inventory, OUT1, OUT1 + 1);
- // RangedWrapper BOTTOM = new RangedWrapper(inventory, OUT2, OUT2 + 1);
- //
- // @Override
- // public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
- //
- // if(capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
- // IItemHandler handler;
- // EnumFacing face = world.getBlockState(pos).getValue(FACING);
- //
- // if(facing == null) {
- // handler = inventory;
- // } else if(facing == face.rotateAround(EnumFacing.Axis.X).getOpposite()) {
- // handler = TOP;
- // } else if(facing == face.rotateY().getOpposite()) {
- // handler = LEFT;
- // } else if(facing == face.rotateAround(EnumFacing.Axis.X)) {
- // handler = BOTTOM;
- // } else {
- // handler = inventory;
- // }
- //
- // return CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.cast(handler);
- // }
- // return super.getCapability(capability, facing);
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement