Advertisement
gmen712

Grinder With GUI, not Furnace

Feb 5th, 2013
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.16 KB | None | 0 0
  1. ***********
  2. MardiffBase
  3. ***********
  4. @Mod (modid = "mardiffMod2", name = "Mardiff's Mod version 0.0.3", version = "0.0.3")
  5.  
  6. @NetworkMod(clientSideRequired = true, serverSideRequired = true, clientPacketHandlerSpec = @SidedPacketHandler(channels = {"mardiffMod"}, packetHandler = net.minecraft.mardiff.SSPPacketHandler.class),
  7. serverPacketHandlerSpec = @SidedPacketHandler(channels = {"mardiffMod"}, packetHandler = net.minecraft.mardiff.SMPPacketHandler.class))
  8.  
  9. public class MardiffBase {
  10.     @Instance("mardiffMod")
  11.         public static MardiffBase instance;
  12.  
  13.     //Gui Blocks
  14.     public static final Block grinder = new Grinder(3078);
  15.  
  16.     @Init
  17.     public void load(FMLInitializationEvent event)
  18.     {
  19.         //Gui Blocks
  20.         GameRegistry.registerBlock(grinder);
  21.         LanguageRegistry.addName(grinder, "Grinder");  
  22.             GameRegistry.registerTileEntity(TileEntityGrinder.class, "containerGrinder");
  23.             NetworkRegistry.instance().registerGuiHandler(this, new GuiHandler());
  24.     }
  25. }
  26.  
  27. *******
  28. Grinder
  29. *******
  30. package net.minecraft.mardiff;
  31.  
  32. import java.util.Random;
  33.  
  34. import net.minecraft.block.BlockContainer;
  35. import net.minecraft.block.material.Material;
  36. import net.minecraft.creativetab.CreativeTabs;
  37. import net.minecraft.entity.item.EntityItem;
  38. import net.minecraft.entity.player.EntityPlayer;
  39. import net.minecraft.inventory.IInventory;
  40. import net.minecraft.item.ItemStack;
  41. import net.minecraft.nbt.NBTTagCompound;
  42. import net.minecraft.tileentity.TileEntity;
  43. import net.minecraft.world.World;
  44.  
  45. public class Grinder extends BlockContainer {
  46.  
  47.         protected Grinder (int id) {
  48.                 super(id, Material.rock);
  49.                 setHardness(4.0F);
  50.                 setResistance(5.0F);
  51.                 setBlockName("grinder");
  52.                 setCreativeTab(MardiffBase.MardiffTab);
  53.         }
  54.  
  55.         @Override
  56.         public boolean onBlockActivated(World world, int x, int y, int z,
  57.                         EntityPlayer player, int idk, float what, float these, float are) {
  58.                 TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
  59.                 if (tileEntity == null || player.isSneaking()) {
  60.                         return false;
  61.                 }
  62.         //code to open gui explained later
  63.         player.openGui(MardiffBase.instance, 0, world, x, y, z);
  64.                 return true;
  65.         }
  66.  
  67.         @Override
  68.         public void breakBlock(World world, int x, int y, int z, int par5, int par6) {
  69.                 dropItems(world, x, y, z);
  70.                 super.breakBlock(world, x, y, z, par5, par6);
  71.         }
  72.  
  73.         private void dropItems(World world, int x, int y, int z){
  74.                 Random rand = new Random();
  75.  
  76.                 TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
  77.                 if (!(tileEntity instanceof IInventory)) {
  78.                         return;
  79.                 }
  80.                 IInventory inventory = (IInventory) tileEntity;
  81.  
  82.                 for (int i = 0; i < inventory.getSizeInventory(); i++) {
  83.                         ItemStack item = inventory.getStackInSlot(i);
  84.  
  85.                         if (item != null && item.stackSize > 0) {
  86.                                 float rx = rand.nextFloat() * 0.8F + 0.1F;
  87.                                 float ry = rand.nextFloat() * 0.8F + 0.1F;
  88.                                 float rz = rand.nextFloat() * 0.8F + 0.1F;
  89.  
  90.                                 EntityItem entityItem = new EntityItem(world,
  91.                                                 x + rx, y + ry, z + rz,
  92.                                                 new ItemStack(item.itemID, item.stackSize, item.getItemDamage()));
  93.  
  94.                                 if (item.hasTagCompound()) {
  95.                                         entityItem.func_92014_d().setTagCompound((NBTTagCompound) item.getTagCompound().copy());
  96.                                 }
  97.  
  98.                                 float factor = 0.05F;
  99.                                 entityItem.motionX = rand.nextGaussian() * factor;
  100.                                 entityItem.motionY = rand.nextGaussian() * factor + 0.2F;
  101.                                 entityItem.motionZ = rand.nextGaussian() * factor;
  102.                                 world.spawnEntityInWorld(entityItem);
  103.                                 item.stackSize = 0;
  104.                         }
  105.                 }
  106.         }
  107.  
  108.         @Override
  109.         public TileEntity createNewTileEntity(World world) {
  110.                 return new TileEntityGrinder();
  111.         }
  112.  
  113. }
  114.  
  115. *****************
  116. TileEntityGrinder
  117. *****************
  118. package net.minecraft.mardiff;
  119.  
  120. import net.minecraft.entity.player.EntityPlayer;
  121. import net.minecraft.inventory.IInventory;
  122. import net.minecraft.item.ItemStack;
  123. import net.minecraft.nbt.NBTTagCompound;
  124. import net.minecraft.nbt.NBTTagList;
  125. import net.minecraft.tileentity.TileEntity;
  126.  
  127. public class TileEntityGrinder extends TileEntity implements IInventory {
  128.  
  129.         private ItemStack[] inv;
  130.  
  131.         public TileEntityGrinder(){
  132.                 inv = new ItemStack[9];
  133.         }
  134.        
  135.         @Override
  136.         public int getSizeInventory() {
  137.                 return inv.length;
  138.         }
  139.  
  140.         @Override
  141.         public ItemStack getStackInSlot(int slot) {
  142.                 return inv[slot];
  143.         }
  144.        
  145.         @Override
  146.         public void setInventorySlotContents(int slot, ItemStack stack) {
  147.                 inv[slot] = stack;
  148.                 if (stack != null && stack.stackSize > getInventoryStackLimit()) {
  149.                         stack.stackSize = getInventoryStackLimit();
  150.                 }              
  151.         }
  152.  
  153.         @Override
  154.         public ItemStack decrStackSize(int slot, int amt) {
  155.                 ItemStack stack = getStackInSlot(slot);
  156.                 if (stack != null) {
  157.                         if (stack.stackSize <= amt) {
  158.                                 setInventorySlotContents(slot, null);
  159.                         } else {
  160.                                 stack = stack.splitStack(amt);
  161.                                 if (stack.stackSize == 0) {
  162.                                         setInventorySlotContents(slot, null);
  163.                                 }
  164.                         }
  165.                 }
  166.                 return stack;
  167.         }
  168.  
  169.         @Override
  170.         public ItemStack getStackInSlotOnClosing(int slot) {
  171.                 ItemStack stack = getStackInSlot(slot);
  172.                 if (stack != null) {
  173.                         setInventorySlotContents(slot, null);
  174.                 }
  175.                 return stack;
  176.         }
  177.        
  178.         @Override
  179.         public int getInventoryStackLimit() {
  180.                 return 64;
  181.         }
  182.  
  183.         @Override
  184.         public boolean isUseableByPlayer(EntityPlayer player) {
  185.                 return worldObj.getBlockTileEntity(xCoord, yCoord, zCoord) == this &&
  186.                 player.getDistanceSq(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5) < 64;
  187.         }
  188.  
  189.         @Override
  190.         public void openChest() {}
  191.  
  192.         @Override
  193.         public void closeChest() {}
  194.        
  195.         @Override
  196.         public void readFromNBT(NBTTagCompound tagCompound) {
  197.                 super.readFromNBT(tagCompound);
  198.                
  199.                 NBTTagList tagList = tagCompound.getTagList("Inventory");
  200.                 for (int i = 0; i < tagList.tagCount(); i++) {
  201.                         NBTTagCompound tag = (NBTTagCompound) tagList.tagAt(i);
  202.                         byte slot = tag.getByte("Slot");
  203.                         if (slot >= 0 && slot < inv.length) {
  204.                                 inv[slot] = ItemStack.loadItemStackFromNBT(tag);
  205.                         }
  206.                 }
  207.         }
  208.  
  209.         @Override
  210.         public void writeToNBT(NBTTagCompound tagCompound) {
  211.                 super.writeToNBT(tagCompound);
  212.                                
  213.                 NBTTagList itemList = new NBTTagList();
  214.                 for (int i = 0; i < inv.length; i++) {
  215.                         ItemStack stack = inv[i];
  216.                         if (stack != null) {
  217.                                 NBTTagCompound tag = new NBTTagCompound();
  218.                                 tag.setByte("Slot", (byte) i);
  219.                                 stack.writeToNBT(tag);
  220.                                 itemList.appendTag(tag);
  221.                         }
  222.                 }
  223.                 tagCompound.setTag("Inventory", itemList);
  224.         }
  225.  
  226.                 @Override
  227.                 public String getInvName() {
  228.                         return "net.minecraft.mardiff.grinder";
  229.                 }
  230. }
  231.  
  232. ****************
  233. ContainerGrinder
  234. ****************
  235. package net.minecraft.mardiff;
  236.  
  237. import net.minecraft.entity.player.EntityPlayer;
  238. import net.minecraft.entity.player.InventoryPlayer;
  239. import net.minecraft.inventory.Container;
  240. import net.minecraft.inventory.Slot;
  241. import net.minecraft.item.ItemStack;
  242.  
  243. public class ContainerGrinder extends Container {
  244.  
  245.         protected TileEntityGrinder tileEntity;
  246.  
  247.         public ContainerGrinder (InventoryPlayer inventoryPlayer, TileEntityGrinder te){
  248.                 tileEntity = te;
  249.  
  250.                 //the Slot constructor takes the IInventory and the slot number in that it binds to
  251.                 //and the x-y coordinates it resides on-screen
  252.                 addSlotToContainer(new Slot(tileEntity, 0, 56, 35));
  253.                 addSlotToContainer(new Slot(tileEntity, 1, 116, 35));
  254.  
  255.                 //commonly used vanilla code that adds the player's inventory
  256.                 bindPlayerInventory(inventoryPlayer);
  257.         }
  258.  
  259.         @Override
  260.         public boolean canInteractWith(EntityPlayer player) {
  261.                 return tileEntity.isUseableByPlayer(player);
  262.         }
  263.  
  264.  
  265.         protected void bindPlayerInventory(InventoryPlayer inventoryPlayer) {
  266.                 for (int i = 0; i < 3; i++) {
  267.                         for (int j = 0; j < 9; j++) {
  268.                                 addSlotToContainer(new Slot(inventoryPlayer, j + i * 9 + 9,
  269.                                                 8 + j * 18, 84 + i * 18));
  270.                         }
  271.                 }
  272.  
  273.                 for (int i = 0; i < 9; i++) {
  274.                         addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 142));
  275.                 }
  276.         }
  277.  
  278.         @Override
  279.         public ItemStack transferStackInSlot(EntityPlayer player, int slot) {
  280.                 ItemStack stack = null;
  281.                 Slot slotObject = (Slot) inventorySlots.get(slot);
  282.  
  283.                 //null checks and checks if the item can be stacked (maxStackSize > 1)
  284.                 if (slotObject != null && slotObject.getHasStack()) {
  285.                         ItemStack stackInSlot = slotObject.getStack();
  286.                         stack = stackInSlot.copy();
  287.  
  288.                         //merges the item into player inventory since its in the tileEntity
  289.                         if (slot < 9) {
  290.                                 if (!this.mergeItemStack(stackInSlot, 9, 45, true)) {
  291.                                         return null;
  292.                                 }
  293.                         }
  294.                         //places it into the tileEntity is possible since its in the player inventory
  295.                         else if (!this.mergeItemStack(stackInSlot, 0, 9, false)) {
  296.                                 return null;
  297.                         }
  298.  
  299.                         if (stackInSlot.stackSize == 0) {
  300.                                 slotObject.putStack(null);
  301.                         } else {
  302.                                 slotObject.onSlotChanged();
  303.                         }
  304.  
  305.                         if (stackInSlot.stackSize == stack.stackSize) {
  306.                                 return null;
  307.                         }
  308.                         slotObject.onPickupFromSlot(player, stackInSlot);
  309.                 }
  310.                 return stack;
  311.         }
  312. }
  313.  
  314. **********
  315. GuiGrinder
  316. **********
  317. package net.minecraft.mardiff;
  318.  
  319. import net.minecraft.client.gui.inventory.GuiContainer;
  320. import net.minecraft.entity.player.InventoryPlayer;
  321. import net.minecraft.util.StatCollector;
  322.  
  323. import org.lwjgl.opengl.GL11;
  324.  
  325. public class GuiGrinder extends GuiContainer {
  326.  
  327.         public GuiGrinder (InventoryPlayer inventoryPlayer,
  328.                         TileEntityGrinder tileEntity) {
  329.                 //the container is instanciated and passed to the superclass for handling
  330.                 super(new ContainerGrinder(inventoryPlayer, tileEntity));
  331.         }
  332.  
  333.         @Override
  334.         protected void drawGuiContainerForegroundLayer(int param1, int param2) {
  335.                 //draw text and stuff here
  336.                 //the parameters for drawString are: string, x, y, color
  337.                 fontRenderer.drawString("Grinder", 8, 6, 4210752);
  338.                 //draws "Inventory" or your regional equivalent
  339.                 fontRenderer.drawString(StatCollector.translateToLocal("container.inventory"), 8, ySize - 96 + 2, 4210752);
  340.         }
  341.  
  342.         @Override
  343.         protected void drawGuiContainerBackgroundLayer(float par1, int par2,
  344.                         int par3) {
  345.                 //draw your Gui here, only thing you need to change is the path
  346.                 int texture = mc.renderEngine.getTexture("/textures/gui/grinder.png");
  347.                 GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
  348.                 this.mc.renderEngine.bindTexture(texture);
  349.                 int x = (width - xSize) / 2;
  350.                 int y = (height - ySize) / 2;
  351.                 this.drawTexturedModalRect(x, y, 0, 0, xSize, ySize);
  352.         }
  353.  
  354. }
  355.  
  356. **********
  357. GuiHandler
  358. **********
  359. package net.minecraft.mardiff;
  360.  
  361. import net.minecraft.entity.player.EntityPlayer;
  362. import net.minecraft.tileentity.TileEntity;
  363. import net.minecraft.world.World;
  364. import cpw.mods.fml.common.network.IGuiHandler;
  365.  
  366. public class GuiHandler implements IGuiHandler {
  367.         //returns an instance of the Container you made earlier
  368.         @Override
  369.         public Object getServerGuiElement(int id, EntityPlayer player, World world,
  370.                         int x, int y, int z) {
  371.                 TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
  372.                 if(tileEntity instanceof TileEntityGrinder){
  373.                         return new ContainerGrinder(player.inventory, (TileEntityGrinder) tileEntity);
  374.                 }
  375.                 return null;
  376.         }
  377.  
  378.         //returns an instance of the Gui you made earlier
  379.         @Override
  380.         public Object getClientGuiElement(int id, EntityPlayer player, World world,
  381.                         int x, int y, int z) {
  382.                 TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
  383.                 if(tileEntity instanceof TileEntityGrinder){
  384.                         return new GuiGrinder(player.inventory, (TileEntityGrinder) tileEntity);
  385.                 }
  386.                 return null;
  387.  
  388.         }
  389. }
  390.  
  391. ****************
  392. SMPPacketHandler
  393. ****************
  394. package net.minecraft.mardiff;
  395.  
  396. import java.io.ByteArrayInputStream;
  397. import java.io.DataInputStream;
  398.  
  399. import net.minecraft.entity.player.EntityPlayer;
  400. import net.minecraft.network.INetworkManager;
  401. import net.minecraft.network.packet.Packet250CustomPayload;
  402. import cpw.mods.fml.common.network.IPacketHandler;
  403. import cpw.mods.fml.common.network.Player;
  404.  
  405. public class SMPPacketHandler implements IPacketHandler{
  406.  
  407.        
  408.         @Override
  409.         public void onPacketData(INetworkManager manager, Packet250CustomPayload payload, Player player){
  410.                 DataInputStream data = new DataInputStream(new ByteArrayInputStream(payload.data));
  411.                 EntityPlayer sender = (EntityPlayer) player;
  412.         }
  413. }
  414.  
  415. ****************
  416. SSPPacketHandler
  417. ****************
  418. package net.minecraft.mardiff;
  419.  
  420.  
  421. import java.io.ByteArrayInputStream;
  422. import java.io.DataInputStream;
  423.  
  424. import net.minecraft.network.INetworkManager;
  425. import net.minecraft.network.packet.Packet250CustomPayload;
  426. import cpw.mods.fml.common.network.IPacketHandler;
  427. import cpw.mods.fml.common.network.Player;
  428.  
  429.  
  430. public class SSPPacketHandler implements IPacketHandler{
  431.         @Override
  432.         public void onPacketData(INetworkManager manager, Packet250CustomPayload payload, Player player){
  433.                 DataInputStream data = new DataInputStream(new ByteArrayInputStream(payload.data));
  434.         }
  435. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement