Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[1.7.10] Placing items from player inventory. (Duplication, non-accurate counts)

Featured Replies

Posted

So, I'm making an item that helps you place blocks at odd locations. You set the opposite side you want the block to place on. So you can build down under your feet. Or behind the block your standing on, so you can build a bridge out in front of you without backing up and shift clicking the whole time.

 

The problem I'm having is, the inventory count gets off, and I end up placing phantom blocks if you do actions too quickly. Here is the code that works, but doesn't work.

 

There really is nothing too special about it. I've used .tryPlaceItemIntoWorld, and iStack.getItem().onUseblahblahblah. Both decriment the count, but the client doesn't get the update. I just added in the player.inventoryContainer.detectAndSendChanges() but I haven't seen anyone else use that. I THINK it works now, but I feel there must be a better way, and I'm not sure I"m not going to continue to get duplication, and phantom blocks.

 

@Override
    public ItemStack onItemRightClick(ItemStack cStack, World world, EntityPlayer player) {
        if (!originSet)
            return cStack;

        player.swingItem();
        Block bBlock = null;
        int iLoc = -1;
        int mySide = -1;
        if (originSet) {
            if (!player.isSneaking()) {
                mySide = originSide;
            }
            else {
                mySide = flipSide(originSide);
            }

        }

        for (int i = 0; i < player.inventory.mainInventory.length; ++i) {
            if (player.inventory.mainInventory[i] != null) {
                bBlock = Block.getBlockFromItem(player.inventory.mainInventory[i].getItem());
                if (bBlock.getMaterial().blocksMovement()) {
                    iStack = player.inventory.getStackInSlot(i);
                    iLoc = i;
                    break;
                }
            }
        }
        if (iStack != null) {
            int myPosY = originY;
            int myPosX = originX;
            int myPosZ = originZ;
            Block block = world.getBlock(originX, originY, originZ);
            while (block.getMaterial().blocksMovement()) {
                if (mySide == 0) {
                    myPosY++;
                }
                if (mySide == 1) {
                    myPosY--;
                }
                if (mySide == 2) {
                    myPosZ++;
                }
                if (mySide == 3) {
                    myPosZ--;
                }
                if (mySide == 4) {
                    myPosX++;
                }
                if (mySide == 5) {
                    myPosX--;
                }
                block = world.getBlock(myPosX, myPosY, myPosZ);
            }
            if (myPosY > 0 && player.getPlayerCoordinates().getDistanceSquared(myPosX, myPosY, myPosZ) < 12) {
                if (iStack.stackSize <= 0) {
                    return cStack;
                }
                if (iStack.tryPlaceItemIntoWorld(player, world, myPosX, myPosY, myPosZ, mySide, originF1, originF2,
                        originF3)) {
                    player.inventoryContainer.detectAndSendChanges();
                    if (iStack.stackSize <= 0) {
                        // iStack = null;
                        player.inventory.mainInventory[iLoc] = null;
                        player.inventoryContainer.detectAndSendChanges();
                    }
                }
                return cStack;
            }
            return cStack;

        }
        return cStack;
    }

  • Author

Okay, I saw an example where they are checking if(!world.isRemote)

 

So let me ask two different questions that will hopefully will get an answer.

 

Should I fire placing the block only remote, or only client side?

 

Second question, I had onItemUse, and onItemRightClick calling the same building method. Sometimes it fires twice, sometimes it only fires once.

 

Is it supposed to fire like that? What determines if onItemUse will fire, or if onItemRightClick will fire?

 

--Code Wrecker

  • Author

Well, I wrote a simple test, and I think I figured out what was happening... ...  :-[

 

onItemUse will prevent onItemRightClick from being called, if onItemUse returns true. So if you return false for onItemUse, onItemRightClick fires off.

 

So all my double building problems, and phantom reports, and everything else... I solved. (I hope)

 

World.isRemote

Detect inventory

Handle block placement in one function, by having the other always return false, just set locations with onItemUse, and false to the other fires.

 

I still don't know if this is the 'right' way to do this, but after a lot of trial and error. I guess I should have just been more methodical.

 

--Code Wrecker

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.