YoungErtu
-
Posts
61 -
Joined
-
Last visited
Posts posted by YoungErtu
-
-
Thanks Guys, you really helped me. The Problem is i didn't have done till now, i'll need to learn that. Thanks.
-
I think you are looking for
WorldSavedData
. You can save stuff to NBT per world or per dimension. There are a few tutorials/examples online.
Thanks, i'll try that out.
-
Hey guys, im currently working on a mod, and i want to add a Guild System, but the thing is i don't know how i could write and read the data on the server. I'll need to implement that player can create their own guilds and then i need to save all Players that are in a Guild. And if the player open the Guild Gui he can see every Player who is in the Guild. How i would get the players which are in the guild if they aren't online? Where do i need to save the data? The most of this should be done in the Guild Gui. How I need to start? Thanks for any help.
-
In my gui elevator mod. If the player is not standing on or near the elevator it wont tp them to the floor they selected and tells console player moved wrongly. I read that if the origin and destination are not too far apart then it is fine however if they are too far i would need a packet to send the player to the destination. I however have a basic understanding of how the packets work and dont know how to use packets to teleport the player. Any help would be great
this is an awesome tutorial:
by coolAlias
EDIT:
but if you already have a packet system than write in your handle Server the code what should happen with the player, in your example he should be teleported.
you need to send the x, y, z coords within the packet where the player should be teleported
-
Well you are never saving your data to the itemstacks'nbt.
override onContainerClosed and save your inventory to the itemstacks nbt there
At the end you were right, sorry and thanks ^^
-
Fixed that by adding this into the Container:
@Override public void onContainerClosed(EntityPlayer player){ World world = player.worldObj; if (!world.isRemote){ ItemStack stack = player.getCurrentEquippedItem(); inventory.writeToNBT(stack.getTagCompound()); } super.onContainerClosed(player); }
-
I still do not understand that mark dirty function.
CoolAlias might have to hop in and help you, because from here it looks like that method is erasing the inventory contents, not saving them.
Similarly, it isn't calling super().
I looked into a Open Source Mod and it seems that this markDirty method is wrong, he uses onContainerClosed i'll try the same, im currently working on that.
-
I posted this under the topic of coolAlias Tutorial and here are the problems that i detected:
Changed everything but still no difference, the Item Inventory is still not saved. Also when I try to set Items to the Inventory in the Item Right-Click Method it is saving the Item but when i take the item and open the inventory again it is still there.
here my 2 classes inventory and container:
Container
package youngertu.fairytail.inventory; 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.ItemArmor; import net.minecraft.item.ItemStack; import youngertu.fairytail.items.keys.KeyBase; public class ContainerKeyRing extends Container { /** The Item Inventory for this Container */ private final InventoryKeyRing inventory; private static final int ARMOR_START = InventoryKeyRing.INV_SIZE, // INV_SIZE = 10, so slots 0 to 9 are the custom inventory, armor starts at the next slot (i.e. 10) ARMOR_END = ARMOR_START+3, // 4 slots total, e.g. 10-13 (10, 11, 12, 13) INV_START = ARMOR_END+1, // start at next slot after armor, e.g. 14 INV_END = INV_START+26, // 27 vanilla inventory slots total (i.e. the first one plus 26 more) HOTBAR_START = INV_END+1, // start at next slot after inventory HOTBAR_END = HOTBAR_START+8; // 9 slots total (i.e. the first one plus 8 more) public ContainerKeyRing(EntityPlayer player, InventoryPlayer inv, InventoryKeyRing bag) { int i = 0; inventory = bag; // CUSTOM INVENTORY SLOTS for (i = 0; i < InventoryKeyRing.INV_SIZE; ++i) { addSlotToContainer(new SlotKeyRing(inventory, i, 80 + (18*(i%5)), 8 + (18 * (int)(i/5)))); } // PLAYER INVENTORY - uses default locations for standard inventory texture file for (i = 0; i < 3; ++i) { for (int j = 0; j < 9; ++j) { addSlotToContainer(new Slot(inv, j + i * 9 + 9, 8 + j * 18, 84 + i * 18)); } } // PLAYER ACTION BAR - uses default locations for standard action bar texture file for (i = 0; i < 9; ++i) { addSlotToContainer(new Slot(inv, i, 8 + i * 18, 142)); } } @Override public boolean canInteractWith(EntityPlayer player) { return inventory.isUseableByPlayer(player); } @Override public ItemStack transferStackInSlot (EntityPlayer player, int par2) { ItemStack itemstack = null; Slot slot = this.inventorySlots.get(par2); if (slot != null && slot.getHasStack()) { ItemStack itemstack1 = slot.getStack(); itemstack = itemstack1.copy(); if (par2 < INV_START) { if (!mergeItemStack(itemstack1, INV_START, HOTBAR_END + 1, true)) return null; slot.onSlotChange(itemstack1, itemstack); } else if (itemstack1.getItem() instanceof KeyBase) { if (!mergeItemStack(itemstack1, 0, InventoryKeyRing.INV_SIZE, false)) return null; } else if (par2 >= INV_START && par2 < HOTBAR_START) { if (!mergeItemStack(itemstack1, HOTBAR_START, HOTBAR_START + 1, false)) return null; } else if (par2 >= HOTBAR_START && par2 < HOTBAR_END + 1) if (!mergeItemStack(itemstack1, INV_START, INV_END + 1, false)) return null; if (itemstack1.stackSize == 0) slot.putStack((ItemStack) null); else slot.onSlotChanged(); if (itemstack1.stackSize == itemstack.stackSize) return null; slot.onPickupFromSlot(player, itemstack1); } return itemstack; } @Override public ItemStack slotClick(int slot, int button, int flag, EntityPlayer player) { if (slot >= 0 && getSlot(slot) != null && getSlot(slot).getStack() == player.getHeldItem()) { return null; } return super.slotClick(slot, button, flag, player); } @Override protected boolean mergeItemStack (ItemStack stack, int start, int end, boolean backwards) { boolean flag1 = false; int k = (backwards ? end - 1 : start); Slot slot; ItemStack itemstack1; if (stack.isStackable()) while (stack.stackSize > 0 && (!backwards && k < end || backwards && k >= start)) { slot = inventorySlots.get(k); itemstack1 = slot.getStack(); if (!slot.isItemValid(stack)) { k += (backwards ? -1 : 1); continue; } if (itemstack1 != null && itemstack1.getItem() == stack.getItem() && (!stack.getHasSubtypes() || stack.getItemDamage() == itemstack1.getItemDamage()) && ItemStack.areItemStackTagsEqual(stack, itemstack1)) { int l = itemstack1.stackSize + stack.stackSize; if (l <= stack.getMaxStackSize() && l <= slot.getSlotStackLimit()) { stack.stackSize = 0; itemstack1.stackSize = l; inventory.markDirty(); flag1 = true; } else if (itemstack1.stackSize < stack.getMaxStackSize() && l < slot.getSlotStackLimit()) { stack.stackSize -= stack.getMaxStackSize() - itemstack1.stackSize; itemstack1.stackSize = stack.getMaxStackSize(); inventory.markDirty(); flag1 = true; } } k += (backwards ? -1 : 1); } if (stack.stackSize > 0) { k = (backwards ? end - 1 : start); while (!backwards && k < end || backwards && k >= start) { slot = inventorySlots.get(k); itemstack1 = slot.getStack(); if (!slot.isItemValid(stack)) { k += (backwards ? -1 : 1); continue; } if (itemstack1 == null) { int l = stack.stackSize; if (l <= slot.getSlotStackLimit()) { slot.putStack(stack.copy()); stack.stackSize = 0; inventory.markDirty(); flag1 = true; break; } else { putStackInSlot(k, new ItemStack(stack.getItem(), slot.getSlotStackLimit(), stack.getItemDamage())); stack.stackSize -= slot.getSlotStackLimit(); inventory.markDirty(); flag1 = true; } } k += (backwards ? -1 : 1); } } return flag1; } }
Inventory
package youngertu.fairytail.inventory; import net.minecraft.client.Minecraft; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import youngertu.fairytail.items.keys.KeyBase; public class InventoryKeyRing extends AbstractInventory { private String name = "Key Ring"; /** The key used to store and retrieve the inventory from NBT */ private static final String SAVE_KEY = "KeyRingInventory"; /** Defining your inventory size this way is handy */ public static final int INV_SIZE = 18; /** Provides NBT Tag Compound to reference */ private final ItemStack invStack; public InventoryKeyRing(ItemStack stack) { inventory = new ItemStack[iNV_SIZE]; this.invStack = stack; if (!invStack.hasTagCompound()) { invStack.setTagCompound(new NBTTagCompound()); } readFromNBT(invStack.getTagCompound()); } @Override public String getName() { return name; } @Override public boolean hasCustomName() { return name.length() > 0; } @Override public int getInventoryStackLimit() { return 1; } /** * For inventories stored in ItemStacks, it is critical to implement this method * in order to write the inventory to the ItemStack's NBT whenever it changes. */ @Override public void markDirty() { for (int i = 0; i < getSizeInventory(); ++i) { if (getStackInSlot(i) != null && getStackInSlot(i).stackSize == 0) { setInventorySlotContents(i, null); } } writeToNBT(invStack.getTagCompound()); } @Override public boolean isUseableByPlayer(EntityPlayer player) { return true; } @Override public boolean isItemValidForSlot(int slot, ItemStack stack) { return stack.getItem() instanceof KeyBase; } @Override protected String getNbtKey() { return SAVE_KEY; } public void copy (AbstractInventory inv) { for (int i = 0; i < inv.getSizeInventory(); ++i) { ItemStack stack = inv.getStackInSlot(i); inventory = (stack == null ? null : stack.copy()); } markDirty(); } @Override public ItemStack removeStackFromSlot(int slot) { ItemStack stack = getStackInSlot(slot); setInventorySlotContents(slot, null); return stack; } }
thanks for your help coolAlias, maybe i understanded something wrong and this is the reason for not saving the items.
EDIT: I added some debug lines and everything works fine, the NBTWriting is successful, but instead of readingNBT when opening the Inventory it writes to NBT, why?
EDIT 2: Fixed ReadingNBT by putting into openInventory but it still writesNBT after opening the Inventory and reading the NBT and this several times 10 times i think.
EDIT 3: writeNBT is called on Client Side when opening the inventory, but Server Side when putting Item in Slot. Reading NBT is only called on Server Side.
When opening the Inventory the writeNBT method is called by markDirty, and how i said before on client side, there is something wrong but i couldn't find it.
EDIT 4: I outcommented the method markDirty to put my Code into onContainerClosed but it doesn't want to work.
-
in your method where you set the entity on fire add this:
if(event.source.getSourceOfDamage() != null) event.source.getSourceOfDamage().setFire(10);
-
It is working with setting entities on fire, but when I take any other damage besides from a mob or a whole bunch of mobs attack me at once it crashes.
@SubscribeEvent
public void livingEvent(LivingHurtEvent event)
{
if(event.entityLiving instanceof EntityPlayer)
{
EntityPlayer player = (EntityPlayer) event.entityLiving;
if(player.inventory.armorItemInSlot(3) != null && player.inventory.armorItemInSlot(3).getItem() == PItems.phoenixHelmet && player.inventory.armorItemInSlot(2) != null && player.inventory.armorItemInSlot(2).getItem() == PItems.phoenixChestplate && player.inventory.armorItemInSlot(1) != null && player.inventory.armorItemInSlot(1).getItem() == PItems.phoenixLeggings && player.inventory.armorItemInSlot(0) != null && player.inventory.armorItemInSlot(0).getItem() == PItems.phoenixBoots)
{
event.source.getEntity().setFire(10);
}
}
}
Crash: http://pastebin.com/SYLDpJDH
try to check if the damage is by a mob.
because your currently code tries to set everying burning which hurts you, but some of them are no entities so they can't be set on fire, if the player gets drown damage the source will be the water, but water is no entity so check if the damage source is by an entity.
-
1) That is not what Mark Dirty is supposed to be doing.
2) That function utterly deletes the entire contained inventory. Good jorb.
3) Never, ever, EVER call
writeToNBT
yourself.
I just followed the Tutorial of coolAlias seems that this doesn't work for 1.8 or im not able to get it to work.
I think i'll start the tutorial from begining.
-
Hello guys ^^
Maybe I am just silly, but I think, that setRotation(...) in Entity-classes doesnt't work...
Is there a way to set the rotation of a EntityThrowable? Do I have to add something in the Model-class, Entity-class or Render-class?
Here are those classes:
I hope you can help ^^
what do you want to do exactly? that could help us.
-
Well you are never saving your data to the itemstacks'nbt.
override onContainerClosed and save your inventory to the itemstacks nbt there
but then you didn't saw this:
/** * For inventories stored in ItemStacks, it is critical to implement this method * in order to write the inventory to the ItemStack's NBT whenever it changes. */ @Override public void markDirty() { for (int i = 0; i < getSizeInventory(); ++i) { if (getStackInSlot(i) != null && getStackInSlot(i).stackSize == 0) inventory[i] = null; } writeToNBT(invStack.getTagCompound()); }
there is a other reason i think, but why?
EDIT: When i try to shift-click the Diamond the game crashes.
EDIT 2: this method is only for TileEntities i'll try to writeNBTTag and readNBTTag
-
Still nothing:
@SubscribeEvent
public void livingEvent(LivingHurtEvent event)
{
if(event.entityLiving instanceof EntityPlayer && event.entityLiving.getEquipmentInSlot(1).getItem() == PItems.phoenixHelmet && event.entityLiving.getEquipmentInSlot(2).getItem() == PItems.phoenixChestplate && event.entityLiving.getEquipmentInSlot(3).getItem() == PItems.phoenixLeggings && event.entityLiving.getEquipmentInSlot(4).getItem() == PItems.phoenixBoots)
{
event.source.getEntity().setFire(10);
}
}
try this:
@SubscribeEvent public void livingEvent(LivingHurtEvent event) { if(event.entityLiving instanceof EntityPlayer) { EntityPlayer player = (EntityPlayer) event.entityLiving; if(player.inventory.armorItemInSlot(3) != null && player.inventory.armorItemInSlot(3).getItem() == MODID.HELMET && player.inventory.armorItemInSlot(2) != null && player.inventory.armorItemInSlot(2).getItem() == MODID.CHESTPLATE && player.inventory.armorItemInSlot(1) != null && player.inventory.armorItemInSlot(1).getItem() == MODID.LEGGINGS && player.inventory.armorItemInSlot(0) != null && player.inventory.armorItemInSlot(0).getItem() == MODID.BOOTS) { event.source.getEntity().setFire(10); } }}
this should work fine. dont try to check everything in one if statement.
-
Hey there community. I have a rather simple request and honestly I think I am just overthinking it or maybe totally misunderstanding it. I am trying to make a pair of shears that do an AoE Shear. So very simply any sheep within a radius of X around the player get sheared all at the same time.
I am trying to make a list like so:
List<EntityLivingBase> shearList = player.worldObj.getEntitiesWithinAABB(EntityLivingBase.class, entity.boundingBox.expand(6.0F, 6.0F, 6.0F));
I did this in the past with an entity that pulsed out an AoE Slow to all things around it so I figured that the process was basically the same. Perhaps maybe that is where I am wrong. Anyway, the code works fine, as it is standard Vanilla Shears code just slightly modified. I just can't get this AoE part to work.
Here is the code:
A shove in the right direction or an example is really all I need.
I have done lists like this before and iterated through them and applied potion effects for example like in this piece of code:
I assumed it would be the same concept? Buuuuttt apparently I am wrong and that is why I am here, for help and to learn.
I would try something like this:
double d0 = 3.0D; List list = world.getEntitiesWithinAABB(this, new AxisAlignedBB(player.posX - d0, player.posY - d0, player.posZ - d0, player.posX + d0, player.posY + 6.0D + d0, player.posZ + d0)); for (int i = 0; i < list.size(); ++i) { //Dont forget to check if Entity is instance of an Sheareable Entity //Then get the entity and do something Entity entity = (Entity)list.get(i); //DO SOMETHING }
this is from the EntityLightningBolt class so hope you can work with it, and also nice to see you here too Halestorm
-
I think the diamond is there because you are adding it everytime you create a new inventory..?
new InventoryKeyring(player.getHeldItem()).setInventorySlotContents(0, new ItemStack(Items.diamond, 4));
thats possible, i'll try out what you said before.
-
Well you are never saving your data to the itemstacks'nbt.
override onContainerClosed and save your inventory to the itemstacks nbt there
it is saving, because when i open it again there is a diamond. but im not able to add or take something.
-
I have the problem that my Item Inventory don't save the Items that i drag in or shift click, after re-opening the gui they are not saved.
but when i use this method in right clicking the item while sneaking:
new InventoryKeyring(player.getHeldItem()).setInventorySlotContents(0, new ItemStack(Items.diamond, 4));
it saves the diamonds, but when i take the diamond and re open the gui the diamond is still there.
Seems for me like a Client/Server Problem, how can i fix that?
or maybe a problem at the method when clicking on a slot to put a item or take a item
My files:
Container
package youngertu.fairytail.inventory; 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.ItemArmor; import net.minecraft.item.ItemStack; import net.minecraft.world.World; import scala.tools.nsc.backend.icode.BBFlags; import youngertu.fairytail.items.ItemKeyRing; public class ContainerKeyring extends Container { private ItemStack bookStack; /** The Item Inventory for this Container */ private final InventoryKeyring inventory; private static final int ARMOR_START = InventoryKeyring.INV_SIZE, // INV_SIZE = 10, so slots 0 to 9 are the custom inventory, armor starts at the next slot (i.e. 10) ARMOR_END = ARMOR_START+3, // 4 slots total, e.g. 10-13 (10, 11, 12, 13) INV_START = ARMOR_END+1, // start at next slot after armor, e.g. 14 INV_END = INV_START+26, // 27 vanilla inventory slots total (i.e. the first one plus 26 more) HOTBAR_START = INV_END+1, // start at next slot after inventory HOTBAR_END = HOTBAR_START+8; // 9 slots total (i.e. the first one plus 8 more) public ContainerKeyring(EntityPlayer player, InventoryPlayer inv, InventoryKeyring bag) { int i = 0; inventory = bag; // CUSTOM INVENTORY SLOTS for (i = 0; i < InventoryKeyring.INV_SIZE; ++i) { addSlotToContainer(new SlotKeyring(inventory, i, 80 + (18*(i%5)), 8 + (18 * (int)(i/5)))); } // PLAYER INVENTORY - uses default locations for standard inventory texture file for (i = 0; i < 3; ++i) { for (int j = 0; j < 9; ++j) { addSlotToContainer(new Slot(inv, j + i * 9 + 9, 8 + j * 18, 84 + i * 18)); } } // PLAYER ACTION BAR - uses default locations for standard action bar texture file for (i = 0; i < 9; ++i) { addSlotToContainer(new Slot(inv, i, 8 + i * 18, 142)); } } @Override public boolean canInteractWith(EntityPlayer player) { return inventory.isUseableByPlayer(player); } @Override public ItemStack transferStackInSlot(EntityPlayer player, int index) { ItemStack itemstack = null; Slot slot = (Slot) this.inventorySlots.get(index); if (slot != null && slot.getHasStack()) { ItemStack itemstack1 = slot.getStack(); itemstack = itemstack1.copy(); // If item is in our custom Inventory or an ARMOR slot if (index < INV_START) { if (!this.mergeItemStack(itemstack1, INV_START, HOTBAR_END+1, true)) { return null; } slot.onSlotChange(itemstack1, itemstack); } else { if (itemstack1.getItem() instanceof ItemArmor) { int type = ((ItemArmor) itemstack1.getItem()).armorType; if (!this.mergeItemStack(itemstack1, ARMOR_START + type, ARMOR_START + type + 1, false)) { return null; } } else if (index >= INV_START) { if (!this.mergeItemStack(itemstack1, 0, ARMOR_START, false)) { return null; } } } if (itemstack1.stackSize == 0) { slot.putStack((ItemStack) null); } else { slot.onSlotChanged(); } if (itemstack1.stackSize == itemstack.stackSize) { return null; } slot.onPickupFromSlot(player, itemstack1); } return itemstack; } @Override public ItemStack slotClick(int slot, int button, int flag, EntityPlayer player) { if (slot >= 0 && getSlot(slot) != null && getSlot(slot).getStack() == player.getHeldItem()) { return null; } return super.slotClick(slot, button, flag, player); } /** * Vanilla method fails to account for stack size limits of one, resulting in only one * item getting placed in the slot and the rest disappearing into thin air; vanilla * method also fails to check whether stack is valid for slot */ @Override protected boolean mergeItemStack(ItemStack stack, int start, int end, boolean backwards) { boolean flag1 = false; int k = (backwards ? end - 1 : start); Slot slot; ItemStack itemstack1; if (stack.isStackable()) { while (stack.stackSize > 0 && (!backwards && k < end || backwards && k >= start)) { slot = (Slot) inventorySlots.get(k); itemstack1 = slot.getStack(); if (!slot.isItemValid(stack)) { k += (backwards ? -1 : 1); continue; } if (itemstack1 != null && itemstack1.getItem() == stack.getItem() && (!stack.getHasSubtypes() || stack.getItemDamage() == itemstack1.getItemDamage()) && ItemStack.areItemStackTagsEqual(stack, itemstack1)) { int l = itemstack1.stackSize + stack.stackSize; if (l <= stack.getMaxStackSize() && l <= slot.getSlotStackLimit()) { stack.stackSize = 0; itemstack1.stackSize = l; inventory.markDirty(); flag1 = true; } else if (itemstack1.stackSize < stack.getMaxStackSize() && l < slot.getSlotStackLimit()) { stack.stackSize -= stack.getMaxStackSize() - itemstack1.stackSize; itemstack1.stackSize = stack.getMaxStackSize(); inventory.markDirty(); flag1 = true; } } k += (backwards ? -1 : 1); } } if (stack.stackSize > 0) { k = (backwards ? end - 1 : start); while (!backwards && k < end || backwards && k >= start) { slot = (Slot) inventorySlots.get(k); itemstack1 = slot.getStack(); if (!slot.isItemValid(stack)) { k += (backwards ? -1 : 1); continue; } if (itemstack1 == null) { int l = stack.stackSize; if (l <= slot.getSlotStackLimit()) { slot.putStack(stack.copy()); stack.stackSize = 0; inventory.markDirty(); flag1 = true; break; } else { putStackInSlot(k, new ItemStack(stack.getItem(), slot.getSlotStackLimit(), stack.getItemDamage())); stack.stackSize -= slot.getSlotStackLimit(); inventory.markDirty(); flag1 = true; } } k += (backwards ? -1 : 1); } } return flag1; } }
Inventory:
package youngertu.fairytail.inventory; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import youngertu.fairytail.items.keys.KeyBase; public class InventoryKeyring extends AbstractInventory { private String name = "Key Ring"; /** The key used to store and retrieve the inventory from NBT */ private static final String SAVE_KEY = "InventoryKeyring"; /** Defining your inventory size this way is handy */ public static final int INV_SIZE = 18; /** Provides NBT Tag Compound to reference */ private final ItemStack invStack; public InventoryKeyring(ItemStack stack) { inventory = new ItemStack[iNV_SIZE]; this.invStack = stack; if (!invStack.hasTagCompound()) { invStack.setTagCompound(new NBTTagCompound()); } readFromNBT(invStack.getTagCompound()); } @Override public String getName() { return name; } @Override public boolean hasCustomName() { return name.length() > 0; } @Override public int getInventoryStackLimit() { return 1; } /** * For inventories stored in ItemStacks, it is critical to implement this method * in order to write the inventory to the ItemStack's NBT whenever it changes. */ @Override public void markDirty() { for (int i = 0; i < getSizeInventory(); ++i) { if (getStackInSlot(i) != null && getStackInSlot(i).stackSize == 0) inventory[i] = null; } writeToNBT(invStack.getTagCompound()); } @Override public boolean isUseableByPlayer(EntityPlayer player) { return true; } @Override public boolean isItemValidForSlot(int slot, ItemStack stack) { return (stack.getItem() instanceof KeyBase); } @Override protected String getNbtKey() { return SAVE_KEY; } }
GuiHandler
public class GuiHandler implements IGuiHandler { @Override public Object getServerGuiElement(int guiId, EntityPlayer player, World world, int x, int y, int z) { switch(guiId) { case FairyTail.GUI_KEYRING_INV: return new ContainerKeyring(player, player.inventory, new InventoryKeyring(player.getHeldItem())); default: return null; } /*if (guiId == FairyTail.GUI_KEYRING_INV) { System.out.println("RETURNED KEYRING INV"); return new ContainerKeyring(player, player.inventory, new InventoryKeyring(player.getHeldItem())); } else { return null; }*/ } @Override public Object getClientGuiElement(int guiId, EntityPlayer player, World world, int x, int y, int z) { switch(guiId) { case FairyTail.GUI_KEYRING_INV: return new GuiKeyring(player, player.inventory, new InventoryKeyring(player.getHeldItem())); default: return null; } /*if (guiId == FairyTail.GUI_KEYRING_INV) { return new GuiKeyring(player, player.inventory, new InventoryKeyring(player.getHeldItem())); } else { return null; }*/ } }
-
0 Oak Wood Planks
1 Spruce Wood Planks
2 Birch Wood Planks
3 Jungle Wood Planks
4 Acacia Wood Planks
5 Dark Oak Wood Planks
source: http://minecraft.gamepedia.com/Data_values#Wood
and then use
new ItemStack(Blocks.planks, amount, metadata from above)
-
you still shouldnt compare itemstacks via ==
thanks now i understand what you mean, its working.
-
What the hell is invStack? Please dont tell me that you are comparing an itemsack and an item and expect it to be equals
getHeldItem() returns an Itemstack not a Item..
-
Well, your canInteractWith just calls inventory.isUseableByPlayer, so obviously need to see that as well... -.-
sorry man
here
package youngertu.fairytail.inventory; 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.util.IChatComponent; import net.minecraftforge.common.util.Constants; import youngertu.fairytail.items.ItemKeyRing; public class InventoryKeyring implements IInventory { private String name = "Key Ring"; /** Defining your inventory size this way is handy */ public static final int INV_SIZE = 18; /** Inventory's size must be same as number of slots you add to the Container class */ private ItemStack[] inventory = new ItemStack[iNV_SIZE]; /** Provides NBT Tag Compound to reference */ private final ItemStack invStack; public InventoryKeyring(ItemStack stack) { this.invStack = stack; if (!invStack.hasTagCompound()) { invStack.setTagCompound(new NBTTagCompound()); } readFromNBT(invStack.getTagCompound()); } @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); setInventorySlotContents(slot, null); return stack; } @Override public void setInventorySlotContents(int slot, ItemStack stack) { inventory[slot] = stack; if (stack != null && stack.stackSize > getInventoryStackLimit()) { stack.stackSize = getInventoryStackLimit(); } markDirty(); } @Override public int getInventoryStackLimit() { return 1; } @Override public void markDirty() { for (int i = 0; i < getSizeInventory(); ++i) { if (getStackInSlot(i) != null && getStackInSlot(i).stackSize == 0) inventory[i] = null; } writeToNBT(invStack.getTagCompound()); } @Override public boolean isUseableByPlayer(EntityPlayer player) { // this will close the inventory if the player tries to move // the item that opened it, but you need to return this method // from the Container's canInteractWith method // an alternative would be to override the slotClick method and // prevent the current item slot from being clicked return player.getHeldItem() == invStack; } @Override public boolean isItemValidForSlot(int slot, ItemStack stack) { return !(stack.getItem() instanceof ItemKeyRing); } public void readFromNBT(NBTTagCompound compound) { NBTTagList items = compound.getTagList("ItemInventory", Constants.NBT.TAG_COMPOUND); for (int i = 0; i < items.tagCount(); ++i) { NBTTagCompound item = items.getCompoundTagAt(i); byte slot = item.getByte("Slot"); if (slot >= 0 && slot < getSizeInventory()) { inventory[slot] = ItemStack.loadItemStackFromNBT(item); } } } public void writeToNBT(NBTTagCompound compound) { NBTTagList items = new NBTTagList(); for (int i = 0; i < getSizeInventory(); ++i) { if (getStackInSlot(i) != null) { NBTTagCompound item = new NBTTagCompound(); item.setByte("Slot", (byte) i); getStackInSlot(i).writeToNBT(item); items.appendTag(item); } } compound.setTag("ItemInventory", items); } @Override public String getName() { return name; } @Override public boolean hasCustomName() { return name.length() > 0; } @Override public IChatComponent getDisplayName() { return null; } @Override public void openInventory(EntityPlayer player) { } @Override public void closeInventory(EntityPlayer player) { } @Override public int getField(int id) { return 0; } @Override public void setField(int id, int value) { } @Override public int getFieldCount() { return 0; } @Override public void clear() { } }
i see that this is the problem:
@Override public boolean isUseableByPlayer(EntityPlayer player) { // this will close the inventory if the player tries to move // the item that opened it, but you need to return this method // from the Container's canInteractWith method // an alternative would be to override the slotClick method and // prevent the current item slot from being clicked return player.getHeldItem() == invStack; }
but why?
Und übrigens danke fürs helfen
-
Need to see your Container class, your canInteractWith method is probably broken.
here my container:
package youngertu.fairytail.inventory; 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.Slot; import net.minecraft.item.ItemStack; import youngertu.fairytail.items.keys.KeyBase; public class ContainerKeyring extends Container { public final InventoryKeyring inventory; private static final int INV_START = InventoryKeyring.INV_SIZE, INV_END = INV_START+26, HOTBAR_START = INV_END+1, HOTBAR_END = HOTBAR_START+8; public ContainerKeyring(EntityPlayer par1Player, InventoryPlayer inventoryPlayer, InventoryKeyring inventoryItem) { this.inventory = inventoryItem; int i; for (i = 0; i < InventoryKeyring.INV_SIZE; ++i) { this.addSlotToContainer(new SlotKeyring(this.inventory, i, 80 + (18 * (int)(i/4)), 8 + (18*(i%4)))); } for (i = 0; i < 3; ++i) { for (int j = 0; j < 9; ++j) { this.addSlotToContainer(new Slot(inventoryPlayer, j + i * 9 + 9, 8 + j * 18, 84 + i * 18)); } } for (i = 0; i < 9; ++i) { this.addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 142)); } } @Override public boolean canInteractWith(EntityPlayer entityplayer) { return inventory.isUseableByPlayer(entityplayer); } /** * 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 index) { ItemStack itemstack = null; Slot slot = (Slot) this.inventorySlots.get(index); if (slot != null && slot.getHasStack()) { ItemStack itemstack1 = slot.getStack(); itemstack = itemstack1.copy(); if (index < INV_START) { if (!this.mergeItemStack(itemstack1, INV_START, HOTBAR_END+1, true)) { return null; } slot.onSlotChange(itemstack1, itemstack); } else { if (itemstack1.getItem() instanceof KeyBase) { if (!this.mergeItemStack(itemstack1, 0, InventoryKeyring.INV_SIZE, false)) { return null; } } if (index >= INV_START && index < HOTBAR_START) { if (!this.mergeItemStack(itemstack1, HOTBAR_START, HOTBAR_END+1, false)) { return null; } } else if (index >= HOTBAR_START && index < HOTBAR_END+1) { if (!this.mergeItemStack(itemstack1, INV_START, INV_END+1, 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; } /** * You should override this method to prevent the player from moving the stack that * opened the inventory, otherwise if the player moves it, the inventory will not * be able to save properly */ @Override public ItemStack slotClick(int slot, int button, int flag, EntityPlayer player) { if (slot >= 0 && getSlot(slot) != null && getSlot(slot).getStack() == player.getHeldItem()) { return null; } return super.slotClick(slot, button, flag, player); } @Override protected boolean mergeItemStack(ItemStack stack, int start, int end, boolean backwards) { boolean flag1 = false; int k = (backwards ? end - 1 : start); Slot slot; ItemStack itemstack1; if (stack.isStackable()) { while (stack.stackSize > 0 && (!backwards && k < end || backwards && k >= start)) { slot = (Slot) inventorySlots.get(k); itemstack1 = slot.getStack(); if (!slot.isItemValid(stack)) { k += (backwards ? -1 : 1); continue; } if (itemstack1 != null && itemstack1.getItem() == stack.getItem() && (!stack.getHasSubtypes() || stack.getItemDamage() == itemstack1.getItemDamage()) && ItemStack.areItemStackTagsEqual(stack, itemstack1)) { int l = itemstack1.stackSize + stack.stackSize; if (l <= stack.getMaxStackSize() && l <= slot.getSlotStackLimit()) { stack.stackSize = 0; itemstack1.stackSize = l; inventory.markDirty(); flag1 = true; } else if (itemstack1.stackSize < stack.getMaxStackSize() && l < slot.getSlotStackLimit()) { stack.stackSize -= stack.getMaxStackSize() - itemstack1.stackSize; itemstack1.stackSize = stack.getMaxStackSize(); inventory.markDirty(); flag1 = true; } } k += (backwards ? -1 : 1); } } if (stack.stackSize > 0) { k = (backwards ? end - 1 : start); while (!backwards && k < end || backwards && k >= start) { slot = (Slot) inventorySlots.get(k); itemstack1 = slot.getStack(); if (!slot.isItemValid(stack)) { k += (backwards ? -1 : 1); continue; } if (itemstack1 == null) { int l = stack.stackSize; if (l <= slot.getSlotStackLimit()) { slot.putStack(stack.copy()); stack.stackSize = 0; inventory.markDirty(); flag1 = true; break; } else { putStackInSlot(k, new ItemStack(stack.getItem(), slot.getSlotStackLimit(), stack.getItemDamage())); stack.stackSize -= slot.getSlotStackLimit(); inventory.markDirty(); flag1 = true; } } k += (backwards ? -1 : 1); } } return flag1; } }
note this is based on the tutorial of coolAlias
-
I created an Item with an Inventory and when i right click, the gui doesn't appear, but i see the mouse for a second, when i spam right click i see the gui for some seconds but its closing by his self, what is the problem?
here my Item Right-Click:
@Overridepublic ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer player) {
if (!world.isRemote)
{
// If player not sneaking, open the inventory gui
if (!player.isSneaking()) {
System.out.println("Item was right_clicked.");
player.openGui(FairyTail.instance, FairyTail.GUI_KEYRING_INV, world, 0, 0, 0);
//Comment: the last 3 variables makes no sense, i tried player position too.
}
else {
System.out.println("Item was right_clicked.");
new InventoryKeyring(player.getHeldItem()).setInventorySlotContents(0, new ItemStack(Items.diamond,4));
}
}
return itemStack;
}
and here my gui handler:
public class GuiHandler implements IGuiHandler { @Override public Object getServerGuiElement(int guiId, EntityPlayer player, World world, int x, int y, int z) { switch(guiId) { case FairyTail.GUI_KEYRING_INV: return new ContainerKeyring(player, player.inventory, new InventoryKeyring(player.getHeldItem())); default: return null; } /*if (guiId == FairyTail.GUI_KEYRING_INV) { System.out.println("RETURNED KEYRING INV"); return new ContainerKeyring(player, player.inventory, new InventoryKeyring(player.getHeldItem())); } else { return null; }*/ } @Override public Object getClientGuiElement(int guiId, EntityPlayer player, World world, int x, int y, int z) { switch(guiId) { case FairyTail.GUI_KEYRING_INV: return new GuiKeyring(player, player.inventory, new InventoryKeyring(player.getHeldItem())); default: return null; } /*if (guiId == FairyTail.GUI_KEYRING_INV) { return new GuiKeyring(player, player.inventory, new InventoryKeyring(player.getHeldItem())); } else { return null; }*/ } }
last the line in the Main Mod file:
public static final int GUI_KEYRING_INV = 10;
and the gui class:
package youngertu.fairytail.gui; import org.lwjgl.opengl.GL11; import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.client.gui.inventory.GuiInventory; import net.minecraft.client.resources.I18n; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.util.ResourceLocation; import youngertu.fairytail.inventory.ContainerKeyring; import youngertu.fairytail.inventory.InventoryKeyring; public class GuiKeyring extends GuiContainer { /** x and y size of the inventory window in pixels. Defined as float, passed as int * These are used for drawing the player model. */ private float xSize_lo; private float ySize_lo; /** ResourceLocation takes 2 parameters: ModId, path to texture at the location: * "src/minecraft/assets/modid/" */ private static final ResourceLocation iconLocation = new ResourceLocation("tutorial:textures/gui/inventory_keyring.png"); /** The inventory to render on screen */ private final InventoryKeyring inventory; public GuiKeyring(EntityPlayer player, InventoryPlayer inv1, InventoryKeyring inv2) { super(new ContainerKeyring(player, inv1, inv2)); this.inventory = inv2; } @Override public void drawScreen(int mouseX, int mouseY, float f) { super.drawScreen(mouseX, mouseY, f); xSize_lo = mouseX; ySize_lo = mouseY; } @Override protected void drawGuiContainerForegroundLayer(int par1, int par2) { String s = inventory.hasCustomName() ? inventory.getName() : I18n.format(inventory.getName()); fontRendererObj.drawString(s, xSize / 2 - fontRendererObj.getStringWidth(s) / 2, 0, 4210752); fontRendererObj.drawString(I18n.format("container.inventory"), 26, ySize - 96 + 4, 4210752); } @Override protected void drawGuiContainerBackgroundLayer(float f, int mouseX, int mouseY) { GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); mc.getTextureManager().bindTexture(iconLocation); int k = (width - xSize) / 2; int l = (height - ySize) / 2; drawTexturedModalRect(k, l, 0, 0, xSize, ySize); GuiInventory.drawEntityOnScreen(k + 51, l + 75, 30, (k + 51) - xSize_lo, (l + 75 - 50) - ySize_lo, mc.thePlayer); } }
[1.8.9] While Holding Item Get Potion Effect?
in Modder Support
Posted
because you using the method which is always true, onUpdate is' executed every tick while the item is in the inventory.
you could simply make a if statement if the player is currently holding the item, like you know if the current holding item is the current itemstack, which activates this effect.