Created
November 3, 2012 22:17
-
-
Save anonymous/4009087 to your computer and use it in GitHub Desktop.
HOW DO PACKETS WORK D:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.aerobit.expbooks.client; | |
import net.aerobit.expbooks.CommonProxy; | |
import net.minecraftforge.client.MinecraftForgeClient; | |
public class ClientProxy extends CommonProxy { | |
@Override | |
public void registerRenderers() { | |
MinecraftForgeClient.preloadTexture(ITEMS_PNG); | |
MinecraftForgeClient.preloadTexture(BLOCK_PNG); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.aerobit.expbooks; | |
import net.aerobit.expbooks.client.GuiExpBook; | |
import net.minecraft.src.EntityPlayer; | |
import net.minecraft.src.World; | |
import cpw.mods.fml.common.network.IGuiHandler; | |
public class CommonProxy implements IGuiHandler { | |
public static String ITEMS_PNG = "/aerobit/expbooks/items.png"; | |
public static String BLOCK_PNG = "/aerobit/expbooks/block.png"; | |
// Client stuff | |
public void registerRenderers() { | |
// Nothing here as this is the server side proxy | |
} | |
@Override | |
public Object getServerGuiElement(int id, EntityPlayer player, World world, int x, int y, int z) { | |
return null; | |
} | |
//returns an instance of the Gui I made earlier | |
@Override | |
public Object getClientGuiElement(int id, EntityPlayer player, World world, int x, int y, int z) { | |
return new GuiExpBook(player); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.aerobit.expbooks; | |
import net.minecraft.src.Block; | |
import net.minecraft.src.Item; | |
import net.minecraft.src.ItemStack; | |
import net.minecraft.src.Packet; | |
import cpw.mods.fml.common.FMLCommonHandler; | |
import cpw.mods.fml.common.Mod; | |
import cpw.mods.fml.common.Mod.Init; | |
import cpw.mods.fml.common.Mod.Instance; | |
import cpw.mods.fml.common.Mod.PostInit; | |
import cpw.mods.fml.common.Mod.PreInit; | |
import cpw.mods.fml.common.Side; | |
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.NetworkMod; | |
import cpw.mods.fml.common.network.NetworkRegistry; | |
import cpw.mods.fml.common.network.PacketDispatcher; | |
import cpw.mods.fml.common.registry.GameRegistry; | |
import cpw.mods.fml.common.registry.LanguageRegistry; | |
@Mod(modid="ExpBooks", name="ExpBooks", version="0.0.1") | |
@NetworkMod(clientSideRequired=true, serverSideRequired=false, | |
channels={"ExpBooks"}, packetHandler = PacketHandler.class) | |
public class ExpBooks { | |
// The instance of your mod that Forge uses. | |
@Instance("ExpBooks") | |
public static ExpBooks instance; | |
// Says where the client and server 'proxy' code is loaded. | |
@SidedProxy(clientSide="net.aerobit.expbooks.client.ClientProxy", serverSide="net.aerobit.expbooks.client.CommonProxy") | |
public static CommonProxy proxy; | |
private final static Item expBookItem = new ItemExpBook(5000); | |
@PreInit | |
public void preInit(FMLPreInitializationEvent event) { | |
// Stub Method | |
} | |
@Init | |
public void load(FMLInitializationEvent event) { | |
proxy.registerRenderers(); | |
GameRegistry.addRecipe(new ItemStack(expBookItem), "x", | |
'x', new ItemStack(Block.dirt)); | |
LanguageRegistry.addName(expBookItem, "Exp Book"); | |
NetworkRegistry.instance().registerGuiHandler(this, proxy); | |
} | |
@PostInit | |
public void postInit(FMLPostInitializationEvent event) { | |
// Stub Method | |
} | |
public static void sendCustomPacket(String type, byte[] data) { | |
Packet p = PacketDispatcher.getPacket(type, data); | |
if (FMLCommonHandler.instance().getSide().equals(Side.CLIENT)) { | |
PacketDispatcher.sendPacketToServer(p); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.aerobit.expbooks.client; | |
import net.aerobit.expbooks.ExpBooks; | |
import net.aerobit.expbooks.ItemExpBook; | |
import net.minecraft.src.EntityPlayer; | |
import net.minecraft.src.GuiButton; | |
import net.minecraft.src.GuiScreen; | |
import org.lwjgl.opengl.GL11; | |
public class GuiExpBook extends GuiScreen { | |
public final int xSizeOfTexture = 176; | |
public final int ySizeOfTexture = 88; | |
private int posX = (this.width - xSizeOfTexture) / 2; | |
private int posY = (this.height - ySizeOfTexture) / 2; | |
private GuiButton plus; | |
private GuiButton minus; | |
public GuiExpBook(EntityPlayer entityPlayer) { | |
} | |
@Override | |
public void updateScreen() { | |
plus.enabled = mc.thePlayer.inventory.getCurrentItem().getItemDamage() > 0; | |
minus.enabled = mc.thePlayer.inventory.getCurrentItem().getItemDamage() < mc.thePlayer.inventory.getCurrentItem().getMaxDamage(); | |
} | |
@Override | |
public void drawScreen(int x, int y, float f) { | |
drawDefaultBackground(); | |
int var4 = this.mc.renderEngine.getTexture("/aerobit/expbooks/GuiExpBook.png"); | |
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); | |
this.mc.renderEngine.bindTexture(var4); | |
int posX = (this.width - xSizeOfTexture) / 2; | |
int posY = (this.height - ySizeOfTexture) / 2; | |
drawTexturedModalRect(posX, posY, 0, 0, xSizeOfTexture, ySizeOfTexture); | |
drawString(fontRenderer, "Level: " + (30-mc.thePlayer.inventory.getCurrentItem().getItemDamage()), posX+64, posY+20, 0xEEEEEE); | |
super.drawScreen(x, y, f); | |
} | |
@Override | |
public void initGui() { | |
this.controlList.clear(); | |
posX = (this.width - xSizeOfTexture) / 2; | |
posY = (this.height - ySizeOfTexture) / 2; | |
this.controlList.add(plus = new GuiButton(0, posX + 20, posY + ySizeOfTexture / 2, 20, 20, "+")); | |
this.controlList.add(minus = new GuiButton(1, posX + xSizeOfTexture-40, posY + ySizeOfTexture / 2, 20, 20, "-")); | |
plus.enabled = mc.thePlayer.inventory.getCurrentItem().getItemDamage() > 0; | |
minus.enabled = mc.thePlayer.inventory.getCurrentItem().getItemDamage() < mc.thePlayer.inventory.getCurrentItem().getMaxDamage(); | |
} | |
@Override | |
public void actionPerformed(GuiButton button) { | |
switch(button.id) { | |
case 0: | |
ExpBooks.sendCustomPacket("ExpBooks", new byte[]{ (byte) 2} ); | |
break; | |
case 1: | |
ExpBooks.sendCustomPacket("ExpBooks", new byte[]{ (byte) 1} ); | |
break; | |
default: | |
break; | |
} | |
} | |
@Override | |
protected void keyTyped(char par1, int par2) { | |
if (par2 == 1 || par2 == this.mc.gameSettings.keyBindInventory.keyCode) { | |
this.mc.thePlayer.closeScreen(); | |
} | |
} | |
@Override | |
public boolean doesGuiPauseGame() | |
{ | |
return false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.aerobit.expbooks; | |
import net.minecraft.src.CreativeTabs; | |
import net.minecraft.src.EntityPlayer; | |
import net.minecraft.src.Item; | |
import net.minecraft.src.ItemStack; | |
import net.minecraft.src.NBTTagCompound; | |
import net.minecraft.src.NBTTagList; | |
import net.minecraft.src.NBTTagString; | |
import net.minecraft.src.World; | |
public class ItemExpBook extends Item { | |
public ItemExpBook(int i) { | |
super(i); | |
this.setMaxStackSize(1); | |
this.setCreativeTab(CreativeTabs.tabMisc); | |
this.setIconIndex(0); | |
this.setItemName("expBookItem"); | |
this.setMaxDamage(30); | |
} | |
public String getTextureFile () { | |
return CommonProxy.ITEMS_PNG; | |
} | |
public static void setDescription(String s, ItemStack itemStack) { | |
if (itemStack.stackTagCompound == null) { | |
itemStack.setTagCompound(new NBTTagCompound()); | |
} | |
if (!itemStack.stackTagCompound.hasKey("display")) { | |
itemStack.stackTagCompound.setTag("display", new NBTTagCompound()); | |
} | |
NBTTagCompound tagCompound = itemStack.stackTagCompound.getCompoundTag("display"); | |
if (!tagCompound.hasKey("Lore")) { | |
tagCompound.setTag("Lore", new NBTTagList()); | |
} | |
NBTTagList loreList = tagCompound.getTagList("Lore"); | |
if(loreList.tagCount() > 0) { | |
for(int i = 0; i <= loreList.tagCount(); i++) { | |
loreList.removeTag(0); | |
} | |
} | |
loreList.appendTag(new NBTTagString("Lore",s)); | |
} | |
public String getDescription(ItemStack itemStack) { | |
return ""; | |
} | |
@Override | |
public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer entityPlayer) { | |
entityPlayer.openGui(ExpBooks.instance, 1, world, (int) entityPlayer.posX, (int) entityPlayer.posY, (int) entityPlayer.posZ); | |
return itemStack; | |
} | |
@Override | |
public void onCreated(ItemStack itemStack, World world, EntityPlayer entityPlayer) { | |
itemStack.setItemDamage(itemStack.getMaxDamage()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.aerobit.expbooks; | |
import java.io.ByteArrayInputStream; | |
import java.io.DataInputStream; | |
import java.io.IOException; | |
import net.minecraft.client.Minecraft; | |
import net.minecraft.src.INetworkManager; | |
import net.minecraft.src.Packet250CustomPayload; | |
import cpw.mods.fml.common.network.IPacketHandler; | |
import cpw.mods.fml.common.network.Player; | |
public class PacketHandler implements IPacketHandler { | |
@Override | |
public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player playerEntity) { | |
if (packet.channel.equals("ExpBooks")) { | |
handleBook(packet); | |
} | |
} | |
private void handleBook(Packet250CustomPayload packet) { | |
Minecraft mc = Minecraft.getMinecraft(); | |
DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(packet.data)); | |
byte input; | |
try { | |
input = inputStream.readByte(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
return; | |
} | |
System.out.println(input); | |
if(input == (byte) 1) { | |
if(mc.thePlayer.inventory.getCurrentItem().getItemDamage() < 30) { | |
mc.thePlayer.inventory.getCurrentItem().damageItem(+1,mc.thePlayer); //this is the code to make it take one off of the stack | |
mc.thePlayer.experienceLevel += 1; | |
ItemExpBook.setDescription("Level: " + (30-mc.thePlayer.inventory.getCurrentItem().getItemDamage()),mc.thePlayer.inventory.getCurrentItem()); | |
} | |
} | |
if(input == (byte) 2) { | |
if(mc.thePlayer.experienceLevel > 0 && mc.thePlayer.inventory.getCurrentItem().getItemDamage() > 0) { | |
mc.thePlayer.inventory.getCurrentItem().damageItem(-1,mc.thePlayer); //this is the code to make it take one off of the stack | |
mc.thePlayer.experienceLevel -= 1; | |
ItemExpBook.setDescription("Level: " + (30-mc.thePlayer.inventory.getCurrentItem().getItemDamage()),mc.thePlayer.inventory.getCurrentItem()); | |
} | |
} | |
} | |
public static String convertStreamToString(java.io.InputStream is) { | |
java.util.Scanner s = new java.util.Scanner(is, "UTF-16").useDelimiter("\\A"); | |
return s.hasNext() ? s.next() : ""; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment