Advertisement
Exokem

Untitled

May 12th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.80 KB | None | 0 0
  1. //Base
  2. package exokem.exkva.objects.machines;
  3.  
  4. import exokem.exkva.tools.ExkvaEnergyStorage;
  5. import net.minecraft.block.state.IBlockState;
  6. import net.minecraft.item.ItemStack;
  7. import net.minecraft.nbt.NBTTagCompound;
  8. import net.minecraft.tileentity.TileEntity;
  9. import net.minecraft.util.EnumFacing;
  10. import net.minecraft.util.ITickable;
  11. import net.minecraft.util.math.BlockPos;
  12. import net.minecraft.world.World;
  13. import net.minecraftforge.common.capabilities.Capability;
  14. import net.minecraftforge.energy.CapabilityEnergy;
  15. import net.minecraftforge.items.CapabilityItemHandler;
  16. import net.minecraftforge.items.ItemStackHandler;
  17.  
  18. import javax.annotation.Nullable;
  19.  
  20. public class TileEntityMachineBase extends TileEntity implements ITickable {
  21.  
  22.     public static int MAX_STORED_POWER;
  23.     public static int RF_PER_TICK;
  24.     public static int RF_PER_TICK_INPUT;
  25.  
  26.     protected ExkvaEnergyStorage energyStorage;
  27.     protected ItemStackHandler inventory;
  28.  
  29.     protected boolean active;
  30.     protected int activeTime;
  31.  
  32.     protected boolean hasEnergy;
  33.  
  34.     public TileEntityMachineBase(int maxPow, int rfpt, int rfin) {
  35.         MAX_STORED_POWER = maxPow;
  36.         RF_PER_TICK = rfpt;
  37.         RF_PER_TICK_INPUT = rfin;
  38.         energyStorage = new ExkvaEnergyStorage(maxPow, rfin);
  39.         hasEnergy = true;
  40.     }
  41.  
  42.     public TileEntityMachineBase() {
  43.         hasEnergy = false;
  44.     }
  45.  
  46.     //----- Access -----//
  47.  
  48.     public int getStoredEnergy() {
  49.         if(hasEnergy) {
  50.             return energyStorage.getEnergyStored();
  51.         } return 0;
  52.     }
  53.  
  54.     public int getActiveTime() {
  55.         return activeTime;
  56.     }
  57.  
  58.     public ItemStackHandler getInventory() {
  59.         return inventory;
  60.     }
  61.  
  62.     public void setStoredEnergy(int data) {
  63.         if(hasEnergy) {
  64.             this.energyStorage.setEnergy(data);
  65.         }
  66.     }
  67.  
  68.     public void setActiveTime(int data) {
  69.         this.activeTime = data;
  70.     }
  71.  
  72.     public boolean isActive() {
  73.         return active || activeTime > 0;
  74.     }
  75.  
  76.     //----- Logic -----//
  77.  
  78.     @Override
  79.     public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newState) {
  80.         return (oldState.getBlock() != newState.getBlock());
  81.     }
  82.  
  83.     @Override
  84.     public void update() {
  85.  
  86.     }
  87.  
  88.     protected ItemStack[] getResult() {
  89.         return null;
  90.     }
  91.  
  92.     protected boolean canActivate() {
  93.         return false;
  94.     }
  95.  
  96.     protected void produce() {
  97.  
  98.     }
  99.  
  100.     //----- NBT -----//
  101.  
  102.     @Override
  103.     public void readFromNBT(NBTTagCompound compound) {
  104.         super.readFromNBT(compound);
  105.         inventory.deserializeNBT(compound.getCompoundTag("inventory"));
  106.         activeTime = compound.getInteger("active_time");
  107.         active = compound.getBoolean("active");
  108.         if(hasEnergy) {
  109.             energyStorage.setEnergy(compound.getInteger("stored_energy"));
  110.         }
  111.     }
  112.  
  113.     @Override
  114.     public NBTTagCompound writeToNBT(NBTTagCompound compound) {
  115.         compound = super.writeToNBT(compound);
  116.         compound.setTag("inventory", inventory.serializeNBT());
  117.         compound.setInteger("active_time", activeTime);
  118.         compound.setBoolean("active", active);
  119.         if(hasEnergy) {
  120.             compound.setInteger("stored_energy", energyStorage.getEnergyStored());
  121.         }
  122.         return compound;
  123.     }
  124.  
  125.     //----- Capability -----//
  126.  
  127.     @Override
  128.     public boolean hasCapability(Capability<?> capability, @Nullable EnumFacing facing) {
  129.         if(capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
  130.             return true;
  131.         }
  132.         if(capability == CapabilityEnergy.ENERGY && hasEnergy) {
  133.             return true;
  134.         }
  135.         return super.hasCapability(capability, facing);
  136.     }
  137.  
  138.     @Override
  139.     public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
  140.         if(capability == CapabilityEnergy.ENERGY && hasEnergy) {
  141.             return CapabilityEnergy.ENERGY.cast(energyStorage);
  142.         }
  143.         return super.getCapability(capability, facing);
  144.     }
  145. }
  146.  
  147.  
  148. //Extension
  149. package exokem.exkva.objects.machines;
  150.  
  151. import exokem.exkva.common.Exkva;
  152. import exokem.exkva.common.ExkvaBlocks;
  153. import exokem.exkva.recipe.RecipeItemConverter;
  154. import exokem.exkva.recipe.RecipeMatterAtomizer;
  155. import net.minecraft.client.Minecraft;
  156. import net.minecraft.item.ItemStack;
  157. import net.minecraft.nbt.NBTTagCompound;
  158. import net.minecraft.tileentity.TileEntity;
  159. import net.minecraft.util.EnumFacing;
  160. import net.minecraft.util.math.BlockPos;
  161. import net.minecraftforge.common.capabilities.Capability;
  162. import net.minecraftforge.energy.CapabilityEnergy;
  163. import net.minecraftforge.items.CapabilityItemHandler;
  164. import net.minecraftforge.items.IItemHandler;
  165. import net.minecraftforge.items.ItemHandlerHelper;
  166. import net.minecraftforge.items.ItemStackHandler;
  167. import net.minecraftforge.items.wrapper.RangedWrapper;
  168.  
  169. import javax.annotation.Nullable;
  170.  
  171. import static exokem.exkva.objects.machines.MachineBase.FACING;
  172.  
  173. public class TileEntityItemConverter extends TileEntityMachineBase {
  174.  
  175.     protected final ItemStackHandler inventory = new ItemStackHandler(4) {
  176.         public ItemStack insertItem(int index, ItemStack stack, boolean simulate) {
  177.             if(isItemValid(index, stack)) {
  178.                 return super.insertItem(index, stack, simulate);
  179.             }
  180.             return stack;
  181.         }
  182.         public boolean isItemValid(int index, ItemStack stack) {
  183.             if(index == 1 || index == 3) {
  184.                 return false;
  185.             } else {
  186.                 return index >= 0;
  187.             }
  188.         }
  189.     };
  190.  
  191.     public TileEntityItemConverter() {
  192.         super();
  193.         super.inventory = this.inventory;
  194.         this.hasEnergy = false;
  195.     }
  196.  
  197.     //----- Constants -----//
  198.  
  199.     private static final int IN = 0;
  200.     private static final int ACTIVE = 1;
  201.     private static final int UPGRADE = 2;
  202.     private static final int OUT = 3;
  203.  
  204.     private static final int PROCESS_TIME = 50;
  205.  
  206.     //----- Data -----//
  207.  
  208.     private RecipeItemConverter lastRecipe;
  209.  
  210.     private int gainMultiplier = 1;
  211.     private int gainAmount = 0;
  212.  
  213.     public int getProcessTime() {
  214.         return PROCESS_TIME;
  215.     }
  216.  
  217.     public void setGainMultiplier(int m) {
  218.         this.gainMultiplier = m;
  219.     }
  220.  
  221.     public void checkAndApplyAdjacencies() {
  222.  
  223.         int x = pos.getX(); int y = pos.getY(); int z = pos.getZ();
  224.  
  225.         if(world.getBlockState(new BlockPos(x + 1, y, z)).getBlock() == ExkvaBlocks.passiveCollector) {
  226.             this.activeTime += ExkvaBlocks.passiveCollector.getData();
  227.         }
  228.         if(world.getBlockState(new BlockPos(x - 1, y, z)).getBlock() == ExkvaBlocks.passiveCollector) {
  229.             this.activeTime += ExkvaBlocks.passiveCollector.getData();
  230.         }
  231.         if(world.getBlockState(new BlockPos(x, y + 1, z)).getBlock() == ExkvaBlocks.passiveCollector) {
  232.             this.activeTime += ExkvaBlocks.passiveCollector.getData();
  233.         }
  234.         if(world.getBlockState(new BlockPos(x, y - 1, z)).getBlock() == ExkvaBlocks.passiveCollector) {
  235.             this.activeTime += ExkvaBlocks.passiveCollector.getData();
  236.         }
  237.         if(world.getBlockState(new BlockPos(x, y, z + 1)).getBlock() == ExkvaBlocks.passiveCollector) {
  238.             this.activeTime += ExkvaBlocks.passiveCollector.getData();
  239.         }
  240.         if(world.getBlockState(new BlockPos(x, y, z - 1)).getBlock() == ExkvaBlocks.passiveCollector) {
  241.             this.activeTime += ExkvaBlocks.passiveCollector.getData();
  242.         }
  243.     }
  244.  
  245.     //----- Logic -----//
  246.  
  247.     @Override
  248.     public void update() {
  249.         if(world.isRemote) {
  250.             return;
  251.         }
  252.  
  253.         if(!inventory.getStackInSlot(IN).isEmpty()) {
  254.             transfer();
  255.         }
  256.  
  257.         if(!isActive() && canActivate()) {
  258.             this.active = true;
  259.             world.setBlockState(pos, world.getBlockState(pos).withProperty(MachineItemConverter.ACTIVE, true));
  260.         }
  261.  
  262.         if(lastRecipe != null) {
  263.             ItemStack[] in = { inventory.getStackInSlot(ACTIVE) };
  264.             if(!lastRecipe.matches(in)) {
  265.                 this.activeTime = 0;
  266.                 this.active = false;
  267.                 world.setBlockState(pos, world.getBlockState(pos).withProperty(MachineItemConverter.ACTIVE, false));
  268.             }
  269.         }
  270.  
  271.         if(this.isActive()) {
  272.             checkAndApplyAdjacencies();
  273.             this.activeTime *= this.gainMultiplier;
  274.             if(this.activeTime == lastRecipe.getData()) {
  275.                 this.produce();
  276.                 this.activeTime = 0;
  277.                 this.active = false;
  278.                 world.setBlockState(pos, world.getBlockState(pos).withProperty(MachineItemConverter.ACTIVE, false));
  279.             }
  280.         } else {
  281.             this.active = false;
  282.             this.activeTime = 0;
  283.         }
  284.  
  285.         markDirty();
  286.     }
  287.  
  288.     @Override
  289.     protected void produce() {
  290.  
  291.         if(inventory.getStackInSlot(OUT).isEmpty()) {
  292.             inventory.setStackInSlot(OUT, getResult(inventory.getStackInSlot(ACTIVE))[0]);
  293.         } else if(ItemHandlerHelper.canItemStacksStack(inventory.getStackInSlot(OUT), getResult(inventory.getStackInSlot(ACTIVE))[0])) {
  294.             inventory.getStackInSlot(OUT).grow(1);
  295.         }
  296.  
  297.         inventory.getStackInSlot(ACTIVE).shrink(1);
  298.     }
  299.  
  300.     @Override
  301.     protected boolean canActivate() {
  302.  
  303.         return (inventory.getStackInSlot(OUT).isEmpty() || ItemHandlerHelper.canItemStacksStack(inventory.getStackInSlot(OUT), lastRecipe.getOutput()[0])) && !inventory.getStackInSlot(ACTIVE).isEmpty();
  304.     }
  305.  
  306.     private void transfer() {
  307.  
  308.         ItemStack in = inventory.getStackInSlot(IN);
  309.         ItemStack ac = inventory.getStackInSlot(ACTIVE);
  310.  
  311.         if(!getResult(in)[0].isEmpty()) {
  312.             if(ac.isEmpty()) {
  313.                 inventory.setStackInSlot(ACTIVE, in);
  314.                 inventory.setStackInSlot(IN, ItemStack.EMPTY);
  315.             } else if(ItemHandlerHelper.canItemStacksStack(ac, in)) {
  316.                 ac.grow(in.getCount());
  317.                 inventory.setStackInSlot(IN, ItemStack.EMPTY);
  318.             }
  319.         }
  320.     }
  321.  
  322.     private ItemStack[] getResult(ItemStack is) {
  323.         ItemStack[] in = { new ItemStack(is.getItem(), 1) };
  324.  
  325.         if(lastRecipe != null) {
  326.             if (lastRecipe.matches(in)) {
  327.                 return lastRecipe.getOutput();
  328.             }
  329.         }
  330.  
  331.         for(RecipeItemConverter recipe : Exkva.ITEM_CONVERTER_RECIPES) {
  332.             if(recipe.matches(in)) {
  333.                 lastRecipe = recipe;
  334.                 return recipe.getOutput();
  335.             }
  336.         }
  337.  
  338.         return new ItemStack[] { ItemStack.EMPTY };
  339.     }
  340.  
  341.     //----- Capability -----//
  342.  
  343. //    RangedWrapper TOP = new RangedWrapper(inventory, IN, IN + 1);
  344. //    RangedWrapper LEFT = new RangedWrapper(inventory, OUT1, OUT1 + 1);
  345. //    RangedWrapper BOTTOM = new RangedWrapper(inventory, OUT2, OUT2 + 1);
  346. //
  347. //    @Override
  348. //    public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
  349. //
  350. //        if(capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
  351. //            IItemHandler handler;
  352. //            EnumFacing face = world.getBlockState(pos).getValue(FACING);
  353. //
  354. //            if(facing == null) {
  355. //                handler = inventory;
  356. //            } else if(facing == face.rotateAround(EnumFacing.Axis.X).getOpposite()) {
  357. //                handler = TOP;
  358. //            } else if(facing == face.rotateY().getOpposite()) {
  359. //                handler = LEFT;
  360. //            } else if(facing == face.rotateAround(EnumFacing.Axis.X)) {
  361. //                handler = BOTTOM;
  362. //            } else {
  363. //                handler = inventory;
  364. //            }
  365. //
  366. //            return CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.cast(handler);
  367. //        }
  368. //        return super.getCapability(capability, facing);
  369. //    }
  370. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement