Jump to content

Sync a levelchunk capability to client on PlayerEvent.


MatNX

Recommended Posts

I have a LevelChunk Capability that allows me to paint blocks. It works by saving a capability storing colors and blockpos arrays to the level. It works well, and I have a rightclickevent that dyes the blocks. As there are mainly visual alterations, I need to sync it to the client using this code:

if (event.getEntity() instanceof ServerPlayer sPlayer) {
	sPlayer.connection.send(new ClientboundLevelChunkWithLightPacket(event.getLevel().getChunkAt(event.getPos()), event.getLevel().getLightEngine(), new BitSet(), new BitSet()));
}

This works for the righclickevent, but not upon loading the chunk. The data, mind you, is still there, anything that happens on the server, such as not allowing already painted blocks to be repainted, still work. Anyone got any idea what the issue could be? Here is my rightclick:

@SubscribeEvent
        public static void rightClickEvent(PlayerInteractEvent.RightClickBlock event) {
            ItemStack stack = event.getItemStack();
            if (!(stack.getItem() instanceof DyeItem) && !(stack.getItem() instanceof PotionItem && PotionUtils.getPotion(stack) == Potions.WATER)) {
                return;
            }
            ColorCapability colorCapability = event.getLevel().getChunkAt(event.getPos()).getCapability(ColorCapabilityProvider.COLOR_CAPABILITY).orElse(null);
            if(stack.getItem() instanceof DyeItem item) {
                int colorint = item.getDyeColor().getTextColor();
                int index = colorCapability.containsPos(event.getPos());
                if (index != -1) {
                    if (colorCapability.getColor(index) == colorint) {
                        return;
                    }
                        colorCapability.setColor(colorint, index);
                } else {
                    colorCapability.addColor(colorint);
                    colorCapability.addPos(event.getPos());
                }
                event.getLevel().playSound((Player) null, event.getPos(), SoundEvents.DYE_USE, SoundSource.BLOCKS, 1.0F, 1.0F);
                event.getEntity().awardStat(Stats.ITEM_USED.get(stack.getItem()));
                if (!event.getEntity().getAbilities().instabuild) {
                    stack.shrink(1);
                }
            }
            else if (stack.getItem() instanceof PotionItem potion && PotionUtils.getPotion(stack) == Potions.WATER) {
                if (colorCapability.containsPos(event.getPos()) != -1) {

                    event.getLevel().playSound((Player) null, event.getPos(), SoundEvents.GENERIC_SPLASH, SoundSource.BLOCKS, 1.0F, 1.0F);
                    event.getEntity().setItemInHand(event.getHand(), ItemUtils.createFilledResult(stack, event.getEntity(), new ItemStack(Items.GLASS_BOTTLE)));
                    event.getEntity().awardStat(Stats.ITEM_USED.get(stack.getItem()));
                    if (!event.getLevel().isClientSide) {
                        ServerLevel $$6 = (ServerLevel) event.getLevel();

                        for (int $$7 = 0; $$7 < 5; ++$$7) {
                            $$6.sendParticles(ParticleTypes.SPLASH, (double) event.getPos().getX() + event.getLevel().random.nextDouble(), (double) (event.getPos().getY() + 1), (double) event.getPos().getZ() + event.getLevel().random.nextDouble(), 1, 0.0, 0.0, 0.0, 1.0);
                        }
                    }

                    event.getLevel().playSound((Player) null, event.getPos(), SoundEvents.BOTTLE_EMPTY, SoundSource.BLOCKS, 1.0F, 1.0F);
                    event.getLevel().gameEvent((Entity) null, GameEvent.FLUID_PLACE, event.getPos());

                    colorCapability.removeColor(colorCapability.containsPos(event.getPos()));
                    colorCapability.removePos(colorCapability.containsPos(event.getPos()));
                }
            }
            if (event.getEntity() instanceof ServerPlayer sPlayer) {
                CriteriaTriggers.CONSUME_ITEM.trigger(sPlayer, stack);
                sPlayer.connection.send(new ClientboundLevelChunkWithLightPacket(event.getLevel().getChunkAt(event.getPos()), event.getLevel().getLightEngine(), new BitSet(), new BitSet()));
            }
            event.getLevel().getChunkAt(event.getPos()).setUnsaved(true);
        }

 

Edited by MatNX
I changed the code somewhat, and since nobody has replied so far, this gives them a bit of an updated look at the problem.
Link to comment
Share on other sites

  • MatNX changed the title to Sync a levelchunk capability to client on PlayerEvent.

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.



×
×
  • Create New...

Important Information

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