|
| 1 | +/* |
| 2 | + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion |
| 3 | + * Copyright (C) 2016-2024 ViaVersion and contributors |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | +package com.viaversion.viaversion.bukkit.providers; |
| 19 | + |
| 20 | +import com.viaversion.viaversion.ViaVersionPlugin; |
| 21 | +import com.viaversion.viaversion.api.connection.UserConnection; |
| 22 | +import com.viaversion.viaversion.api.minecraft.BlockPosition; |
| 23 | +import com.viaversion.viaversion.bukkit.platform.PaperViaInjector; |
| 24 | +import com.viaversion.viaversion.protocols.v1_21_2to1_21_4.provider.PickItemProvider; |
| 25 | +import java.util.UUID; |
| 26 | +import org.bukkit.Bukkit; |
| 27 | +import org.bukkit.GameMode; |
| 28 | +import org.bukkit.Location; |
| 29 | +import org.bukkit.Material; |
| 30 | +import org.bukkit.block.Block; |
| 31 | +import org.bukkit.entity.Entity; |
| 32 | +import org.bukkit.entity.Player; |
| 33 | +import org.bukkit.inventory.ItemFactory; |
| 34 | +import org.bukkit.inventory.ItemStack; |
| 35 | +import org.bukkit.inventory.PlayerInventory; |
| 36 | +import org.bukkit.inventory.meta.BlockStateMeta; |
| 37 | +import org.checkerframework.checker.nullness.qual.Nullable; |
| 38 | + |
| 39 | +public final class BukkitPickItemProvider extends PickItemProvider { |
| 40 | + private static final boolean HAS_PLACEMENT_MATERIAL_METHOD = PaperViaInjector.hasMethod("org.bukkit.block.BlockData", "getPlacementMaterial"); |
| 41 | + private static final boolean HAS_SPAWN_EGG_METHOD = PaperViaInjector.hasMethod(ItemFactory.class, "getSpawnEgg"); |
| 42 | + private static final double BLOCK_RANGE = 4.5 + 1; |
| 43 | + private static final double BLOCK_RANGE_SQUARED = BLOCK_RANGE * BLOCK_RANGE; |
| 44 | + private static final double ENTITY_RANGE = 3 + 3; |
| 45 | + private final ViaVersionPlugin plugin; |
| 46 | + |
| 47 | + public BukkitPickItemProvider(final ViaVersionPlugin plugin) { |
| 48 | + this.plugin = plugin; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void pickItemFromBlock(final UserConnection connection, final BlockPosition blockPosition, final boolean includeData) { |
| 53 | + final UUID uuid = connection.getProtocolInfo().getUuid(); |
| 54 | + plugin.getServer().getScheduler().runTask(plugin, () -> { |
| 55 | + final Player player = plugin.getServer().getPlayer(uuid); |
| 56 | + if (player == null) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + final Location playerLocation = player.getLocation(); |
| 61 | + if (blockPosition.distanceFromCenterSquared(playerLocation.getX(), playerLocation.getY(), playerLocation.getZ()) > BLOCK_RANGE_SQUARED) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + final Block block = player.getWorld().getBlockAt(blockPosition.x(), blockPosition.y(), blockPosition.z()); |
| 66 | + if (block.getType() == Material.AIR) { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + final ItemStack item = blockToItem(block, includeData && player.getGameMode() == GameMode.CREATIVE); |
| 71 | + if (item != null) { |
| 72 | + pickItem(player, item); |
| 73 | + } |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + private @Nullable ItemStack blockToItem(final Block block, final boolean includeData) { |
| 78 | + if (HAS_PLACEMENT_MATERIAL_METHOD) { |
| 79 | + final ItemStack item = new ItemStack(block.getBlockData().getPlacementMaterial(), 1); |
| 80 | + if (includeData && item.getItemMeta() instanceof final BlockStateMeta blockStateMeta) { |
| 81 | + blockStateMeta.setBlockState(block.getState()); |
| 82 | + } |
| 83 | + return item; |
| 84 | + } else if (block.getType().isItem()) { |
| 85 | + return new ItemStack(block.getType(), 1); |
| 86 | + } |
| 87 | + return null; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public void pickItemFromEntity(final UserConnection connection, final int entityId, final boolean includeData) { |
| 92 | + if (!HAS_SPAWN_EGG_METHOD) { |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + final UUID uuid = connection.getProtocolInfo().getUuid(); |
| 97 | + plugin.getServer().getScheduler().runTask(plugin, () -> { |
| 98 | + final Player player = plugin.getServer().getPlayer(uuid); |
| 99 | + if (player == null) { |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + final Entity entity = player.getWorld().getNearbyEntities(player.getLocation(), ENTITY_RANGE, ENTITY_RANGE, ENTITY_RANGE).stream() |
| 104 | + .filter(e -> e.getEntityId() == entityId) |
| 105 | + .findAny() |
| 106 | + .orElse(null); |
| 107 | + if (entity == null) { |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + final Material spawnEggType = Bukkit.getItemFactory().getSpawnEgg(entity.getType()); |
| 112 | + if (spawnEggType != null) { |
| 113 | + pickItem(player, new ItemStack(spawnEggType, 1)); |
| 114 | + } |
| 115 | + }); |
| 116 | + } |
| 117 | + |
| 118 | + private void pickItem(final Player player, final ItemStack item) { |
| 119 | + // Find matching item |
| 120 | + final PlayerInventory inventory = player.getInventory(); |
| 121 | + final ItemStack[] contents = inventory.getStorageContents(); |
| 122 | + int sourceSlot = -1; |
| 123 | + for (int i = 0; i < contents.length; i++) { |
| 124 | + final ItemStack content = contents[i]; |
| 125 | + if (content == null || !content.isSimilar(item)) { |
| 126 | + continue; |
| 127 | + } |
| 128 | + |
| 129 | + sourceSlot = i; |
| 130 | + break; |
| 131 | + } |
| 132 | + |
| 133 | + if (sourceSlot != -1) { |
| 134 | + moveToHotbar(inventory, sourceSlot, contents); |
| 135 | + } else if (player.getGameMode() == GameMode.CREATIVE) { |
| 136 | + spawnItem(item, inventory, contents); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + private void spawnItem(final ItemStack item, final PlayerInventory inventory, final ItemStack[] contents) { |
| 141 | + final int targetSlot = findEmptyHotbarSlot(inventory, inventory.getHeldItemSlot()); |
| 142 | + inventory.setHeldItemSlot(targetSlot); |
| 143 | + final ItemStack heldItem = inventory.getItem(targetSlot); |
| 144 | + int emptySlot = targetSlot; |
| 145 | + if (heldItem != null && heldItem.getType() != Material.AIR) { |
| 146 | + // Swap to the first free slot in the inventory, else add it to the current hotbar slot |
| 147 | + for (int i = 0; i < contents.length; i++) { |
| 148 | + if (contents[i] == null || contents[i].getType() == Material.AIR) { |
| 149 | + emptySlot = i; |
| 150 | + break; |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + inventory.setItem(emptySlot, heldItem); |
| 156 | + inventory.setItemInMainHand(item); |
| 157 | + } |
| 158 | + |
| 159 | + private void moveToHotbar(final PlayerInventory inventory, final int sourceSlot, final ItemStack[] contents) { |
| 160 | + if (sourceSlot <= 9) { |
| 161 | + inventory.setHeldItemSlot(sourceSlot); |
| 162 | + return; |
| 163 | + } |
| 164 | + |
| 165 | + final int heldSlot = inventory.getHeldItemSlot(); |
| 166 | + final int targetSlot = findEmptyHotbarSlot(inventory, heldSlot); |
| 167 | + inventory.setHeldItemSlot(targetSlot); |
| 168 | + final ItemStack heldItem = inventory.getItem(targetSlot); |
| 169 | + inventory.setItemInMainHand(contents[sourceSlot]); |
| 170 | + inventory.setItem(sourceSlot, heldItem); |
| 171 | + } |
| 172 | + |
| 173 | + private int findEmptyHotbarSlot(final PlayerInventory inventory, final int heldSlot) { |
| 174 | + for (int i = 0; i < 9; i++) { |
| 175 | + final ItemStack item = inventory.getItem(i); |
| 176 | + if (item == null || item.getType() == Material.AIR) { |
| 177 | + return i; |
| 178 | + } |
| 179 | + } |
| 180 | + return heldSlot; |
| 181 | + } |
| 182 | +} |
0 commit comments