Jump to content

[1.15.2] Question about custom recipe


NGYF

Recommended Posts

I've make a new block to craft something, but it has some problems.

When I take the result item, the number of material item doesn't appear to decrease. But if I take the material away, the number shows in right way.

For example, item A needs 3 item B and 2 item C to craft. When I take away A, number of B and C doesn't change.

When I close the block's gui and reopen it or take away B or C, the number decrease properly.

The code relevant to the problem is here:

public class WorkshopResultSlot extends Slot {

    private final IItemHandler field_75239_a;
    private final IInventory transferInv = new Inventory(3);
    private final PlayerEntity player;
    private int amountCrafted;
    public WorkshopRecipe workshopRecipe;
    public NonNullList<ItemStack> itemStacks = NonNullList.create();

    public WorkshopResultSlot(PlayerEntity player, IItemHandler craftingInventory, IInventory inventoryIn, int slotIndex, int xPosition, int yPosition) {
        super(inventoryIn, slotIndex, xPosition, yPosition);
        this.player = player;
        this.field_75239_a = craftingInventory;
        for(int i = 0; i < 3; i++){
            this.transferInv.setInventorySlotContents(i, this.field_75239_a.getStackInSlot(i));
        }
    }
  
  //other code...
  
    public ItemStack onTake(PlayerEntity thePlayer, ItemStack stack) {
        this.onCrafting(stack);
        net.minecraftforge.common.ForgeHooks.setCraftingPlayer(thePlayer);
        NonNullList<ItemStack> nonnulllist = thePlayer.world.getRecipeManager().getRecipeNonNull(IRecipeTypeArk.WORKSHOP, this.transferInv, thePlayer.world);
        net.minecraftforge.common.ForgeHooks.setCraftingPlayer(null);
        for (int i = 0; i < nonnulllist.size(); ++i) {
            ItemStack itemstack = this.field_75239_a.getStackInSlot(i);
            ItemStack itemstack1 = nonnulllist.get(i);
            int count = this.field_75239_a.getStackInSlot(i).getCount();
          //===============================
          //Belows is the problem code
          //===============================
            for(int j = 0; j < this.itemStacks.size(); ++j) {
                if (!itemstack.isEmpty()) {
                    if (this.itemStacks.get(j) != null) {
                        if (this.field_75239_a.getStackInSlot(i).getItem() == this.itemStacks.get(j).getItem()) {
                            this.field_75239_a.extractItem(i, this.itemStacks.get(j).getCount(), false);
                            count = this.field_75239_a.getStackInSlot(i).getCount();
                            itemstack = this.field_75239_a.getStackInSlot(i);
                            System.out.print(this.field_75239_a.getStackInSlot(i).getCount());
                          //The output is right, but in game the number doesn't change until I take the material item
                        }
                    }
                }
            }
          //====================================
                if (!itemstack1.isEmpty()) {
                    if (itemstack.isEmpty()) {
                        this.field_75239_a.insertItem(i, itemstack1, false);
                    } else if (ItemStack.areItemsEqual(itemstack, itemstack1) && ItemStack.areItemStackTagsEqual(itemstack, itemstack1)) {
                        itemstack1.grow(itemstack.getCount());
                        this.field_75239_a.insertItem(i, itemstack1, false);
                    } else if (!this.player.inventory.addItemStackToInventory(itemstack1)) {
                        this.player.dropItem(itemstack1, false);
                    }
                }
        }
        return stack;
    }
}

I mark the problem code. When I delete 'for' and replace 'j' with 'i', the item's number decrease when I take result item, and the decreased number is 'this.itemStacks.get(i).getCount()'. (But it isn't my purpose.)

 

I put the code to github, here is the link: https://github.com/NGYF/Arknights-Mod

The java files I think it is necessary:

All of https://github.com/NGYF/Arknights-Mod/tree/master/src/main/java/arknights/recipe

https://github.com/NGYF/Arknights-Mod/blob/master/src/main/java/arknights/container/WorkshopContainer.java

https://github.com/NGYF/Arknights-Mod/blob/master/src/main/java/arknights/tileentity/WorkshopEntity.java

 

Could you tell me how to deal with the problem? Thanks for your help!

Link to comment
Share on other sites

Because, fundamentally, that code runs on the client and the client controls bugger all. Only the server can modify that data.

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.

Link to comment
Share on other sites

On 3/15/2020 at 9:21 PM, Draco18s said:

Because, fundamentally, that code runs on the client and the client controls bugger all. Only the server can modify that data.

 

Actually, the data changed correctly, but the number showed wrong. When I take away the item it shows the right number immediately.

And I can delete some code to make it decrease the same number as 'this.itemStacks.get(i).getCount()' immediately, so I don't think the code runs on the client.

But I don't have any idea to solve it. What do you think about it? Thank you.

Link to comment
Share on other sites

Ah, I misunderstood the problem.

 

So basically what I said was inverted.

"The server controls that code and the client knows fuckall about the data until you pick the item up."

You either need to run similar code on the client or use packets.

Edited by Draco18s

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.

Link to comment
Share on other sites

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



×
×
  • Create New...

Important Information

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