public class CustomSlot extends Slot {
private final EntityPlayer player;
public CustomSlot(EntityPlayer player, IInventory inventory, int slotIndex, int xPos, int yPos) {
super(inventory, slotIndex, xPos, yPos);
this.player = player;
}
//При доставании из слота.
@Override
public void onPickupFromSlot(EntityPlayer player, ItemStack itemStack) {
this.onSlotChanged();
if (!this.player.worldObj.isRemote)
if (this.getSlotIndex() == 0 /*индекс твоего слота*/ && (this.getStack() == null || this.getStack().getItem() != Items.chainmail_helmet)) {...
if(SlotPNV.getItem() == harvestItems.pnv) {
int slotIDpnv = 0;
ExtendedPlayer ep = ExtendedPlayer.get(player);
if (ep == null) return;
ItemStack pnv = ep.inventory.getStackInSlot(slotIDpnv);
if (pnv != null && pnv.getItem == MyItems.itemPNV) {
//Если эвент клиентский, то рендер, если сервер, то давать эффект ночного зрения.
}
}
нет не работает.
подобное можно по-разному делать
public class PlayerInv implements IExtendedEntityProperties {
public final static String TAG = SMTCommon.MODID + ":AddInv";
public final InventoryBasic inv = new InventoryBasic(null, false, 8 * 3);
public PlayerInv() {}
boolean usedPNV, last_usedPNV;
public void update(EntityPlayer player) {
ItemStack pnv = inv.getStackInSlot(0);
if (pnv != null && pnv.getItem() instanceof ItemCustomArmorDummy)
usedPNV = true; else usedPNV = false;
if (usedPNV && !last_usedPNV) player.addPotionEffect(new PotionEffect(Potion.nightVision.id, Integer.MAX_VALUE, 0, true));
if (!usedPNV && last_usedPNV) player.removePotionEffect(Potion.nightVision.id);
last_usedPNV = usedPNV;
}
@Override
public void init(Entity entity, World world) {
}
public static void reg(Entity entity) {
entity.registerExtendedProperties(PlayerInv.TAG, new PlayerInv());
}
public static PlayerInv get(Entity entity) {
return entity != null ? (PlayerInv)entity.getExtendedProperties(TAG) : null;
}
@Override
public void saveNBTData(NBTTagCompound nbt) {
NBTTagCompound compound = new NBTTagCompound();
NBTTagList list = new NBTTagList();
for (int i = 0; i < inv.getSizeInventory(); ++i) {
if (inv.getStackInSlot(i) == null) continue;
NBTTagCompound slot_nbt = new NBTTagCompound();
inv.getStackInSlot(i).writeToNBT(slot_nbt);
slot_nbt.setByte("Slot", (byte)i);
list.appendTag(slot_nbt);
}
compound.setTag("Inventory", list);
nbt.setTag(TAG, compound);
}
@Override
public void loadNBTData(NBTTagCompound nbt) {
if (nbt.hasKey(TAG)) {
NBTTagCompound compound = (NBTTagCompound)nbt.getTag(TAG);
if (compound.hasKey("Inventory", NBT.TAG_LIST)) {
NBTTagList list = compound.getTagList("Inventory", NBT.TAG_COMPOUND);
for (int i = 0; i < list.tagCount(); ++i) {
NBTTagCompound slot_nbt = list.getCompoundTagAt(i);
inv.setInventorySlotContents((slot_nbt.getByte("Slot") & 255), ItemStack.loadItemStackFromNBT(slot_nbt));
}
}
}
}
}
@SubscribeEvent
public void addEntityConstructing(EntityEvent.EntityConstructing event) {
if (event.entity instanceof EntityPlayer) {
if (PlayerInv.get((EntityPlayer)event.entity) == null)
PlayerInv.reg((EntityPlayer)event.entity);
}
}
@SubscribeEvent
public void onLivingEvent(LivingEvent.LivingUpdateEvent event) {
if (event.entity instanceof EntityPlayer) {
PlayerInv pi = PlayerInv.get((EntityPlayer)event.entity);
if (pi != null) pi.update((EntityPlayer)event.entity);
}
}
@SubscribeEvent
public void onRenderModelForPlayer(RenderPlayerEvent.Specials.Post event) {
PlayerInv pi = PlayerInv.get((EntityPlayer)event.entity);
if (pi == null) return;
ItemStack pnv = pi.inv.getStackInSlot(0);
if (pnv == null || !(pnv.getItem() instanceof ItemCustomArmorDummy)) return;
ItemCustomArmorDummy ivcad = (ItemCustomArmorDummy)pnv.getItem();
ArmorCustomModel acm = ivcad.getModel(event.entityPlayer, pnv, -1);
if (acm != null) {
acm.partHead.set(event.renderer.modelBipedMain.bipedHead);
acm.partBody.set(event.renderer.modelBipedMain.bipedBody);
acm.partRightArm.set(event.renderer.modelBipedMain.bipedRightArm);
acm.partLeftArm.set(event.renderer.modelBipedMain.bipedLeftArm);
acm.partRightLeg.set(event.renderer.modelBipedMain.bipedRightLeg);
acm.partLeftLeg.set(event.renderer.modelBipedMain.bipedLeftLeg);
acm.render(event.entityPlayer);
}
}
Это мои методы.И в общем handler ошибки с reg , get , update
Это главный класс у меня.+smtcommon у мя нет
Вряд-ли, так как типа контейнер должен быть постоянно открытым, а он будет открытым, пока не закроется его GUI.на контейнерах так будет работать ?
ublic class SlotPNV extends Slot {
public SlotPNV(IInventory inventory, int slotIndex, int x, int y)
{
super(inventory, slotIndex, x, y);
}
public boolean isItemValid(ItemStack stack, EntityPlayer player) {
return stack.getItem() == HarvestItems.pnv?true:(stack.getItem() ==HarvestItems.pnv);
}
}
public class InventoryCustomPlayer extends InventoryPlayer implements IInventory {
private final String name = "Inventory";
private final String tagName = "CustomInvTag";
public static final int INV_SIZE = 2;
public ItemStack[] inventory = new ItemStack[INV_SIZE];
public InventoryCustomPlayer(EntityPlayer player) {
super(player);
super.player = player;
}
public void copy(InventoryCustomPlayer inv) {
Вот пример PNV и с Weis код,шо мне делать ?
for(int i = 0; i < inv.getSizeInventory(); ++i) {
ItemStack stack = inv.getStackInSlot(i);
this.inventory[i] = stack == null?null:stack.copy();
}
this.markDirty();
}
public int getSizeInventory() {
return this.inventory.length;
}
public ItemStack getStackInSlot(int slot) {
return this.inventory[slot];
}
public ItemStack decrStackSize(int slot, int amount) {
ItemStack stack = this.getStackInSlot(slot);
if(stack != null) {
if(stack.stackSize > amount) {
stack = stack.splitStack(amount);
this.markDirty();
} else {
this.setInventorySlotContents(slot, (ItemStack)null);
}
}
return stack;
}
public ItemStack getStackInSlotOnClosing(int slot) {
ItemStack stack = this.getStackInSlot(slot);
this.setInventorySlotContents(slot, (ItemStack)null);
return stack;
}
public void setInventorySlotContents(int slot, ItemStack stack) {
this.inventory[slot] = stack;
if(stack != null && stack.stackSize > this.getInventoryStackLimit()) {
stack.stackSize = this.getInventoryStackLimit();
}
this.markDirty();
}
public String getInventoryName() {
return "Inventory";
}
public boolean hasCustomInventoryName() {
return "Inventory".length() > 0;
}
public int getInventoryStackLimit() {
return 1;
}
public void markDirty() {
for(int i = 0; i < this.getSizeInventory(); ++i) {
if(this.getStackInSlot(i) != null && this.getStackInSlot(i).stackSize == 0) {
this.inventory[i] = null;
}
}
}
public boolean isUseableByPlayer(EntityPlayer player) {
return true;
}
public void openInventory() {}
public void closeInventory() {}
public boolean isItemValidForSlot(int slot, ItemStack stack) {
return false;
}
public void writeToNBT(NBTTagCompound compound) {
NBTTagList items = new NBTTagList();
for(int i = 0; i < this.getSizeInventory(); ++i) {
if(this.getStackInSlot(i) != null) {
NBTTagCompound item = new NBTTagCompound();
item.setByte("Slot", (byte)i);
this.getStackInSlot(i).writeToNBT(item);
items.appendTag(item);
}
}
compound.setTag("CustomInvTag", items);
}
public void readFromNBT(NBTTagCompound compound) {
NBTTagList items = compound.getTagList("CustomInvTag", compound.getId());
for(int i = 0; i < items.tagCount(); ++i) {
NBTTagCompound item = items.getCompoundTagAt(i);
byte slot = item.getByte("Slot");
if(slot >= 0 && slot < this.getSizeInventory()) {
this.inventory[slot] = ItemStack.loadItemStackFromNBT(item);
}
}
}
}
public class GuiHandler implements IGuiHandler {
public Object getServerGuiElement(int guiId, EntityPlayer player, World world, int x, int y, int z) {
return guiId == Harvest.GUI_CUSTOM_INV?new ContainerCustomPlayer(player, player.inventory, ExtendedPlayer.get(player).inventory):null;
}
public Object getClientGuiElement(int guiId, EntityPlayer player, World world, int x, int y, int z) {
return guiId == Harvest.GUI_CUSTOM_INV?new GuiCustomPlayerInventory(player, player.inventory, ExtendedPlayer.get(player).inventory):null;
}
}
public class ContainerCustomPlayer extends Container {
private final EntityPlayer player;
private static final int ARMOR_START = 1;
private static final int ARMOR_END = 4;
private static final int INV_START = 4;
private static final int INV_END = 4;
private static final int HOTBAR_START = 5;
private static final int HOTBAR_END = 13;
public ContainerCustomPlayer(EntityPlayer player, InventoryPlayer inventoryPlayer, InventoryCustomPlayer inventoryCustom) {
this.player = player;
this.addSlotToContainer(new SlotBackpack(inventoryCustom, 0, 26, 44));//26 44
this.addSlotToContainer(new SlotPNV(inventoryCustom, 1, 26, 8));//26 8
int i;
for(i = 0; i < 4; ++i) {
this.addSlotToContainer(new SlotArmor(player, inventoryPlayer, inventoryPlayer.getSizeInventory() - 1 - i, 8, 8 + i * 18, i));
}
int j;
for(j = 0; j < 9; ++j) {
this.addSlotToContainer(new Slot(inventoryPlayer, j + 9, 8 + j * 18, 84));
}
for(j = 0; j < 9; ++j) {
this.addSlotToContainer(new Slot(inventoryPlayer, j + 18, 8 + j * 18, 102));
}
for(j = 0; j < 9; ++j) {
this.addSlotToContainer(new Slot(inventoryPlayer, j + 27, 8 + j * 18, 120));
}
for(i = 0; i < 9; ++i) {
this.addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 142));
}
}
public boolean canInteractWith(EntityPlayer player) {
return true;
}
public ItemStack transferStackInSlot(EntityPlayer player, int par2) {
return null;
}
public void onContainerClosed(EntityPlayer player) {
super.onContainerClosed(player);
Slot slot = (Slot)super.inventorySlots.get(0);
if(slot != null && slot.getHasStack()) {
Item var6 = slot.getStack().getItem();
ItemStack itemstack1;
int var7;
if(var6 == HarvestItems.smallRucksack) {
for(var7 = 18; var7 < 36; ++var7) {
itemstack1 = player.inventory.getStackInSlotOnClosing(var7);
if(itemstack1 != null) {
player.dropPlayerItemWithRandomChoice(itemstack1, false);
}
}
} else if(var6 == HarvestItems.mediumRucksack) {
for(var7 = 27; var7 < 36; ++var7) {
itemstack1 = player.inventory.getStackInSlotOnClosing(var7);
if(itemstack1 != null) {
player.dropPlayerItemWithRandomChoice(itemstack1, false);
}
}
}
} else {
for(int i = 9; i < 36; ++i) {
ItemStack itemstack = player.inventory.getStackInSlotOnClosing(i);
if(itemstack != null) {
player.dropPlayerItemWithRandomChoice(itemstack, false);
}
}
}
}
public ItemStack slotClick(int num, int var1, int typeClick, EntityPlayer player) {
ItemStack equipStack = ExtendedPlayer.get(player).inventory.getStackInSlot(0);
if(equipStack != null) {
Item equip = equipStack.getItem();
if(equip == HarvestItems.smallRucksack) {
if(num < 32 && num > 13) {
return null;
}
} else if(equip == HarvestItems.mediumRucksack) {
if(num < 32 && num > 22) {
return null;
}
} else if(equip == HarvestItems.bigRucksack) {
;
}
} else if(num < 32 && num > 4) {
return null;
}
return super.slotClick(num, var1, typeClick, player);
}
}