9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-25 18:09:27 +00:00

Merge pull request #10 from Xiao-MoMi/main

上游更新
This commit is contained in:
jhqwqmc
2025-02-12 17:28:23 +08:00
committed by GitHub
4 changed files with 18 additions and 28 deletions

View File

@@ -14,6 +14,7 @@ import net.momirealms.craftengine.core.block.PushReaction;
import net.momirealms.craftengine.core.block.properties.Property;
import net.momirealms.craftengine.core.entity.player.InteractionHand;
import net.momirealms.craftengine.core.item.Item;
import net.momirealms.craftengine.core.item.ItemKeys;
import net.momirealms.craftengine.core.loot.parameter.LootParameters;
import net.momirealms.craftengine.core.plugin.config.ConfigManager;
import net.momirealms.craftengine.core.util.Key;
@@ -37,6 +38,7 @@ import org.bukkit.event.world.GenericGameEvent;
import org.bukkit.inventory.ItemStack;
import java.util.List;
import java.util.Optional;
// TODO interact waterlogged blocks
public class BlockEventListener implements Listener {
@@ -121,12 +123,7 @@ public class BlockEventListener implements Listener {
BukkitServerPlayer serverPlayer = plugin.adapt(player);
Item<ItemStack> itemInHand = serverPlayer.getItemInHand(InteractionHand.MAIN_HAND);
Key itemId;
if (itemInHand == null) {
itemId = Key.of("minecraft:air");
} else {
itemId = itemInHand.id();
}
Key itemId = Optional.ofNullable(itemInHand).map(Item::id).orElse(ItemKeys.AIR);
// do not drop if it's not the correct tool
if (!state.settings().isCorrectTool(itemId)) {
return;

View File

@@ -17,16 +17,13 @@ public class ItemUtils {
return item.getAmount() == 0;
}
// 1.21.4+
public static void setItem(PlayerInventory inventory, int slot, ItemStack itemStack) {
try {
Object nmsInventory = Reflections.method$CraftInventoryPlayer$getInventory
.invoke(inventory);
Object nmsInventory$items = Reflections.field$Inventory$items
.get(nmsInventory);
Object nmsItemStack = Reflections.method$CraftItemStack$asNMSMirror
.invoke(null, itemStack);
Reflections.method$NonNullList$set
.invoke(nmsInventory$items, slot, nmsItemStack);
Object nmsInventory = Reflections.method$CraftInventoryPlayer$getInventory.invoke(inventory);
Object nmsInventory$items = Reflections.field$Inventory$items.get(nmsInventory);
Object nmsItemStack = Reflections.method$CraftItemStack$asNMSMirror.invoke(null, itemStack);
Reflections.method$NonNullList$set.invoke(nmsInventory$items, slot, nmsItemStack);
} catch (InvocationTargetException | IllegalAccessException e) {
CraftEngine.instance().logger().warn("Failed to set item", e);
}

View File

@@ -41,7 +41,6 @@ public class Reflections {
}
public static final Unsafe UNSAFE;
public static final String INVENTORY$ITEMS = VersionHelper.isMojmap() ? "items" : "i";
static {
try {
@@ -3895,17 +3894,10 @@ public class Reflections {
BukkitReflectionUtils.assembleMCClass("world.entity.player.PlayerInventory")
)
);
public static final Method method$CraftInventoryPlayer$getInventory = requireNonNull(
ReflectionUtils.getMethod(
clazz$CraftInventoryPlayer,
new String[]{ "getInventory" }
)
);
public static final Field field$Inventory$items = requireNonNull(
ReflectionUtils.getDeclaredField(
clazz$Inventory,
INVENTORY$ITEMS
clazz$CraftInventoryPlayer, new String[]{ "getInventory" }
)
);
@@ -3915,12 +3907,15 @@ public class Reflections {
)
);
public static final Field field$Inventory$items = requireNonNull(
ReflectionUtils.getInstanceDeclaredField(
clazz$Inventory, clazz$NonNullList, 0
)
);
public static final Method method$NonNullList$set = requireNonNull(
ReflectionUtils.getMethod(
clazz$NonNullList,
Object.class,
int.class,
Object.class
clazz$NonNullList, Object.class, int.class, Object.class
)
);
}

View File

@@ -3,6 +3,7 @@ package net.momirealms.craftengine.core.item;
import net.momirealms.craftengine.core.util.Key;
public class ItemKeys {
public static final Key AIR = Key.of("minecraft:air");
public static final Key TRIDENT = Key.of("minecraft:trident");
public static final Key SHIELD = Key.of("minecraft:shield");
public static final Key BOW = Key.of("minecraft:bow");