Advertisement
Guest User

Untitled

a guest
Apr 24th, 2020
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.63 KB | None | 0 0
  1. package com.ben.heaven.init;
  2.  
  3. import com.ben.heaven.blocks.MachineBuilder;
  4.  
  5. import net.minecraft.block.Block;
  6. import net.minecraft.block.material.Material;
  7. import net.minecraft.client.renderer.block.model.ModelResourceLocation;
  8. import net.minecraft.item.Item;
  9. import net.minecraftforge.client.event.ModelRegistryEvent;
  10. import net.minecraftforge.client.model.ModelLoader;
  11. import net.minecraftforge.event.RegistryEvent;
  12. import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
  13.  
  14. public class MachineBlock {
  15.  
  16. public static Block machine_builder;
  17.  
  18. public static void init()
  19. {
  20. machine_builder = new MachineBuilder("machine_builder", Material.ROCK, 2);
  21. }
  22.  
  23. @SubscribeEvent
  24. public static void registerBlocks(RegistryEvent.Register<Block> event)
  25. {
  26. event.getRegistry().registerAll(machine_builder);
  27. }
  28.  
  29. @SubscribeEvent
  30. public static void registerRenders(ModelRegistryEvent event)
  31. {
  32. //blocks
  33. registerRender(Item.getItemFromBlock(machine_builder));
  34. }
  35.  
  36. private static void registerRender(Item item)
  37. {
  38. ModelLoader.setCustomModelResourceLocation(item, 0, new ModelResourceLocation(item.getRegistryName(), "inventory"));
  39. }
  40. }
  41.  
  42.  
  43. package com.ben.heaven.tileentity;
  44.  
  45. import com.ben.heaven.init.MachineBuilderRecipes;
  46.  
  47. import net.minecraft.item.ItemStack;
  48. import net.minecraft.nbt.NBTTagCompound;
  49. import net.minecraft.nbt.NBTTagList;
  50.  
  51. public class TileEntityMachineBuilder
  52. {
  53. private ItemStack[] contents = new ItemStack[4];
  54. private int workingTime = 0;
  55. private int workingTimeNeeded = 200;
  56.  
  57. @Override
  58. public void writeToNBT(NBTTagCompound compound)
  59. {
  60. super.writeToNBT(compound);
  61. NBTTagList nbttaglist = new NBTTagList();
  62.  
  63. for (int i = 0; i < this.contents.length; ++i) //pour les slots
  64. {
  65. if (this.contents[i] != null)
  66. {
  67. NBTTagCompound nbttagcompound1 = new NBTTagCompound();
  68. nbttagcompound1.setByte("Slot", (byte)i);
  69. this.contents[i].writeToNBT(nbttagcompound1);
  70. nbttaglist.appendTag(nbttagcompound1);
  71. }
  72. }
  73.  
  74. compound.setTag("Items", nbttaglist);
  75. compound.setShort("workingTime",(short)this.workingTime);
  76. compound.setShort("workingTimeNeeded", (short)this.workingTimeNeeded);
  77. }
  78.  
  79. @Override
  80. public void readFromNBT(NBTTagCompound compound)
  81. {
  82. super.readFromNBT(compound);
  83.  
  84. NBTTagList nbttaglist = compound.getTagList("Items", 10);
  85. this.contents = new ItemStack[this.getSizeInventory()];
  86.  
  87. for (int i = 0; i < nbttaglist.tagCount(); ++i)
  88. {
  89. NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
  90. int j = nbttagcompound1.getByte("Slot") & 255;
  91.  
  92. if (j >= 0 && j < this.contents.length)
  93. {
  94. this.contents[j] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
  95. }
  96. }
  97.  
  98. this.workingTime = compound.getShort("workingTime");*
  99. this.workingTimeNeeded = compound.getShort("workingTimeNeeded");
  100. }
  101.  
  102. @Override
  103. public int getSizeInventory()
  104. {
  105. return this.contents.length;
  106. }
  107.  
  108. @Override
  109. public ItemStack getStackInSlot(int slotIndex) {
  110. return this.contents[slotIndex];
  111. }
  112.  
  113. @Override
  114. public ItemStack decrStackSize(int slotIndex, int amount) {
  115. if (this.contents[slotIndex] != null)
  116. {
  117. ItemStack itemstack;
  118.  
  119. if (this.contents[slotIndex].stackSize <= amount)
  120. {
  121. itemstack = this.contents[slotIndex];
  122. this.contents[slotIndex] = null;
  123. this.markDirty();
  124. return itemstack;
  125. }
  126. else
  127. {
  128. itemstack = this.contents[slotIndex].splitStack(amount);
  129.  
  130. }
  131. }
  132. }
  133.  
  134. public boolean isBurning()
  135. {
  136. return this.workingTime > 0;
  137. }
  138.  
  139. private boolean canSmelt()
  140. {
  141. if (this.contents[0] == null || this.contents[1] == null || this.contents[2] == null) //Si les trois premiers slots sont vides
  142. {
  143. return false; //On ne peut pas lancer le processus
  144. }
  145. else
  146. {
  147. ItemStack itemstack = MachineBuilderRecipes.smelting().getSmeltingResult(new ItemStack[]{this.contents[0], this.contents[1], this.contents[2]}); //Il y a une erreur ici, c'est normal, on y vient après (c'est pour les recettes)
  148. if (itemstack == null) return false; //rapport avec les recettes
  149. if (this.contents[3] == null) return true; //vérifications du slot d'output
  150. if (!this.contents[3].isItemEqual(itemstack)) return false; //ici aussi
  151. int result = contents[3].stackSize + itemstack.stackSize;
  152. return result <= getInventoryStackLimit() && result <= this.contents[3].getMaxStackSize(); //Et là aussi décidément
  153.  
  154. }
  155.  
  156. }
  157.  
  158. public void updateEntity() //Méthode exécutée à chaque tick
  159. {
  160. if(this.isBurning() && this.canSmelt()) //Si on "cuit" et que notre recette et toujours bonne, on continue
  161. {
  162. ++this.workingTime; //incrémentation
  163. }
  164. if(this.canSmelt() && !this.isBurning()) //Si la recette est bonne mais qu'elle n'est toujours pas lancée, on la lance
  165. {
  166. this.workingTime = 1; //La méthode isBurning() renverra true maintenant (1>0)
  167. }
  168. if(this.canSmelt() && this.workingTime == this.workingTimeNeeded) //Si on est arrivé au bout du temps de cuisson et que la recette est toujours bonne
  169. {
  170. this.smeltItem(); //on "cuit" les items
  171. this.workingTime = 0; //et on réinitialise le temps de cuisson
  172. }
  173. if(!this.canSmelt()) //Si la recette la recette n'est plus bonne
  174. {
  175. this.workingTime= 0; //le temps de cuisson est de 0
  176. }
  177. }
  178.  
  179. public void smeltItem()
  180. {
  181. if (this.canSmelt())
  182. {
  183. ItemStack itemstack = MachineBuilderRecipes.smelting().getSmeltingResult(new ItemStack[]{this.contents[0], this.contents[1], this.contents[2]}); //On récupère l'output de la recette
  184. if (this.contents[3] == null) //Si il y a rien dans le slot d'output
  185. {
  186. this.contents[3] = itemstack.copy(); //On met directement l'ItemStack
  187. }
  188. else if (this.contents[3].getItem() == itemstack.getItem()) //Et si l'item que l'on veut est le même que celui qu'il y a déjà
  189. {
  190. this.contents[3].stackSize += itemstack.stackSize; // Alors ont incrémente l'ItemStack
  191. }
  192.  
  193. --this.contents[0].stackSize; //On décrémente les slots d'input
  194. --this.contents[1].stackSize;
  195. --this.contents[2].stackSize;
  196.  
  197. if (this.contents[0].stackSize <= 0) //Si les slots sont vides, on remet à null le slot
  198. {
  199. this.contents[0] = null;
  200. }
  201. if (this.contents[1].stackSize <= 0)
  202. {
  203. this.contents[1] = null;
  204. }
  205. if (this.contents[2].stackSize <= 0)
  206. {
  207. this.contents[2] = null;
  208. }
  209. }
  210. }
  211. }
  212.  
  213. package com.ben.heaven.blocks;
  214.  
  215. import com.ben.heaven.tileentity.TileEntityMachineBuilder;
  216.  
  217. import net.minecraft.block.Block;
  218. import net.minecraft.block.BlockContainer;
  219. import net.minecraft.block.material.Material;
  220. import net.minecraft.entity.item.EntityItem;
  221. import net.minecraft.entity.player.EntityPlayer;
  222. import net.minecraft.inventory.IInventory;
  223. import net.minecraft.item.ItemStack;
  224. import net.minecraft.nbt.NBTTagCompound;
  225. import net.minecraft.tileentity.TileEntity;
  226. import net.minecraft.world.World;
  227.  
  228. public class MachineBuilder extends BlockContainer
  229. {
  230. public MachineBuilder(String name, Material materialIn, int level)
  231. {
  232. super(materialIn);
  233. this.setResistance(8.0F);
  234. this.setHarvestLevel("pickaxe", level);
  235. setUnlocalizedName(name);
  236. setRegistryName(name);
  237. }
  238.  
  239. public void breakBlock(World world, int x, int y, int z, Block block, int metadata)
  240. {
  241. TileEntity tileentity = world.getTileEntity(x, y, z);
  242.  
  243. if (tileentity instanceof IInventory)
  244. {
  245. IInventory inv = (IInventory)tileentity;
  246. for (int i1 = 0; i1 < inv.getSizeInventory(); ++i1)
  247. {
  248. ItemStack itemstack = inv.getStackInSlot(i1);
  249.  
  250. if (itemstack != null)
  251. {
  252. float f = world.rand.nextFloat() * 0.8F + 0.1F;
  253. float f1 = world.rand.nextFloat() * 0.8F + 0.1F;
  254. EntityItem entityitem;
  255.  
  256. for (float f2 = world.rand.nextFloat() * 0.8F + 0.1F; itemstack.stackSize > 0; world.spawnEntityInWorld(entityitem))
  257. {
  258. int j1 = world.rand.nextInt(21) + 10;
  259.  
  260. if (j1 > itemstack.stackSize)
  261. {
  262. j1 = itemstack.stackSize;
  263. }
  264.  
  265. itemstack.stackSize -= j1;
  266. entityitem = new EntityItem(world, (double)((float)x + f), (double)((float)y + f1), (double)((float)z + f2), new ItemStack(itemstack.getItem(), j1, itemstack.getItemDamage()));
  267. float f3 = 0.05F;
  268. entityitem.motionX = (double)((float)world.rand.nextGaussian() * f3);
  269. entityitem.motionY = (double)((float)world.rand.nextGaussian() * f3 + 0.2F);
  270. entityitem.motionZ = (double)((float)world.rand.nextGaussian() * f3);
  271.  
  272. if (itemstack.hasTagCompound())
  273. {
  274. entityitem.getEntityItem().setTagCompound((NBTTagCompound)itemstack.getTagCompound().copy());
  275. }
  276. }
  277. }
  278. }
  279.  
  280. @Override
  281. public TileEntity createNewTileEntity(World world, int metadata)
  282. {
  283. return new TileEntityMachineBuilder();
  284. }
  285.  
  286. public boolean hasTileEntity(int metadata)
  287. {
  288. return true;
  289. }
  290.  
  291. public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float hitx, float hity, float hitz)
  292. {
  293. if (world.isRemote)
  294. {
  295. return true;
  296. }
  297. else
  298. {
  299. player.openGui(Heaven.instance, 0, world, x, y, z);
  300. return true;
  301. }
  302. }
  303. }
  304.  
  305. package com.ben.heaven.container;
  306.  
  307. import com.ben.heaven.tileentity.TileEntityMachineBuilder;
  308.  
  309. import net.minecraft.entity.player.EntityPlayer;
  310. import net.minecraft.entity.player.InventoryPlayer;
  311. import net.minecraft.inventory.Container;
  312. import net.minecraft.inventory.Slot;
  313. import net.minecraft.item.ItemStack;
  314.  
  315. public class ContainerMachineBuilder extends Container
  316. {
  317. private TileEntityMachineBuilder tileMachineTuto;
  318.  
  319. public ContainerMachineBuilder(TileEntityMachineBuilder tile, InventoryPlayer inventory)
  320.  
  321. {
  322.  
  323. this.tileMachineTuto = tile;
  324.  
  325. this.addSlotToContainer(new Slot(tile, 0, 49, 75)); //Lancez votre jeu en debug pour calibrer vos slots
  326.  
  327. this.addSlotToContainer(new Slot(tile, 1, 89, 75));
  328.  
  329. this.addSlotToContainer(new Slot(tile, 2, 129, 75));
  330.  
  331. this.addSlotToContainer(new SlotResult(tile, 3, 89, 135)); //Ici c'est un slot que j'ai créer, on le fera après
  332.  
  333. this.bindPlayerInventory(inventory); //Les containers ont été vus dans un tutoriel de robin, merci de d'y référer
  334.  
  335. }
  336.  
  337. @Override
  338. public boolean canInteractWith(EntityPlayer player) {
  339. return this.tileMachineTuto.isUseableByPlayer(player);
  340. }
  341.  
  342. private void bindPlayerInventory(InventoryPlayer inventory)
  343. {
  344. int i;
  345. for (i = 0; i < 3; ++i)
  346. {
  347. for (int j = 0; j < 9; ++j)
  348. {
  349. this.addSlotToContainer(new Slot(inventory, j + i * 9 + 9, 17 + j * 18, 171 + i * 18));
  350. }
  351. }
  352.  
  353. for (i = 0; i < 9; ++i)
  354. {
  355. this.addSlotToContainer(new Slot(inventory, i, 17 + i * 18, 229));
  356. }
  357. }
  358.  
  359. public ItemStack transferStackInSlot(EntityPlayer player, int quantity)
  360. {
  361. ItemStack itemstack = null;
  362. Slot slot = (Slot)this.inventorySlots.get(quantity);
  363.  
  364. if (slot != null && slot.getHasStack())
  365. {
  366. ItemStack itemstack1 = slot.getStack();
  367. itemstack = itemstack1.copy();
  368.  
  369. if (quantity < this.tileMachineTuto.getSizeInventory())
  370. {
  371. if (!this.mergeItemStack(itemstack1, this.tileMachineTuto.getSizeInventory(), this.inventorySlots.size(), true))
  372. {
  373. return null;
  374. }
  375. }
  376. else if (!this.mergeItemStack(itemstack1, 0, this.tileMachineTuto.getSizeInventory(), false))
  377. {
  378. return null;
  379. }
  380.  
  381. if (itemstack1.stackSize == 0)
  382. {
  383. slot.putStack((ItemStack)null);
  384. }
  385. else
  386. {
  387.  
  388. slot.onSlotChanged();
  389. }
  390. }
  391.  
  392. return itemstack;
  393. }
  394.  
  395. public void onContainerClosed(EntityPlayer player)
  396. {
  397. super.onContainerClosed(player);
  398. this.tileMachineTuto.closeInventory();
  399. }
  400.  
  401. }
  402.  
  403. package com.ben.heaven;
  404.  
  405. import net.minecraft.entity.player.EntityPlayer;
  406. import net.minecraft.inventory.IInventory;
  407. import net.minecraft.inventory.Slot;
  408. import net.minecraft.item.ItemStack;
  409.  
  410. public class SlotsResult
  411. {
  412. public class SlotResult extends Slot {
  413.  
  414.  
  415. public SlotResult(IInventory inventory, int id, int x, int y)
  416. {
  417. super(inventory, id, x, y);
  418. }
  419.  
  420. @Override
  421. public boolean isItemValid(ItemStack stack) //Interdit la pose d'items dans le slot
  422. {
  423. return false;
  424. }
  425.  
  426. public ItemStack decrStackSize(int amount)
  427. {
  428. return super.decrStackSize(amount);
  429. }
  430.  
  431. public void onPickupFromSlot(EntityPlayer player, ItemStack stack)
  432. {
  433. super.onCrafting(stack);
  434. super.onPickupFromSlot(player, stack);
  435. }
  436. }
  437.  
  438.  
  439. }
  440.  
  441.  
  442. package com.ben.heaven.gui;
  443.  
  444. import org.lwjgl.opengl.GL11;
  445.  
  446. import com.ben.heaven.References;
  447. import com.ben.heaven.container.ContainerMachineBuilder;
  448. import com.ben.heaven.tileentity.TileEntityMachineBuilder;
  449.  
  450. import net.minecraft.client.gui.inventory.GuiContainer;
  451. import net.minecraft.client.resources.I18n;
  452. import net.minecraft.entity.player.InventoryPlayer;
  453. import net.minecraft.inventory.IInventory;
  454. import net.minecraft.util.ResourceLocation;
  455.  
  456. public class GuiMachineBuilder extends GuiContainer
  457. {
  458. private static final ResourceLocation texture = new ResourceLocation(References.MODID,"textures/gui/container/guiMachineTuto.png");
  459.  
  460. @SuppressWarnings("unused")
  461.  
  462. private TileEntityMachineBuilder tileMachineTuto;
  463.  
  464. private IInventory playerInv;
  465.  
  466.  
  467.  
  468. public GuiMachineBuilder(TileEntityMachineBuilder tile, InventoryPlayer inventory)
  469.  
  470. {
  471. super(new ContainerMachineBuilder(tile, inventory));
  472. this.tileMachineTuto = tile;
  473. this.playerInv = inventory;
  474. this.allowUserInput = false;
  475. this.ySize = 256;
  476. this.xSize = 256;
  477. }
  478.  
  479. @Override
  480. protected void drawGuiContainerBackgroundLayer(float partialRenderTick, int x, int y)
  481. {
  482.  
  483.  
  484. GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
  485. this.mc.getTextureManager().bindTexture(texture);
  486. int k = (this.width - this.xSize) / 2;
  487. int l = (this.height - this.ySize) / 2;
  488. this.drawTexturedModalRect(k, l, 0, 0, this.xSize, this.ySize);
  489. this.drawTexturedModalRect(0, 0, 176, 14, 100 + 1, 16);
  490.  
  491. }
  492.  
  493. protected void drawGuiContainerForegroundLayer(int x, int y)
  494. {
  495. this.fontRendererObj.drawString(this.playerInv.hasCustomInventoryName() ? this.playerInv.getInventoryName() : I18n.format(this.playerInv.getInventoryName()), 10, this.ySize - 98, 4210752);
  496. }
  497.  
  498.  
  499. }
  500.  
  501. package com.ben.heaven;
  502.  
  503. import com.ben.heaven.init.ArmorMod;
  504. import com.ben.heaven.init.ArmorModSpe;
  505. import com.ben.heaven.init.BlocksOre;
  506. import com.ben.heaven.init.DecoBlock;
  507. import com.ben.heaven.init.ItemMod;
  508. import com.ben.heaven.init.ItemModOre;
  509. import com.ben.heaven.init.SmeltingRecipesMod;
  510. import com.ben.heaven.proxy.ServerProxy;
  511. import com.ben.heaven.tileentity.TileEntityMachineBuilder;
  512. import com.ben.heaven.world.WorldGenHeaven;
  513.  
  514. import net.minecraft.block.Block;
  515. import net.minecraft.creativetab.CreativeTabs;
  516. import net.minecraft.init.Blocks;
  517. import net.minecraft.init.Items;
  518. import net.minecraft.item.Item;
  519. import net.minecraft.item.ItemStack;
  520. import net.minecraftforge.event.RegistryEvent;
  521. import net.minecraftforge.fml.common.Mod;
  522. import net.minecraftforge.fml.common.Mod.EventHandler;
  523. import net.minecraftforge.fml.common.SidedProxy;
  524. import net.minecraftforge.fml.common.event.FMLInitializationEvent;
  525. import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
  526. import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
  527. import net.minecraftforge.fml.common.registry.GameRegistry;
  528. import net.minecraftforge.fml.relauncher.Side;
  529. import net.minecraftforge.fml.relauncher.SideOnly;
  530.  
  531. @Mod(modid = References.MODID, name = References.NAME, version = References.VERSION, acceptedMinecraftVersions = References.MINECRAFT_VERSION)
  532. public class Heaven
  533. {
  534. @SidedProxy(clientSide = References.CLIENT_PROXY, serverSide = References.SERVER_PROXY, modId = References.MODID)
  535. public static ServerProxy proxy;
  536.  
  537. @EventHandler
  538. public void preinit(FMLPreInitializationEvent event)
  539. {
  540. BlocksOre.init();
  541. DecoBlock.init();
  542. ItemMod.init();
  543. ItemModOre.init();
  544. ArmorMod.init();
  545. ArmorModSpe.init();
  546. }
  547.  
  548. @EventHandler
  549. public void init(FMLInitializationEvent event)
  550. {
  551. proxy.register();
  552. SmeltingRecipesMod.init();
  553. GameRegistry.registerWorldGenerator(new WorldGenHeaven(), 0);
  554. GameRegistry.registerTileEntity(TileEntityMachineBuilder.class, "Heaven:MachineBuilderTileEntity");
  555. }
  556.  
  557. @EventHandler
  558. public void postinit(FMLPostInitializationEvent event)
  559. {
  560.  
  561. }
  562.  
  563. public static CreativeTabs tabHeavenDeco = new CreativeTabs("tabHeavenDeco")
  564. {
  565. @SideOnly(Side.CLIENT)
  566. public ItemStack getTabIconItem()
  567. {
  568. return new ItemStack(DecoBlock.lava_deco_block);
  569. }
  570.  
  571. };
  572.  
  573. public static CreativeTabs tabHeavenOres = new CreativeTabs("tabHeavenOres")
  574. {
  575. @SideOnly(Side.CLIENT)
  576. public ItemStack getTabIconItem()
  577. {
  578. return new ItemStack(BlocksOre.shiny_ore);
  579. }
  580.  
  581. };
  582.  
  583. public static CreativeTabs tabHeavenTools = new CreativeTabs("tabHeavenTools")
  584. {
  585. @SideOnly(Side.CLIENT)
  586. public ItemStack getTabIconItem()
  587. {
  588. return new ItemStack(ItemModOre.mercure_sword);
  589. }
  590.  
  591. };
  592.  
  593. public static CreativeTabs tabHeavenMaterials = new CreativeTabs("tabHeavenMaterials")
  594. {
  595. @SideOnly(Side.CLIENT)
  596. public ItemStack getTabIconItem()
  597. {
  598. return new ItemStack(ItemMod.stone_stick);
  599. }
  600. };
  601.  
  602. public static CreativeTabs tabHeavenArmor = new CreativeTabs("tabHeavenArmor")
  603. {
  604. @SideOnly(Side.CLIENT)
  605. public ItemStack getTabIconItem()
  606. {
  607. return new ItemStack(ArmorMod.mercure_chestplate);
  608. }
  609. };
  610. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement