Jump to content

The item given to the player disappears


Recommended Posts

Hi there i have problem with my mod. I create this mod for learning forge. Mod adds new item to minecraft and in the overided method "OnBlockDestroyed" i give to player 2 diamonds but when i click on this item in inventory dissapear or when i drop this item.



package com.example.examplemod;

import net.minecraft.block.BlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.player.ClientPlayerEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.util.datafix.fixes.ItemStackUUID;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.ToolType;
import net.minecraftforge.fml.common.registry.GameRegistry;

public class Hammer extends Item {

    public Hammer() {
        super(new Item.Properties().group(ItemGroup.TOOLS).maxStackSize(4).addToolType(ToolType.AXE, 3).defaultMaxDamage(100).maxDamage(200));

    public boolean onBlockDestroyed(ItemStack itemStack, World world, BlockState blockState, BlockPos blockPos, LivingEntity livingEntity) {
        ClientPlayerEntity clientPlayerEntity = Minecraft.getInstance().player;

        clientPlayerEntity.sendChatMessage("You destroy: "+ blockState.getBlock().getRegistryName());
        clientPlayerEntity.inventory.addItemStackToInventory(new ItemStack(Items.DIAMOND, 2));

        return super.onBlockDestroyed(itemStack, world, blockState, blockPos, livingEntity);
Link to comment
Share on other sites

12 minutes ago, diesieben07 said:

You are reaching across logical sides. You need to use the living entity given to you and also check World#isRemote to ensure you only modify the inventory on the server.

Ok thanks, it works. I still have a question, how can I download "ServerPlayerEntity" in my case I convert "LivingEntity" to "ServerPlayerEntity" but is there any way to get "ServerPlayerEntity" from anywhere

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.

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.

  • Create New...

Important Information

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