mirror of
https://github.com/GTNewHorizons/Baubles-Expanded.git
synced 2026-11-22 04:52:28 +00:00
Initial commit
This commit is contained in:
commit
e37de23340
41 files changed
+2123
No files matched your search
@@ -0,0 +1,7 @@
|
||||
package baubles.api;
|
||||
|
||||
public enum BaubleType {
|
||||
RING,
|
||||
AMULET,
|
||||
BELT
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package baubles.api;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @author Azanor
|
||||
*
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class BaublesApi {
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package baubles.api;
|
||||
|
||||
import net.minecraft.entity.EntityLivingBase;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Azanor
|
||||
*
|
||||
* This interface should be extended by items that can be worn in bauble slots
|
||||
*
|
||||
*/
|
||||
|
||||
public interface IBauble {
|
||||
|
||||
/**
|
||||
* This method return the type of bauble this is.
|
||||
* Type is used to determine the slots it can go into.
|
||||
*/
|
||||
public BaubleType getBaubleType(ItemStack itemstack);
|
||||
|
||||
/**
|
||||
* This method is called once per tick if the bauble is being worn by a player
|
||||
*/
|
||||
public void onWornTick(ItemStack itemstack, EntityLivingBase player);
|
||||
|
||||
/**
|
||||
* This method is called when the bauble is equipped by a player
|
||||
*/
|
||||
public void onEquipped(ItemStack itemstack, EntityLivingBase player);
|
||||
|
||||
/**
|
||||
* This method is called when the bauble is unequipped by a player
|
||||
*/
|
||||
public void onUnequipped(ItemStack itemstack, EntityLivingBase player);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
|
||||
package baubles.client;
|
||||
|
||||
import net.minecraft.client.multiplayer.WorldClient;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.world.World;
|
||||
import baubles.client.gui.GuiPlayerExpanded;
|
||||
import baubles.common.Baubles;
|
||||
import baubles.common.CommonProxy;
|
||||
import baubles.common.event.KeyHandler;
|
||||
import cpw.mods.fml.client.FMLClientHandler;
|
||||
import cpw.mods.fml.common.FMLCommonHandler;
|
||||
|
||||
public class ClientProxy extends CommonProxy {
|
||||
|
||||
@Override
|
||||
public void registerHandlers() {}
|
||||
|
||||
@Override
|
||||
public void registerKeyBindings() {
|
||||
keyHandler = new KeyHandler();
|
||||
FMLCommonHandler.instance().bus().register(keyHandler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
|
||||
if (world instanceof WorldClient) {
|
||||
switch (ID) {
|
||||
case Baubles.GUI: return new GuiPlayerExpanded(player);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public World getClientWorld() {
|
||||
return FMLClientHandler.instance().getClient().theWorld;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
package baubles.client.gui;
|
||||
|
||||
import net.minecraft.client.gui.GuiButton;
|
||||
import net.minecraft.client.gui.achievement.GuiAchievements;
|
||||
import net.minecraft.client.gui.achievement.GuiStats;
|
||||
import net.minecraft.client.gui.inventory.GuiContainerCreative;
|
||||
import net.minecraft.client.renderer.InventoryEffectRenderer;
|
||||
import net.minecraft.client.renderer.OpenGlHelper;
|
||||
import net.minecraft.client.renderer.RenderHelper;
|
||||
import net.minecraft.client.renderer.entity.RenderManager;
|
||||
import net.minecraft.client.resources.I18n;
|
||||
import net.minecraft.entity.EntityLivingBase;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.opengl.GL12;
|
||||
|
||||
import baubles.common.Baubles;
|
||||
import baubles.common.container.ContainerPlayerExpanded;
|
||||
|
||||
public class GuiPlayerExpanded extends InventoryEffectRenderer {
|
||||
|
||||
protected static final ResourceLocation background =
|
||||
new ResourceLocation("baubles","textures/gui/expanded_inventory.png");
|
||||
|
||||
/**
|
||||
* x size of the inventory window in pixels. Defined as float, passed as int
|
||||
*/
|
||||
private float xSizeFloat;
|
||||
/**
|
||||
* y size of the inventory window in pixels. Defined as float, passed as int.
|
||||
*/
|
||||
private float ySizeFloat;
|
||||
|
||||
public GuiPlayerExpanded(EntityPlayer player)
|
||||
{
|
||||
super(new ContainerPlayerExpanded(player.inventory, !player.worldObj.isRemote, player));
|
||||
this.allowUserInput = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called from the main game loop to update the screen.
|
||||
*/
|
||||
@Override
|
||||
public void updateScreen()
|
||||
{
|
||||
if (this.mc.playerController.isInCreativeMode())
|
||||
{
|
||||
this.mc.displayGuiScreen(new GuiContainerCreative(this.mc.thePlayer));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the buttons (and other controls) to the screen in question.
|
||||
*/
|
||||
@Override
|
||||
public void initGui()
|
||||
{
|
||||
this.buttonList.clear();
|
||||
|
||||
if (this.mc.playerController.isInCreativeMode())
|
||||
{
|
||||
this.mc.displayGuiScreen(new GuiContainerCreative(this.mc.thePlayer));
|
||||
}
|
||||
else
|
||||
{
|
||||
super.initGui();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Draw the foreground layer for the GuiContainer (everything in front of the items)
|
||||
*/
|
||||
@Override
|
||||
protected void drawGuiContainerForegroundLayer(int p_146979_1_, int p_146979_2_)
|
||||
{
|
||||
this.fontRendererObj.drawString(I18n.format("container.crafting", new Object[0]), 106, 16, 4210752);
|
||||
}
|
||||
|
||||
/**
|
||||
* Draws the screen and all the components in it.
|
||||
*/
|
||||
@Override
|
||||
public void drawScreen(int par1, int par2, float par3)
|
||||
{
|
||||
super.drawScreen(par1, par2, par3);
|
||||
this.xSizeFloat = (float)par1;
|
||||
this.ySizeFloat = (float)par2;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void drawGuiContainerBackgroundLayer(float p_146976_1_, int p_146976_2_, int p_146976_3_)
|
||||
{
|
||||
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
|
||||
this.mc.getTextureManager().bindTexture(background);
|
||||
int k = this.guiLeft;
|
||||
int l = this.guiTop;
|
||||
this.drawTexturedModalRect(k, l, 0, 0, this.xSize, this.ySize);
|
||||
drawPlayerModel(k + 51, l + 75, 30, (float)(k + 51) - this.xSizeFloat, (float)(l + 75 - 50) - this.ySizeFloat, this.mc.thePlayer);
|
||||
}
|
||||
|
||||
public static void drawPlayerModel(int x, int y, int scale, float yaw, float pitch, EntityLivingBase playerdrawn)
|
||||
{
|
||||
GL11.glEnable(GL11.GL_COLOR_MATERIAL);
|
||||
GL11.glPushMatrix();
|
||||
GL11.glTranslatef((float)x, (float)y, 50.0F);
|
||||
GL11.glScalef((float)(-scale), (float)scale, (float)scale);
|
||||
GL11.glRotatef(180.0F, 0.0F, 0.0F, 1.0F);
|
||||
float f2 = playerdrawn.renderYawOffset;
|
||||
float f3 = playerdrawn.rotationYaw;
|
||||
float f4 = playerdrawn.rotationPitch;
|
||||
float f5 = playerdrawn.prevRotationYawHead;
|
||||
float f6 = playerdrawn.rotationYawHead;
|
||||
GL11.glRotatef(135.0F, 0.0F, 1.0F, 0.0F);
|
||||
RenderHelper.enableStandardItemLighting();
|
||||
GL11.glRotatef(-135.0F, 0.0F, 1.0F, 0.0F);
|
||||
GL11.glRotatef(-((float)Math.atan((double)(pitch / 40.0F))) * 20.0F, 1.0F, 0.0F, 0.0F);
|
||||
playerdrawn.renderYawOffset = (float)Math.atan((double)(yaw / 40.0F)) * 20.0F;
|
||||
playerdrawn.rotationYaw = (float)Math.atan((double)(yaw / 40.0F)) * 40.0F;
|
||||
playerdrawn.rotationPitch = -((float)Math.atan((double)(pitch / 40.0F))) * 20.0F;
|
||||
playerdrawn.rotationYawHead = playerdrawn.rotationYaw;
|
||||
playerdrawn.prevRotationYawHead = playerdrawn.rotationYaw;
|
||||
GL11.glTranslatef(0.0F, playerdrawn.yOffset, 0.0F);
|
||||
RenderManager.instance.playerViewY = 180.0F;
|
||||
RenderManager.instance.renderEntityWithPosYaw(playerdrawn, 0.0D, 0.0D, 0.0D, 0.0F, 1.0F);
|
||||
playerdrawn.renderYawOffset = f2;
|
||||
playerdrawn.rotationYaw = f3;
|
||||
playerdrawn.rotationPitch = f4;
|
||||
playerdrawn.prevRotationYawHead = f5;
|
||||
playerdrawn.rotationYawHead = f6;
|
||||
GL11.glPopMatrix();
|
||||
RenderHelper.disableStandardItemLighting();
|
||||
GL11.glDisable(GL12.GL_RESCALE_NORMAL);
|
||||
OpenGlHelper.setActiveTexture(OpenGlHelper.lightmapTexUnit);
|
||||
GL11.glDisable(GL11.GL_TEXTURE_2D);
|
||||
OpenGlHelper.setActiveTexture(OpenGlHelper.defaultTexUnit);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void actionPerformed(GuiButton button)
|
||||
{
|
||||
if (button.id == 0)
|
||||
{
|
||||
this.mc.displayGuiScreen(new GuiAchievements(this, this.mc.thePlayer.getStatFileWriter()));
|
||||
}
|
||||
|
||||
if (button.id == 1)
|
||||
{
|
||||
this.mc.displayGuiScreen(new GuiStats(this, this.mc.thePlayer.getStatFileWriter()));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void keyTyped(char par1, int par2) {
|
||||
if (par2 == Baubles.proxy.keyHandler.key.getKeyCode())
|
||||
{
|
||||
this.mc.thePlayer.closeScreen();
|
||||
}
|
||||
super.keyTyped(par1, par2);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package baubles.common;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import net.minecraft.creativetab.CreativeTabs;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import baubles.common.event.EventHandlerEntity;
|
||||
import baubles.common.lib.CreativeTabBaubles;
|
||||
import baubles.common.lib.PlayerHandler;
|
||||
import baubles.common.network.EventHandlerNetwork;
|
||||
import baubles.common.network.PacketPipeline;
|
||||
import cpw.mods.fml.common.FMLCommonHandler;
|
||||
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;
|
||||
|
||||
@Mod(modid = Baubles.MODID, name = Baubles.MODNAME, version = Baubles.VERSION, dependencies="required-after:Forge@[10.12.0.1034,);")
|
||||
|
||||
public class Baubles {
|
||||
|
||||
public static final String MODID = "Baubles";
|
||||
public static final String MODNAME = "Baubles";
|
||||
public static final String VERSION = "1.0.0.0";
|
||||
|
||||
@SidedProxy(clientSide = "baubles.client.ClientProxy", serverSide = "baubles.common.CommonProxy")
|
||||
public static CommonProxy proxy;
|
||||
|
||||
@Instance(value=Baubles.MODID)
|
||||
public static Baubles instance;
|
||||
|
||||
|
||||
public EventHandlerNetwork networkEventHandler;
|
||||
public EventHandlerEntity entityEventHandler;
|
||||
public PlayerHandler playerHandler;
|
||||
public File modDir;
|
||||
|
||||
public static final PacketPipeline packetPipeline = new PacketPipeline();
|
||||
public static final Logger logger = LogManager.getLogger("Baubles");
|
||||
public static final int GUI = 0;
|
||||
public static CreativeTabs tabBaubles = new CreativeTabBaubles(CreativeTabs.getNextID(), "baubles");
|
||||
|
||||
@EventHandler
|
||||
public void preInit(FMLPreInitializationEvent event) {
|
||||
event.getModMetadata().version = Baubles.VERSION;
|
||||
modDir = event.getModConfigurationDirectory();
|
||||
|
||||
try {
|
||||
Config.initialize(event.getSuggestedConfigurationFile());
|
||||
} catch (Exception e) {
|
||||
Baubles.logger.error("BAUBLES has a problem loading it's configuration");
|
||||
} finally {
|
||||
if (Config.config!=null) Config.save();
|
||||
}
|
||||
|
||||
entityEventHandler = new EventHandlerEntity();
|
||||
playerHandler = new PlayerHandler();
|
||||
|
||||
MinecraftForge.EVENT_BUS.register(entityEventHandler);
|
||||
FMLCommonHandler.instance().bus().register(new EventHandlerNetwork());
|
||||
proxy.registerHandlers();
|
||||
|
||||
/////////////////////
|
||||
|
||||
Config.save();
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void init(FMLInitializationEvent evt) {
|
||||
packetPipeline.initialise();
|
||||
NetworkRegistry.INSTANCE.registerGuiHandler(instance, proxy);
|
||||
proxy.registerKeyBindings();
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void postInit(FMLPostInitializationEvent evt) {
|
||||
Config.initRecipe();
|
||||
packetPipeline.postInitialise();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
|
||||
package baubles.common;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.world.World;
|
||||
import baubles.common.container.ContainerPlayerExpanded;
|
||||
import baubles.common.event.KeyHandler;
|
||||
import cpw.mods.fml.common.network.IGuiHandler;
|
||||
|
||||
|
||||
|
||||
public class CommonProxy implements IGuiHandler {
|
||||
|
||||
public KeyHandler keyHandler;
|
||||
|
||||
public void registerHandlers() {}
|
||||
|
||||
|
||||
@Override
|
||||
public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
|
||||
switch (ID) {
|
||||
case Baubles.GUI: return new ContainerPlayerExpanded(player.inventory, !world.isRemote, player);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public World getClientWorld() {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public void registerKeyBindings() {}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package baubles.common;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import net.minecraft.init.Items;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraftforge.common.config.Configuration;
|
||||
import baubles.common.items.ItemRing;
|
||||
import cpw.mods.fml.common.registry.GameRegistry;
|
||||
|
||||
|
||||
public class Config {
|
||||
|
||||
public static Configuration config;
|
||||
public static Item itemRing;
|
||||
|
||||
public static void initialize(File file)
|
||||
{
|
||||
config = new Configuration(file);
|
||||
config.load();
|
||||
|
||||
itemRing =(new ItemRing()).setUnlocalizedName("Ring");
|
||||
GameRegistry.registerItem(itemRing, "Ring", Baubles.MODID);
|
||||
|
||||
//save it
|
||||
config.save();
|
||||
}
|
||||
|
||||
|
||||
public static void save()
|
||||
{
|
||||
config.save();
|
||||
}
|
||||
|
||||
public static void initRecipe() {
|
||||
GameRegistry.addShapedRecipe(
|
||||
new ItemStack(itemRing), new Object[] {
|
||||
"PIP", "IPI", "PIP",
|
||||
Character.valueOf('I'), new ItemStack(Items.iron_ingot),
|
||||
Character.valueOf('P'), new ItemStack(Items.potionitem,1,8226)});
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,263 @@
|
||||
package baubles.common.container;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.entity.player.InventoryPlayer;
|
||||
import net.minecraft.inventory.Container;
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.inventory.InventoryCraftResult;
|
||||
import net.minecraft.inventory.InventoryCrafting;
|
||||
import net.minecraft.inventory.Slot;
|
||||
import net.minecraft.inventory.SlotCrafting;
|
||||
import net.minecraft.item.ItemArmor;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.item.crafting.CraftingManager;
|
||||
import baubles.api.BaubleType;
|
||||
import baubles.api.IBauble;
|
||||
import baubles.common.Baubles;
|
||||
|
||||
public class ContainerPlayerExpanded extends Container
|
||||
{
|
||||
/**
|
||||
* The crafting matrix inventory.
|
||||
*/
|
||||
public InventoryCrafting craftMatrix = new InventoryCrafting(this, 2, 2);
|
||||
public IInventory craftResult = new InventoryCraftResult();
|
||||
public InventoryBaubles baubles;
|
||||
/**
|
||||
* Determines if inventory manipulation should be handled.
|
||||
*/
|
||||
public boolean isLocalWorld;
|
||||
private final EntityPlayer thePlayer;
|
||||
|
||||
public ContainerPlayerExpanded(InventoryPlayer playerInv, boolean par2, EntityPlayer player)
|
||||
{
|
||||
this.isLocalWorld = par2;
|
||||
this.thePlayer = player;
|
||||
baubles = new InventoryBaubles(player);
|
||||
baubles.setEventHandler(this);
|
||||
if (!player.worldObj.isRemote) {
|
||||
baubles.stackList = Baubles.instance.playerHandler.getPlayerBaubles(player).stackList;
|
||||
}
|
||||
|
||||
this.addSlotToContainer(new SlotCrafting(playerInv.player, this.craftMatrix, this.craftResult, 0, 144, 36));
|
||||
int i;
|
||||
int j;
|
||||
|
||||
for (i = 0; i < 2; ++i)
|
||||
{
|
||||
for (j = 0; j < 2; ++j)
|
||||
{
|
||||
this.addSlotToContainer(new Slot(this.craftMatrix, j + i * 2, 106 + j * 18, 26 + i * 18));
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < 4; ++i)
|
||||
{
|
||||
final int k = i;
|
||||
this.addSlotToContainer(new Slot(playerInv, playerInv.getSizeInventory() - 1 - i, 8, 8 + i * 18)
|
||||
{
|
||||
@Override
|
||||
public int getSlotStackLimit() { return 1; }
|
||||
@Override
|
||||
public boolean isItemValid(ItemStack par1ItemStack)
|
||||
{
|
||||
if (par1ItemStack == null) return false;
|
||||
return par1ItemStack.getItem().isValidArmor(par1ItemStack, k, thePlayer);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
this.addSlotToContainer(new SlotBauble(baubles,BaubleType.AMULET,0,80,8 + 0 * 18));
|
||||
this.addSlotToContainer(new SlotBauble(baubles,BaubleType.RING,1,80,8 + 1 * 18));
|
||||
this.addSlotToContainer(new SlotBauble(baubles,BaubleType.RING,2,80,8 + 2 * 18));
|
||||
this.addSlotToContainer(new SlotBauble(baubles,BaubleType.BELT,3,80,8 + 3 * 18));
|
||||
|
||||
for (i = 0; i < 3; ++i)
|
||||
{
|
||||
for (j = 0; j < 9; ++j)
|
||||
{
|
||||
this.addSlotToContainer(new Slot(playerInv, j + (i + 1) * 9, 8 + j * 18, 84 + i * 18));
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < 9; ++i)
|
||||
{
|
||||
this.addSlotToContainer(new Slot(playerInv, i, 8 + i * 18, 142));
|
||||
}
|
||||
|
||||
this.onCraftMatrixChanged(this.craftMatrix);
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback for when the crafting matrix is changed.
|
||||
*/
|
||||
@Override
|
||||
public void onCraftMatrixChanged(IInventory par1IInventory)
|
||||
{
|
||||
this.craftResult.setInventorySlotContents(0, CraftingManager.getInstance().findMatchingRecipe(this.craftMatrix, this.thePlayer.worldObj));
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when the container is closed.
|
||||
*/
|
||||
@Override
|
||||
public void onContainerClosed(EntityPlayer player)
|
||||
{
|
||||
super.onContainerClosed(player);
|
||||
|
||||
for (int i = 0; i < 4; ++i)
|
||||
{
|
||||
ItemStack itemstack = this.craftMatrix.getStackInSlotOnClosing(i);
|
||||
|
||||
if (itemstack != null)
|
||||
{
|
||||
player.dropPlayerItemWithRandomChoice(itemstack, false);
|
||||
}
|
||||
}
|
||||
|
||||
this.craftResult.setInventorySlotContents(0, (ItemStack)null);
|
||||
if (!player.worldObj.isRemote) {
|
||||
Baubles.instance.playerHandler.setPlayerBaubles(player, baubles);
|
||||
Baubles.instance.playerHandler.updatePlayerBaubles(player);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canInteractWith(EntityPlayer par1EntityPlayer)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when a player shift-clicks on a slot. You must override this or you will crash when someone does that.
|
||||
*/
|
||||
@Override
|
||||
public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2)
|
||||
{
|
||||
ItemStack itemstack = null;
|
||||
Slot slot = (Slot)this.inventorySlots.get(par2);
|
||||
|
||||
if (slot != null && slot.getHasStack())
|
||||
{
|
||||
ItemStack itemstack1 = slot.getStack();
|
||||
itemstack = itemstack1.copy();
|
||||
|
||||
if (par2 == 0)
|
||||
{
|
||||
if (!this.mergeItemStack(itemstack1, 9+4, 45+4, true))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
slot.onSlotChange(itemstack1, itemstack);
|
||||
}
|
||||
else if (par2 >= 1 && par2 < 5)
|
||||
{
|
||||
if (!this.mergeItemStack(itemstack1, 9+4, 45+4, false))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
else if (par2 >= 5 && par2 < 9)
|
||||
{
|
||||
if (!this.mergeItemStack(itemstack1, 9+4, 45+4, false))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
else if (itemstack.getItem() instanceof ItemArmor &&
|
||||
!((Slot)this.inventorySlots.get(5 + ((ItemArmor)itemstack.getItem()).armorType)).getHasStack())
|
||||
{
|
||||
int j = 5 + ((ItemArmor)itemstack.getItem()).armorType;
|
||||
|
||||
if (!this.mergeItemStack(itemstack1, j, j + 1, false))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
else if (itemstack.getItem() instanceof IBauble &&
|
||||
((IBauble)itemstack.getItem()).getBaubleType(itemstack)==BaubleType.AMULET &&
|
||||
!((Slot)this.inventorySlots.get(9)).getHasStack())
|
||||
{
|
||||
int j = 9;
|
||||
if (!this.mergeItemStack(itemstack1, j, j + 1, false))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
else if (itemstack.getItem() instanceof IBauble &&
|
||||
((IBauble)itemstack.getItem()).getBaubleType(itemstack)==BaubleType.RING &&
|
||||
!((Slot)this.inventorySlots.get(10)).getHasStack())
|
||||
{
|
||||
int j = 10;
|
||||
if (!this.mergeItemStack(itemstack1, j, j + 1, false))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
else if (itemstack.getItem() instanceof IBauble &&
|
||||
((IBauble)itemstack.getItem()).getBaubleType(itemstack)==BaubleType.RING &&
|
||||
!((Slot)this.inventorySlots.get(11)).getHasStack())
|
||||
{
|
||||
int j = 11;
|
||||
if (!this.mergeItemStack(itemstack1, j, j + 1, false))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
else if (itemstack.getItem() instanceof IBauble &&
|
||||
((IBauble)itemstack.getItem()).getBaubleType(itemstack)==BaubleType.BELT &&
|
||||
!((Slot)this.inventorySlots.get(12)).getHasStack())
|
||||
{
|
||||
int j = 12;
|
||||
if (!this.mergeItemStack(itemstack1, j, j + 1, false))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
else if (par2 >= 9+4 && par2 < 36+4)
|
||||
{
|
||||
if (!this.mergeItemStack(itemstack1, 36+4, 45+4, false))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
else if (par2 >= 36+4 && par2 < 45+4)
|
||||
{
|
||||
if (!this.mergeItemStack(itemstack1, 9+4, 36+4, false))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
else if (!this.mergeItemStack(itemstack1, 9+4, 45+4, false))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (itemstack1.stackSize == 0)
|
||||
{
|
||||
slot.putStack((ItemStack)null);
|
||||
}
|
||||
else
|
||||
{
|
||||
slot.onSlotChanged();
|
||||
}
|
||||
|
||||
if (itemstack1.stackSize == itemstack.stackSize)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
slot.onPickupFromSlot(par1EntityPlayer, itemstack1);
|
||||
}
|
||||
|
||||
return itemstack;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean func_94530_a(ItemStack par1ItemStack, Slot par2Slot)
|
||||
{
|
||||
return par2Slot.inventory != this.craftResult && super.func_94530_a(par1ItemStack, par2Slot);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,220 @@
|
||||
package baubles.common.container;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.inventory.Container;
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
import baubles.api.BaubleType;
|
||||
import baubles.api.IBauble;
|
||||
|
||||
public class InventoryBaubles implements IInventory {
|
||||
public ItemStack[] stackList;
|
||||
private Container eventHandler;
|
||||
public WeakReference<EntityPlayer> player;
|
||||
|
||||
public InventoryBaubles(EntityPlayer player) {
|
||||
this.stackList = new ItemStack[4];
|
||||
this.player = new WeakReference<EntityPlayer>(player);
|
||||
}
|
||||
|
||||
public Container getEventHandler() {
|
||||
return eventHandler;
|
||||
}
|
||||
|
||||
public void setEventHandler(Container eventHandler) {
|
||||
this.eventHandler = eventHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of slots in the inventory.
|
||||
*/
|
||||
@Override
|
||||
public int getSizeInventory() {
|
||||
return this.stackList.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the stack in slot i
|
||||
*/
|
||||
@Override
|
||||
public ItemStack getStackInSlot(int par1) {
|
||||
return par1 >= this.getSizeInventory() ? null : this.stackList[par1];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name of the inventory
|
||||
*/
|
||||
@Override
|
||||
public String getInventoryName() {
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns if the inventory is named
|
||||
*/
|
||||
@Override
|
||||
public boolean hasCustomInventoryName() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* When some containers are closed they call this on each slot, then drop
|
||||
* whatever it returns as an EntityItem - like when you close a workbench
|
||||
* GUI.
|
||||
*/
|
||||
@Override
|
||||
public ItemStack getStackInSlotOnClosing(int par1) {
|
||||
if (this.stackList[par1] != null) {
|
||||
ItemStack itemstack = this.stackList[par1];
|
||||
this.stackList[par1] = null;
|
||||
return itemstack;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes from an inventory slot (first arg) up to a specified number
|
||||
* (second arg) of items and returns them in a new stack.
|
||||
*/
|
||||
@Override
|
||||
public ItemStack decrStackSize(int par1, int par2) {
|
||||
if (this.stackList[par1] != null) {
|
||||
ItemStack itemstack;
|
||||
|
||||
if (this.stackList[par1].stackSize <= par2) {
|
||||
itemstack = this.stackList[par1];
|
||||
this.stackList[par1] = null;
|
||||
|
||||
if (itemstack !=null && itemstack.getItem() instanceof IBauble) {
|
||||
((IBauble)itemstack.getItem()).onUnequipped(itemstack, player.get());
|
||||
}
|
||||
|
||||
if (eventHandler!=null) this.eventHandler.onCraftMatrixChanged(this);
|
||||
return itemstack;
|
||||
} else {
|
||||
itemstack = this.stackList[par1].splitStack(par2);
|
||||
|
||||
if (this.stackList[par1].stackSize == 0) {
|
||||
this.stackList[par1] = null;
|
||||
}
|
||||
|
||||
if (itemstack !=null && itemstack.getItem() instanceof IBauble) {
|
||||
((IBauble)itemstack.getItem()).onUnequipped(itemstack, player.get());
|
||||
}
|
||||
if (eventHandler!=null) this.eventHandler.onCraftMatrixChanged(this);
|
||||
return itemstack;
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the given item stack to the specified slot in the inventory (can be
|
||||
* crafting or armor sections).
|
||||
*/
|
||||
@Override
|
||||
public void setInventorySlotContents(int par1, ItemStack stack) {
|
||||
this.stackList[par1] = stack;
|
||||
if (stack !=null && stack.getItem() instanceof IBauble) {
|
||||
((IBauble)stack.getItem()).onEquipped(stack, player.get());
|
||||
}
|
||||
if (eventHandler!=null) this.eventHandler.onCraftMatrixChanged(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the maximum stack size for a inventory slot.
|
||||
*/
|
||||
@Override
|
||||
public int getInventoryStackLimit() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* For tile entities, ensures the chunk containing the tile entity is saved
|
||||
* to disk later - the game won't think it hasn't changed and skip it.
|
||||
*/
|
||||
@Override
|
||||
public void markDirty() { }
|
||||
|
||||
/**
|
||||
* Do not make give this method the name canInteractWith because it clashes
|
||||
* with Container
|
||||
*/
|
||||
@Override
|
||||
public boolean isUseableByPlayer(EntityPlayer par1EntityPlayer) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void openInventory() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void closeInventory() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if automation is allowed to insert the given stack (ignoring
|
||||
* stack size) into the given slot.
|
||||
*/
|
||||
@Override
|
||||
public boolean isItemValidForSlot(int i, ItemStack stack) {
|
||||
if (stack == null || !(stack.getItem() instanceof IBauble))
|
||||
return false;
|
||||
if (i == 0
|
||||
&& ((IBauble) stack.getItem()).getBaubleType(stack) == BaubleType.AMULET)
|
||||
return true;
|
||||
if ((i == 1 || i == 2)
|
||||
&& ((IBauble) stack.getItem()).getBaubleType(stack) == BaubleType.RING)
|
||||
return true;
|
||||
if (i == 3
|
||||
&& ((IBauble) stack.getItem()).getBaubleType(stack) == BaubleType.BELT)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public void saveNBT(EntityPlayer player) {
|
||||
NBTTagCompound tags = player.getEntityData();
|
||||
NBTTagList tagList = new NBTTagList();
|
||||
NBTTagCompound invSlot;
|
||||
for (int i = 0; i < this.stackList.length; ++i) {
|
||||
if (this.stackList[i] != null) {
|
||||
invSlot = new NBTTagCompound();
|
||||
invSlot.setByte("Slot", (byte) i);
|
||||
this.stackList[i].writeToNBT(invSlot);
|
||||
tagList.appendTag(invSlot);
|
||||
}
|
||||
}
|
||||
tags.setTag("Baubles.Inventory", tagList);
|
||||
}
|
||||
|
||||
public void readNBT(EntityPlayer player) {
|
||||
NBTTagCompound tags = player.getEntityData();
|
||||
NBTTagList tagList = tags.getTagList("Baubles.Inventory", 10);
|
||||
for (int i = 0; i < tagList.tagCount(); ++i) {
|
||||
NBTTagCompound nbttagcompound = (NBTTagCompound) tagList.getCompoundTagAt(i);
|
||||
int j = nbttagcompound.getByte("Slot") & 255;
|
||||
ItemStack itemstack = ItemStack.loadItemStackFromNBT(nbttagcompound);
|
||||
if (itemstack != null) {
|
||||
this.stackList[j] = itemstack;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void dropItems(EntityPlayer player) {
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
if (this.stackList[i] != null) {
|
||||
player.dropPlayerItemWithRandomChoice(this.stackList[i], true);
|
||||
this.stackList[i] = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package baubles.common.container;
|
||||
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.inventory.Slot;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import baubles.api.BaubleType;
|
||||
import baubles.api.IBauble;
|
||||
|
||||
public class SlotBauble extends Slot
|
||||
{
|
||||
|
||||
BaubleType type;
|
||||
|
||||
public SlotBauble(IInventory par2IInventory, BaubleType type, int par3, int par4, int par5)
|
||||
{
|
||||
super(par2IInventory, par3, par4, par5);
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check if the stack is a valid item for this slot. Always true beside for the armor slots.
|
||||
*/
|
||||
@Override
|
||||
public boolean isItemValid(ItemStack stack)
|
||||
{
|
||||
return stack!=null && stack.getItem() !=null &&
|
||||
stack.getItem() instanceof IBauble &&
|
||||
((IBauble)stack.getItem()).getBaubleType(stack)==this.type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSlotStackLimit()
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package baubles.common.event;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraftforge.event.entity.living.LivingDeathEvent;
|
||||
import net.minecraftforge.event.entity.player.PlayerEvent;
|
||||
import baubles.api.IBauble;
|
||||
import baubles.common.Baubles;
|
||||
import baubles.common.container.InventoryBaubles;
|
||||
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
||||
|
||||
public class EventHandlerEntity {
|
||||
|
||||
@SubscribeEvent
|
||||
public void playerTick(PlayerEvent.LivingUpdateEvent event) {
|
||||
|
||||
//player events
|
||||
if (event.entity instanceof EntityPlayer) {
|
||||
EntityPlayer player = (EntityPlayer)event.entity;
|
||||
|
||||
InventoryBaubles baubles = Baubles.instance.playerHandler.getPlayerBaubles(player);
|
||||
for (int a=0;a<baubles.getSizeInventory();a++) {
|
||||
if (baubles.getStackInSlot(a)!=null && baubles.getStackInSlot(a).getItem() instanceof IBauble) {
|
||||
((IBauble)baubles.getStackInSlot(a).getItem()).onWornTick(baubles.getStackInSlot(a), player);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void playerDeath(LivingDeathEvent event) {
|
||||
if (event.entity instanceof EntityPlayer) {
|
||||
EntityPlayer player = (EntityPlayer)event.entity;
|
||||
Baubles.instance.playerHandler.getPlayerBaubles(player).dropItems(player);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package baubles.common.event;
|
||||
|
||||
import net.minecraft.client.settings.KeyBinding;
|
||||
|
||||
import org.lwjgl.input.Keyboard;
|
||||
|
||||
import baubles.common.Baubles;
|
||||
import baubles.common.network.PacketOpenBaublesInventory;
|
||||
import cpw.mods.fml.client.FMLClientHandler;
|
||||
import cpw.mods.fml.client.registry.ClientRegistry;
|
||||
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
||||
import cpw.mods.fml.common.gameevent.TickEvent.Phase;
|
||||
import cpw.mods.fml.common.gameevent.TickEvent.PlayerTickEvent;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
|
||||
public class KeyHandler {
|
||||
|
||||
public KeyBinding key = new KeyBinding("Baubles Inventory",
|
||||
Keyboard.KEY_B, "key.categories.inventory");
|
||||
|
||||
public KeyHandler() {
|
||||
ClientRegistry.registerKeyBinding(key);
|
||||
}
|
||||
|
||||
@SideOnly(value=Side.CLIENT)
|
||||
@SubscribeEvent
|
||||
public void playerTick(PlayerTickEvent event) {
|
||||
if (event.side == Side.SERVER) return;
|
||||
if (event.phase == Phase.START) {
|
||||
if (key.getIsKeyPressed() && FMLClientHandler.instance().getClient().inGameHasFocus) {
|
||||
Baubles.packetPipeline.sendToServer(new PacketOpenBaublesInventory(event.player));
|
||||
// event.player.openGui(Baubles.instance, Baubles.GUI, event.player.worldObj, 0, 0, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package baubles.common.items;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.client.renderer.texture.IIconRegister;
|
||||
import net.minecraft.creativetab.CreativeTabs;
|
||||
import net.minecraft.entity.EntityLivingBase;
|
||||
import net.minecraft.item.EnumRarity;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.potion.Potion;
|
||||
import net.minecraft.potion.PotionEffect;
|
||||
import net.minecraft.util.IIcon;
|
||||
import baubles.api.BaubleType;
|
||||
import baubles.api.IBauble;
|
||||
import baubles.common.Baubles;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
|
||||
public class ItemRing extends Item implements IBauble
|
||||
{
|
||||
|
||||
public ItemRing()
|
||||
{
|
||||
super();
|
||||
this.setMaxStackSize(1);
|
||||
this.setHasSubtypes(true);
|
||||
this.setMaxDamage(0);
|
||||
setCreativeTab(Baubles.tabBaubles);
|
||||
}
|
||||
|
||||
public IIcon icon;
|
||||
@SideOnly(Side.CLIENT)
|
||||
@Override
|
||||
public void registerIcons(IIconRegister ir) {
|
||||
icon = ir.registerIcon("baubles:ring");
|
||||
}
|
||||
@SideOnly(Side.CLIENT)
|
||||
@Override
|
||||
public IIcon getIconFromDamage(int meta) {
|
||||
return icon;
|
||||
}
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
@Override
|
||||
public void getSubItems(Item par1, CreativeTabs par2CreativeTabs,List par3List) {
|
||||
par3List.add(new ItemStack(this,1,0));
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaubleType getBaubleType(ItemStack itemstack) {
|
||||
return BaubleType.RING;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onWornTick(ItemStack itemstack, EntityLivingBase player) {
|
||||
if (itemstack.getItemDamage()==0 && !player.isPotionActive(Potion.digSpeed)) {
|
||||
player.addPotionEffect(new PotionEffect(Potion.digSpeed.id,40,0));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasEffect(ItemStack par1ItemStack, int a) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EnumRarity getRarity(ItemStack par1ItemStack) {
|
||||
return EnumRarity.rare;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUnlocalizedName(ItemStack par1ItemStack)
|
||||
{
|
||||
return super.getUnlocalizedName() + "." + par1ItemStack.getItemDamage();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEquipped(ItemStack itemstack, EntityLivingBase player) {
|
||||
if (!player.worldObj.isRemote) {
|
||||
player.worldObj.playSoundAtEntity(player, "random.orb", 0.1F, 1.3f);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUnequipped(ItemStack itemstack, EntityLivingBase player) {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package baubles.common.lib;
|
||||
|
||||
import net.minecraft.creativetab.CreativeTabs;
|
||||
import net.minecraft.item.Item;
|
||||
import baubles.common.Config;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
|
||||
public final class CreativeTabBaubles extends CreativeTabs
|
||||
{
|
||||
public CreativeTabBaubles(int par1, String par2Str)
|
||||
{
|
||||
super(par1, par2Str);
|
||||
}
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
|
||||
/**
|
||||
* the itemID for the item to be displayed on the tab
|
||||
*/
|
||||
@Override
|
||||
public Item getTabIconItem()
|
||||
{
|
||||
return Config.itemRing;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package baubles.common.lib;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import baubles.common.container.InventoryBaubles;
|
||||
|
||||
public class PlayerHandler {
|
||||
private HashMap<String,InventoryBaubles> playerBaubles = new HashMap<String,InventoryBaubles>();
|
||||
|
||||
public InventoryBaubles getPlayerBaubles(EntityPlayer player) {
|
||||
if (!playerBaubles.containsKey(player.getCommandSenderName())) {
|
||||
InventoryBaubles inventory = new InventoryBaubles(player);
|
||||
playerBaubles.put(player.getCommandSenderName(), inventory);
|
||||
}
|
||||
return playerBaubles.get(player.getCommandSenderName());
|
||||
}
|
||||
|
||||
public void setPlayerBaubles(EntityPlayer player, InventoryBaubles inventory) {
|
||||
playerBaubles.put(player.getCommandSenderName(), inventory);
|
||||
}
|
||||
|
||||
public void updatePlayerBaubles(EntityPlayer player) {
|
||||
InventoryBaubles inventory = getPlayerBaubles(player);
|
||||
inventory.saveNBT(player);
|
||||
}
|
||||
|
||||
public void loadPlayerBaubles(EntityPlayer player) {
|
||||
NBTTagCompound data = player.getEntityData();
|
||||
if (!data.hasKey("Baubles")) {
|
||||
data.setTag("Baubles", new NBTTagCompound());
|
||||
}
|
||||
InventoryBaubles inventory = new InventoryBaubles(player);
|
||||
inventory.readNBT(player);
|
||||
playerBaubles.put(player.getCommandSenderName(), inventory);
|
||||
updatePlayerBaubles(player);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package baubles.common.network;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
|
||||
|
||||
/**
|
||||
* AbstractPacket class. Should be the parent of all packets wishing to use the PacketPipeline.
|
||||
* @author sirgingalot
|
||||
*/
|
||||
public abstract class AbstractPacket {
|
||||
|
||||
/**
|
||||
* Encode the packet data into the ByteBuf stream. Complex data sets may need specific data handlers (See @link{cpw.mods.fml.common.network.ByteBuffUtils})
|
||||
*
|
||||
* @param ctx channel context
|
||||
* @param buffer the buffer to encode into
|
||||
*/
|
||||
public abstract void encodeInto(ChannelHandlerContext ctx, ByteBuf buffer);
|
||||
|
||||
/**
|
||||
* Decode the packet data from the ByteBuf stream. Complex data sets may need specific data handlers (See @link{cpw.mods.fml.common.network.ByteBuffUtils})
|
||||
*
|
||||
* @param ctx channel context
|
||||
* @param buffer the buffer to decode from
|
||||
*/
|
||||
public abstract void decodeInto(ChannelHandlerContext ctx, ByteBuf buffer);
|
||||
|
||||
/**
|
||||
* Handle a packet on the client side. Note this occurs after decoding has completed.
|
||||
*
|
||||
* @param player the player reference
|
||||
*/
|
||||
public abstract void handleClientSide(EntityPlayer player);
|
||||
|
||||
/**
|
||||
* Handle a packet on the server side. Note this occurs after decoding has completed.
|
||||
*
|
||||
* @param player the player reference
|
||||
*/
|
||||
public abstract void handleServerSide(EntityPlayer player);
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package baubles.common.network;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import baubles.common.Baubles;
|
||||
import cpw.mods.fml.common.FMLCommonHandler;
|
||||
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
||||
import cpw.mods.fml.common.gameevent.PlayerEvent;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
|
||||
public class EventHandlerNetwork {
|
||||
|
||||
@SubscribeEvent
|
||||
public void playerLoggedInEvent (PlayerEvent.PlayerLoggedInEvent event) {
|
||||
Side side = FMLCommonHandler.instance().getEffectiveSide();
|
||||
if (side == Side.SERVER) {
|
||||
Baubles.instance.playerHandler.loadPlayerBaubles(event.player);
|
||||
}
|
||||
}
|
||||
|
||||
HashMap<Integer,Long> lastPlayerUpdate = new HashMap<Integer,Long>();
|
||||
|
||||
@SubscribeEvent
|
||||
public void playerUpdateEvent (PlayerEvent event) {
|
||||
Side side = FMLCommonHandler.instance().getEffectiveSide();
|
||||
if (side == Side.SERVER) {
|
||||
Baubles.instance.playerHandler.updatePlayerBaubles(event.player);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package baubles.common.network;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import baubles.common.Baubles;
|
||||
|
||||
public class PacketOpenBaublesInventory extends AbstractPacket {
|
||||
|
||||
public PacketOpenBaublesInventory() {}
|
||||
|
||||
public PacketOpenBaublesInventory(EntityPlayer player) {}
|
||||
|
||||
@Override
|
||||
public void encodeInto(ChannelHandlerContext ctx, ByteBuf buffer) {}
|
||||
|
||||
@Override
|
||||
public void decodeInto(ChannelHandlerContext ctx, ByteBuf buffer) {}
|
||||
|
||||
@Override
|
||||
public void handleClientSide(EntityPlayer player) {}
|
||||
|
||||
@Override
|
||||
public void handleServerSide(EntityPlayer player) {
|
||||
player.openGui(Baubles.instance, Baubles.GUI, player.worldObj, (int)player.posX, (int)player.posY, (int)player.posZ);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,216 @@
|
||||
package baubles.common.network;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.channel.ChannelHandler;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.MessageToMessageCodec;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.EnumMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.network.INetHandler;
|
||||
import net.minecraft.network.NetHandlerPlayServer;
|
||||
import cpw.mods.fml.common.FMLCommonHandler;
|
||||
import cpw.mods.fml.common.network.FMLEmbeddedChannel;
|
||||
import cpw.mods.fml.common.network.FMLOutboundHandler;
|
||||
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;
|
||||
|
||||
/**
|
||||
* Packet pipeline class. Directs all registered packet data to be handled by the packets themselves.
|
||||
* @author sirgingalot
|
||||
* some code from: cpw
|
||||
*/
|
||||
@ChannelHandler.Sharable
|
||||
public class PacketPipeline extends MessageToMessageCodec<FMLProxyPacket, AbstractPacket> {
|
||||
|
||||
private EnumMap<Side, FMLEmbeddedChannel> channels;
|
||||
private LinkedList<Class<? extends AbstractPacket>> packets = new LinkedList<Class<? extends AbstractPacket>>();
|
||||
private boolean isPostInitialised = false;
|
||||
|
||||
/**
|
||||
* Register your packet with the pipeline. Discriminators are automatically set.
|
||||
*
|
||||
* @param clazz the class to register
|
||||
*
|
||||
* @return whether registration was successful. Failure may occur if 256 packets have been registered or if the registry already contains this packet
|
||||
*/
|
||||
public boolean registerPacket(Class<? extends AbstractPacket> clazz) {
|
||||
if (this.packets.size() > 256) {
|
||||
// You should log here!!
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this.packets.contains(clazz)) {
|
||||
// You should log here!!
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this.isPostInitialised) {
|
||||
// You should log here!!
|
||||
return false;
|
||||
}
|
||||
|
||||
this.packets.add(clazz);
|
||||
return true;
|
||||
}
|
||||
|
||||
// In line encoding of the packet, including discriminator setting
|
||||
@Override
|
||||
protected void encode(ChannelHandlerContext ctx, AbstractPacket msg, List<Object> out) throws Exception {
|
||||
ByteBuf buffer = Unpooled.buffer();
|
||||
Class<? extends AbstractPacket> clazz = msg.getClass();
|
||||
if (!this.packets.contains(msg.getClass())) {
|
||||
throw new NullPointerException("No Packet Registered for: " + msg.getClass().getCanonicalName());
|
||||
}
|
||||
|
||||
byte discriminator = (byte) this.packets.indexOf(clazz);
|
||||
buffer.writeByte(discriminator);
|
||||
msg.encodeInto(ctx, buffer);
|
||||
FMLProxyPacket proxyPacket = new FMLProxyPacket(buffer.copy(), ctx.channel().attr(NetworkRegistry.FML_CHANNEL).get());
|
||||
out.add(proxyPacket);
|
||||
}
|
||||
|
||||
// In line decoding and handling of the packet
|
||||
@Override
|
||||
protected void decode(ChannelHandlerContext ctx, FMLProxyPacket msg, List<Object> out) throws Exception {
|
||||
ByteBuf payload = msg.payload();
|
||||
byte discriminator = payload.readByte();
|
||||
Class<? extends AbstractPacket> clazz = this.packets.get(discriminator);
|
||||
if (clazz == null) {
|
||||
throw new NullPointerException("No packet registered for discriminator: " + discriminator);
|
||||
}
|
||||
|
||||
AbstractPacket pkt = clazz.newInstance();
|
||||
pkt.decodeInto(ctx, payload.slice());
|
||||
|
||||
EntityPlayer player;
|
||||
switch (FMLCommonHandler.instance().getEffectiveSide()) {
|
||||
case CLIENT:
|
||||
player = this.getClientPlayer();
|
||||
pkt.handleClientSide(player);
|
||||
break;
|
||||
|
||||
case SERVER:
|
||||
INetHandler netHandler = ctx.channel().attr(NetworkRegistry.NET_HANDLER).get();
|
||||
player = ((NetHandlerPlayServer) netHandler).playerEntity;
|
||||
pkt.handleServerSide(player);
|
||||
break;
|
||||
|
||||
default:
|
||||
}
|
||||
|
||||
out.add(pkt);
|
||||
}
|
||||
|
||||
// Method to call from FMLInitializationEvent
|
||||
public void initialise() {
|
||||
this.channels = NetworkRegistry.INSTANCE.newChannel("BAUBLE", this);
|
||||
|
||||
//packets
|
||||
registerPacket(PacketOpenBaublesInventory.class);
|
||||
|
||||
}
|
||||
|
||||
// Method to call from FMLPostInitializationEvent
|
||||
// Ensures that packet discriminators are common between server and client by using logical sorting
|
||||
public void postInitialise() {
|
||||
if (this.isPostInitialised) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.isPostInitialised = true;
|
||||
Collections.sort(this.packets, new Comparator<Class<? extends AbstractPacket>>() {
|
||||
|
||||
@Override
|
||||
public int compare(Class<? extends AbstractPacket> clazz1, Class<? extends AbstractPacket> clazz2) {
|
||||
int com = String.CASE_INSENSITIVE_ORDER.compare(clazz1.getCanonicalName(), clazz2.getCanonicalName());
|
||||
if (com == 0) {
|
||||
com = clazz1.getCanonicalName().compareTo(clazz2.getCanonicalName());
|
||||
}
|
||||
|
||||
return com;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
private EntityPlayer getClientPlayer() {
|
||||
return Minecraft.getMinecraft().thePlayer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send this message to everyone.
|
||||
* <p/>
|
||||
* Adapted from CPW's code in cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper
|
||||
*
|
||||
* @param message The message to send
|
||||
*/
|
||||
public void sendToAll(AbstractPacket message) {
|
||||
this.channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.ALL);
|
||||
this.channels.get(Side.SERVER).writeAndFlush(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send this message to the specified player.
|
||||
* <p/>
|
||||
* Adapted from CPW's code in cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper
|
||||
*
|
||||
* @param message The message to send
|
||||
* @param player The player to send it to
|
||||
*/
|
||||
public void sendTo(AbstractPacket message, EntityPlayerMP player) {
|
||||
this.channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.PLAYER);
|
||||
this.channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(player);
|
||||
this.channels.get(Side.SERVER).writeAndFlush(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send this message to everyone within a certain range of a point.
|
||||
* <p/>
|
||||
* Adapted from CPW's code in cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper
|
||||
*
|
||||
* @param message The message to send
|
||||
* @param point The {@link cpw.mods.fml.common.network.NetworkRegistry.TargetPoint} around which to send
|
||||
*/
|
||||
public void sendToAllAround(AbstractPacket message, NetworkRegistry.TargetPoint point) {
|
||||
this.channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.ALLAROUNDPOINT);
|
||||
this.channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(point);
|
||||
this.channels.get(Side.SERVER).writeAndFlush(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send this message to everyone within the supplied dimension.
|
||||
* <p/>
|
||||
* Adapted from CPW's code in cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper
|
||||
*
|
||||
* @param message The message to send
|
||||
* @param dimensionId The dimension id to target
|
||||
*/
|
||||
public void sendToDimension(AbstractPacket message, int dimensionId) {
|
||||
this.channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.DIMENSION);
|
||||
this.channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(dimensionId);
|
||||
this.channels.get(Side.SERVER).writeAndFlush(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send this message to the server.
|
||||
* <p/>
|
||||
* Adapted from CPW's code in cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper
|
||||
*
|
||||
* @param message The message to send
|
||||
*/
|
||||
public void sendToServer(AbstractPacket message) {
|
||||
this.channels.get(Side.CLIENT).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.TOSERVER);
|
||||
this.channels.get(Side.CLIENT).writeAndFlush(message);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user