Guest User

Untitled

a guest
Mar 25th, 2020
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.85 KB | None | 0 0
  1. package com.aiko.cenamicolmod.te;
  2.  
  3. import com.aiko.cenamicolmod.recipes.RecipesAnvil;
  4. import net.minecraft.entity.player.EntityPlayer;
  5. import net.minecraft.entity.player.InventoryPlayer;
  6. import net.minecraft.init.Items;
  7. import net.minecraft.inventory.Container;
  8. import net.minecraft.inventory.ItemStackHelper;
  9. import net.minecraft.item.ItemStack;
  10. import net.minecraft.nbt.NBTTagCompound;
  11. import net.minecraft.tileentity.TileEntityLockable;
  12. import net.minecraft.util.ITickable;
  13. import net.minecraft.util.NonNullList;
  14. import net.minecraftforge.oredict.OreDictionary;
  15.  
  16. public class TileEntityAnvil extends TileEntityLockable implements ITickable {
  17.  
  18.     private NonNullList<ItemStack> stacks = NonNullList.withSize(5, ItemStack.EMPTY);
  19.     private String customName;
  20.     private int timePassed = 0;
  21.     private int burningTimeLeft = 0;
  22.  
  23.     @Override
  24.     public void readFromNBT(NBTTagCompound compound) {
  25.         super.readFromNBT(compound);
  26.         this.stacks = NonNullList.<ItemStack>withSize(this.getSizeInventory(), ItemStack.EMPTY);
  27.         ItemStackHelper.loadAllItems(compound, this.stacks);
  28.  
  29.         if (compound.hasKey("CustomName", 8)) {
  30.             this.customName = compound.getString("CustomName");
  31.         }
  32.         this.burningTimeLeft = compound.getInteger("burningTimeLeft");
  33.         this.timePassed = compound.getInteger("timePassed");
  34.     }
  35.  
  36.     @Override
  37.     public NBTTagCompound writeToNBT(NBTTagCompound compound) {
  38.         super.writeToNBT(compound);
  39.         ItemStackHelper.saveAllItems(compound, this.stacks);
  40.  
  41.         if (this.hasCustomName()) {
  42.             compound.setString("CustomName", this.customName);
  43.         }
  44.  
  45.         compound.setInteger("burningTimeLeft", this.burningTimeLeft);
  46.         compound.setInteger("timePassed", this.timePassed);
  47.  
  48.         return compound;
  49.     }
  50.  
  51.     @Override
  52.     public boolean hasCustomName() {
  53.         return this.customName != null && !this.customName.isEmpty();
  54.     }
  55.  
  56.     @Override
  57.     public String getName() {
  58.         return hasCustomName() ? this.customName : "tile.custom_furnace";
  59.     }
  60.  
  61.     public void setCustomName(String name) {
  62.         this.customName = name;
  63.     }
  64.  
  65.     @Override
  66.     public int getField(int id) {
  67.         switch (id) {
  68.             case 0:
  69.                 return this.burningTimeLeft;
  70.             case 1:
  71.                 return this.timePassed;
  72.         }
  73.         return 0;
  74.     }
  75.  
  76.     @Override
  77.     public void setField(int id, int value) {
  78.         switch (id) {
  79.             case 0:
  80.                 this.burningTimeLeft = value;
  81.                 break;
  82.             case 1:
  83.                 this.timePassed = value;
  84.         }
  85.     }
  86.  
  87.     @Override
  88.     public int getFieldCount() {
  89.         return 2;
  90.     }
  91.  
  92.     @Override
  93.     public int getSizeInventory() {
  94.         return this.stacks.size();
  95.     }
  96.  
  97.     @Override
  98.     public ItemStack getStackInSlot(int index) {
  99.         return this.stacks.get(index);
  100.     }
  101.  
  102.     @Override
  103.     public ItemStack decrStackSize(int index, int count) {
  104.         return ItemStackHelper.getAndSplit(this.stacks, index, count);
  105.     }
  106.  
  107.     @Override
  108.     public ItemStack removeStackFromSlot(int index) {
  109.         return ItemStackHelper.getAndRemove(stacks, index);
  110.     }
  111.  
  112.     @Override
  113.     public void setInventorySlotContents(int index, ItemStack stack) {
  114.         this.stacks.set(index, stack);
  115.  
  116.         if (stack.getCount() > this.getInventoryStackLimit()) {
  117.             stack.setCount(this.getInventoryStackLimit());
  118.         }
  119.     }
  120.  
  121.     @Override
  122.     public int getInventoryStackLimit() {
  123.         return 64;
  124.     }
  125.  
  126.     @Override
  127.     public boolean isEmpty() {
  128.         for(ItemStack stack : this.stacks) {
  129.             if (!stack.isEmpty()) {
  130.                 return false;
  131.             }
  132.         }
  133.         return true;
  134.     }
  135.  
  136.     @Override
  137.     public void clear() {
  138.         for(int i = 0; i < this.stacks.size(); i++) {
  139.             this.stacks.set(i, ItemStack.EMPTY);
  140.         }
  141.     }
  142.  
  143.     @Override
  144.     public void openInventory(EntityPlayer player) {}
  145.  
  146.     @Override
  147.     public void closeInventory(EntityPlayer player) {}
  148.  
  149.     @Override
  150.     public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn) {
  151.         return null;
  152.     }
  153.  
  154.     @Override
  155.     public String getGuiID() {
  156.         return null;
  157.     }
  158.  
  159.     @Override
  160.     public boolean isItemValidForSlot(int index, ItemStack stack) {
  161.         // Le slot 3 je n'autorise que les planches de bois
  162.         if (index == 2)
  163.             return OreDictionary.getOres("plankWood").contains(
  164.                     new ItemStack(stack.getItem(), 1,
  165.                             OreDictionary.WILDCARD_VALUE));
  166.         // Le slot 4 je n'autorise que le blé
  167.         if (index == 3)
  168.             return stack.getItem() == Items.WHEAT;
  169.         // Le slot 5 (celui du résultat) je n'autorise rien
  170.         if (index == 4)
  171.             return false;
  172.         // Sinon pour les slots 1 et 2 on met ce qu'on veut
  173.         return true;
  174.     }
  175.  
  176.     /** Vérifie la distance entre le joueur et le bloc et que le bloc soit toujours présent */
  177.     public boolean isUsableByPlayer(EntityPlayer player) {
  178.         return this.world.getTileEntity(this.pos) != this ? false : player.getDistanceSq((double) this.pos.getX() + 0.5D, (double) this.pos.getY() + 0.5D, (double) this.pos.getZ() + 0.5D) <= 64.0D;
  179.     }
  180.  
  181.     public boolean hasFuelEmpty() {
  182.         return this.getStackInSlot(2).isEmpty() || this.getStackInSlot(3).isEmpty();
  183.     }
  184.  
  185.     public ItemStack getRecipeResult() {
  186.         return RecipesAnvil.getRecipeResult(new ItemStack[] {
  187.                 this.getStackInSlot(0), this.getStackInSlot(1) });
  188.     }
  189.  
  190.     public boolean canSmelt() {
  191.         // On récupère le résultat de la recette
  192.         ItemStack result = this.getRecipeResult();
  193.  
  194.         // Le résultat est null si il n'y a pas de recette associée, donc on retourne faux
  195.         if (result != null) {
  196.  
  197.             // On récupère le contenu du slot de résultat
  198.             ItemStack slot4 = this.getStackInSlot(4);
  199.  
  200.             // Si il est vide on renvoie vrai
  201.             if (slot4.isEmpty())
  202.                 return true;
  203.  
  204.             // Sinon on vérifie que ce soit le même objet, les même métadata et que la taille finale ne sera pas trop grande
  205.             if (slot4.getItem() == result.getItem() && slot4.getItemDamage() == result.getItemDamage()) {
  206.                 int newStackSize = slot4.getCount() + result.getCount();
  207.                 if (newStackSize <= this.getInventoryStackLimit() && newStackSize <= slot4.getMaxStackSize()) {
  208.                     return true;
  209.                 }
  210.             }
  211.         }
  212.         return false;
  213.     }
  214.  
  215.     public void smelt() {
  216.         // Cette fonction n'est appelée que si result != null, c'est pourquoi on ne fait pas de null check
  217.         ItemStack result = this.getRecipeResult();
  218.         // On enlève un item de chaque ingrédient
  219.         this.decrStackSize(0, 1);
  220.         this.decrStackSize(1, 1);
  221.         // On récupère le slot de résultat
  222.         ItemStack stack4 = this.getStackInSlot(4);
  223.         // Si il est vide
  224.         if (stack4.isEmpty()) {
  225.             // On y insère une copie du résultat
  226.             this.setInventorySlotContents(4, result.copy());
  227.         } else {
  228.             // Sinon on augmente le nombre d'objets de l'ItemStack
  229.             stack4.setCount(stack4.getCount() + result.getCount());
  230.         }
  231.     }
  232.  
  233.     /** Temps de cuisson de la recette */
  234.     public int getFullRecipeTime() {
  235.         return 200;
  236.     }
  237.  
  238.     /** Temps que dure 1 unité de carburant (ici : 1 planche + 1 blé) */
  239.     public int getFullBurnTime() {
  240.         return 300;
  241.     }
  242.  
  243.     /** Renvoie vrai si le feu est allumé */
  244.     public boolean isBurning() {
  245.         return burningTimeLeft > 0;
  246.     }
  247.  
  248.     @Override
  249.     public void update() {
  250.         if (!this.world.isRemote) {
  251.  
  252.             /* Si le carburant brûle, on réduit réduit le temps restant */
  253.             if (this.isBurning()) {
  254.                 this.burningTimeLeft--;
  255.             }
  256.  
  257.             /*
  258.              * Si la on peut faire cuire la recette et que le four ne cuit pas
  259.              * alors qu'il peut, alors on le met en route
  260.              */
  261.             if (!this.isBurning() && this.canSmelt() && !this.hasFuelEmpty()) {
  262.                 this.burningTimeLeft = this.getFullBurnTime();
  263.                 this.decrStackSize(2, 1);
  264.                 this.decrStackSize(3, 1);
  265.             }
  266.  
  267.             /* Si on peut faire cuire la recette et que le feu cuit */
  268.             if (this.isBurning() && this.canSmelt()) {
  269.                 this.timePassed++;
  270.                 if (timePassed >= this.getFullRecipeTime()) {
  271.                     timePassed = 0;
  272.                     this.smelt();
  273.                 }
  274.             } else {
  275.                 timePassed = 0;
  276.             }
  277.             this.markDirty();
  278.         }
  279.     }
  280.  
  281. }
Advertisement
Add Comment
Please, Sign In to add comment