Jump to content

[1.8]iinventory howto prevent a player to move certain slot in inventory<Solved>


Recommended Posts

Posted

good days

 

as in the title explain i wanna make an inventory whith a gui in which you just can interacc whith the enable itemSlots

 

is becose i make a gui that allow acces to mobs inventory things like armours and held item , and i dont wanna have player exploting this to dissarm armed mobs or pillage expensive items from mobs or other players

Posted

yes but

i want them to be visible  and you can not move this items anywhere

 

the other tink i notice is that aparently you can not change the declared container  on the fly and i need someting like that

but is little hars to explain in english and

tats gonna need video but no time now

Posted

Only the slots that you actually register via

addSlotToContainer

in your Container class will be usable by the player.

 

IInventory has a "isUsableByplayer" method right? Or is that on Container? Either way, override it to false and/or some method to check if they can edit or not and you should be fine, no?

 

By default its true, or its something like:

 

@Override
    public boolean isUseableByPlayer(EntityPlayer par1EntityPlayer)
    {
        return true;
    }

 

Sometimes people use this to determine if player is within 8 blocks of the object:

 

@Override
    public boolean isUseableByPlayer(EntityPlayer playerIn)
    {
        return worldObj.getTileEntity(pos) != this ? false : 
              playerIn.getDistanceSq(pos.getX()+0.5D, pos.getY()+0.5D, 
              pos.getZ()+0.5D) <= 64.0D;
    }

 

Isn't that checking if the player is able to utilize the inventory, as in, modify it?

www.YouTube.com/WeiseGamer

www.twitter.com/WeiseGamer

Posted

That would be for the whole container though.

 

True, if the slots are all assigned to the same IInventory.

 

this.addSlotToContainer(new Slot(IInventory, slotIndex, xPos, yPos))

 

Make the "enable slots" have their own IInventory? Or make a custom Slot called "enableSlot" and @Override onSlotClicked.

 

I feel there are a few ways of doing what he wants, but its a little hard to understand the expected result.

 

Or add an IF statement that checks the slotClicked on isUsableByPlayer (I can't recall if the slot is passed into this class or not).

www.YouTube.com/WeiseGamer

www.twitter.com/WeiseGamer

Posted

good nigths

 

sorry i been working on this and forget all about the post

 

I found  its posible to, on the fly change the Container and the the gui background using a trick calling again the gui handler, in mi case

 

player.openGui(Mercenary.instance, guiHandlers.GUIMERCENARIA00, worldIn, 0, 0, 0);

 

but must be done in the rigth moment and has its downsides,  its throws to the ground, whatever the itemStack you have in your hand

 

this code has some issues, sometimes the armor gets stuck and the gui/container don't change, i think is becoze it take some tics to the server to recive the packages or whatever and apply changes to inventory, so its end whith something null

 

Video

 

here i can open the inventory of any entityLiving mob set armours change weapons, thats what i wanna control,

i create 3 sets of custome container/gui 

 

the first for  entities whitout armour or  armour whitout pockets  just five slots for armour and main weapon

the second for the armours whith sevent pockets six for munition and one for SubWeapon

the third for armours whith 23 pockets , one subWeapon plus 6 slots plus backpack 16 slots

 

the gui now change in base to what its inside the entity chest slot

 

 

###

Questions

- ¿is posible to send a package whith an NBTTag compound  whith all the items  ffrom local to server ?? i need to fix sync

 

- ¿how do you do the canTakeStack ting

i found the code

    public boolean canTakeStack(EntityPlayer playerIn)
    {
        return true;
    }
     @SideOnly(Side.CLIENT)
    public boolean canBeHovered()
    {
        return true;
    }

but no idea of how actually use them

 

well thanks

Posted

 

 

(hiding them, by setting e.g. xCoord to -999)

thats good thats solve most of the troubles but was not so easy i have to make seters a getters to properly control gui and container from iinventory  and add some methods to delete the arrayList in the container and recreate it as i whish at demand,

 

for some reason i could not override  the other method in the original container class whitout end whit weird behaveours

if i override  slotClick(int slotId, int clickedButton, int mode, EntityPlayer playerIn) whiout changes it works until player try to drag someting then crash.  or left all the slots unaccesibles  close/open to fix , whith this method i could avoid the player to make click and get the contens for individual slots 

 

ñaa i gonna end written some rules to avoid open the gui over some mobs and creatures

 

###

the nest question is how do you prevent to put items that are not armour in the armmour slots, so you can only put helmet items in the helmet slots

and boot in boots slot

im pretty sure i read something about it in a guide from jabellar but now idont find it anywhere 

 

Posted

 

well is becoze i been alredy tryng that and don't works

this is in the IInventory class, but this is just for when player pickup somethig or drop something in the inventory

i think must do same but in the slot class and thats trouble

 

if i extend the Slot.class whit mi mercenarySlot.class for some reason the forge Container.class don't accep mi  slot's  and i end whith a bunch of empty slots in the gui,  is like all slots that reach to Container.class is null or becomes null and it sends its like that

 

// ###################################################################################################
/**
 * Returns true if automation is allowed to insert the given stack (ignoring
 * stack size) into the given slot.
 */
@Override
public boolean isItemValidForSlot(int index, ItemStack stack) {


	if (index > 0 & index < 5  )
	{

		 /** Stores the armor type: 0 is helmet, 1 is plate, 2 is legs and 3 is boots */

		int armadura = armasDisparables.esArmadura(stack);

		if (armadura < 0)
		{
			return false;
		}

		//botas
		if (index == 1 & armadura != 3)
		{
			return false;
		}

		//pantalones
		if (index == 2 & armadura != 2)
		{
			return false;
		}

		//chest
		if (index == 3 & armadura != 1)
		{
			return false;
		}

		//casco
		if (index == 4 & armadura != 0)
		{
			return false;
		}


	}

	return true;

}

// ###################################################################################################

 

package mercenarymod.gui.containers;

//package net.minecraft.inventory;

import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class mercenarySlot extends Slot {
/** The index of the slot in the inventory. */
private final int slotIndex;
/** The inventory we want to extract a slot from. */
public final IInventory inventory;
/** the id of the slot(also the index in the inventory arraylist) */
public int slotNumber;
/** display position of the inventory slot on the screen x axis */
public int xDisplayPosition;
/** display position of the inventory slot on the screen y axis */
public int yDisplayPosition;
private static final String __OBFID = "CL_00001762";
private boolean habilitado = true;

// ##########################################################################################3
public mercenarySlot(IInventory inventoryIn, int index, int xPosition, int yPosition) {
	super(inventoryIn, index, xPosition, yPosition);
	this.inventory = inventoryIn;
	this.slotIndex = index;
	this.xDisplayPosition = xPosition;
	this.yDisplayPosition = yPosition;
}

// ##########################################################################################3
@Override
/**
 * if par2 has more items than par1, onCrafting(item,countIncrease) is
 * called
 */
public void onSlotChange(ItemStack p_75220_1_, ItemStack p_75220_2_) {
	if (p_75220_1_ != null && p_75220_2_ != null) {
		if (p_75220_1_.getItem() == p_75220_2_.getItem()) {
			int i = p_75220_2_.stackSize - p_75220_1_.stackSize;

			if (i > 0) {
				this.onCrafting(p_75220_1_, i);
			}
		}
	}
}

// ##########################################################################################3
@Override
/**
 * the itemStack passed in is the output - ie, iron ingots, and pickaxes,
 * not ore and wood. Typically increases an internal count then calls
 * onCrafting(item).
 */
protected void onCrafting(ItemStack stack, int amount) {
}

// ##########################################################################################3
@Override
/**
 * the itemStack passed in is the output - ie, iron ingots, and pickaxes,
 * not ore and wood.
 */
protected void onCrafting(ItemStack stack) {
}

// ##########################################################################################3
@Override
public void onPickupFromSlot(EntityPlayer playerIn, ItemStack stack) {
	this.onSlotChanged();
}

// ##########################################################################################3
@Override
/**
 * Check if the stack is a valid item for this slot. Always true beside for
 * the armor slots.
 */


public boolean isItemValid(ItemStack stack) {

	return true;

}

// ##########################################################################################3
@Override
/**
 * Helper fnct to get the stack in the slot.
 */
public ItemStack getStack() {
	return this.inventory.getStackInSlot(this.slotIndex);
}

// ##########################################################################################3
@Override
/**
 * Returns if this slot contains a stack.
 */
public boolean getHasStack() {
	return this.getStack() != null;
}

// ##########################################################################################3
@Override
/**
 * Helper method to put a stack in the slot.
 */
public void putStack(ItemStack stack) {
	this.inventory.setInventorySlotContents(this.slotIndex, stack);
	this.onSlotChanged();
}

// ##########################################################################################3
@Override
/**
 * Called when the stack in a Slot changes
 */
public void onSlotChanged() {
	this.inventory.markDirty();
}

// ##########################################################################################3
@Override
/**
 * Returns the maximum stack size for a given slot (usually the same as
 * getInventoryStackLimit(), but 1 in the case of armor slots)
 */
public int getSlotStackLimit() {
	return this.inventory.getInventoryStackLimit();
}

// ##########################################################################################3
@Override
public int getItemStackLimit(ItemStack stack) {
	return this.getSlotStackLimit();
}

// ##########################################################################################3
@Override
@SideOnly(Side.CLIENT)
public String getSlotTexture() {
	return backgroundName;
}

// ##########################################################################################3
@Override
/**
 * Decrease the size of the stack in slot (first int arg) by the amount of
 * the second int arg. Returns the new stack.
 */
public ItemStack decrStackSize(int amount) {
	return this.inventory.decrStackSize(this.slotIndex, amount);
}

// ##########################################################################################3
@Override
/**
 * returns true if the slot exists in the given inventory and location
 */
public boolean isSlotInInventory(IInventory inv, int slotIn) {
	return inv == this.inventory && slotIn == this.slotIndex;
}

// ##########################################################################################3
@Override
/**
 * Return whether this slot's stack can be taken from this slot.
 */
public boolean canTakeStack(EntityPlayer playerIn) {
	return habilitado;
}

// ##########################################################################################3
@Override
/**
 * Actualy only call when we want to render the white square effect over the
 * slots. Return always True, except for the armor slot of the Donkey/Mule
 * (we can't interact with the Undead and Skeleton horses)
 */
@SideOnly(Side.CLIENT)
public boolean canBeHovered() {
	return habilitado;
}

// ##########################################################################################3
public void setHabilitado(boolean b) {
	habilitado = b;
}

// ##########################################################################################3
public boolean getHabilitado() {
	return habilitado;
}

}

 

 

 

package mercenarymod.gui.containers;

import net.minecraft.entity.Entity;

import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.ICrafting;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.MathHelper;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

import mercenarymod.entidades.armasDisparables;
import mercenarymod.items.armasdefuego.inventario.inventarioArmas;
import mercenarymod.items.armasdefuego.inventario.inventarioArmasyEntidades;
import mercenarymod.tileentity.ModTileEntity;
import mercenarymod.utilidades.util;

public class ContainerMenuMercenario02 extends Container {

private int bolsillos = 0;

private inventarioArmasyEntidades inventarioenlaArmadura;

private IInventory playerInv = null;

private EntityLivingBase entidad = null;

/**
 * The current drag mode (0 : evenly split, 1 : one item by slot, 2 : not
 * used ?)
 */
//private int dragMode = -1;
/** The current drag event (0 : start, 1 : add slot : 2 : end) */
//private int dragEvent;
/** The list of slots where the itemstack holds will be distributed */
//private final Set dragSlots = Sets.newHashSet();

// ##############################################################################################################################################

private void cantidaddeBolsillos() {

}

// ##############################################################################################################################################
/*
 * SLOTS:
 *
 * Tile Entity 0-8 ........ 0 - 8 Player Inventory 9-35 .. 9 - 35 Player
 * Inventory 0-8 ... 36 - 44
 */
public ContainerMenuMercenario02(IInventory playerInv, inventarioArmasyEntidades te) {
	// super();
	this.inventarioenlaArmadura = te;
	this.bolsillos = te.getBolsillos();
	this.entidad = te.getEntity();
	this.playerInv = playerInv;

	// System.out.println("################# ContainerMenuMercenario02");

	// weapon slot
	this.addSlotToContainer(new   mercenarySlot(te, 0, 26, );

	// entity armor slots
	this.addSlotToContainer(new   mercenarySlot(te, 4, 8, );
	this.addSlotToContainer(new   mercenarySlot(te, 3, 8, 26));
	this.addSlotToContainer(new   mercenarySlot(te, 2, 8, 44));
	this.addSlotToContainer(new   mercenarySlot(te, 1, 8, 62));

	// Sub weapon slot
	this.addSlotToContainer(new   mercenarySlot(te, 5, -999, 26));

	// six pockets tactical vest

	this.addSlotToContainer(new   mercenarySlot(te, 6, -999, );
	this.addSlotToContainer(new   mercenarySlot(te, 7, -999, );

	this.addSlotToContainer(new   mercenarySlot(te, 8, -999, 26));
	this.addSlotToContainer(new   mercenarySlot(te, 9, -999, 26));

	this.addSlotToContainer(new   mercenarySlot(te, 10, -999, 44));
	this.addSlotToContainer(new   mercenarySlot(te, 11, -999, 44));

	// BackPack
	for (int x = 0; x < 4; ++x) {

		this.addSlotToContainer(new   mercenarySlot(te, 12 + (x * 4), -999, 8 + (18 * x)));
		this.addSlotToContainer(new   mercenarySlot(te, 13 + (x * 4), -999, 8 + (18 * x)));
		this.addSlotToContainer(new   mercenarySlot(te, 14 + (x * 4), -999, 8 + (18 * x)));
		this.addSlotToContainer(new   mercenarySlot(te, 15 + (x * 4), -999, 8 + (18 * x)));

	}



	// 0 - 27 chest slots

	// 28 - 54
	// Player Inventory, Slot 9-35, Slot IDs 9-35
	for (int y = 0; y < 3; ++y) {
		for (int x = 0; x < 9; ++x) {
			this.addSlotToContainer(new   mercenarySlot(playerInv, x + y * 9 + 9, 8 + x * 18, 84 + y * 18));
		}
	}

	// 55 - 64
	// Player Inventory, Slot 0-8, Slot IDs 36-44
	for (int x = 0; x < 9; ++x) {
		this.addSlotToContainer(new   mercenarySlot(playerInv, x, 8 + x * 18, 142));
	}

	// te.readFromNBT(null);

	ItemStack chestIS = this.entidad.getEquipmentInSlot(3);

	if (chestIS != null) {

		int sn = armasDisparables.tieneBolsillos(chestIS);
		this.bolsillos = sn;

		switch (sn) {
		case 0:
			this.cambiarA0();
			break;
		case 7:
			this.cambiarA7();
			break;
		case 23:
			this.cambiarA23();
			break;
		}
	}

}
// ##############################################################################################################################################

public void cambiarA0() {
	inventoryItemStacks = Lists.newArrayList();
	/** the list of all slots in the inventory */
	inventorySlots = Lists.newArrayList();
	crafters = Lists.newArrayList();

	// System.out.println("################# ContainerMenu cambiarA0");

	// weapon slot
	this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 0, 26, );

	// entity armor slots
	this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 4, 8, );
	this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 3, 8, 26));
	this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 2, 8, 44));
	this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 1, 8, 62));

	// Sub weapon slot
	this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 5, -999, 26));

	// six pockets tactical vest

	this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 6, -999, );
	this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 7, -999, );

	this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 8, -999, 26));
	this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 9, -999, 26));

	this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 10, -999, 44));
	this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 11, -999, 44));

	// BackPack
	for (int x = 0; x < 4; ++x) {

		this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 12 + (x * 4), -999, 8 + (18 * x)));
		this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 13 + (x * 4), -999, 8 + (18 * x)));
		this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 14 + (x * 4), -999, 8 + (18 * x)));
		this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 15 + (x * 4), -999, 8 + (18 * x)));

	}

	/*
	 * Tile Entity 0-8 ........ 0-27 Player Inventory 9-35 .. 28 - 55 Player
	 * Inventory 0-8 ... 56 - 64
	 */

	// 0 - 27 chest slots

	// 28 - 54
	// Player Inventory, Slot 9-35, Slot IDs 9-35
	for (int y = 0; y < 3; ++y) {
		for (int x = 0; x < 9; ++x) {
			this.addSlotToContainer(new   mercenarySlot(this.playerInv, x + y * 9 + 9, 8 + x * 18, 84 + y * 18));
		}
	}

	// 55 - 64
	// Player Inventory, Slot 0-8, Slot IDs 36-44
	for (int x = 0; x < 9; ++x) {
		this.addSlotToContainer(new   mercenarySlot(this.playerInv, x, 8 + x * 18, 142));
	}

	// System.out.println("inventorySlots"+inventorySlots);
	// System.out.println("inventorySlots="+inventorySlots.size());

}
// ##############################################################################################################################################

public void cambiarA7() {
	inventoryItemStacks = Lists.newArrayList();
	/** the list of all slots in the inventory */
	inventorySlots = Lists.newArrayList();
	crafters = Lists.newArrayList();

	// System.out.println("################# ContainerMenu cambiarA7");

	// weapon slot
	this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 0, 26, );

	// entity armor slots
	this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 4, 8, );
	this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 3, 8, 26));
	this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 2, 8, 44));
	this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 1, 8, 62));

	// Sub weapon slot
	this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 5, 26, 26));

	// six pockets tactical vest

	this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 6, 53, );
	this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 7, 71, );

	this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 8, 53, 26));
	this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 9, 71, 26));

	this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 10, 53, 44));
	this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 11, 71, 44));

	// BackPack
	for (int x = 0; x < 4; ++x) {

		this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 12 + (x * 4), -999, 8 + (18 * x)));
		this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 13 + (x * 4), -999, 8 + (18 * x)));
		this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 14 + (x * 4), -999, 8 + (18 * x)));
		this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 15 + (x * 4), -999, 8 + (18 * x)));

	}

	/*
	 * Tile Entity 0-8 ........ 0-27 Player Inventory 9-35 .. 28 - 55 Player
	 * Inventory 0-8 ... 56 - 64
	 */

	// 0 - 27 chest slots

	// 28 - 54
	// Player Inventory, Slot 9-35, Slot IDs 9-35
	for (int y = 0; y < 3; ++y) {
		for (int x = 0; x < 9; ++x) {
			this.addSlotToContainer(new   mercenarySlot(this.playerInv, x + y * 9 + 9, 8 + x * 18, 84 + y * 18));
		}
	}

	// 55 - 64
	// Player Inventory, Slot 0-8, Slot IDs 36-44
	for (int x = 0; x < 9; ++x) {
		this.addSlotToContainer(new   mercenarySlot(this.playerInv, x, 8 + x * 18, 142));
	}

	// System.out.println("inventorySlots"+inventorySlots);
	// System.out.println("inventorySlots="+inventorySlots.size());

}
// ##############################################################################################################################################

public void cambiarA23() {
	inventoryItemStacks = Lists.newArrayList();
	/** the list of all slots in the inventory */
	inventorySlots = Lists.newArrayList();
	crafters = Lists.newArrayList();

	// System.out.println("################# ContainerMenu cambiarA7");

	// weapon slot
	this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 0, 26, );

	// entity armor slots
	this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 4, 8, );
	this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 3, 8, 26));
	this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 2, 8, 44));
	this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 1, 8, 62));

	// Sub weapon slot
	this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 5, 26, 26));

	// six pockets tactical vest

	this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 6, 53, );
	this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 7, 71, );

	this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 8, 53, 26));
	this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 9, 71, 26));

	this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 10, 53, 44));
	this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 11, 71, 44));

	// BackPack
	for (int x = 0; x < 4; ++x) {

		this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 12 + (x * 4), 98, 8 + (18 * x)));
		this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 13 + (x * 4), 116, 8 + (18 * x)));
		this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 14 + (x * 4), 134, 8 + (18 * x)));
		this.addSlotToContainer(new   mercenarySlot(inventarioenlaArmadura, 15 + (x * 4), 152, 8 + (18 * x)));

	}

	/*
	 * Tile Entity 0-8 ........ 0-27 Player Inventory 9-35 .. 28 - 55 Player
	 * Inventory 0-8 ... 56 - 64
	 */

	// 0 - 27 chest slots

	// 28 - 54
	// Player Inventory, Slot 9-35, Slot IDs 9-35
	for (int y = 0; y < 3; ++y) {
		for (int x = 0; x < 9; ++x) {
			this.addSlotToContainer(new   mercenarySlot(this.playerInv, x + y * 9 + 9, 8 + x * 18, 84 + y * 18));
		}
	}

	// 55 - 64
	// Player Inventory, Slot 0-8, Slot IDs 36-44
	for (int x = 0; x < 9; ++x) {
		this.addSlotToContainer(new   mercenarySlot(this.playerInv, x, 8 + x * 18, 142));
	}

	// System.out.println("inventorySlots"+inventorySlots);
	// System.out.println("inventorySlots="+inventorySlots.size());

}

// ##############################################################################################################################################
@Override
public boolean canInteractWith(EntityPlayer playerIn) {
	return this.inventarioenlaArmadura.isUseableByPlayer(playerIn);
}

// ##############################################################################################################################################
@Override
public ItemStack transferStackInSlot(EntityPlayer playerIn, int fromSlot) {
	ItemStack previous = null;
	Slot slot = (Slot) this.inventorySlots.get(fromSlot);

	if (slot != null && slot.getHasStack()) {
		ItemStack current = slot.getStack();
		previous = current.copy();

		if (fromSlot < 28) {
			// From TE Inventory to Player Inventory
			if (!this.mergeItemStack(current, 28, 63, true))
				return null;
		} else {
			// From Player Inventory to TE Inventory
			if (!this.mergeItemStack(current, 0, 28, false))
				return null;
		}

		if (current.stackSize == 0)
			slot.putStack((ItemStack) null);
		else
			slot.onSlotChanged();

		if (current.stackSize == previous.stackSize)
			return null;
		slot.onPickupFromSlot(playerIn, current);
	}

	return previous;
}

// ##############################################################################################################################################

 

 

package mercenarymod.items.armasdefuego.inventario;

import java.util.ArrayList;
import java.util.Random;

import mercenarymod.Mercenary;
import mercenarymod.entidades.armasDisparables;
import mercenarymod.gui.guiHandlers;
import mercenarymod.gui.containers.ContainerMenuMercenario00;
import mercenarymod.gui.containers.ContainerMenuMercenario01;
import mercenarymod.gui.containers.ContainerMenuMercenario02;
import mercenarymod.gui.guis.guiMenuMercenario02;
import mercenarymod.items.MercenaryModItems;
import mercenarymod.tileentity.ModTileEntity;
import mercenarymod.utilidades.util;
import net.minecraft.block.BlockChest;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.monster.EntitySkeleton;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.ContainerDispenser;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntityDispenser;
import net.minecraft.tileentity.TileEntityLockable;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.ChatComponentTranslation;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.IChatComponent;
import net.minecraft.world.IWorldNameable;
import net.minecraft.world.World;

//###################################################################################################
public class inventarioArmasyEntidades implements IInventory 
// extends TileEntityDispenser implements IInventory {
{
// private ItemStack pistola = null;

private int SizeInventory = 28;

private ItemStack[] Items = new ItemStack[sizeInventory];
private boolean[] slotHabilitado = new boolean[sizeInventory];

private World worldIn = null;

private static final Random RNG = new Random();

private String customName = "algo";

private EntityLivingBase entidad = null;

private EntityLivingBase playerIn = null;

private double posEX = 0;
private double posEY = 0;
private double posEZ = 0;

private ItemStack chestItem = null;
private int numerodeserie = 0;
private int bolsillos = 0;

private ContainerMenuMercenario02 containerMercenario = null;

private guiMenuMercenario02 gui02 = null;

// ###################################################################################################

public inventarioArmasyEntidades(World worldIn, EntityLivingBase entidad, EntityLivingBase playerIn) {
	super();
	// this.pistola = entidad.getHeldItem() ;
	this.worldIn = worldIn;
	this.entidad = entidad;
	this.playerIn = playerIn;

	// System.out.println("\n###\n###\n###\ninventarioArmasyEntidades=" +
	// worldIn.isRemote + "\n###");

	NBTTagCompound tagCompund = null;

	if (this.entidad != null) {

		this.readFromNBT(null);

	}

}
// ###################################################################################################

/**
 * Returns the number of slots in the inventory.
 */
@Override
public int getSizeInventory() // ItemStack pistola
{

	return SizeInventory;// SizeInventory;
}
// ###################################################################################################

/**
 * inicializa cuales slots son validos y cuales estaran deshabilitados
 */
//  a try to disable the armour slots whiout hide them 

// @Override
public void habilitarSlots() // ItemStack pistola
{

	for (int n = 0; n < SizeInventory; n++) {

		slotHabilitado[n] = true;

		if (n <= this.bolsillos + 4) {
			// slotHabilitado[n] = true;

		} else {
			//
			Items[n] = new ItemStack(MercenaryModItems.vacio, 1, 1);
			slotHabilitado[n] = false;
		}

		// System.out.println("mundo="+worldIn.isRemote+"
		// slotHabilitado["+n+"]="+slotHabilitado[n]);

	}

}

// ###################################################################################################
/**
 * Add the given ItemStack to this Dispenser. Return the Slot the Item was
 * placed in or -1 if no free slot is available.
 */
public int addItemStack(ItemStack stack) {

	for (int i = 0; i < Items.length; ++i) {
		if ((Items[i] == null || Items[i].getItem() == null)) {
			this.setInventorySlotContents(i, stack);
			return i;
		}
	}

	return -1;
}

// ###################################################################################################
/**
 * Returns the maximum stack size for a inventory slot. Seems to always be
 * 64, possibly will be extended. *Isn't this more of a set than a get?*
 */
@Override
public int getInventoryStackLimit() {
	return 64;
}

// ###################################################################################################
/**
 * 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.
 */

public void markDirty() {
	if (worldIn != null) {

		// System.out.println("\n\n Marcado como sucio en el mundo=" +
		// worldIn.isRemote);

		writeToNBT(new NBTTagCompound());

	}
}

// ###################################################################################################
/**
 * Do not make give this method the name canInteractWith because it clashes
 * with Container
 */
@Override
public boolean isUseableByPlayer(EntityPlayer player) {
	return true;
}

@Override
public void openInventory(EntityPlayer player) {

	// System.out.println("###### openInventory() ");

}

@Override
public void closeInventory(EntityPlayer player) {

	// System.out.println("###### closeInventory() ");

}

// ###################################################################################################
/**
 * Returns true if automation is allowed to insert the given stack (ignoring
 * stack size) into the given slot.
 */
@Override
public boolean isItemValidForSlot(int index, ItemStack stack) {


	if (index > 0 & index < 5  )
	{

		 /** Stores the armor type: 0 is helmet, 1 is plate, 2 is legs and 3 is boots */

		int armadura = armasDisparables.esArmadura(stack);

		if (armadura < 0)
		{
			return false;
		}

		//botas
		if (index == 1 & armadura != 3)
		{
			return false;
		}

		//pantalones
		if (index == 2 & armadura != 2)
		{
			return false;
		}

		//chest
		if (index == 3 & armadura != 1)
		{
			return false;
		}

		//casco
		if (index == 4 & armadura != 0)
		{
			return false;
		}


	}

	return true;

}

// ###################################################################################################

@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() {
	for (int i = 0; i < Items.length; ++i) {
		Items[i] = null;
	}

	// System.out.println("$$$ clear()");

}

@Override
public String getName() {
	return this.customName;
}

/**
 * Returns true if this thing is named
 */
@Override
public boolean hasCustomName() {
	return true;
}

// @Override
public void setCustomName(String name) {
	this.customName = name;
}

@Override
public IChatComponent getDisplayName() {
	IChatComponent displayname = new ChatComponentText(EnumChatFormatting.RED + "" + entidad.getName() + " id:/" + entidad.getEntityId());
	return displayname;
}

// #########################################################################3
// @Override
public void readFromNBT(NBTTagCompound compound) {
	// super.readFromNBT(compound);

	// System.out.println("\n### read from nbt mundo=" + worldIn.isRemote);

	// Items = new ItemStack[this.getSizeInventory()];

	this.clear();

	Items[0] = this.entidad.getEquipmentInSlot(0);// .getHeldItem();
	Items[1] = this.entidad.getEquipmentInSlot(1);// getCurrentArmor(0);
													// boots
	Items[2] = this.entidad.getEquipmentInSlot(2);// getCurrentArmor(1);
													// pants
	Items[3] = this.entidad.getEquipmentInSlot(3);// getCurrentArmor(2);
													// chest
	Items[4] = this.entidad.getEquipmentInSlot(4);// getCurrentArmor(3);
													// helmet

	ItemStack chestIS =  this.entidad.getEquipmentInSlot(3); //Items[3]; //




	this.bolsillos = 0;
	this.numerodeserie = 0;

	if (chestIS != null) {

//get the amount of slots in this chest Armour Item
		int sn = armasDisparables.tieneBolsillos(chestIS);
		this.bolsillos = sn;

//set slots pattern in Container
		if (containerMercenario != null) {
			// System.out.println("#######################
			// containerMercenario="+sn);

			switch (sn) {
			case 0:
				containerMercenario.cambiarA0();
				break;
			case 7:
				containerMercenario.cambiarA7();
				break;
			case 23:
				containerMercenario.cambiarA23();
				break;
			}
		}

//set gui background in gui
		if (gui02 != null) {
			// System.out.println("####################### gui02="+sn);

			gui02.guiN = sn;

		}

		this.chestItem = chestIS;
//serial number from armour to know diferences betwint instance of the same kind of item
		numerodeserie = getInttag(chestIS, "numerodeserie");

		if (numerodeserie < 1) {
			numerodeserie = util.getSerialAlHazar();
			setInttag(chestIS, "numerodeserie", numerodeserie);
		}

		if (sn > 0) {

			NBTTagCompound compound2 = chestIS.getTagCompound();

			if (compound2 != null) {

				NBTTagList nbttaglist2 = compound2.getTagList("Items", 10);

				for (int i = 0; i < nbttaglist2.tagCount(); ++i) {

					NBTTagCompound nbttagcompound1 = nbttaglist2.getCompoundTagAt(i);
					int j = nbttagcompound1.getByte("Slot") & 255;

					// System.out.println("\n### read from nbt");
					if (j >= 0 && j < Items.length) {
						Items[j] = ItemStack.loadItemStackFromNBT(nbttagcompound1);

						if (Items[j] != null) {
							// System.out.println("mundo="+worldIn.isRemote+"
							// slot read" + j + " =" +
							// Items[j].getUnlocalizedName());
						}

					}
				}

				if (compound2.hasKey("CustomName", ) {
					this.customName = compound2.getString("CustomName");
				}

			}

		}

	} else {

//change slots order in Container to just basic five
		if (containerMercenario != null) {
			containerMercenario.cambiarA0();
		}

//change Gui BackGroundto basic five background
		if (gui02 != null) {
			gui02.guiN = 0;
		}

	}

	// System.out.println("mundo="+worldIn.isRemote+"
	// bolsillos="+this.bolsillos);
	// System.out.println("mundo="+worldIn.isRemote+"
	// numerodeserie="+this.numerodeserie);

	habilitarSlots();

}

// #########################################################################3
// @Override
public void writeToNBT(NBTTagCompound compound) {
	// super.writeToNBT(compound);

	// System.out.println("\n### write to nbt mundo=" + worldIn.isRemote);

	ItemStack chestIS = Items[3];// this.entidad.getEquipmentInSlot(3);

	int numerodeserieIS = 0;

	if (chestIS != null) {
		numerodeserieIS = getInttag(chestIS, "numerodeserie");

		if (numerodeserieIS < 1) {
			numerodeserieIS = util.getSerialAlHazar();
			setInttag(chestIS, "numerodeserie", numerodeserieIS);
		}
	}

	// System.out.println(worldIn.isRemote + " sn0=" + this.numerodeserie);
	// System.out.println(worldIn.isRemote + " sn1=" + numerodeserieIS);

	if (numerodeserieIS != numerodeserie) {

		this.entidad.setCurrentItemOrArmor(3, Items[3]);

		readFromNBT(null);

		// System.out.println("###" + "\n\n\n" + "Chest Item hass change");

		if (playerIn instanceof EntityPlayer & worldIn.isRemote) {
			// ((EntityPlayer) playerIn).openGui(Mercenary.instance,
			// guiHandlers.GUIMERCENARIA00, worldIn, 0, 0, 0);
		}

	}

	else

	{

		if (chestIS != null) {

			int sn = armasDisparables.tieneBolsillos(chestIS);

			if (sn > 0) {

				NBTTagList nbttaglist = new NBTTagList();

				for (int i = 5; i < Items.length; ++i) {
					if (Items[i] != null) {

						NBTTagCompound nbttagcompound1 = new NBTTagCompound();
						nbttagcompound1.setByte("Slot", (byte) i);
						Items[i].writeToNBT(nbttagcompound1);
						nbttaglist.appendTag(nbttagcompound1);

					}
				}

				NBTTagCompound chestcompound = chestIS.getTagCompound();
				if (chestcompound == null) {
					chestcompound = new NBTTagCompound();
				}

				if (this.hasCustomName()) {
					chestcompound.setString("CustomName", this.customName);
				}

				chestcompound.setTag("Items", nbttaglist);
				chestIS.setTagCompound(chestcompound);

				this.Items[3] = chestIS;

			}

		}

		this.entidad.setCurrentItemOrArmor(0, Items[0]);
		this.entidad.setCurrentItemOrArmor(1, Items[1]);
		this.entidad.setCurrentItemOrArmor(2, Items[2]);
		this.entidad.setCurrentItemOrArmor(3, Items[3]);
		this.entidad.setCurrentItemOrArmor(4, Items[4]);

	}

}

// #########################################################################3

public int getDispenseSlot() {
	int i = -1;
	int j = 1;

	for (int k = 0; k < Items.length; ++k) {
		if (Items[k] != null && RNG.nextInt(j++) == 0) {
			i = k;
		}
	}

	return i;
}

// #########################################################################3
public String getGuiID() {
	return "minecraft:chest";
}

// #########################################################################3
public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn) {
	return new ContainerMenuMercenario02(playerInventory, this);
}

// #########################################################################3
@Override
public ItemStack getStackInSlot(int index) {

	/*
	 * if (index == 3){ //System.out.println("\n\n"+worldIn.isRemote);
	 * 
	 * if (Items[3] == null){ //System.out.println(" SlotContents"+index+
	 * " st="+"null" ); } else { //System.out.println(" SlotContents"+index+
	 * " st="+Items[3].getUnlocalizedName() ); }
	 * 
	 * }
	 * 
	 */

	if (index < 0 || index >= this.getSizeInventory())
		return null;

	// System.out.println("getStackInSlot"+index);

	return this.Items[index];
}

// #########################################################################3
@Override
public ItemStack decrStackSize(int index, int count) {

	if (this.getStackInSlot(index) != null) {
		ItemStack itemstack;

		if (this.getStackInSlot(index).stackSize <= count) {
			itemstack = this.getStackInSlot(index);
			this.setInventorySlotContents(index, null);
			this.markDirty();
			return itemstack;
		} else {
			itemstack = this.getStackInSlot(index).splitStack(count);

			if (this.getStackInSlot(index).stackSize <= 0) {
				this.setInventorySlotContents(index, null);
			} else {
				// Just to show that changes happened
				this.setInventorySlotContents(index, this.getStackInSlot(index));
			}

			this.markDirty();

			return itemstack;
		}
	} else {
		return null;
	}
}

// #########################################################################3
@Override
public ItemStack getStackInSlotOnClosing(int index) {

	ItemStack stack = this.getStackInSlot(index);

	this.setInventorySlotContents(index, null);

	return stack;
}

// #########################################################################3
@Override
public void setInventorySlotContents(int index, ItemStack stack) {

	if (index == 3) {
		// System.out.println("\n\n" + worldIn.isRemote);

		if (Items[3] == null) {
			// System.out.println(" SlotContents" + index + " st=" +
			// "null");
		} else {
			// System.out.println(" SlotContents" + index + " st=" +
			// Items[3].getUnlocalizedName());
		}

		if (stack == null) {
			// System.out.println(" setInventorySlotContents" + index + "
			// st=" + "null");
		} else {
			// System.out.println(" setInventorySlotContents" + index + "
			// st=" + stack.getUnlocalizedName());
		}

		if (playerIn instanceof EntityPlayer) {
			// ((EntityPlayer) playerIn).openGui(Mercenary.instance,
			// guiHandlers.GUIMERCENARIA00, worldIn, 0, 0, 0);
		}

	}

	if (index < 0 || index >= this.getSizeInventory())
		return;

	if (stack != null && stack.stackSize > this.getInventoryStackLimit())
		stack.stackSize = this.getInventoryStackLimit();

	if (stack != null && stack.stackSize == 0)
		stack = null;

	this.Items[index] = stack;

	this.markDirty();

}

public String getCustomName() {
	return this.customName;
}

// #########################################################################3

public static float getFloattag(ItemStack item, String tag) {

	NBTTagCompound etiquetas = item.getTagCompound();
	if (etiquetas == null) {
		etiquetas = new NBTTagCompound();
		item.setTagCompound(etiquetas);
		return 0.0F;
	}

	float ex = etiquetas.getFloat(tag);
	return ex;

}

// #########################################################################3

public static void setFloattag(ItemStack item, String tag, float value) {

	NBTTagCompound etiquetas = item.getTagCompound();
	if (etiquetas == null) {
		etiquetas = new NBTTagCompound();
	}

	etiquetas.setFloat(tag, value);
	item.setTagCompound(etiquetas);

}

// #########################################################################3

public static int getInttag(ItemStack item, String tag) {

	NBTTagCompound etiquetas = item.getTagCompound();
	if (etiquetas == null) {
		etiquetas = new NBTTagCompound();
		item.setTagCompound(etiquetas);
		return 0;
	}

	int ex = etiquetas.getInteger(tag);
	return ex;

}

// #########################################################################3

public static void setInttag(ItemStack item, String tag, int value) {

	NBTTagCompound etiquetas = item.getTagCompound();
	if (etiquetas == null) {
		etiquetas = new NBTTagCompound();
	}

	etiquetas.setInteger(tag, value);
	item.setTagCompound(etiquetas);

}
// #########################################################################3

public static int[] getIntArraytag(ItemStack item, String tag) {

	NBTTagCompound etiquetas = item.getTagCompound();
	if (etiquetas == null) {
		etiquetas = new NBTTagCompound();
		item.setTagCompound(etiquetas);
		int[] empty = { 0 };
		return empty;
	}

	int[] ex = etiquetas.getIntArray(tag);
	return ex;

}
// #########################################################################3

public static void setIntArraytag(ItemStack item, String tag, int[] value) {

	NBTTagCompound etiquetas = item.getTagCompound();
	if (etiquetas == null) {
		etiquetas = new NBTTagCompound();
	}

	etiquetas.setIntArray(tag, value);
	item.setTagCompound(etiquetas);

}

// #########################################################################3

public static Boolean getBooleantag(ItemStack item, String tag) {

	NBTTagCompound etiquetas = item.getTagCompound();
	if (etiquetas == null) {
		etiquetas = new NBTTagCompound();
		item.setTagCompound(etiquetas);
		return false;
	}

	boolean ex = etiquetas.getBoolean(tag);

	return ex;

}

// #########################################################################3

public static void setBooleantag(ItemStack item, String tag, boolean value) {

	NBTTagCompound etiquetas = item.getTagCompound();
	if (etiquetas == null) {
		etiquetas = item.getTagCompound();
	}

	etiquetas.setBoolean(tag, value);
	item.setTagCompound(etiquetas);

}

// #########################################################################3
public int getBolsillos() {
	return this.bolsillos;
}

// #########################################################################3
public EntityLivingBase getEntity() {
	return this.entidad;
}

public void setContainer(ContainerMenuMercenario02 containerMercenario) {
	this.containerMercenario = containerMercenario;
}

public void setGui(guiMenuMercenario02 gui02) {
	this.gui02 = gui02;
}

}

 

i been thinking maiby i fucked up whith basic java i dont see becoze tireness or something

 

this thing whith the inventory is for unification only one inventory that could work in a vainilla cown, in my custom creepers as well in an elf from the lord of rings mod (asuming its gone a be 1.8 or 1.9 version)

and the inventory must be simple but awesome

 

in today test  the thing goes very well whith littlemaid mod mobs mod and mi mob

when the main gun gets empty it automatically switch to watever is in sub weapon slot

 

and have a creeper that can chase the player whith an ak 47, but first fix this gui then fix the exploting thing bugs and the other thing whit the spawn in the world not working 

 

 

Posted

You never call

setHabilitado()

anywhere.

 

There's also no god damn reason you need to override every single method of the slot or container classes.  For example, here's a custom slot class I use:

 

public class SlotIInventory extends Slot {

public SlotIInventory(IInventory p_i1824_1_, int p_i1824_2_, int p_i1824_3_, int p_i1824_4_) {
	super(p_i1824_1_, p_i1824_2_, p_i1824_3_, p_i1824_4_);
}

@Override
public boolean isItemValid(ItemStack stack) {
	if(inventory.isItemValidForSlot(slotNumber, stack)) {
		ItemStack ss = stack.copy();
		ss.stackSize = 1;
		inventory.setInventorySlotContents(slotNumber, ss);
	}
	return false;
    }

@Override
public boolean canTakeStack(EntityPlayer par1EntityPlayer) {
	inventory.setInventorySlotContents(slotNumber, null);
	return false;
}
}

 

That's it.  No

isSlotInInventory

, no

decrStackSize

, just the methods I actually NEED.

 

Second:

		// Player Inventory, Slot 9-35, Slot IDs 9-35
	for (int y = 0; y < 3; ++y) {
		for (int x = 0; x < 9; ++x) {
			this.addSlotToContainer(new   mercenarySlot(playerInv, x + y * 9 + 9, 8 + x * 18, 84 + y * 18));
		}
	}

Why the fuck are you making the player's inventory slots your custom slot type?  WHY?

 

THIRD when you call cambiarA0, cambiarA7, or cambiarA23, you're adding the player's inventory slots again.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

and thats solve it all

 

fix the slot.class and whith that i fix the other things

now i can display the entity inventorys, whitout hiden slots  disabling the player from steal from the entityes.

fix the thing whith the armour slots now only allow the rigth armour type

 

package mercenarymod.gui.containers;

import mercenarymod.entidades.armasDisparables;

//package net.minecraft.inventory;

import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class mercenarySlot extends Slot {
/** The index of the slot in the inventory. */
private final int slotIndex;
/** The inventory we want to extract a slot from. */
public final IInventory inventory;
/** the id of the slot(also the index in the inventory arraylist) */
public int slotNumber;
/** display position of the inventory slot on the screen x axis */
public int xDisplayPosition;
/** display position of the inventory slot on the screen y axis */
public int yDisplayPosition;
private static final String __OBFID = "CL_00001762";
private boolean habilitado = true;

// ##########################################################################################3
public mercenarySlot(IInventory inventoryIn, int index, int xPosition, int yPosition, boolean habilitado) {
	super(inventoryIn, index, xPosition, yPosition);
	this.inventory = inventoryIn;
	this.slotIndex = index;
	this.xDisplayPosition = xPosition;
	this.yDisplayPosition = yPosition;
	this.habilitado = habilitado;
}
// ##########################################################################################3
@Override
/**
 * Check if the stack is a valid item for this slot. Always true beside for
 * the armor slots.
 */
public boolean isItemValid(ItemStack stack) {

	int index = this.slotIndex;

	if (index > 0 & index < 5  )
	{

		 /** Stores the armor type: 0 is helmet, 1 is plate, 2 is legs and 3 is boots */

		int armadura = armasDisparables.esArmadura(stack);

		if (armadura < 0)
		{
			return false;
		}

		//botas
		if (index == 1 & armadura != 3)
		{
			return false;
		}

		//pantalones
		if (index == 2 & armadura != 2)
		{
			return false;
		}

		//chest
		if (index == 3 & armadura != 1)
		{
			return false;
		}

		//casco
		if (index == 4 & armadura != 0)
		{
			return false;
		}


	}

	return habilitado;
}
// ##########################################################################################3
@Override
/**
 * Return whether this slot's stack can be taken from this slot.
 */
public boolean canTakeStack(EntityPlayer playerIn) {
	return habilitado;
}

// ##########################################################################################3
@Override
/**
 * Actualy only call when we want to render the white square effect over the
 * slots. Return always True, except for the armor slot of the Donkey/Mule
 * (we can't interact with the Undead and Skeleton horses)
 */
@SideOnly(Side.CLIENT)
public boolean canBeHovered() {
	return habilitado;
}

// ##########################################################################################3
public void setHabilitado(boolean b) {
	habilitado = b;
}

// ##########################################################################################3
public boolean getHabilitado() {
	return habilitado;
}

}  

 

 

package mercenarymod.gui.containers;

import net.minecraft.entity.Entity;

import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.ICrafting;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.MathHelper;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

import mercenarymod.entidades.armasDisparables;
import mercenarymod.items.armasdefuego.inventario.inventarioArmas;
import mercenarymod.items.armasdefuego.inventario.inventarioArmasyEntidades;
import mercenarymod.tileentity.ModTileEntity;
import mercenarymod.utilidades.util;

public class ContainerMenuMercenario02 extends Container {


private inventarioArmasyEntidades inventarioenlaArmadura;

private IInventory playerInv = null;

private EntityLivingBase entidad = null;


// ##############################################################################################################################################



// ##############################################################################################################################################
public ContainerMenuMercenario02(IInventory playerInv, inventarioArmasyEntidades te,  boolean canbeTaken ) {
	// super();
	this.inventarioenlaArmadura = te;
	this.entidad = te.getEntity();
	this.playerInv = playerInv;

		// te.readFromNBT(null);

	ItemStack chestIS = this.entidad.getEquipmentInSlot(3);

	int sn = 0;

	if (chestIS != null) {
		//get the slots number from armour and change to that
		sn = armasDisparables.tieneBolsillos(chestIS);
	}

	switch (sn) {
	case 0:
		this.cambiarA0(canbeTaken);
		break;
	case 7:
		this.cambiarA7(canbeTaken);
		break;
	case 23:
		this.cambiarA23(canbeTaken);
		break;
	}




}
// ##############################################################################################################################################

public void cambiarA0( boolean canbeTaken ) {

	//reset all inventory arrays to Zero
	inventoryItemStacks = Lists.newArrayList();
	inventorySlots = Lists.newArrayList();
	crafters = Lists.newArrayList();


	// 0 - 27 chest slots
	// weapon slot
	this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 0, 26, 8, canbeTaken));

	// entity armor slots
	this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 4, 8, 8, canbeTaken));
	this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 3, 8, 26, canbeTaken));
	this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 2, 8, 44, canbeTaken));
	this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 1, 8, 62, canbeTaken));

	// Sub weapon slot
	this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 5, 26, 26, false));

	// six pockets tactical vest

	this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 6, 53, 8, false));
	this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 7, 71, 8, false));

	this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 8, 53, 26, false));
	this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 9, 71, 26, false));

	this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 10, 53, 44, false));
	this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 11, 71, 44, false));

	// BackPack
	for (int x = 0; x < 4; ++x) {

		this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 12 + (x * 4), 98, 8 + (18 * x), false));
		this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 13 + (x * 4), 116, 8 + (18 * x), false));
		this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 14 + (x * 4), 134, 8 + (18 * x), false));
		this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 15 + (x * 4), 152, 8 + (18 * x), false));

	}

	// 28 - 54
	// Player Inventory, Slot 28 - 54, Slot IDs 28 - 54
	for (int y = 0; y < 3; ++y) {
		for (int x = 0; x < 9; ++x) {
			this.addSlotToContainer(new Slot(this.playerInv, x + y * 9 + 9, 8 + x * 18, 84 + y * 18));
		}
	}

	// 55 - 63
	// Player Inventory, Slot 55-63, Slot IDs 55-63
	for (int x = 0; x < 9; ++x) {
		this.addSlotToContainer(new Slot(this.playerInv, x, 8 + x * 18, 142));
	}

}
// ##############################################################################################################################################

public void cambiarA7(boolean canbeTaken) {
	//reset all inventory arrays to Zero
	inventoryItemStacks = Lists.newArrayList();
	inventorySlots = Lists.newArrayList();
	crafters = Lists.newArrayList();


	// 0 - 27 chest slots
	// weapon slot
	this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 0, 26, 8, canbeTaken));

	// entity armor slots
	this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 4, 8, 8, canbeTaken));
	this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 3, 8, 26, canbeTaken));
	this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 2, 8, 44, canbeTaken));
	this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 1, 8, 62, canbeTaken));

	// Sub weapon slot
	this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 5, 26, 26, canbeTaken));

	// six pockets tactical vest

	this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 6, 53, 8, canbeTaken));
	this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 7, 71, 8, canbeTaken));

	this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 8, 53, 26, canbeTaken));
	this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 9, 71, 26, canbeTaken));

	this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 10, 53, 44, canbeTaken));
	this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 11, 71, 44, canbeTaken));

	// BackPack
	for (int x = 0; x < 4; ++x) {

		this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 12 + (x * 4), 98, 8 + (18 * x), false));
		this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 13 + (x * 4), 116, 8 + (18 * x), false));
		this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 14 + (x * 4), 134, 8 + (18 * x), false));
		this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 15 + (x * 4), 152, 8 + (18 * x), false));

	}

	// 28 - 54
	// Player Inventory, Slot 28 - 54, Slot IDs 28 - 54
	for (int y = 0; y < 3; ++y) {
		for (int x = 0; x < 9; ++x) {
			this.addSlotToContainer(new Slot(this.playerInv, x + y * 9 + 9, 8 + x * 18, 84 + y * 18));
		}
	}

	// 55 - 63
	// Player Inventory, Slot 55-63, Slot IDs 55-63
	for (int x = 0; x < 9; ++x) {
		this.addSlotToContainer(new Slot(this.playerInv, x, 8 + x * 18, 142));
	}

}
// ##############################################################################################################################################

public void cambiarA23(boolean canbeTaken) {

	//reset all inventory arrays to Zero
	inventoryItemStacks = Lists.newArrayList();
	inventorySlots = Lists.newArrayList();
	crafters = Lists.newArrayList();


	// 0 - 27 chest slots
	// weapon slot
	this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 0, 26, 8, canbeTaken));

	// entity armor slots
	this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 4, 8, 8, canbeTaken));
	this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 3, 8, 26, canbeTaken));
	this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 2, 8, 44, canbeTaken));
	this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 1, 8, 62, canbeTaken));

	// Sub weapon slot
	this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 5, 26, 26, canbeTaken));

	// six pockets tactical vest

	this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 6, 53, 8, canbeTaken));
	this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 7, 71, 8, canbeTaken));

	this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 8, 53, 26, canbeTaken));
	this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 9, 71, 26, canbeTaken));

	this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 10, 53, 44, canbeTaken));
	this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 11, 71, 44, canbeTaken));

	// BackPack
	for (int x = 0; x < 4; ++x) {

		this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 12 + (x * 4), 98, 8 + (18 * x), canbeTaken));
		this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 13 + (x * 4), 116, 8 + (18 * x), canbeTaken));
		this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 14 + (x * 4), 134, 8 + (18 * x), canbeTaken));
		this.addSlotToContainer(new mercenarySlot(inventarioenlaArmadura, 15 + (x * 4), 152, 8 + (18 * x), canbeTaken));

	}

	// 28 - 54
	// Player Inventory, Slot 28 - 54, Slot IDs 28 - 54
	for (int y = 0; y < 3; ++y) {
		for (int x = 0; x < 9; ++x) {
			this.addSlotToContainer(new Slot(this.playerInv, x + y * 9 + 9, 8 + x * 18, 84 + y * 18));
		}
	}

	// 55 - 63
	// Player Inventory, Slot 55-63, Slot IDs 55-63
	for (int x = 0; x < 9; ++x) {
		this.addSlotToContainer(new Slot(this.playerInv, x, 8 + x * 18, 142));
	}

}

// ##############################################################################################################################################
@Override
public boolean canInteractWith(EntityPlayer playerIn) {
	return this.inventarioenlaArmadura.isUseableByPlayer(playerIn);
}

// ##############################################################################################################################################
@Override
public ItemStack transferStackInSlot(EntityPlayer playerIn, int fromSlot) {
	ItemStack previous = null;
	Slot slot = (Slot) this.inventorySlots.get(fromSlot);

	if (slot != null && slot.getHasStack()) {
		ItemStack current = slot.getStack();
		previous = current.copy();

		if (fromSlot < 28) {
			// From TE Inventory to Player Inventory
			if (!this.mergeItemStack(current, 28, 63, true))
				return null;
		} else {
			// From Player Inventory to TE Inventory
			if (!this.mergeItemStack(current, 0, 28, false))
				return null;
		}

		if (current.stackSize == 0)
			slot.putStack((ItemStack) null);
		else
			slot.onSlotChanged();

		if (current.stackSize == previous.stackSize)
			return null;
		slot.onPickupFromSlot(playerIn, current);
	}

	return previous;
}

// ##############################################################################################################################################

// 0 -4 armadura y arma

// 5 arma secundaria

// 6-11 pockets

// 12 -27 backpack

// 28 -54 inventory

// 55 -63 hotbar


// #################################################################################################################################################################################

}

 

 

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Version 1.19 - Forge 41.0.63 I want to create a wolf entity that I can ride, so far it seems to be working, but the problem is that when I get on the wolf, I can’t control it. I then discovered that the issue is that the server doesn’t detect that I’m riding the wolf, so I’m struggling with synchronization. However, it seems to not be working properly. As I understand it, the server receives the packet but doesn’t register it correctly. I’m a bit new to Java, and I’ll try to provide all the relevant code and prints *The comments and prints are translated by chatgpt since they were originally in Spanish* Thank you very much in advance No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. MountableWolfEntity package com.vals.valscraft.entity; import com.vals.valscraft.network.MountSyncPacket; import com.vals.valscraft.network.NetworkHandler; import net.minecraft.client.Minecraft; import net.minecraft.network.syncher.EntityDataAccessor; import net.minecraft.network.syncher.EntityDataSerializers; import net.minecraft.network.syncher.SynchedEntityData; import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.Mob; import net.minecraft.world.entity.ai.attributes.AttributeSupplier; import net.minecraft.world.entity.ai.attributes.Attributes; import net.minecraft.world.entity.animal.Wolf; import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.Entity; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; import net.minecraft.world.level.Level; import net.minecraft.world.phys.Vec3; import net.minecraftforge.event.TickEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.network.PacketDistributor; public class MountableWolfEntity extends Wolf { private boolean hasSaddle; private static final EntityDataAccessor<Byte> DATA_ID_FLAGS = SynchedEntityData.defineId(MountableWolfEntity.class, EntityDataSerializers.BYTE); public MountableWolfEntity(EntityType<? extends Wolf> type, Level level) { super(type, level); this.hasSaddle = false; } @Override protected void defineSynchedData() { super.defineSynchedData(); this.entityData.define(DATA_ID_FLAGS, (byte)0); } public static AttributeSupplier.Builder createAttributes() { return Wolf.createAttributes() .add(Attributes.MAX_HEALTH, 20.0) .add(Attributes.MOVEMENT_SPEED, 0.3); } @Override public InteractionResult mobInteract(Player player, InteractionHand hand) { ItemStack itemstack = player.getItemInHand(hand); if (itemstack.getItem() == Items.SADDLE && !this.hasSaddle()) { if (!player.isCreative()) { itemstack.shrink(1); } this.setSaddle(true); return InteractionResult.SUCCESS; } else if (!level.isClientSide && this.hasSaddle()) { player.startRiding(this); MountSyncPacket packet = new MountSyncPacket(true); // 'true' means the player is mounted NetworkHandler.CHANNEL.sendToServer(packet); // Ensure the server handles the packet return InteractionResult.SUCCESS; } return InteractionResult.PASS; } @Override public void travel(Vec3 travelVector) { if (this.isVehicle() && this.getControllingPassenger() instanceof Player) { System.out.println("The wolf has a passenger."); System.out.println("The passenger is a player."); Player player = (Player) this.getControllingPassenger(); // Ensure the player is the controller this.setYRot(player.getYRot()); this.yRotO = this.getYRot(); this.setXRot(player.getXRot() * 0.5F); this.setRot(this.getYRot(), this.getXRot()); this.yBodyRot = this.getYRot(); this.yHeadRot = this.yBodyRot; float forward = player.zza; float strafe = player.xxa; if (forward <= 0.0F) { forward *= 0.25F; } this.flyingSpeed = this.getSpeed() * 0.1F; this.setSpeed((float) this.getAttributeValue(Attributes.MOVEMENT_SPEED) * 1.5F); this.setDeltaMovement(new Vec3(strafe, travelVector.y, forward).scale(this.getSpeed())); this.calculateEntityAnimation(this, false); } else { // The wolf does not have a passenger or the passenger is not a player System.out.println("No player is mounted, or the passenger is not a player."); super.travel(travelVector); } } public boolean hasSaddle() { return this.hasSaddle; } public void setSaddle(boolean hasSaddle) { this.hasSaddle = hasSaddle; } @Override protected void dropEquipment() { super.dropEquipment(); if (this.hasSaddle()) { this.spawnAtLocation(Items.SADDLE); this.setSaddle(false); } } @SubscribeEvent public static void onServerTick(TickEvent.ServerTickEvent event) { if (event.phase == TickEvent.Phase.START) { MinecraftServer server = net.minecraftforge.server.ServerLifecycleHooks.getCurrentServer(); if (server != null) { for (ServerPlayer player : server.getPlayerList().getPlayers()) { if (player.isPassenger() && player.getVehicle() instanceof MountableWolfEntity) { MountableWolfEntity wolf = (MountableWolfEntity) player.getVehicle(); System.out.println("Tick: " + player.getName().getString() + " is correctly mounted on " + wolf); } } } } } private boolean lastMountedState = false; @Override public void tick() { super.tick(); if (!this.level.isClientSide) { // Only on the server boolean isMounted = this.isVehicle() && this.getControllingPassenger() instanceof Player; // Only print if the state changed if (isMounted != lastMountedState) { if (isMounted) { Player player = (Player) this.getControllingPassenger(); // Verify the passenger is a player System.out.println("Server: Player " + player.getName().getString() + " is now mounted."); } else { System.out.println("Server: The wolf no longer has a passenger."); } lastMountedState = isMounted; } } } @Override public void addPassenger(Entity passenger) { super.addPassenger(passenger); if (passenger instanceof Player) { Player player = (Player) passenger; if (!this.level.isClientSide && player instanceof ServerPlayer) { // Send the packet to the server to indicate the player is mounted NetworkHandler.CHANNEL.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) player), new MountSyncPacket(true)); } } } @Override public void removePassenger(Entity passenger) { super.removePassenger(passenger); if (passenger instanceof Player) { Player player = (Player) passenger; if (!this.level.isClientSide && player instanceof ServerPlayer) { // Send the packet to the server to indicate the player is no longer mounted NetworkHandler.CHANNEL.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) player), new MountSyncPacket(false)); } } } @Override public boolean isControlledByLocalInstance() { Entity entity = this.getControllingPassenger(); return entity instanceof Player; } @Override public void positionRider(Entity passenger) { if (this.hasPassenger(passenger)) { double xOffset = Math.cos(Math.toRadians(this.getYRot() + 90)) * 0.4; double zOffset = Math.sin(Math.toRadians(this.getYRot() + 90)) * 0.4; passenger.setPos(this.getX() + xOffset, this.getY() + this.getPassengersRidingOffset() + passenger.getMyRidingOffset(), this.getZ() + zOffset); } } } MountSyncPacket package com.vals.valscraft.network; import com.vals.valscraft.entity.MountableWolfEntity; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.player.Player; import net.minecraftforge.network.NetworkEvent; import java.util.function.Supplier; public class MountSyncPacket { private final boolean isMounted; public MountSyncPacket(boolean isMounted) { this.isMounted = isMounted; } public void encode(FriendlyByteBuf buffer) { buffer.writeBoolean(isMounted); } public static MountSyncPacket decode(FriendlyByteBuf buffer) { return new MountSyncPacket(buffer.readBoolean()); } public void handle(NetworkEvent.Context context) { context.enqueueWork(() -> { ServerPlayer player = context.getSender(); // Get the player from the context if (player != null) { // Verifies if the player has dismounted if (!isMounted) { Entity vehicle = player.getVehicle(); if (vehicle instanceof MountableWolfEntity wolf) { // Logic to remove the player as a passenger wolf.removePassenger(player); System.out.println("Server: Player " + player.getName().getString() + " is no longer mounted."); } } } }); context.setPacketHandled(true); // Marks the packet as handled } } networkHandler package com.vals.valscraft.network; import com.vals.valscraft.valscraft; import net.minecraft.resources.ResourceLocation; import net.minecraftforge.network.NetworkRegistry; import net.minecraftforge.network.simple.SimpleChannel; import net.minecraftforge.network.NetworkEvent; import java.util.function.Supplier; public class NetworkHandler { private static final String PROTOCOL_VERSION = "1"; public static final SimpleChannel CHANNEL = NetworkRegistry.newSimpleChannel( new ResourceLocation(valscraft.MODID, "main"), () -> PROTOCOL_VERSION, PROTOCOL_VERSION::equals, PROTOCOL_VERSION::equals ); public static void init() { int packetId = 0; // Register the mount synchronization packet CHANNEL.registerMessage( packetId++, MountSyncPacket.class, MountSyncPacket::encode, MountSyncPacket::decode, (msg, context) -> msg.handle(context.get()) // Get the context with context.get() ); } }  
    • Do you use features of inventory profiles next (ipnext) or is there a change without it?
    • Remove rubidium - you are already using embeddium, which is a fork of rubidium
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.