Created
July 3, 2025 20:13
-
-
Save mworzala/f478fb90fb65803648e3015b5ce3d88b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public void handlePickBlock(@NotNull PlayerPickBlockEvent event) { | |
| var player = event.getPlayer(); | |
| if (player.getGameMode() != GameMode.CREATIVE) return; // Sanity | |
| var world = MapWorld.forPlayerOptional(player); | |
| if (world == null || !world.canEdit(player)) return; // Sanity | |
| // First try to get the block from the item registry | |
| var block = event.getBlock(); | |
| var itemStack = world.itemRegistry().getItemStack(block, event.isIncludeData()); | |
| // Otherwise create the item stack from the block | |
| if (itemStack == null) { | |
| var material = BlockUtil.getItem(block); | |
| if (material == null) return; // Sanity | |
| var builder = ItemStack.builder(material); | |
| if (event.isIncludeData() && !block.properties().isEmpty()) { | |
| builder.set(DataComponents.BLOCK_STATE, new ItemBlockState(block.properties())); | |
| builder.set(DataComponents.LORE, block.properties().entrySet().stream() | |
| .<Component>map(entry -> Component.text() | |
| .decoration(TextDecoration.ITALIC, false) | |
| .append(Component.text(entry.getKey(), NamedTextColor.GRAY)) | |
| .append(Component.text("=", NamedTextColor.DARK_GRAY)) | |
| .append(Component.text(entry.getValue(), NamedTextColor.WHITE)) | |
| .build()) | |
| .toList()); | |
| builder.set(DataComponents.ENCHANTMENT_GLINT_OVERRIDE, true); | |
| } | |
| itemStack = builder.build(); | |
| } | |
| // Still no item, nothing to do | |
| if (itemStack == null) return; | |
| var inventory = player.getInventory(); | |
| // If the item is already on the hotbar, swap to it | |
| for (int i = 0; i < 9; i++) { | |
| if (!inventory.getItemStack(i).isSimilar(itemStack)) | |
| continue; | |
| player.setHeldItemSlot((byte) i); | |
| break; | |
| } | |
| int targetSlot = player.getHeldSlot(); | |
| var targetItem = inventory.getItemStack(targetSlot); | |
| if (targetItem.isSimilar(itemStack)) return; | |
| if (!targetItem.isAir()) { | |
| // Try to find an empty slot | |
| for (int i = 0; i < 9; i++) { | |
| if (inventory.getItemStack(i).isAir()) { | |
| targetSlot = i; | |
| break; | |
| } | |
| } | |
| // If we didnt find an empty slot its fine we can keep the original and replace. | |
| } | |
| // If the item already exists in the inventory, swap to it | |
| int existingSlot = -1; | |
| for (int i = 9; i < inventory.getSize(); i++) { | |
| if (inventory.getItemStack(i).isSimilar(itemStack)) { | |
| existingSlot = i; | |
| break; | |
| } | |
| } | |
| if (existingSlot != -1) { | |
| var existingItem = inventory.getItemStack(existingSlot); | |
| inventory.setItemStack(existingSlot, itemStack); | |
| inventory.setItemStack(targetSlot, existingItem); | |
| } else { | |
| inventory.setItemStack(targetSlot, itemStack); | |
| if (targetSlot != player.getHeldSlot()) { | |
| player.setHeldItemSlot((byte) targetSlot); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment