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.

Featured Replies

Posted

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.

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

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.