Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ben.heaven.init;
- import com.ben.heaven.blocks.MachineBuilder;
- import net.minecraft.block.Block;
- import net.minecraft.block.material.Material;
- import net.minecraft.client.renderer.block.model.ModelResourceLocation;
- import net.minecraft.item.Item;
- import net.minecraftforge.client.event.ModelRegistryEvent;
- import net.minecraftforge.client.model.ModelLoader;
- import net.minecraftforge.event.RegistryEvent;
- import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
- public class MachineBlock {
- public static Block machine_builder;
- public static void init()
- {
- machine_builder = new MachineBuilder("machine_builder", Material.ROCK, 2);
- }
- @SubscribeEvent
- public static void registerBlocks(RegistryEvent.Register<Block> event)
- {
- event.getRegistry().registerAll(machine_builder);
- }
- @SubscribeEvent
- public static void registerRenders(ModelRegistryEvent event)
- {
- //blocks
- registerRender(Item.getItemFromBlock(machine_builder));
- }
- private static void registerRender(Item item)
- {
- ModelLoader.setCustomModelResourceLocation(item, 0, new ModelResourceLocation(item.getRegistryName(), "inventory"));
- }
- }
- package com.ben.heaven.tileentity;
- import com.ben.heaven.init.MachineBuilderRecipes;
- import net.minecraft.item.ItemStack;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.nbt.NBTTagList;
- public class TileEntityMachineBuilder
- {
- private ItemStack[] contents = new ItemStack[4];
- private int workingTime = 0;
- private int workingTimeNeeded = 200;
- @Override
- public void writeToNBT(NBTTagCompound compound)
- {
- super.writeToNBT(compound);
- NBTTagList nbttaglist = new NBTTagList();
- for (int i = 0; i < this.contents.length; ++i) //pour les slots
- {
- if (this.contents[i] != null)
- {
- NBTTagCompound nbttagcompound1 = new NBTTagCompound();
- nbttagcompound1.setByte("Slot", (byte)i);
- this.contents[i].writeToNBT(nbttagcompound1);
- nbttaglist.appendTag(nbttagcompound1);
- }
- }
- compound.setTag("Items", nbttaglist);
- compound.setShort("workingTime",(short)this.workingTime);
- compound.setShort("workingTimeNeeded", (short)this.workingTimeNeeded);
- }
- @Override
- public void readFromNBT(NBTTagCompound compound)
- {
- super.readFromNBT(compound);
- NBTTagList nbttaglist = compound.getTagList("Items", 10);
- this.contents = new ItemStack[this.getSizeInventory()];
- for (int i = 0; i < nbttaglist.tagCount(); ++i)
- {
- NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
- int j = nbttagcompound1.getByte("Slot") & 255;
- if (j >= 0 && j < this.contents.length)
- {
- this.contents[j] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
- }
- }
- this.workingTime = compound.getShort("workingTime");*
- this.workingTimeNeeded = compound.getShort("workingTimeNeeded");
- }
- @Override
- public int getSizeInventory()
- {
- return this.contents.length;
- }
- @Override
- public ItemStack getStackInSlot(int slotIndex) {
- return this.contents[slotIndex];
- }
- @Override
- public ItemStack decrStackSize(int slotIndex, int amount) {
- if (this.contents[slotIndex] != null)
- {
- ItemStack itemstack;
- if (this.contents[slotIndex].stackSize <= amount)
- {
- itemstack = this.contents[slotIndex];
- this.contents[slotIndex] = null;
- this.markDirty();
- return itemstack;
- }
- else
- {
- itemstack = this.contents[slotIndex].splitStack(amount);
- }
- }
- }
- public boolean isBurning()
- {
- return this.workingTime > 0;
- }
- private boolean canSmelt()
- {
- if (this.contents[0] == null || this.contents[1] == null || this.contents[2] == null) //Si les trois premiers slots sont vides
- {
- return false; //On ne peut pas lancer le processus
- }
- else
- {
- 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)
- if (itemstack == null) return false; //rapport avec les recettes
- if (this.contents[3] == null) return true; //vérifications du slot d'output
- if (!this.contents[3].isItemEqual(itemstack)) return false; //ici aussi
- int result = contents[3].stackSize + itemstack.stackSize;
- return result <= getInventoryStackLimit() && result <= this.contents[3].getMaxStackSize(); //Et là aussi décidément
- }
- }
- public void updateEntity() //Méthode exécutée à chaque tick
- {
- if(this.isBurning() && this.canSmelt()) //Si on "cuit" et que notre recette et toujours bonne, on continue
- {
- ++this.workingTime; //incrémentation
- }
- if(this.canSmelt() && !this.isBurning()) //Si la recette est bonne mais qu'elle n'est toujours pas lancée, on la lance
- {
- this.workingTime = 1; //La méthode isBurning() renverra true maintenant (1>0)
- }
- if(this.canSmelt() && this.workingTime == this.workingTimeNeeded) //Si on est arrivé au bout du temps de cuisson et que la recette est toujours bonne
- {
- this.smeltItem(); //on "cuit" les items
- this.workingTime = 0; //et on réinitialise le temps de cuisson
- }
- if(!this.canSmelt()) //Si la recette la recette n'est plus bonne
- {
- this.workingTime= 0; //le temps de cuisson est de 0
- }
- }
- public void smeltItem()
- {
- if (this.canSmelt())
- {
- 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
- if (this.contents[3] == null) //Si il y a rien dans le slot d'output
- {
- this.contents[3] = itemstack.copy(); //On met directement l'ItemStack
- }
- 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à
- {
- this.contents[3].stackSize += itemstack.stackSize; // Alors ont incrémente l'ItemStack
- }
- --this.contents[0].stackSize; //On décrémente les slots d'input
- --this.contents[1].stackSize;
- --this.contents[2].stackSize;
- if (this.contents[0].stackSize <= 0) //Si les slots sont vides, on remet à null le slot
- {
- this.contents[0] = null;
- }
- if (this.contents[1].stackSize <= 0)
- {
- this.contents[1] = null;
- }
- if (this.contents[2].stackSize <= 0)
- {
- this.contents[2] = null;
- }
- }
- }
- }
- package com.ben.heaven.blocks;
- import com.ben.heaven.tileentity.TileEntityMachineBuilder;
- import net.minecraft.block.Block;
- import net.minecraft.block.BlockContainer;
- import net.minecraft.block.material.Material;
- import net.minecraft.entity.item.EntityItem;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.inventory.IInventory;
- import net.minecraft.item.ItemStack;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraft.world.World;
- public class MachineBuilder extends BlockContainer
- {
- public MachineBuilder(String name, Material materialIn, int level)
- {
- super(materialIn);
- this.setResistance(8.0F);
- this.setHarvestLevel("pickaxe", level);
- setUnlocalizedName(name);
- setRegistryName(name);
- }
- public void breakBlock(World world, int x, int y, int z, Block block, int metadata)
- {
- TileEntity tileentity = world.getTileEntity(x, y, z);
- if (tileentity instanceof IInventory)
- {
- IInventory inv = (IInventory)tileentity;
- for (int i1 = 0; i1 < inv.getSizeInventory(); ++i1)
- {
- ItemStack itemstack = inv.getStackInSlot(i1);
- if (itemstack != null)
- {
- float f = world.rand.nextFloat() * 0.8F + 0.1F;
- float f1 = world.rand.nextFloat() * 0.8F + 0.1F;
- EntityItem entityitem;
- for (float f2 = world.rand.nextFloat() * 0.8F + 0.1F; itemstack.stackSize > 0; world.spawnEntityInWorld(entityitem))
- {
- int j1 = world.rand.nextInt(21) + 10;
- if (j1 > itemstack.stackSize)
- {
- j1 = itemstack.stackSize;
- }
- itemstack.stackSize -= j1;
- entityitem = new EntityItem(world, (double)((float)x + f), (double)((float)y + f1), (double)((float)z + f2), new ItemStack(itemstack.getItem(), j1, itemstack.getItemDamage()));
- float f3 = 0.05F;
- entityitem.motionX = (double)((float)world.rand.nextGaussian() * f3);
- entityitem.motionY = (double)((float)world.rand.nextGaussian() * f3 + 0.2F);
- entityitem.motionZ = (double)((float)world.rand.nextGaussian() * f3);
- if (itemstack.hasTagCompound())
- {
- entityitem.getEntityItem().setTagCompound((NBTTagCompound)itemstack.getTagCompound().copy());
- }
- }
- }
- }
- @Override
- public TileEntity createNewTileEntity(World world, int metadata)
- {
- return new TileEntityMachineBuilder();
- }
- public boolean hasTileEntity(int metadata)
- {
- return true;
- }
- public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float hitx, float hity, float hitz)
- {
- if (world.isRemote)
- {
- return true;
- }
- else
- {
- player.openGui(Heaven.instance, 0, world, x, y, z);
- return true;
- }
- }
- }
- package com.ben.heaven.container;
- import com.ben.heaven.tileentity.TileEntityMachineBuilder;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.entity.player.InventoryPlayer;
- import net.minecraft.inventory.Container;
- import net.minecraft.inventory.Slot;
- import net.minecraft.item.ItemStack;
- public class ContainerMachineBuilder extends Container
- {
- private TileEntityMachineBuilder tileMachineTuto;
- public ContainerMachineBuilder(TileEntityMachineBuilder tile, InventoryPlayer inventory)
- {
- this.tileMachineTuto = tile;
- this.addSlotToContainer(new Slot(tile, 0, 49, 75)); //Lancez votre jeu en debug pour calibrer vos slots
- this.addSlotToContainer(new Slot(tile, 1, 89, 75));
- this.addSlotToContainer(new Slot(tile, 2, 129, 75));
- this.addSlotToContainer(new SlotResult(tile, 3, 89, 135)); //Ici c'est un slot que j'ai créer, on le fera après
- this.bindPlayerInventory(inventory); //Les containers ont été vus dans un tutoriel de robin, merci de d'y référer
- }
- @Override
- public boolean canInteractWith(EntityPlayer player) {
- return this.tileMachineTuto.isUseableByPlayer(player);
- }
- private void bindPlayerInventory(InventoryPlayer inventory)
- {
- int i;
- for (i = 0; i < 3; ++i)
- {
- for (int j = 0; j < 9; ++j)
- {
- this.addSlotToContainer(new Slot(inventory, j + i * 9 + 9, 17 + j * 18, 171 + i * 18));
- }
- }
- for (i = 0; i < 9; ++i)
- {
- this.addSlotToContainer(new Slot(inventory, i, 17 + i * 18, 229));
- }
- }
- public ItemStack transferStackInSlot(EntityPlayer player, int quantity)
- {
- ItemStack itemstack = null;
- Slot slot = (Slot)this.inventorySlots.get(quantity);
- if (slot != null && slot.getHasStack())
- {
- ItemStack itemstack1 = slot.getStack();
- itemstack = itemstack1.copy();
- if (quantity < this.tileMachineTuto.getSizeInventory())
- {
- if (!this.mergeItemStack(itemstack1, this.tileMachineTuto.getSizeInventory(), this.inventorySlots.size(), true))
- {
- return null;
- }
- }
- else if (!this.mergeItemStack(itemstack1, 0, this.tileMachineTuto.getSizeInventory(), false))
- {
- return null;
- }
- if (itemstack1.stackSize == 0)
- {
- slot.putStack((ItemStack)null);
- }
- else
- {
- slot.onSlotChanged();
- }
- }
- return itemstack;
- }
- public void onContainerClosed(EntityPlayer player)
- {
- super.onContainerClosed(player);
- this.tileMachineTuto.closeInventory();
- }
- }
- package com.ben.heaven;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.inventory.IInventory;
- import net.minecraft.inventory.Slot;
- import net.minecraft.item.ItemStack;
- public class SlotsResult
- {
- public class SlotResult extends Slot {
- public SlotResult(IInventory inventory, int id, int x, int y)
- {
- super(inventory, id, x, y);
- }
- @Override
- public boolean isItemValid(ItemStack stack) //Interdit la pose d'items dans le slot
- {
- return false;
- }
- public ItemStack decrStackSize(int amount)
- {
- return super.decrStackSize(amount);
- }
- public void onPickupFromSlot(EntityPlayer player, ItemStack stack)
- {
- super.onCrafting(stack);
- super.onPickupFromSlot(player, stack);
- }
- }
- }
- package com.ben.heaven.gui;
- import org.lwjgl.opengl.GL11;
- import com.ben.heaven.References;
- import com.ben.heaven.container.ContainerMachineBuilder;
- import com.ben.heaven.tileentity.TileEntityMachineBuilder;
- import net.minecraft.client.gui.inventory.GuiContainer;
- import net.minecraft.client.resources.I18n;
- import net.minecraft.entity.player.InventoryPlayer;
- import net.minecraft.inventory.IInventory;
- import net.minecraft.util.ResourceLocation;
- public class GuiMachineBuilder extends GuiContainer
- {
- private static final ResourceLocation texture = new ResourceLocation(References.MODID,"textures/gui/container/guiMachineTuto.png");
- @SuppressWarnings("unused")
- private TileEntityMachineBuilder tileMachineTuto;
- private IInventory playerInv;
- public GuiMachineBuilder(TileEntityMachineBuilder tile, InventoryPlayer inventory)
- {
- super(new ContainerMachineBuilder(tile, inventory));
- this.tileMachineTuto = tile;
- this.playerInv = inventory;
- this.allowUserInput = false;
- this.ySize = 256;
- this.xSize = 256;
- }
- @Override
- protected void drawGuiContainerBackgroundLayer(float partialRenderTick, int x, int y)
- {
- GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
- this.mc.getTextureManager().bindTexture(texture);
- int k = (this.width - this.xSize) / 2;
- int l = (this.height - this.ySize) / 2;
- this.drawTexturedModalRect(k, l, 0, 0, this.xSize, this.ySize);
- this.drawTexturedModalRect(0, 0, 176, 14, 100 + 1, 16);
- }
- protected void drawGuiContainerForegroundLayer(int x, int y)
- {
- this.fontRendererObj.drawString(this.playerInv.hasCustomInventoryName() ? this.playerInv.getInventoryName() : I18n.format(this.playerInv.getInventoryName()), 10, this.ySize - 98, 4210752);
- }
- }
- package com.ben.heaven;
- import com.ben.heaven.init.ArmorMod;
- import com.ben.heaven.init.ArmorModSpe;
- import com.ben.heaven.init.BlocksOre;
- import com.ben.heaven.init.DecoBlock;
- import com.ben.heaven.init.ItemMod;
- import com.ben.heaven.init.ItemModOre;
- import com.ben.heaven.init.SmeltingRecipesMod;
- import com.ben.heaven.proxy.ServerProxy;
- import com.ben.heaven.tileentity.TileEntityMachineBuilder;
- import com.ben.heaven.world.WorldGenHeaven;
- import net.minecraft.block.Block;
- import net.minecraft.creativetab.CreativeTabs;
- import net.minecraft.init.Blocks;
- import net.minecraft.init.Items;
- import net.minecraft.item.Item;
- import net.minecraft.item.ItemStack;
- import net.minecraftforge.event.RegistryEvent;
- import net.minecraftforge.fml.common.Mod;
- import net.minecraftforge.fml.common.Mod.EventHandler;
- import net.minecraftforge.fml.common.SidedProxy;
- import net.minecraftforge.fml.common.event.FMLInitializationEvent;
- import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
- import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
- import net.minecraftforge.fml.common.registry.GameRegistry;
- import net.minecraftforge.fml.relauncher.Side;
- import net.minecraftforge.fml.relauncher.SideOnly;
- @Mod(modid = References.MODID, name = References.NAME, version = References.VERSION, acceptedMinecraftVersions = References.MINECRAFT_VERSION)
- public class Heaven
- {
- @SidedProxy(clientSide = References.CLIENT_PROXY, serverSide = References.SERVER_PROXY, modId = References.MODID)
- public static ServerProxy proxy;
- @EventHandler
- public void preinit(FMLPreInitializationEvent event)
- {
- BlocksOre.init();
- DecoBlock.init();
- ItemMod.init();
- ItemModOre.init();
- ArmorMod.init();
- ArmorModSpe.init();
- }
- @EventHandler
- public void init(FMLInitializationEvent event)
- {
- proxy.register();
- SmeltingRecipesMod.init();
- GameRegistry.registerWorldGenerator(new WorldGenHeaven(), 0);
- GameRegistry.registerTileEntity(TileEntityMachineBuilder.class, "Heaven:MachineBuilderTileEntity");
- }
- @EventHandler
- public void postinit(FMLPostInitializationEvent event)
- {
- }
- public static CreativeTabs tabHeavenDeco = new CreativeTabs("tabHeavenDeco")
- {
- @SideOnly(Side.CLIENT)
- public ItemStack getTabIconItem()
- {
- return new ItemStack(DecoBlock.lava_deco_block);
- }
- };
- public static CreativeTabs tabHeavenOres = new CreativeTabs("tabHeavenOres")
- {
- @SideOnly(Side.CLIENT)
- public ItemStack getTabIconItem()
- {
- return new ItemStack(BlocksOre.shiny_ore);
- }
- };
- public static CreativeTabs tabHeavenTools = new CreativeTabs("tabHeavenTools")
- {
- @SideOnly(Side.CLIENT)
- public ItemStack getTabIconItem()
- {
- return new ItemStack(ItemModOre.mercure_sword);
- }
- };
- public static CreativeTabs tabHeavenMaterials = new CreativeTabs("tabHeavenMaterials")
- {
- @SideOnly(Side.CLIENT)
- public ItemStack getTabIconItem()
- {
- return new ItemStack(ItemMod.stone_stick);
- }
- };
- public static CreativeTabs tabHeavenArmor = new CreativeTabs("tabHeavenArmor")
- {
- @SideOnly(Side.CLIENT)
- public ItemStack getTabIconItem()
- {
- return new ItemStack(ArmorMod.mercure_chestplate);
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement