Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ***********
- MardiffBase
- ***********
- @Mod (modid = "mardiffMod2", name = "Mardiff's Mod version 0.0.3", version = "0.0.3")
- @NetworkMod(clientSideRequired = true, serverSideRequired = true, clientPacketHandlerSpec = @SidedPacketHandler(channels = {"mardiffMod"}, packetHandler = net.minecraft.mardiff.SSPPacketHandler.class),
- serverPacketHandlerSpec = @SidedPacketHandler(channels = {"mardiffMod"}, packetHandler = net.minecraft.mardiff.SMPPacketHandler.class))
- public class MardiffBase {
- @Instance("mardiffMod")
- public static MardiffBase instance;
- //Gui Blocks
- public static final Block grinder = new Grinder(3078);
- @Init
- public void load(FMLInitializationEvent event)
- {
- //Gui Blocks
- GameRegistry.registerBlock(grinder);
- LanguageRegistry.addName(grinder, "Grinder");
- GameRegistry.registerTileEntity(TileEntityGrinder.class, "containerGrinder");
- NetworkRegistry.instance().registerGuiHandler(this, new GuiHandler());
- }
- }
- *******
- Grinder
- *******
- package net.minecraft.mardiff;
- import java.util.Random;
- import net.minecraft.block.BlockContainer;
- import net.minecraft.block.material.Material;
- import net.minecraft.creativetab.CreativeTabs;
- 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 Grinder extends BlockContainer {
- protected Grinder (int id) {
- super(id, Material.rock);
- setHardness(4.0F);
- setResistance(5.0F);
- setBlockName("grinder");
- setCreativeTab(MardiffBase.MardiffTab);
- }
- @Override
- public boolean onBlockActivated(World world, int x, int y, int z,
- EntityPlayer player, int idk, float what, float these, float are) {
- TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
- if (tileEntity == null || player.isSneaking()) {
- return false;
- }
- //code to open gui explained later
- player.openGui(MardiffBase.instance, 0, world, x, y, z);
- return true;
- }
- @Override
- public void breakBlock(World world, int x, int y, int z, int par5, int par6) {
- dropItems(world, x, y, z);
- super.breakBlock(world, x, y, z, par5, par6);
- }
- private void dropItems(World world, int x, int y, int z){
- Random rand = new Random();
- TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
- if (!(tileEntity instanceof IInventory)) {
- return;
- }
- IInventory inventory = (IInventory) tileEntity;
- for (int i = 0; i < inventory.getSizeInventory(); i++) {
- ItemStack item = inventory.getStackInSlot(i);
- if (item != null && item.stackSize > 0) {
- float rx = rand.nextFloat() * 0.8F + 0.1F;
- float ry = rand.nextFloat() * 0.8F + 0.1F;
- float rz = rand.nextFloat() * 0.8F + 0.1F;
- EntityItem entityItem = new EntityItem(world,
- x + rx, y + ry, z + rz,
- new ItemStack(item.itemID, item.stackSize, item.getItemDamage()));
- if (item.hasTagCompound()) {
- entityItem.func_92014_d().setTagCompound((NBTTagCompound) item.getTagCompound().copy());
- }
- float factor = 0.05F;
- entityItem.motionX = rand.nextGaussian() * factor;
- entityItem.motionY = rand.nextGaussian() * factor + 0.2F;
- entityItem.motionZ = rand.nextGaussian() * factor;
- world.spawnEntityInWorld(entityItem);
- item.stackSize = 0;
- }
- }
- }
- @Override
- public TileEntity createNewTileEntity(World world) {
- return new TileEntityGrinder();
- }
- }
- *****************
- TileEntityGrinder
- *****************
- package net.minecraft.mardiff;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.inventory.IInventory;
- import net.minecraft.item.ItemStack;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.nbt.NBTTagList;
- import net.minecraft.tileentity.TileEntity;
- public class TileEntityGrinder extends TileEntity implements IInventory {
- private ItemStack[] inv;
- public TileEntityGrinder(){
- inv = new ItemStack[9];
- }
- @Override
- public int getSizeInventory() {
- return inv.length;
- }
- @Override
- public ItemStack getStackInSlot(int slot) {
- return inv[slot];
- }
- @Override
- public void setInventorySlotContents(int slot, ItemStack stack) {
- inv[slot] = stack;
- if (stack != null && stack.stackSize > getInventoryStackLimit()) {
- stack.stackSize = getInventoryStackLimit();
- }
- }
- @Override
- public ItemStack decrStackSize(int slot, int amt) {
- ItemStack stack = getStackInSlot(slot);
- if (stack != null) {
- if (stack.stackSize <= amt) {
- setInventorySlotContents(slot, null);
- } else {
- stack = stack.splitStack(amt);
- if (stack.stackSize == 0) {
- setInventorySlotContents(slot, null);
- }
- }
- }
- return stack;
- }
- @Override
- public ItemStack getStackInSlotOnClosing(int slot) {
- ItemStack stack = getStackInSlot(slot);
- if (stack != null) {
- setInventorySlotContents(slot, null);
- }
- return stack;
- }
- @Override
- public int getInventoryStackLimit() {
- return 64;
- }
- @Override
- public boolean isUseableByPlayer(EntityPlayer player) {
- return worldObj.getBlockTileEntity(xCoord, yCoord, zCoord) == this &&
- player.getDistanceSq(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5) < 64;
- }
- @Override
- public void openChest() {}
- @Override
- public void closeChest() {}
- @Override
- public void readFromNBT(NBTTagCompound tagCompound) {
- super.readFromNBT(tagCompound);
- NBTTagList tagList = tagCompound.getTagList("Inventory");
- for (int i = 0; i < tagList.tagCount(); i++) {
- NBTTagCompound tag = (NBTTagCompound) tagList.tagAt(i);
- byte slot = tag.getByte("Slot");
- if (slot >= 0 && slot < inv.length) {
- inv[slot] = ItemStack.loadItemStackFromNBT(tag);
- }
- }
- }
- @Override
- public void writeToNBT(NBTTagCompound tagCompound) {
- super.writeToNBT(tagCompound);
- NBTTagList itemList = new NBTTagList();
- for (int i = 0; i < inv.length; i++) {
- ItemStack stack = inv[i];
- if (stack != null) {
- NBTTagCompound tag = new NBTTagCompound();
- tag.setByte("Slot", (byte) i);
- stack.writeToNBT(tag);
- itemList.appendTag(tag);
- }
- }
- tagCompound.setTag("Inventory", itemList);
- }
- @Override
- public String getInvName() {
- return "net.minecraft.mardiff.grinder";
- }
- }
- ****************
- ContainerGrinder
- ****************
- package net.minecraft.mardiff;
- 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 ContainerGrinder extends Container {
- protected TileEntityGrinder tileEntity;
- public ContainerGrinder (InventoryPlayer inventoryPlayer, TileEntityGrinder te){
- tileEntity = te;
- //the Slot constructor takes the IInventory and the slot number in that it binds to
- //and the x-y coordinates it resides on-screen
- addSlotToContainer(new Slot(tileEntity, 0, 56, 35));
- addSlotToContainer(new Slot(tileEntity, 1, 116, 35));
- //commonly used vanilla code that adds the player's inventory
- bindPlayerInventory(inventoryPlayer);
- }
- @Override
- public boolean canInteractWith(EntityPlayer player) {
- return tileEntity.isUseableByPlayer(player);
- }
- protected void bindPlayerInventory(InventoryPlayer inventoryPlayer) {
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 9; j++) {
- addSlotToContainer(new Slot(inventoryPlayer, j + i * 9 + 9,
- 8 + j * 18, 84 + i * 18));
- }
- }
- for (int i = 0; i < 9; i++) {
- addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 142));
- }
- }
- @Override
- public ItemStack transferStackInSlot(EntityPlayer player, int slot) {
- ItemStack stack = null;
- Slot slotObject = (Slot) inventorySlots.get(slot);
- //null checks and checks if the item can be stacked (maxStackSize > 1)
- if (slotObject != null && slotObject.getHasStack()) {
- ItemStack stackInSlot = slotObject.getStack();
- stack = stackInSlot.copy();
- //merges the item into player inventory since its in the tileEntity
- if (slot < 9) {
- if (!this.mergeItemStack(stackInSlot, 9, 45, true)) {
- return null;
- }
- }
- //places it into the tileEntity is possible since its in the player inventory
- else if (!this.mergeItemStack(stackInSlot, 0, 9, false)) {
- return null;
- }
- if (stackInSlot.stackSize == 0) {
- slotObject.putStack(null);
- } else {
- slotObject.onSlotChanged();
- }
- if (stackInSlot.stackSize == stack.stackSize) {
- return null;
- }
- slotObject.onPickupFromSlot(player, stackInSlot);
- }
- return stack;
- }
- }
- **********
- GuiGrinder
- **********
- package net.minecraft.mardiff;
- import net.minecraft.client.gui.inventory.GuiContainer;
- import net.minecraft.entity.player.InventoryPlayer;
- import net.minecraft.util.StatCollector;
- import org.lwjgl.opengl.GL11;
- public class GuiGrinder extends GuiContainer {
- public GuiGrinder (InventoryPlayer inventoryPlayer,
- TileEntityGrinder tileEntity) {
- //the container is instanciated and passed to the superclass for handling
- super(new ContainerGrinder(inventoryPlayer, tileEntity));
- }
- @Override
- protected void drawGuiContainerForegroundLayer(int param1, int param2) {
- //draw text and stuff here
- //the parameters for drawString are: string, x, y, color
- fontRenderer.drawString("Grinder", 8, 6, 4210752);
- //draws "Inventory" or your regional equivalent
- fontRenderer.drawString(StatCollector.translateToLocal("container.inventory"), 8, ySize - 96 + 2, 4210752);
- }
- @Override
- protected void drawGuiContainerBackgroundLayer(float par1, int par2,
- int par3) {
- //draw your Gui here, only thing you need to change is the path
- int texture = mc.renderEngine.getTexture("/textures/gui/grinder.png");
- GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
- this.mc.renderEngine.bindTexture(texture);
- int x = (width - xSize) / 2;
- int y = (height - ySize) / 2;
- this.drawTexturedModalRect(x, y, 0, 0, xSize, ySize);
- }
- }
- **********
- GuiHandler
- **********
- package net.minecraft.mardiff;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraft.world.World;
- import cpw.mods.fml.common.network.IGuiHandler;
- public class GuiHandler implements IGuiHandler {
- //returns an instance of the Container you made earlier
- @Override
- public Object getServerGuiElement(int id, EntityPlayer player, World world,
- int x, int y, int z) {
- TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
- if(tileEntity instanceof TileEntityGrinder){
- return new ContainerGrinder(player.inventory, (TileEntityGrinder) tileEntity);
- }
- return null;
- }
- //returns an instance of the Gui you made earlier
- @Override
- public Object getClientGuiElement(int id, EntityPlayer player, World world,
- int x, int y, int z) {
- TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
- if(tileEntity instanceof TileEntityGrinder){
- return new GuiGrinder(player.inventory, (TileEntityGrinder) tileEntity);
- }
- return null;
- }
- }
- ****************
- SMPPacketHandler
- ****************
- package net.minecraft.mardiff;
- import java.io.ByteArrayInputStream;
- import java.io.DataInputStream;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.network.INetworkManager;
- import net.minecraft.network.packet.Packet250CustomPayload;
- import cpw.mods.fml.common.network.IPacketHandler;
- import cpw.mods.fml.common.network.Player;
- public class SMPPacketHandler implements IPacketHandler{
- @Override
- public void onPacketData(INetworkManager manager, Packet250CustomPayload payload, Player player){
- DataInputStream data = new DataInputStream(new ByteArrayInputStream(payload.data));
- EntityPlayer sender = (EntityPlayer) player;
- }
- }
- ****************
- SSPPacketHandler
- ****************
- package net.minecraft.mardiff;
- import java.io.ByteArrayInputStream;
- import java.io.DataInputStream;
- import net.minecraft.network.INetworkManager;
- import net.minecraft.network.packet.Packet250CustomPayload;
- import cpw.mods.fml.common.network.IPacketHandler;
- import cpw.mods.fml.common.network.Player;
- public class SSPPacketHandler implements IPacketHandler{
- @Override
- public void onPacketData(INetworkManager manager, Packet250CustomPayload payload, Player player){
- DataInputStream data = new DataInputStream(new ByteArrayInputStream(payload.data));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement