diff options
Diffstat (limited to 'src/main/java')
9 files changed, 678 insertions, 16 deletions
diff --git a/src/main/java/gmail/Lance5057/blocks/CrestMount.java b/src/main/java/gmail/Lance5057/blocks/CrestMount.java index 66f0762..1401057 100644 --- a/src/main/java/gmail/Lance5057/blocks/CrestMount.java +++ b/src/main/java/gmail/Lance5057/blocks/CrestMount.java @@ -4,14 +4,22 @@ import static net.minecraftforge.common.util.ForgeDirection.EAST; import static net.minecraftforge.common.util.ForgeDirection.NORTH; import static net.minecraftforge.common.util.ForgeDirection.SOUTH; import static net.minecraftforge.common.util.ForgeDirection.WEST; +import gmail.Lance5057.com.mod_TinkersDefense; +import gmail.Lance5057.gui.Gui_CrestMount; + +import java.util.Random; + +import net.minecraft.block.Block; import net.minecraft.block.BlockContainer; import net.minecraft.block.material.Material; import net.minecraft.client.renderer.texture.IIconRegister; -import net.minecraft.entity.EntityLivingBase; +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.tileentity.TileEntityFurnace; -import net.minecraft.util.MathHelper; +import net.minecraft.tileentity.TileEntityChest; import net.minecraft.world.World; public class CrestMount extends BlockContainer { @@ -21,6 +29,66 @@ public class CrestMount extends BlockContainer { super(Material.iron); this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F); } + + @Override + public void onBlockAdded(World world, int i, int j, int k) + { + super.onBlockAdded(world, i, j, k); + world.markBlockForUpdate(i, j, k); + } + + @Override + public boolean onBlockActivated(World world, int x, int y, int z, + EntityPlayer player, int metadata, float what, float these, float are) { + TileEntity tileEntity = world.getTileEntity(x, y, z); + if (tileEntity == null || player.isSneaking()) { + return false; + } + //code to open gui explained later + player.openGui(mod_TinkersDefense.instance, 0, world, x, y, z); + return true; + } + + @Override + public void breakBlock(World world, int x, int y, int z, Block 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.getTileEntity(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.getItem(), item.stackSize, item.getItemDamage())); + + if (item.hasTagCompound()) { + entityItem.getEntityItem().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; + } + } +} //You don't want the normal render type, or it wont render properly. @Override @@ -46,7 +114,8 @@ public class CrestMount extends BlockContainer { @Override public TileEntity createNewTileEntity(World p_149915_1_, int p_149915_2_) { - return new TileEntity_CrestMount(); + TileEntity_CrestMount te = new TileEntity_CrestMount(); + return te; } @Override @@ -75,4 +144,5 @@ public class CrestMount extends BlockContainer { } return j1; - }} + } +} diff --git a/src/main/java/gmail/Lance5057/blocks/TileEntity_CrestMount.java b/src/main/java/gmail/Lance5057/blocks/TileEntity_CrestMount.java index 486689c..2e9d848 100644 --- a/src/main/java/gmail/Lance5057/blocks/TileEntity_CrestMount.java +++ b/src/main/java/gmail/Lance5057/blocks/TileEntity_CrestMount.java @@ -1,7 +1,224 @@ package gmail.Lance5057.blocks; +import java.util.Iterator; +import java.util.List; + +import gmail.Lance5057.com.mod_TinkersDefense; +import gmail.Lance5057.proxy.Handler_CrestMount; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.ContainerChest; +import net.minecraft.inventory.IInventory; +import net.minecraft.inventory.InventoryLargeChest; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; +import net.minecraft.network.Packet; import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.AxisAlignedBB; +import net.minecraft.world.World; + +public class TileEntity_CrestMount extends TileEntity implements IInventory{ + + private ItemStack[] inventory = new ItemStack[4]; + private String name = "Item"; + private int facing; + private int numUsingPlayers; + private int ticksSinceSync; + + @Override + public int getSizeInventory() { + return inventory.length; + } + + @Override + public ItemStack getStackInSlot(int slot) { + return inventory[slot]; + } + + @Override + public ItemStack decrStackSize(int slot, int amount) { + ItemStack stack = getStackInSlot(slot); + if(stack != null) + { + if(stack.stackSize > amount) + { + stack = stack.splitStack(amount); + markDirty(); + } + else + { + 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 void setInventorySlotContents(int slot, ItemStack itemstack) { + this.inventory[slot] = itemstack; + + if (itemstack != null && itemstack.stackSize > this.getInventoryStackLimit()) + { + itemstack.stackSize = this.getInventoryStackLimit(); + } + + markDirty(); + } + + @Override + public String getInventoryName() { + return name; + } + + @Override + public boolean hasCustomInventoryName() { + return name.length() > 0; + } + + @Override + public int getInventoryStackLimit() { + return 1; + } + + @Override + public boolean isUseableByPlayer(EntityPlayer player) { + return true; + } + + @Override + public void openInventory() { + if (worldObj == null) return; + numUsingPlayers++; + worldObj.addBlockEvent(xCoord, yCoord, zCoord, mod_TinkersDefense.block_CrestMount, 1, numUsingPlayers); + } + + @Override + public void closeInventory() { + if (worldObj == null) return; + numUsingPlayers--; + worldObj.addBlockEvent(xCoord, yCoord, zCoord, mod_TinkersDefense.block_CrestMount, 1, numUsingPlayers); + } + + @Override + public boolean receiveClientEvent(int p_145842_1_, int p_145842_2_) + { + if (p_145842_1_ == 1) + { + this.numUsingPlayers = p_145842_2_; + return true; + } + else + { + return super.receiveClientEvent(p_145842_1_, p_145842_2_); + } + } + + @Override + public void updateEntity() + { + super.updateEntity(); + ++this.ticksSinceSync; + float f; + + if (!this.worldObj.isRemote && this.numUsingPlayers != 0 && (this.ticksSinceSync + this.xCoord + this.yCoord + this.zCoord) % 200 == 0) + { + this.numUsingPlayers = 0; + f = 5.0F; + List list = this.worldObj.getEntitiesWithinAABB(EntityPlayer.class, AxisAlignedBB.getBoundingBox((double)((float)this.xCoord - f), (double)((float)this.yCoord - f), (double)((float)this.zCoord - f), (double)((float)(this.xCoord + 1) + f), (double)((float)(this.yCoord + 1) + f), (double)((float)(this.zCoord + 1) + f))); + Iterator iterator = list.iterator(); + + while (iterator.hasNext()) + { + EntityPlayer entityplayer = (EntityPlayer)iterator.next(); + + if (entityplayer.openContainer instanceof ContainerChest) + { + IInventory iinventory = ((ContainerChest)entityplayer.openContainer).getLowerChestInventory(); + + if (iinventory == this || iinventory instanceof InventoryLargeChest && ((InventoryLargeChest)iinventory).isPartOfLargeChest(this)) + { + ++this.numUsingPlayers; + } + } + } + } + } + + @Override + public boolean isItemValidForSlot(int p_94041_1_, ItemStack itemstack) { + return true; + } + + public int getFacing() + { + return this.facing; + } + + public void setFacing(int facing_o) + { + facing = facing_o; + } + + @Override + public Packet getDescriptionPacket() + { + return Handler_CrestMount.getPacket(this); + } + + public void readFromNBT(NBTTagCompound p_145839_1_) + { + super.readFromNBT(p_145839_1_); + NBTTagList nbttaglist = p_145839_1_.getTagList("Items", 10); + this.inventory = new ItemStack[this.getSizeInventory()]; + + if (p_145839_1_.hasKey("CustomName", 8)) + { + this.name = p_145839_1_.getString("CustomName"); + } + + for (int i = 0; i < nbttaglist.tagCount(); ++i) + { + NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i); + int j = nbttagcompound1.getByte("Slot") & 255; + + if (j >= 0 && j < this.inventory.length) + { + this.inventory[j] = ItemStack.loadItemStackFromNBT(nbttagcompound1); + } + } + } + + public void writeToNBT(NBTTagCompound p_145841_1_) + { + super.writeToNBT(p_145841_1_); + NBTTagList nbttaglist = new NBTTagList(); + + for (int i = 0; i < this.inventory.length; ++i) + { + if (this.inventory[i] != null) + { + NBTTagCompound nbttagcompound1 = new NBTTagCompound(); + nbttagcompound1.setByte("Slot", (byte)i); + this.inventory[i].writeToNBT(nbttagcompound1); + nbttaglist.appendTag(nbttagcompound1); + } + } -public class TileEntity_CrestMount extends TileEntity { + p_145841_1_.setTag("Items", nbttaglist); + if (this.hasCustomInventoryName()) + { + p_145841_1_.setString("CustomName", this.name); + } + } } diff --git a/src/main/java/gmail/Lance5057/com/mod_TinkersDefense.java b/src/main/java/gmail/Lance5057/com/mod_TinkersDefense.java index 929b307..318e0e8 100644 --- a/src/main/java/gmail/Lance5057/com/mod_TinkersDefense.java +++ b/src/main/java/gmail/Lance5057/com/mod_TinkersDefense.java @@ -11,15 +11,15 @@ import gmail.Lance5057.blocks.TileEntity_CrestMount; import gmail.Lance5057.items.AeonSteelIngot; import gmail.Lance5057.items.DogbeariumIngot; import gmail.Lance5057.items.QueensGoldIngot; -import gmail.Lance5057.items.TinkerArmor; import gmail.Lance5057.proxy.CommonProxy; +import gmail.Lance5057.proxy.Handler_CrestMount; import net.minecraft.block.Block; import net.minecraft.block.material.Material; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.init.Items; import net.minecraft.item.Item; -import net.minecraft.item.ItemArmor.ArmorMaterial; import net.minecraft.item.ItemStack; +import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.fluids.Fluid; import net.minecraftforge.fluids.FluidRegistry; import net.minecraftforge.fluids.FluidStack; @@ -34,12 +34,14 @@ import tconstruct.smeltery.TinkerSmeltery; import tconstruct.tools.TinkerTools; import cpw.mods.fml.common.Mod; import cpw.mods.fml.common.Mod.EventHandler; +import cpw.mods.fml.common.Mod.Instance; import cpw.mods.fml.common.SidedProxy; import cpw.mods.fml.common.event.FMLInitializationEvent; import cpw.mods.fml.common.event.FMLPostInitializationEvent; import cpw.mods.fml.common.event.FMLPreInitializationEvent; +import cpw.mods.fml.common.network.NetworkRegistry; +import cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper; import cpw.mods.fml.common.registry.GameRegistry; -import net.minecraftforge.common.config.*; @Mod(modid="tinkersdefense", version="1.0") public class mod_TinkersDefense @@ -47,6 +49,12 @@ public class mod_TinkersDefense public static String MODID = "tinkersdefense"; public static String VERSION = "1.0"; +private static int modGuiIndex = 0; +public static final int GUI_ITEM_INV = modGuiIndex++; + +@Instance("tinkersdefense") +public static mod_TinkersDefense instance = new mod_TinkersDefense(); + public static CreativeTabs tabName = new CreativeTabs("tabName") { public Item getTabIconItem() @@ -55,6 +63,8 @@ return Items.arrow; } }; +public static SimpleNetworkWrapper network; + public static Item item_AeonSteelIngot; public static Block block_AeonSteelBlock; @@ -83,9 +93,16 @@ public static Item item_TinkerArmor; @SidedProxy(clientSide = "gmail.Lance5057.proxy.ClientProxy", serverSide = "gmail.Lance5057.proxy.CommonProxy") public static CommonProxy proxy; + + + @EventHandler public void preInit(FMLPreInitializationEvent e) { + //Network + network = NetworkRegistry.INSTANCE.newSimpleChannel("tDefense"); + Handler_CrestMount.INSTANCE.ordinal(); + //Renderers proxy.registerRenderers(); @@ -192,14 +209,22 @@ public void preInit(FMLPreInitializationEvent e) .setBlockName("CrestMount") .setCreativeTab(tabName); - GameRegistry.registerTileEntity(TileEntity_CrestMount.class, "Tile_CrestMount"); - GameRegistry.registerBlock(block_CrestMount, "Block_CrestMount"); + //item_TinkerArmor = new TinkerArmor(ArmorMaterial.IRON, 4, 1).setUnlocalizedName("Tinker_Armor"); //GameRegistry.registerItem(item_TinkerArmor,"Tinker Armor"); } @EventHandler +public void load(FMLInitializationEvent evt) +{ + GameRegistry.registerTileEntity(TileEntity_CrestMount.class, "Tile_CrestMount"); + GameRegistry.registerBlock(block_CrestMount, "Block_CrestMount"); + NetworkRegistry.INSTANCE.registerGuiHandler(mod_TinkersDefense.instance, new CommonProxy()); + MinecraftForge.EVENT_BUS.register(this); +} + +@EventHandler public void init(FMLInitializationEvent e) { System.out.print(MODID); @@ -267,6 +292,7 @@ public void init(FMLInitializationEvent e) TConstructRegistry.addToolRecipe(tool_roundShield, TinkerTools.largePlate, TinkerTools.toolRod, TinkerTools.frypanHead); TConstructRegistry.addToolRecipe(tool_heaterShield, TinkerTools.largePlate, TinkerTools.toughRod, TinkerTools.largePlate, TinkerTools.toughBinding); + } @EventHandler diff --git a/src/main/java/gmail/Lance5057/gui/Container_CrestMount.java b/src/main/java/gmail/Lance5057/gui/Container_CrestMount.java new file mode 100644 index 0000000..8db0940 --- /dev/null +++ b/src/main/java/gmail/Lance5057/gui/Container_CrestMount.java @@ -0,0 +1,88 @@ +package gmail.Lance5057.gui; + +import gmail.Lance5057.blocks.TileEntity_CrestMount; +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 Container_CrestMount extends Container +{ + protected TileEntity_CrestMount tileEntity; + + public Container_CrestMount (InventoryPlayer inventoryPlayer, TileEntity_CrestMount te){ + tileEntity = te; + te.openInventory(); + + //the Slot constructor takes the IInventory and the slot number in that it binds to + //and the x-y coordinates it resides on-screen + for (int i = 0; i < 4; i++) { + addSlotToContainer(new Slot(tileEntity, i, 62 + 18, 17 + i * 18)); + } + + //commonly used vanilla code that adds the player's inventory + bindPlayerInventory(inventoryPlayer); + } + + 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 boolean canInteractWith(EntityPlayer p_75145_1_) { + return tileEntity.isUseableByPlayer(p_75145_1_); + } + + @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 < 4) { + if (!this.mergeItemStack(stackInSlot, 0, 35, true)) { + return null; + } + } + //places it into the tileEntity is possible since its in the player inventory + else if (!this.mergeItemStack(stackInSlot, 0, 4, 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; + } + + @Override + public void onContainerClosed(EntityPlayer entityplayer) + { + super.onContainerClosed(entityplayer); + tileEntity.closeInventory(); + } + +} diff --git a/src/main/java/gmail/Lance5057/gui/GuiHandler_CrestMount.java b/src/main/java/gmail/Lance5057/gui/GuiHandler_CrestMount.java new file mode 100644 index 0000000..604f088 --- /dev/null +++ b/src/main/java/gmail/Lance5057/gui/GuiHandler_CrestMount.java @@ -0,0 +1,32 @@ +package gmail.Lance5057.gui; + +import gmail.Lance5057.blocks.TileEntity_CrestMount; +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_CrestMount 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.getTileEntity(x, y, z); + if(tileEntity instanceof TileEntity_CrestMount){ + return new Container_CrestMount(player.inventory, (TileEntity_CrestMount) 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.getTileEntity(x, y, z); + if(tileEntity instanceof TileEntity_CrestMount){ + return new Gui_CrestMount((Container_CrestMount) new Container_CrestMount(player.inventory, new TileEntity_CrestMount())); + } + return null; + + } +} diff --git a/src/main/java/gmail/Lance5057/gui/Gui_CrestMount.java b/src/main/java/gmail/Lance5057/gui/Gui_CrestMount.java new file mode 100644 index 0000000..221f5c1 --- /dev/null +++ b/src/main/java/gmail/Lance5057/gui/Gui_CrestMount.java @@ -0,0 +1,28 @@ +package gmail.Lance5057.gui; + +import org.lwjgl.opengl.GL11; + +import gmail.Lance5057.blocks.TileEntity_CrestMount; +import net.minecraft.client.gui.inventory.GuiContainer; +import net.minecraft.entity.player.InventoryPlayer; +import net.minecraft.util.ResourceLocation; + +public class Gui_CrestMount extends GuiContainer { + + private ResourceLocation RL = new ResourceLocation("tinkersdefense", "textures/gui/inv_crestmount.png"); + + public Gui_CrestMount(Container_CrestMount container) { + super(container); + } + + @Override + protected void drawGuiContainerBackgroundLayer(float p_146976_1_, + int p_146976_2_, int p_146976_3_) { + this.mc.renderEngine.bindTexture(RL); + int x = (width - xSize) / 2; + int y = (height - ySize) / 2; + this.drawTexturedModalRect(x, y, 0, 0, xSize, ySize); + + } + +} diff --git a/src/main/java/gmail/Lance5057/proxy/ClientProxy.java b/src/main/java/gmail/Lance5057/proxy/ClientProxy.java index db933ea..32c768d 100644 --- a/src/main/java/gmail/Lance5057/proxy/ClientProxy.java +++ b/src/main/java/gmail/Lance5057/proxy/ClientProxy.java @@ -2,10 +2,10 @@ package gmail.Lance5057.proxy; import gmail.Lance5057.blocks.Renderer_CrestMount; import gmail.Lance5057.blocks.TileEntity_CrestMount; -import gmail.Lance5057.proxy.CommonProxy; import gmail.Lance5057.items.ModelTinkerArmor; -import gmail.Lance5057.items.TinkerArmor; import net.minecraft.client.model.ModelBiped; +import net.minecraft.world.World; +import cpw.mods.fml.client.FMLClientHandler; import cpw.mods.fml.client.registry.ClientRegistry; @@ -17,6 +17,11 @@ public class ClientProxy extends CommonProxy { // This is for rendering entities and so forth later on ClientRegistry.bindTileEntitySpecialRenderer(TileEntity_CrestMount.class, new Renderer_CrestMount()); } + @Override + public World getClientWorld() + { + return FMLClientHandler.instance().getClient().theWorld; + } public void registerTileEntitySpecialRenderer() { diff --git a/src/main/java/gmail/Lance5057/proxy/CommonProxy.java b/src/main/java/gmail/Lance5057/proxy/CommonProxy.java index 164bf21..1b2ff1e 100644 --- a/src/main/java/gmail/Lance5057/proxy/CommonProxy.java +++ b/src/main/java/gmail/Lance5057/proxy/CommonProxy.java @@ -1,8 +1,15 @@ package gmail.Lance5057.proxy; +import gmail.Lance5057.blocks.TileEntity_CrestMount; +import gmail.Lance5057.com.mod_TinkersDefense; +import gmail.Lance5057.gui.Container_CrestMount; +import gmail.Lance5057.gui.Gui_CrestMount; import net.minecraft.client.model.ModelBiped; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.world.World; +import cpw.mods.fml.common.network.IGuiHandler; -public class CommonProxy { +public class CommonProxy implements IGuiHandler { // Client stuff public void registerRenderers() { @@ -17,5 +24,34 @@ public class CommonProxy { public ModelBiped getArmorModel(int id) { return null; - } -}
\ No newline at end of file + } + + public World getClientWorld() + { + return null; + } + + @Override + public Object getServerGuiElement(int ID, EntityPlayer player, + World world, int x, int y, int z) { + // Hooray, no 'magic' numbers - we know exactly which Gui this refers to + if (ID == mod_TinkersDefense.GUI_ITEM_INV) + { + // Use the player's held item to create the inventory + return new Container_CrestMount(player.inventory, new TileEntity_CrestMount()); + } + return null; + } + + @Override + public Object getClientGuiElement(int ID, EntityPlayer player, + World world, int x, int y, int z) { + if (ID == mod_TinkersDefense.GUI_ITEM_INV) + { + // We have to cast the new container as our custom class + // and pass in currently held item for the inventory + return new Gui_CrestMount((Container_CrestMount) new Container_CrestMount(player.inventory, new TileEntity_CrestMount())); + } + return null; + } + }
\ No newline at end of file diff --git a/src/main/java/gmail/Lance5057/proxy/Handler_CrestMount.java b/src/main/java/gmail/Lance5057/proxy/Handler_CrestMount.java new file mode 100644 index 0000000..7923c94 --- /dev/null +++ b/src/main/java/gmail/Lance5057/proxy/Handler_CrestMount.java @@ -0,0 +1,160 @@ +package gmail.Lance5057.proxy; + +import gmail.Lance5057.blocks.TileEntity_CrestMount; +import gmail.Lance5057.com.mod_TinkersDefense; +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.SimpleChannelInboundHandler; + +import java.util.EnumMap; + +import net.minecraft.network.Packet; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.World; +import cpw.mods.fml.common.FMLCommonHandler; +import cpw.mods.fml.common.network.FMLEmbeddedChannel; +import cpw.mods.fml.common.network.FMLIndexedMessageToMessageCodec; +import cpw.mods.fml.common.network.NetworkRegistry; +import cpw.mods.fml.common.network.internal.FMLProxyPacket; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; + +/** +* Handles the packet wrangling for CrestMount +* @author cpw +* +*/ +public enum Handler_CrestMount { +INSTANCE; +/** +* Our channel "pair" from {@link NetworkRegistry} +*/ +private EnumMap<Side, FMLEmbeddedChannel> channels; +/** +* Make our packet handler, and add an {@link CrestMountCodec} always +*/ +private Handler_CrestMount() +{ +this.channels = NetworkRegistry.INSTANCE.newChannel("tDefense", new CrestMountCodec()); + if (FMLCommonHandler.instance().getSide() == Side.CLIENT) + { + addClientHandler(); + } +} + + +@SideOnly(Side.CLIENT) +private void addClientHandler() { +FMLEmbeddedChannel clientChannel = this.channels.get(Side.CLIENT); + +String codec = clientChannel.findChannelHandlerNameForType(CrestMountCodec.class); +clientChannel.pipeline().addAfter(codec, "ClientHandler", new Message_CrestMountHandler()); +} + +private static class Message_CrestMountHandler extends SimpleChannelInboundHandler<Message_CrestMount> +{ +@Override +protected void channelRead0(ChannelHandlerContext ctx, Message_CrestMount msg) throws Exception +{ + World world = mod_TinkersDefense.proxy.getClientWorld(); + TileEntity te = world.getTileEntity(msg.x, msg.y, msg.z); +// if (te instanceof TileEntity_CrestMount) +// { +// ((TileEntity_CrestMount) te).handlePacketData(msg.type, msg.items); +// } + } +} +/** +* This is our "message". In fact, {@link FMLIndexedMessageToMessageCodec} +* can handle many messages on the same channel at once, using a +* discriminator byte. But for CrestMount, we only need the one message, so +* we have just this. +* +* @author cpw +* +*/ +public static class Message_CrestMount +{ +int x; +int y; +int z; +int[] items; +} +/** +* This is the codec that automatically transforms the +* {@link FMLProxyPacket} which wraps the client and server custom payload +* packets into a message we care about. +* +* @author cpw +* +*/ +private class CrestMountCodec extends FMLIndexedMessageToMessageCodec<Message_CrestMount> +{ +/** +* We register our discriminator bytes here. We only have the one type, so we only +* register one. +*/ +public CrestMountCodec() +{ +addDiscriminator(0, Message_CrestMount.class); +} +@Override +public void encodeInto(ChannelHandlerContext ctx, Message_CrestMount msg, ByteBuf target) throws Exception +{ +target.writeInt(msg.x); +target.writeInt(msg.y); +target.writeInt(msg.z); +target.writeBoolean(msg.items != null); +if (msg.items != null) +{ +int[] items = msg.items; +for (int j = 0; j < items.length; j++) +{ +int i = items[j]; +target.writeInt(i); +} +} +} +@Override +public void decodeInto(ChannelHandlerContext ctx, ByteBuf dat, Message_CrestMount msg) +{ +msg.x = dat.readInt(); +msg.y = dat.readInt(); +msg.z = dat.readInt(); +boolean hasStacks = dat.readBoolean(); +msg.items = new int[0]; +if (hasStacks) +{ +msg.items = new int[4]; +for (int i = 0; i < msg.items.length; i++) +{ +msg.items[i] = dat.readInt(); +} +} +} +} +/** +* This is a utility method called to transform a packet from a custom +* packet into a "system packet". We're called from +* {@link TileEntity#getDescriptionPacket()} in this case, but there are +* others. All network packet methods in minecraft have been adapted to +* handle {@link FMLProxyPacket} but general purpose objects can't be +* handled sadly. +* +* This method uses the {@link CrestMountCodec} to transform a custom packet +* {@link Message_CrestMount} into an {@link FMLProxyPacket} by using the +* utility method {@link FMLEmbeddedChannel#generatePacketFrom(Object)} on +* the channel to do exactly that. +* +* @param tileEntityCrestMount +* @return +*/ + public static Packet getPacket(TileEntity_CrestMount te) + { + Message_CrestMount msg = new Message_CrestMount(); + msg.x = te.xCoord; + msg.y = te.yCoord; + msg.z = te.zCoord; + return INSTANCE.channels.get(Side.SERVER).generatePacketFrom(msg); + } +} |
