Advertisement
Exokem

Untitled

Apr 16th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.72 KB | None | 0 0
  1. public class GuiArcFurnace extends GuiContainer {
  2.  
  3.     private static final ResourceLocation TEXTURES = new ResourceLocation(Exkva.MODID + ":textures/gui/arc_furnace.png");
  4.     private final InventoryPlayer player;
  5.     private final TileEntityArcFurnace tileentity;
  6.  
  7.     public GuiArcFurnace(InventoryPlayer player, TileEntityArcFurnace tileentity) {
  8.         super(new ContainerArcFurnace(player, tileentity));
  9.         this.player = player;
  10.         this.tileentity = tileentity;
  11.     }
  12.  
  13.     @Override
  14.     protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) {
  15.         String tileName = this.tileentity.getBlockType().getLocalizedName();
  16.         this.fontRenderer.drawString(tileName, (this.xSize / 2 - this.fontRenderer.getStringWidth(tileName) / 2) + 3, 8, 4210752);
  17.         this.fontRenderer.drawString(this.player.getDisplayName().getUnformattedText(), 8, this.ySize - 96 + 2, 4210752);
  18.     }
  19.  
  20.     @Override
  21.     public void drawScreen(int mouseX, int mouseY, float partialTicks){
  22.         this.drawDefaultBackground();
  23.         super.drawScreen(mouseX, mouseY, partialTicks);
  24.         this.renderHoveredToolTip(mouseX, mouseY);
  25.     }
  26.  
  27.     @Override
  28.     protected void drawGuiContainerBackgroundLayer(float v, int i, int i1) {
  29.         GlStateManager.color(1.0f, 1.0f, 1.0f, 1.0f);
  30.         this.mc.getTextureManager().bindTexture(TEXTURES);
  31.         this.drawTexturedModalRect(this.guiLeft, this.guiTop, 0, 0, this.xSize, this.ySize);
  32.  
  33.         int progressMax = getRefineProgressScaled();
  34.         this.drawTexturedModalRect(82, 33, 176, 14, progressMax, 17);
  35.     }
  36.  
  37.     private int getRefineProgressScaled() {
  38.         int progress = tileentity.getActiveTime();
  39.         int end = tileentity.getRefineTime();
  40.  
  41.         System.out.println(progress + " " + end);
  42.         return (progress / end) * 34;
  43.     }
  44. }
  45.  
  46. public class TileEntityArcFurnace extends TileEntity implements ITickable {
  47.  
  48.     private static final int IN1  = 0;
  49.     private static final int IN2  = 1;
  50.     private static final int IN3  = 2;
  51.     private static final int FUEL = 3;
  52.     private static final int OUT  = 4;
  53.  
  54.     protected final ItemStackHandler inventory = new ItemStackHandler(5) {
  55.         public ItemStack insertItem(int index, ItemStack stack, boolean simulate) {
  56.             if(isItemValid(index, stack)) {
  57.                 return super.insertItem(index, stack, simulate);
  58.             }
  59.             return stack;
  60.         }
  61.         public boolean isItemValid(int index, ItemStack stack) {
  62.             if(index == 4) {
  63.                 return false;
  64.             } else if(index != 3) {
  65.                 return true;
  66.             } else {
  67.                 return TileEntityFurnace.isItemFuel(stack);
  68.             }
  69.         }
  70.     };
  71.  
  72.     private boolean active;
  73.  
  74.     private int activeTime;
  75.  
  76.     @Override
  77.     public NBTTagCompound getUpdateTag() {
  78.         NBTTagCompound tagCompound = new NBTTagCompound();
  79.         tagCompound.setInteger("time_active", activeTime);
  80.         return tagCompound;
  81.     }
  82.  
  83.     @Override
  84.     public void handleUpdateTag(NBTTagCompound tag) {
  85.         this.activeTime = tag.getInteger("time_active");
  86.     }
  87.  
  88.     @Override
  89.     public void update() {
  90.  
  91.         if(world.isRemote) { return; }
  92.  
  93.         if(this.canRefine() && !this.isActive()) {
  94.             this.activeTime = 0;
  95.             this.active = true;
  96.             world.setBlockState(pos, world.getBlockState(pos).withProperty(MachineArcFurnace.ACTIVE, true));
  97.         }
  98.  
  99.         if(inventory.getStackInSlot(IN1).isEmpty() && inventory.getStackInSlot(IN2).isEmpty() && inventory.getStackInSlot(IN3).isEmpty()) {
  100.             this.activeTime = 0;
  101.             this.active = false;
  102.             world.setBlockState(pos, world.getBlockState(pos).withProperty(MachineArcFurnace.ACTIVE, false));
  103.         }
  104.  
  105.         if(this.isActive()) {
  106.             this.activeTime++;
  107.             if(this.activeTime == this.getRefineTime()) {
  108.                 this.refineItem();
  109.                 this.activeTime = 0;
  110.                 this.active = false;
  111.                 world.setBlockState(pos, world.getBlockState(pos).withProperty(MachineArcFurnace.ACTIVE, false));
  112.             }
  113.         } else {
  114.             this.active = false;
  115.             this.activeTime = 0;
  116.         }
  117.  
  118.         markDirty();
  119.     }
  120.  
  121.     public void refineItem() {
  122.         ItemStack[] in = { inventory.getStackInSlot(IN1), inventory.getStackInSlot(IN2), inventory.getStackInSlot(IN3) };
  123.         ItemStack out = getResult();
  124.  
  125.         ItemStack currentOut = inventory.getStackInSlot(OUT);
  126.         if(currentOut.isEmpty()) {
  127.             inventory.setStackInSlot(OUT, out);
  128.         } else if(ItemHandlerHelper.canItemStacksStack(currentOut, out)) {
  129.             currentOut.grow(out.getCount());
  130.         }
  131.  
  132.         for(ItemStack i : in) {
  133.             i.shrink(1);
  134.         }
  135.  
  136.         markDirty();
  137.     }
  138.  
  139.     @Override
  140.     public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newState) {
  141.         return (oldState.getBlock() != newState.getBlock());
  142.     }
  143.  
  144.     public int getActiveTime() {
  145.         return this.activeTime;
  146.     }
  147.  
  148.     public int getRefineTime() {
  149.         return 200;
  150.     }
  151.  
  152.     public ItemStackHandler getInventory() {
  153.         return inventory;
  154.     }
  155.  
  156.     public boolean isActive() {
  157.         return active;
  158.     }
  159.  
  160.     protected boolean canRefine() {
  161.         return inventory.getStackInSlot(OUT).isEmpty() && !this.getResult().isEmpty();
  162.     }
  163.  
  164.     @Override
  165.     public boolean hasCapability(Capability<?> capability, @Nullable EnumFacing facing) {
  166.         if(capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) { return true; }
  167.         return super.hasCapability(capability, facing);
  168.     }
  169.  
  170.     private ItemStack getResult() {
  171.  
  172.         ItemStack[] in = { inventory.getStackInSlot(IN1), inventory.getStackInSlot(IN2), inventory.getStackInSlot(IN3) };
  173.         for(RecipeArcFurnace recipe : Exkva.ARC_FURNACE_RECIPES) {
  174.             if(recipe.matches(in)) {
  175.                 return (recipe.getOutput().copy());
  176.             }
  177.         }
  178.  
  179.         return ItemStack.EMPTY;
  180.     }
  181.  
  182.     @Override
  183.     public void readFromNBT(NBTTagCompound compound) {
  184.         super.readFromNBT(compound);
  185.         inventory.deserializeNBT(compound.getCompoundTag("inv"));
  186.         activeTime = compound.getInteger("active_time");
  187.     }
  188.  
  189.     @Override
  190.     public NBTTagCompound writeToNBT(NBTTagCompound compound) {
  191.         compound = super.writeToNBT(compound);
  192.         compound.setTag("inv", inventory.serializeNBT());
  193.         compound.setInteger("active", activeTime);
  194.         return compound;
  195.     }
  196.  
  197.     @Override
  198.     public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
  199.         return super.getCapability(capability, facing);
  200.     }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement