Jump to content

[1.6.4][SOLVED]Ghost Items in Container


AceScottie

Recommended Posts

I have looked into this quite a lot and all the usual errors are not present in my code.

Whole creating a new container and shift-clicking items in or out of the container, the items will leave a ghost item behind. After looking around its mostly the slotObject.onSlotChanged(); area that needs to be added or edited however i already have that code as it should be (i think).

 

Container.class

 

 

 

package OreManager.container;

 

import OreManager.block.TileEntityOreModel;

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.ItemStack;

 

public class ChangeContainer extends Container {

 

@Override

public boolean canInteractWith(EntityPlayer entityplayer) {

return TileEntity.isUseableByPlayer(entityplayer);

}

 

protected TileEntityOreModel TileEntity;

 

 

public ChangeContainer(InventoryPlayer inventoryPlayer, TileEntityOreModel TE) {

TileEntity = TE;

 

 

for(int i = 0; i < 1; i++) {

for(int j = 0; j < 1; j++) {

addSlotToContainer(new Slot(TileEntity, j + i * 3, 25 + j * 18, 18 + i * 18));

 

}

}

for(int i = 0; i < 1; i++) {

for(int j = 0; j < 1; j++) {

addSlotToContainer(new Slot(TileEntity, j + i * 3, 76 + j * 18, 18 + i * 18));

 

}

}

bindPlayerInventory(inventoryPlayer);

 

}

 

private void bindPlayerInventory(InventoryPlayer inventoryPlayer) {

for(int i = 0; i < 3; i++) {

for(int j = 0; j < 9; j++) {

addSlotToContainer( new Slot(inventoryPlayer, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));

}

}

for(int i = 0; i < 9; i++) {

addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 142));

}

 

}

 

 

@Override

public ItemStack transferStackInSlot(EntityPlayer player, int par2) {

 

 

 

ItemStack itemstack = null;

Slot slotObject = (Slot)inventorySlots.get(par2);

Slot inSlot = (Slot) this.inventorySlots.get(0);

Slot outSlot = (Slot) this.inventorySlots.get(1);

if(slotObject != null && slotObject.getHasStack()) {

ItemStack stackInSlot = slotObject.getStack();

itemstack = stackInSlot.copy();

 

if (par2 < 2) {

if(!this.mergeItemStack(stackInSlot, 2, 35, true)) {

return null;

 

}

slotObject.onSlotChange(stackInSlot, itemstack);

}

else if(!this.mergeItemStack(stackInSlot, 0, 2, false)){

return null;

}

if (!this.mergeItemStack(stackInSlot, 31, 40, false)) {

return null;

}

else if (par2 >= 31 && par2 < 40

&& !this.mergeItemStack(stackInSlot, 4, 31, false)) {

return null;

}

else if (!this.mergeItemStack(stackInSlot, 4, 40, false)) {

return null;

}

 

if(stackInSlot.stackSize == 0) {

slotObject.putStack((ItemStack)null);

 

}

else {

slotObject.onSlotChanged();

}

 

if(stackInSlot.stackSize == itemstack.stackSize) {

return null;

}

slotObject.onPickupFromSlot(player, stackInSlot);

}

return itemstack;

 

}

}

 

 

 

any other information can be provided on request.

I am still learning java so please show examples if necessary. I don't like to copy paste code, i like to know what the code does and how it does it.

 

(This is an old mod i plan to update to 1.7.10 once i finished this section of it)

 

Link to comment
Share on other sites

I thought it was a pretty basic class so i haven't looked much into this one.

 

 

 

public class OreChangerGuiHandler implements IGuiHandler {

 

@Override

    public Object getServerGuiElement(int id, EntityPlayer player, World world,

                    int x, int y, int z) {

            TileEntity tileEntity = world.getBlockTileEntity(x, y, z);

            if(tileEntity instanceof TileEntityOreModel){

                    return new ChangeContainer(player.inventory, (TileEntityOreModel) tileEntity);

            }

            return null;

    }

    @Override

    public Object getClientGuiElement(int id, EntityPlayer player, World world,

                    int x, int y, int z) {

            TileEntity tileEntity = world.getBlockTileEntity(x, y, z);

            if(tileEntity instanceof TileEntityOreModel){

                    return new OreChangerGUI(player.inventory, (TileEntityOreModel) tileEntity);

            }

            return null;

 

    }

 

 

 

actually thinking about it, if this is because of a serverside - clientside issue it may have something to do with my proxy classes. Back when i first started modding having a clommon proxy and a client proxy really messed up the mod i was working on and just kept throwing out errors. removing the classes made the mod work so i paid hem no mind.

Just in case ill include my current client proxy although my common proxy is completly empty apart from the basic public class commonproxy line

 

 

 

public class ClientProxy extends CommonProxy {

 

 

public void registerOreManager() {

ClientRegistry.bindTileEntitySpecialRenderer(TileEntityOreModel.class, new RenderOreManager());

 

MinecraftForgeClient.registerItemRenderer(OreManager.blockOreChanger.blockID, new ItemRenderOreManager());

 

//LanguageRegistry.instance().addStringLocalization();

 

}

}

 

 

Link to comment
Share on other sites

No, your GuiHandler looks fine, and the proxies shouldn't have anything to do with it. Are you doing anything out of the ordinary in the Gui class? For reference, this is all you need for a functional container-based gui class.

 

How about your custom IInventory class, anything funky in there?

 

Otherwise, nothing really comes to mind. You do have a lot of unused and unnecessary code in your Container, stuff like:

// using loops for one iteration:
for(int i = 0; i < 1; i++) {
  for(int j = 0; j < 1; j++) {
  }
}

// unused variables:
Slot inSlot = (Slot) this.inventorySlots.get(0);
Slot outSlot = (Slot) this.inventorySlots.get(1);

Link to comment
Share on other sites

Yea there is a lot of unused or unneeded code  while I'm working on it. i generally run optimizations after it actually works so I know i can test other things if what I'm doing isn't working.

 

My GUI class

 

 

public class OreChangerGUI extends GuiContainer {

public static final ResourceLocation field_110421_t = new ResourceLocation("OreManager:textures/gui/OreChanger.png");

 

public OreChangerGUI(InventoryPlayer inventoryPlayer, TileEntityOreModel TileEntity) {

super(new ChangeContainer(inventoryPlayer, TileEntity));

 

}

 

@Override

protected void drawGuiContainerBackgroundLayer(float par1, int par2, int par3) {

GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);

this.mc.getTextureManager().bindTexture(field_110421_t);

int k = (this.width - this. xSize) / 2;

int y = (this.height - this.ySize) / 2;

this.drawTexturedModalRect(k, y, 0, 2, xSize, ySize);

 

}

 

//@Override

protected void drawGuiContainerForgroundLayout(int param1, int param2) {

fontRenderer.drawString("OreChangerimput", 8, 6, 4210752);

fontRenderer.drawString(StatCollector.translateToLocal("oremanager:/gui/OreChanger.png"), 8, ySize - 96 + 2, 4210752);

 

}

 

 

}

 

 

 

 

and i don't actually have a custom IInventory class. I was trying to get ISidedInventory working but this lead to some strange issues so i plan to take a look into that later. Right now my TileEntity Class just extends IInventory (net.minecraft)

Link to comment
Share on other sites

I dont Have any IInventory class of my own. am I'm importing the one from net.minecraft.inventory.IInventory

 

I dont know if this will help with solving this but i have uploaded all my source code to my dropbox account so it can be downloaded quickly and easily.

[edited out] Oremanager.zip [/edited out]

Link to comment
Share on other sites

Not gonna download a random file from the internet. Make a GitHub for your mod.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

Ok i added the !world.isRemote but still no change, not getting any errors and i have tried it with and without the other pieces of code in that function.

 

 

 

@Override

public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int metadata, float hitx, float hity, float hitz){

if (!world.isRemote){

FMLNetworkHandler.openGui(player, OreManager.instance, OreManager.guiIdOreManagerGUI, world, x, y, z);;

}

 

TileEntity tileEntity = world.getBlockTileEntity(x, y, z);

if(tileEntity == null || player.isSneaking()) {

return false;

}

player.openGui(OreManager.instance, 0, world, x, y, z);

return true;

}

 

 

Link to comment
Share on other sites

If your TileEntity implements IInventory, then that is your custom inventory class.

 

sorry i literally just realized what you meant by this (i think).

You mean my Tile Entity class IS my IInventory class ? i feel really stupid now :)

Anyway i posted a link to a github with it all on so you can see everything i have. Please ignore any junk code i got rid of the for(int i = 0; i < 1; i++) part already as that was making another part stupid to manage.

Link to comment
Share on other sites

Ok i added the !world.isRemote but still no change, not getting any errors and i have tried it with and without the other pieces of code in that function.

 

 

 

@Override

public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int metadata, float hitx, float hity, float hitz){

if (!world.isRemote){

FMLNetworkHandler.openGui(player, OreManager.instance, OreManager.guiIdOreManagerGUI, world, x, y, z);;

}

 

TileEntity tileEntity = world.getBlockTileEntity(x, y, z);

if(tileEntity == null || player.isSneaking()) {

return false;

}

player.openGui(OreManager.instance, 0, world, x, y, z);

return true;

}

 

 

 

I dont know what this code is supposed to do at all.

Just use this:

 

 
if(!world.isRemote) {
        TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
        if (tileEntity != null) player.openGui(OreManager.instance, 0, world, x, y, z);
}
        return true;

Link to comment
Share on other sites

 

I dont know what this code is supposed to do at all.

Just use this:

 

 
if(!world.isRemote) {
        TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
        if (tileEntity != null) player.openGui(OreManager.instance, 0, world, x, y, z);
}
        return true;

 

still same, no change at all.

 

Dont know if this helps at all but a few related bugs are:

Shift clicking will put items into  slot2 (if slot1 is full) even though its set not to receive items (cant place them there by normal methods).

Using a hopper with more than 1 item will destroy the stack and only leave 1 item(both on input and output).

 

 

Updated github with new code https://github.com/AceScottie/OreManager

Link to comment
Share on other sites

After a little bit of searching i can safely say the issue im having is with my Container class.

I have found a little code online that i will post below which dose fix the ghost item but, however creates a few of its own.

~Firstly it only accepts ores (this is what i wanted anyway but may have to be modified for others)

~Secondly I can only shift click into the GUI, i cannot shift click them out.

~Lastly The very top left player inventory slot cannot shift click into the GUI

 

There are some lines commented out of the code, these lines were causing me to crash after a little while.

 

 

 

  public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2)

    {

    ItemStack var3 = null;

    Slot var4 = (Slot)this.inventorySlots.get(par2);

    if (var4 != null && var4.getHasStack())

    {

    ItemStack var5 = var4.getStack();

    var3 = var5.copy();

    if (par2 == 2)

    {

  // if (!this.mergeItemStack(var5, 3, 39, true))

  //  {

//    return null;

  //  }

    var4.onSlotChange(var5, var3);

    }

    else if (par2 != 1 && par2 != 0)

    {

    if (FurnaceRecipes.smelting().getSmeltingResult(var5) != null)

    {

    if (!this.mergeItemStack(var5, 0, 1, false))

    {

    return null;

    }

    }

    else if (TileEntityOreModel.isItemOre(var5))

    {

    if (!this.mergeItemStack(var5, 1, 2, false))

    {

    return null;

    }

    }

    else if (par2 >= 3 && par2 < 30)

    {

  //  if (!this.mergeItemStack(var5, 30, 39, false))

  // {

//    return null;

  // }

    }

    else if (par2 >= 30 && par2 < 39 && !this.mergeItemStack(var5, 3, 30, false))

    {

    return null;

    }

    }

  //  else if (!this.mergeItemStack(var5, 3, 39, false))

  //  {

//    return null;

  //  }

    if (var5.stackSize == 0)

    {

    var4.putStack((ItemStack)null);

    }

    else

    {

    var4.onSlotChanged();

    }

    if (var5.stackSize == var3.stackSize)

    {

    return null;

    }

    var4.onPickupFromSlot(par1EntityPlayer, var5);

    }

    return var3;

    }

 

 

 

I am currently in the process of comparing my original code to this one but to make sure ill put my code here

 

 

@Override

public ItemStack transferStackInSlot(EntityPlayer player, int par2) {

 

 

 

ItemStack itemstack = null;

Slot slotObject = (Slot)inventorySlots.get(par2);

Slot inSlot = (Slot) this.inventorySlots.get(0);

Slot outSlot = (Slot) this.inventorySlots.get(1);

if(slotObject != null && slotObject.getHasStack()) {

ItemStack stackInSlot = slotObject.getStack();

itemstack = stackInSlot.copy();

 

if (par2 < 2) {

if(!this.mergeItemStack(stackInSlot, 2, 35, true)) {

return null;

 

}

slotObject.onSlotChange(stackInSlot, itemstack);

}

else if(!this.mergeItemStack(stackInSlot, 0, 2, false)){

return null;

}

if (!this.mergeItemStack(stackInSlot, 31, 40, false)) {

return null;

}

else if (par2 >= 31 && par2 < 40

&& !this.mergeItemStack(stackInSlot, 4, 31, false)) {

return null;

}

else if (!this.mergeItemStack(stackInSlot, 4, 40, false)) {

return null;

}

 

if(stackInSlot.stackSize == 0) {

slotObject.putStack((ItemStack)null);

 

}

else {

slotObject.onSlotChanged();

}

 

if(stackInSlot.stackSize == itemstack.stackSize) {

return null;

}

slotObject.onPickupFromSlot(player, stackInSlot);

}

return itemstack;

 

}

 

 

Link to comment
Share on other sites

[sOLVED]

 

The problem was i had too much code :)

I had some left over code after bug ficxing an Item duplication bug (i put 9 slots in a 2 slot GUI).

Fixed it now.

 

In my Container class i have removed the following lines

 

 

 

if (!this.mergeItemStack(stackInSlot, 31, 40, false)) {

return null;

}

else if (par2 >= 31 && par2 < 40

&& !this.mergeItemStack(stackInSlot, 4, 31, false)) {

return null;

}

else if (!this.mergeItemStack(stackInSlot, 4, 40, false)) {

return null;

}

else if (par2 >= 30 && par2 < 39 && !this.mergeItemStack(stackInSlot, 3, 30, false))

    {

    return null;

    }

 

 

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.


  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hi! If you have your forge installation file not in the system disc on PC (D, E etc.), drag it to disk C and try to start again. In my case it solved the problem.
    • KILAT77 : Waspada Situs Scam dengan Withdraw Yang Tidak Dibayar Di era digital saat ini, banyak orang yang mencari keberuntungan melalui situs perjudian online. Namun, dibalik gemerlap janji-janji manis kemenangan, ada bahaya yang mengintai. Salah satu situs yang patut diwaspadai adalah KILAT77. Situs ini mendapat reputasi buruk karena banyak laporan dari pengguna yang mengklaim bahwa mereka tidak bisa menarik dana kemenangan mereka. Dalam artikel ini, kita akan membahas mengapa ROTER88 dianggap sebagai situs scam dan bagaimana Anda bisa melindungi diri dari penipuan serupa. Pengalaman Pengguna: Penarikan Tidak Dibayar Beberapa pengguna telah melaporkan pengalaman buruk mereka dengan KILAT77. Mereka mengaku bahwa setelah memenangkan sejumlah uang dan mencoba menariknya, proses penarikan mereka ditolak tanpa alasan yang jelas. Bahkan, beberapa pengguna melaporkan bahwa akun mereka tiba-tiba diblokir setelah mencoba melakukan penarikan, sehingga mereka kehilangan akses ke dana mereka sama sekali.
    • It is a dupe mod issue Remove Rubidium - you are already using Embeddium which is a fork of Rubidium
    • I made a block entity in forge 1.20.1, I want to prevent hopper from taking input slot item, i tried to override the extractItem method, it prevented hopper from taking input slot item, but the player also unable to take/change the item in input slot unless the slot is empty. public class FluidSeparatorBlockEntity extends BlockEntity implements MenuProvider { private static final int INPUT_SLOT = 0; private final CustomItemHandler itemHandler = new CustomItemHandler(3){ @Override protected void onContentsChanged(int slot) { setChanged(); } @Override public boolean isItemValid(int slot, @NotNull ItemStack stack) { return slot == INPUT_SLOT; } @Override public @NotNull ItemStack extractItem(int slot, int amount, boolean simulate) { if (slot == INPUT_SLOT) { return ItemStack.EMPTY; } return super.extractItem(slot, amount, simulate); } }; private LazyOptional<IItemHandler> lazyItemHandler = LazyOptional.empty(); protected final ContainerData data; private int progress = 0; private int maxProgress = 78; public FluidSeparatorBlockEntity(BlockPos pPos, BlockState pBlockState) { super(ModBlockEntities.FLUID_SEPARATOR_BE.get(), pPos, pBlockState); this.data = new ContainerData() { @Override public int get(int pIndex) { return switch (pIndex) { case 0 -> FluidSeparatorBlockEntity.this.progress; case 1, 2 -> FluidSeparatorBlockEntity.this.maxProgress; default -> 0; }; } @Override public void set(int pIndex, int pValue) { switch (pIndex) { case 0 -> FluidSeparatorBlockEntity.this.progress = pValue; case 1, 2 -> FluidSeparatorBlockEntity.this.maxProgress = pValue; } } @Override public int getCount() { return 3; } }; } @Override public @NotNull <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) { if(cap == ForgeCapabilities.ITEM_HANDLER) { return lazyItemHandler.cast(); } return super.getCapability(cap, side); } @Override public void onLoad() { super.onLoad(); lazyItemHandler = LazyOptional.of(() -> itemHandler); } @Override public void invalidateCaps() { super.invalidateCaps(); lazyItemHandler.invalidate(); } public void drops() { SimpleContainer inventory = new SimpleContainer(itemHandler.getSlots()); for(int i = 0; i < itemHandler.getSlots(); i++) { inventory.setItem(i, itemHandler.getStackInSlot(i)); } Containers.dropContents(this.level, this.worldPosition, inventory); } @Override public Component getDisplayName() { return Component.translatable("block.chemmaster.fluid_separator"); } @Nullable @Override public AbstractContainerMenu createMenu(int pContainerId, Inventory pPlayerInventory, Player pPlayer) { return new FluidSeparatorMenu(pContainerId, pPlayerInventory, this, this.data); } @Override protected void saveAdditional(CompoundTag pTag) { pTag.put("inventory", itemHandler.serializeNBT()); pTag.putInt("fluid_separator.progress", progress); super.saveAdditional(pTag); } @Override public void load(CompoundTag pTag) { super.load(pTag); itemHandler.deserializeNBT(pTag.getCompound("inventory")); progress = pTag.getInt("fluid_separator.progress"); } public void tick(Level pLevel, BlockPos pPos, BlockState pState) { ItemStack inputStack = this.itemHandler.getStackInSlot(INPUT_SLOT); if (inputStack.getCount() < 2) { resetProgress(); return; } if(hasRecipe()) { increaseCraftingProgress(); setChanged(pLevel, pPos, pState); if(hasProgressFinished()) { craftItem(); resetProgress(); } } else { resetProgress(); } } private void resetProgress() { progress = 0; } private void craftItem() { Optional<FluidSeparatingRecipe> recipe = getCurrentRecipe(); if (recipe.isPresent()) { List<ItemStack> results = recipe.get().getOutputs(); ItemStack inputStack = this.itemHandler.getStackInSlot(INPUT_SLOT); if (inputStack.getCount() < 2) { // If there are not enough items, do not proceed with crafting return; } // Extract the input item from the input slot this.itemHandler.internalExtractItem(INPUT_SLOT, 2, false); // Loop through each result item and find suitable output slots for (ItemStack result : results) { int outputSlot = findSuitableOutputSlot(result); if (outputSlot != -1) { this.itemHandler.setStackInSlot(outputSlot, new ItemStack(result.getItem(), this.itemHandler.getStackInSlot(outputSlot).getCount() + result.getCount())); } else { // Handle the case where no suitable output slot is found // This can be logging an error, throwing an exception, or any other handling logic System.err.println("No suitable output slot found for item: " + result); } } } } private int findSuitableOutputSlot(ItemStack result) { // Implement logic to find a suitable output slot for the given result // Return the slot index or -1 if no suitable slot is found for (int i = 0; i < this.itemHandler.getSlots(); i++) { // Ensure we do not place the output item in the input slot if (i == INPUT_SLOT) { continue; } ItemStack stackInSlot = this.itemHandler.getStackInSlot(i); if (stackInSlot.isEmpty() || (stackInSlot.getItem() == result.getItem() && stackInSlot.getCount() + result.getCount() <= stackInSlot.getMaxStackSize())) { return i; } } return -1; } private boolean hasRecipe() { Optional<FluidSeparatingRecipe> recipe = getCurrentRecipe(); if (recipe.isEmpty()) { return false; } List<ItemStack> results = recipe.get().getOutputs(); for (ItemStack result : results) { if (!canInsertAmountIntoOutputSlot(result) || !canInsertItemIntoOutputSlot(result.getItem())) { return false; } } return true; } private Optional<FluidSeparatingRecipe> getCurrentRecipe(){ SimpleContainer inventory = new SimpleContainer(this.itemHandler.getSlots()); for (int i = 0; i < itemHandler.getSlots(); i++) { inventory.setItem(i, this.itemHandler.getStackInSlot(i)); } return this.level.getRecipeManager().getRecipeFor(FluidSeparatingRecipe.Type.INSTANCE, inventory, level); } private boolean canInsertAmountIntoOutputSlot(ItemStack result) { for (int i = 1; i < this.itemHandler.getSlots(); i++) { ItemStack stackInSlot = this.itemHandler.getStackInSlot(i); if (stackInSlot.isEmpty() || (stackInSlot.getItem() == result.getItem() && stackInSlot.getCount() + result.getCount() <= stackInSlot.getMaxStackSize())) { return true; } } return false; } private boolean canInsertItemIntoOutputSlot(Item item) { for (int i = 1; i < this.itemHandler.getSlots(); i++) { ItemStack stackInSlot = this.itemHandler.getStackInSlot(i); if (stackInSlot.isEmpty() || stackInSlot.getItem() == item) { return true; } } return false; } private boolean hasProgressFinished() { return progress >= maxProgress; } private void increaseCraftingProgress() { progress++; } }  
  • Topics

×
×
  • Create New...

Important Information

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