Jump to content

Recommended Posts

Posted (edited)

Hey,

 

I have a TileEntity whose value increase each tick.

It drops an item and the value is set to 0 when I right click on the block.

 

I have a TileEntityRenderer who prints that value on my block.

 

The TER works fine except when I click on the block, the value keep increasing on the render.

But I know it's zero because I doesn't drop.

 

Thanks in advance

 

EDIT: I guess I have to send a packet, is there any vanilla packet for that ?

 

Edited by QuantumSoul
Posted

My Block:

    public ActionResultType onBlockActivated(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult rayTraceResult)
    {
        if (!world.isRemote)
        {
            final TileEntity tileEntity = world.getTileEntity(pos);
            if (tileEntity instanceof BitcoinMinerBlock)
                if (!((BitcoinMinerBlock) tileEntity).execute(player))
                    return ActionResultType.FAIL;
        }
        return ActionResultType.SUCCESS;
    }

 

BitcoinTileEntity#execute:

    public boolean execute(PlayerEntity player)
    {
        if (value >= 1.0D)
        {
            for (ItemStack stack : getBitcoinStacks(value))
                player.addItemStackToInventory(stack);

            value = 0.0D;
            NetworkInit.CHANNEL.send(PacketDistributor.PLAYER.with(() -> (ServerPlayerEntity) player), new BtcResetValuePacket(pos));

            return true;
        }

        return false;
    }

I checked and player is a ServerPlayerEntity

 

And here's my handle function in BtcResetValuePacket:

    public static void handle(BtcResetValuePacket packet, Supplier<NetworkEvent.Context> context)
    {
        context.get().enqueueWork(() ->
        {
            TileEntity te = context.get().getSender().world.getTileEntity(packet.pos);
            if (te instanceof BitcoinTileEntity)
                ((BitcoinTileEntity) te).resetValue();
        });

        context.get().setPacketHandled(true);
    }

I get a NPE at Context#getSender.

 

And BitcoinTileEntity#resetValue

    @OnlyIn(Dist.CLIENT)
    public void resetValue()
    {
        value = 0;
    }

 

Posted (edited)
8 minutes ago, diesieben07 said:
  1. Do not use @OnlyIn.
  2. Well, yes. This is a server-to-client packet. The sender is the server, not a specific player. The concept of "packet sender" only makes sense on the server, which is a) explained in this method's Javadoc and b) evident by the fact that it returns ServerPlayerEntity, which by definition can only exist on the server. 

1. Why ? I was not sure about it

 

2.

1 minute ago, QuantumSoul said:

evident by the fact that it returns ServerPlayerEntity, which by definition can only exist on the server. 

I thought it was logical since it's sent by server

 

3.

Problem solved using

        context.get().enqueueWork(() ->
        {
            TileEntity te = ((ClientPlayNetHandler)context.get().getNetworkManager().getNetHandler()).getWorld().getTileEntity(packet.pos);
            if (te instanceof BitcoinTileEntity)
                ((BitcoinTileEntity) te).resetValue();
        });

        context.get().setPacketHandled(true);

 

4. Forge docs talk about world.isBlockLoaded(BlockPos) should I add it here ? It's deprecated so idk

Edited by QuantumSoul
Posted
1 minute ago, diesieben07 said:

@OnlyIn is a marker annotation that Forge applies to the decompiled vanilla code, to signify methods that are present only in the client resp. server distribution.

The only reason to apply it to your own code is you override a vanilla method that already has the annotation.

Ok thanks

 

2 minutes ago, diesieben07 said:

It's sent by the server, to the client. In other words: It is received and handled on the client. Which has no idea about server players.

Makes sense now

 

2 minutes ago, diesieben07 said:

Which part of the docs are you talking about?

https://mcforge.readthedocs.io/en/1.14.x/networking/simpleimpl/

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.