mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-30 12:29:15 +00:00
Merge branch 'Xiao-MoMi:dev' into dev
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
package net.momirealms.craftengine.bukkit.api.event;
|
||||
|
||||
import net.momirealms.craftengine.bukkit.entity.furniture.BukkitFurniture;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.Cancellable;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.bukkit.event.player.PlayerEvent;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class FurnitureAttemptBreakEvent extends PlayerEvent implements Cancellable {
|
||||
private static final HandlerList HANDLER_LIST = new HandlerList();
|
||||
private boolean cancelled;
|
||||
private final BukkitFurniture furniture;
|
||||
|
||||
public FurnitureAttemptBreakEvent(@NotNull Player player,
|
||||
@NotNull BukkitFurniture furniture) {
|
||||
super(player);
|
||||
this.furniture = furniture;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Player player() {
|
||||
return getPlayer();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public BukkitFurniture furniture() {
|
||||
return this.furniture;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Location location() {
|
||||
return this.furniture.location();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static HandlerList getHandlerList() {
|
||||
return HANDLER_LIST;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public HandlerList getHandlers() {
|
||||
return getHandlerList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCancelled() {
|
||||
return this.cancelled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCancelled(boolean cancel) {
|
||||
this.cancelled = cancel;
|
||||
}
|
||||
}
|
||||
@@ -347,7 +347,7 @@ public class BlockEventListener implements Listener {
|
||||
Block block = blocks.get(i);
|
||||
Location location = block.getLocation();
|
||||
BlockPos blockPos = new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ());
|
||||
ImmutableBlockState state = manager.getImmutableBlockState(BlockStateUtils.blockDataToId(block.getBlockData()));
|
||||
ImmutableBlockState state = this.manager.getImmutableBlockState(BlockStateUtils.blockDataToId(block.getBlockData()));
|
||||
if (state != null && !state.isEmpty()) {
|
||||
WorldPosition position = new WorldPosition(world, Vec3d.atCenterOf(blockPos));
|
||||
ContextHolder.Builder builder = ContextHolder.builder()
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package net.momirealms.craftengine.bukkit.item;
|
||||
|
||||
import net.momirealms.craftengine.bukkit.nms.FastNMS;
|
||||
import net.momirealms.craftengine.bukkit.plugin.BukkitCraftEngine;
|
||||
import net.momirealms.craftengine.bukkit.util.KeyUtils;
|
||||
import net.momirealms.craftengine.core.item.*;
|
||||
import net.momirealms.craftengine.core.item.behavior.ItemBehavior;
|
||||
import net.momirealms.craftengine.core.item.modifier.ItemDataModifier;
|
||||
@@ -10,7 +10,6 @@ import net.momirealms.craftengine.core.plugin.context.event.EventTrigger;
|
||||
import net.momirealms.craftengine.core.plugin.context.function.Function;
|
||||
import net.momirealms.craftengine.core.registry.Holder;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -19,22 +18,23 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class BukkitCustomItem extends AbstractCustomItem<ItemStack> {
|
||||
private final Material material;
|
||||
private final Object item;
|
||||
private final Object clientItem;
|
||||
|
||||
public BukkitCustomItem(Holder<Key> id, Material material, Key materialKey, Key clientBoundMaterialKey,
|
||||
public BukkitCustomItem(Holder<Key> id, Object item, Object clientItem, Key materialKey, Key clientBoundMaterialKey,
|
||||
List<ItemBehavior> behaviors,
|
||||
List<ItemDataModifier<ItemStack>> modifiers, List<ItemDataModifier<ItemStack>> clientBoundModifiers,
|
||||
ItemSettings settings,
|
||||
Map<EventTrigger, List<Function<PlayerOptionalContext>>> events) {
|
||||
super(id, materialKey, clientBoundMaterialKey, behaviors, modifiers, clientBoundModifiers, settings, events);
|
||||
this.material = material;
|
||||
this.item = item;
|
||||
this.clientItem = clientItem;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack buildItemStack(ItemBuildContext context, int count) {
|
||||
ItemStack item = new ItemStack(this.material);
|
||||
ItemStack item = FastNMS.INSTANCE.method$CraftItemStack$asCraftMirror(FastNMS.INSTANCE.constructor$ItemStack(this.item, count));
|
||||
Item<ItemStack> wrapped = BukkitCraftEngine.instance().itemManager().wrap(item);
|
||||
wrapped.count(count);
|
||||
for (ItemDataModifier<ItemStack> modifier : this.modifiers) {
|
||||
modifier.apply(wrapped, context);
|
||||
}
|
||||
@@ -43,7 +43,7 @@ public class BukkitCustomItem extends AbstractCustomItem<ItemStack> {
|
||||
|
||||
@Override
|
||||
public Item<ItemStack> buildItem(ItemBuildContext context) {
|
||||
ItemStack item = new ItemStack(this.material);
|
||||
ItemStack item = FastNMS.INSTANCE.method$CraftItemStack$asCraftMirror(FastNMS.INSTANCE.constructor$ItemStack(this.item, 1));
|
||||
Item<ItemStack> wrapped = BukkitCraftEngine.instance().itemManager().wrap(item);
|
||||
for (ItemDataModifier<ItemStack> modifier : dataModifiers()) {
|
||||
modifier.apply(wrapped, context);
|
||||
@@ -51,24 +51,33 @@ public class BukkitCustomItem extends AbstractCustomItem<ItemStack> {
|
||||
return BukkitCraftEngine.instance().itemManager().wrap(wrapped.load());
|
||||
}
|
||||
|
||||
public static Builder<ItemStack> builder(Material material) {
|
||||
return new BuilderImpl(material);
|
||||
public Object clientItem() {
|
||||
return clientItem;
|
||||
}
|
||||
|
||||
public Object item() {
|
||||
return item;
|
||||
}
|
||||
|
||||
public static Builder<ItemStack> builder(Object item, Object clientBoundItem) {
|
||||
return new BuilderImpl(item, clientBoundItem);
|
||||
}
|
||||
|
||||
public static class BuilderImpl implements Builder<ItemStack> {
|
||||
private Holder<Key> id;
|
||||
private final Key materialKey;
|
||||
private final Material material;
|
||||
private Key clientBoundMaterialKey;
|
||||
private Key itemKey;
|
||||
private final Object item;
|
||||
private Key clientBoundItemKey;
|
||||
private final Object clientBoundItem;
|
||||
private final Map<EventTrigger, List<Function<PlayerOptionalContext>>> events = new EnumMap<>(EventTrigger.class);
|
||||
private final List<ItemBehavior> behaviors = new ArrayList<>(4);
|
||||
private final List<ItemDataModifier<ItemStack>> modifiers = new ArrayList<>(4);
|
||||
private final List<ItemDataModifier<ItemStack>> clientBoundModifiers = new ArrayList<>(4);
|
||||
private ItemSettings settings;
|
||||
|
||||
public BuilderImpl(Material material) {
|
||||
this.material = material;
|
||||
this.materialKey = KeyUtils.namespacedKey2Key(material.getKey());
|
||||
public BuilderImpl(Object item, Object clientBoundItem) {
|
||||
this.item = item;
|
||||
this.clientBoundItem = clientBoundItem;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -78,8 +87,14 @@ public class BukkitCustomItem extends AbstractCustomItem<ItemStack> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder<ItemStack> clientBoundMaterial(Key clientBoundMaterialKey) {
|
||||
this.clientBoundMaterialKey = clientBoundMaterialKey;
|
||||
public Builder<ItemStack> clientBoundMaterial(Key clientBoundMaterial) {
|
||||
this.clientBoundItemKey = clientBoundMaterial;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder<ItemStack> material(Key material) {
|
||||
this.itemKey = material;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -134,7 +149,7 @@ public class BukkitCustomItem extends AbstractCustomItem<ItemStack> {
|
||||
@Override
|
||||
public CustomItem<ItemStack> build() {
|
||||
this.modifiers.addAll(this.settings.modifiers());
|
||||
return new BukkitCustomItem(this.id, this.material, this.materialKey, this.clientBoundMaterialKey, List.copyOf(this.behaviors),
|
||||
return new BukkitCustomItem(this.id, this.item, this.clientBoundItem, this.itemKey, this.clientBoundItemKey, List.copyOf(this.behaviors),
|
||||
List.copyOf(this.modifiers), List.copyOf(this.clientBoundModifiers), this.settings, this.events);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import net.momirealms.craftengine.bukkit.plugin.BukkitCraftEngine;
|
||||
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.CoreReflections;
|
||||
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.MBuiltInRegistries;
|
||||
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.MRegistries;
|
||||
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.MRegistryOps;
|
||||
import net.momirealms.craftengine.bukkit.util.ItemUtils;
|
||||
import net.momirealms.craftengine.bukkit.util.KeyUtils;
|
||||
import net.momirealms.craftengine.core.entity.player.Player;
|
||||
@@ -201,11 +202,18 @@ public class BukkitItemManager extends AbstractItemManager<ItemStack> {
|
||||
|
||||
@Override
|
||||
protected CustomItem.Builder<ItemStack> createPlatformItemBuilder(Holder<Key> id, Key materialId, Key clientBoundMaterialId) {
|
||||
Material material = ResourceConfigUtils.requireNonNullOrThrow(Registry.MATERIAL.get(KeyUtils.toNamespacedKey(materialId)), () -> new LocalizedResourceConfigException("warning.config.item.invalid_material", materialId.toString()));
|
||||
if (!clientBoundMaterialId.equals(materialId)) {
|
||||
ResourceConfigUtils.requireNonNullOrThrow(Registry.MATERIAL.get(KeyUtils.toNamespacedKey(clientBoundMaterialId)), () -> new LocalizedResourceConfigException("warning.config.item.invalid_material", clientBoundMaterialId.toString()));
|
||||
Object item = FastNMS.INSTANCE.method$Registry$getValue(MBuiltInRegistries.ITEM, KeyUtils.toResourceLocation(materialId));
|
||||
Object clientBoundItem = materialId == clientBoundMaterialId ? item : FastNMS.INSTANCE.method$Registry$getValue(MBuiltInRegistries.ITEM, KeyUtils.toResourceLocation(clientBoundMaterialId));
|
||||
if (item == null) {
|
||||
throw new LocalizedResourceConfigException("warning.config.item.invalid_material", materialId.toString());
|
||||
}
|
||||
return BukkitCustomItem.builder(material).id(id).clientBoundMaterial(clientBoundMaterialId);
|
||||
if (clientBoundItem == null) {
|
||||
throw new LocalizedResourceConfigException("warning.config.item.invalid_material", clientBoundMaterialId.toString());
|
||||
}
|
||||
return BukkitCustomItem.builder(item, clientBoundItem)
|
||||
.id(id)
|
||||
.material(materialId)
|
||||
.clientBoundMaterial(clientBoundMaterialId);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package net.momirealms.craftengine.bukkit.item;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.momirealms.craftengine.bukkit.nms.FastNMS;
|
||||
import net.momirealms.craftengine.core.entity.player.Player;
|
||||
import net.momirealms.craftengine.core.item.CustomItem;
|
||||
import net.momirealms.craftengine.core.item.Item;
|
||||
@@ -25,6 +26,7 @@ import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
@SuppressWarnings("DuplicatedCode")
|
||||
public class LegacyNetworkItemHandler implements NetworkItemHandler<ItemStack> {
|
||||
|
||||
@Override
|
||||
@@ -32,9 +34,9 @@ public class LegacyNetworkItemHandler implements NetworkItemHandler<ItemStack> {
|
||||
Optional<CustomItem<ItemStack>> optionalCustomItem = wrapped.getCustomItem();
|
||||
boolean hasDifferentMaterial = false;
|
||||
if (optionalCustomItem.isPresent()) {
|
||||
CustomItem<ItemStack> customItem = optionalCustomItem.get();
|
||||
if (!customItem.material().equals(wrapped.vanillaId())) {
|
||||
wrapped = wrapped.transmuteCopy(customItem.material());
|
||||
BukkitCustomItem customItem = (BukkitCustomItem) optionalCustomItem.get();
|
||||
if (customItem.item() != FastNMS.INSTANCE.method$ItemStack$getItem(wrapped.getLiteralObject())) {
|
||||
wrapped = wrapped.unsafeTransmuteCopy(customItem.item(), wrapped.count());
|
||||
hasDifferentMaterial = true;
|
||||
}
|
||||
}
|
||||
@@ -61,10 +63,11 @@ public class LegacyNetworkItemHandler implements NetworkItemHandler<ItemStack> {
|
||||
if (!Config.interceptItem()) return Optional.empty();
|
||||
return new OtherItem(wrapped, false).process();
|
||||
} else {
|
||||
CustomItem<ItemStack> customItem = optionalCustomItem.get();
|
||||
boolean hasDifferentMaterial = !wrapped.vanillaId().equals(customItem.clientBoundMaterial());
|
||||
BukkitCustomItem customItem = (BukkitCustomItem) optionalCustomItem.get();
|
||||
Object serverItem = FastNMS.INSTANCE.method$ItemStack$getItem(wrapped.getLiteralObject());
|
||||
boolean hasDifferentMaterial = serverItem == customItem.item() && serverItem != customItem.clientItem();
|
||||
if (hasDifferentMaterial) {
|
||||
wrapped = wrapped.transmuteCopy(customItem.clientBoundMaterial());
|
||||
wrapped = wrapped.unsafeTransmuteCopy(customItem.clientItem(), wrapped.count());
|
||||
}
|
||||
if (!customItem.hasClientBoundDataModifier()) {
|
||||
if (!Config.interceptItem() && !hasDifferentMaterial) return Optional.empty();
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package net.momirealms.craftengine.bukkit.item;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.momirealms.craftengine.bukkit.nms.FastNMS;
|
||||
import net.momirealms.craftengine.core.entity.player.Player;
|
||||
import net.momirealms.craftengine.core.item.*;
|
||||
import net.momirealms.craftengine.core.item.modifier.ArgumentModifier;
|
||||
@@ -33,9 +34,9 @@ public final class ModernNetworkItemHandler implements NetworkItemHandler<ItemSt
|
||||
Optional<CustomItem<ItemStack>> optionalCustomItem = wrapped.getCustomItem();
|
||||
boolean hasDifferentMaterial = false;
|
||||
if (optionalCustomItem.isPresent()) {
|
||||
CustomItem<ItemStack> customItem = optionalCustomItem.get();
|
||||
if (!customItem.material().equals(wrapped.vanillaId())) {
|
||||
wrapped = wrapped.transmuteCopy(customItem.material());
|
||||
BukkitCustomItem customItem = (BukkitCustomItem) optionalCustomItem.get();
|
||||
if (customItem.item() != FastNMS.INSTANCE.method$ItemStack$getItem(wrapped.getLiteralObject())) {
|
||||
wrapped = wrapped.unsafeTransmuteCopy(customItem.item(), wrapped.count());
|
||||
hasDifferentMaterial = true;
|
||||
}
|
||||
}
|
||||
@@ -64,10 +65,11 @@ public final class ModernNetworkItemHandler implements NetworkItemHandler<ItemSt
|
||||
if (!Config.interceptItem()) return Optional.empty();
|
||||
return new OtherItem(wrapped, false).process();
|
||||
} else {
|
||||
CustomItem<ItemStack> customItem = optionalCustomItem.get();
|
||||
boolean hasDifferentMaterial = !wrapped.vanillaId().equals(customItem.clientBoundMaterial());
|
||||
BukkitCustomItem customItem = (BukkitCustomItem) optionalCustomItem.get();
|
||||
Object serverItem = FastNMS.INSTANCE.method$ItemStack$getItem(wrapped.getLiteralObject());
|
||||
boolean hasDifferentMaterial = serverItem == customItem.item() && serverItem != customItem.clientItem();
|
||||
if (hasDifferentMaterial) {
|
||||
wrapped = wrapped.transmuteCopy(customItem.clientBoundMaterial());
|
||||
wrapped = wrapped.unsafeTransmuteCopy(customItem.clientItem(), wrapped.count());
|
||||
}
|
||||
if (!customItem.hasClientBoundDataModifier()) {
|
||||
if (!Config.interceptItem() && !hasDifferentMaterial) return Optional.empty();
|
||||
|
||||
@@ -526,9 +526,16 @@ public class ComponentItemFactory1_20_5 extends BukkitItemFactory<ComponentItemW
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ComponentItemWrapper transmuteCopy(ComponentItemWrapper item1, Key item, int amount) {
|
||||
Object itemStack1 = item1.getLiteralObject();
|
||||
Object itemStack2 = FastNMS.INSTANCE.method$ItemStack$transmuteCopy(itemStack1, FastNMS.INSTANCE.method$Registry$getValue(MBuiltInRegistries.ITEM, KeyUtils.toResourceLocation(item)), amount);
|
||||
protected ComponentItemWrapper transmuteCopy(ComponentItemWrapper item, Key newItem, int amount) {
|
||||
Object itemStack1 = item.getLiteralObject();
|
||||
Object itemStack2 = FastNMS.INSTANCE.method$ItemStack$transmuteCopy(itemStack1, FastNMS.INSTANCE.method$Registry$getValue(MBuiltInRegistries.ITEM, KeyUtils.toResourceLocation(newItem)), amount);
|
||||
return new ComponentItemWrapper(FastNMS.INSTANCE.method$CraftItemStack$asCraftMirror(itemStack2));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ComponentItemWrapper unsafeTransmuteCopy(ComponentItemWrapper item, Object newItem, int amount) {
|
||||
Object itemStack1 = item.getLiteralObject();
|
||||
Object itemStack2 = FastNMS.INSTANCE.method$ItemStack$transmuteCopy(itemStack1, newItem, amount);
|
||||
return new ComponentItemWrapper(FastNMS.INSTANCE.method$CraftItemStack$asCraftMirror(itemStack2));
|
||||
}
|
||||
}
|
||||
@@ -308,7 +308,7 @@ public class UniversalItemFactory extends BukkitItemFactory<LegacyItemWrapper> {
|
||||
@Override
|
||||
protected LegacyItemWrapper mergeCopy(LegacyItemWrapper item1, LegacyItemWrapper item2) {
|
||||
Object itemStack = ItemObject.copy(item2.getLiteralObject());
|
||||
ItemObject.setCustomDataTag(itemStack, TagCompound.clone(FastNMS.INSTANCE.field$ItemStack$getOrCreateTag(item1.getLiteralObject())));
|
||||
FastNMS.INSTANCE.method$ItemStack$setTag(itemStack, TagCompound.clone(FastNMS.INSTANCE.field$ItemStack$getOrCreateTag(item1.getLiteralObject())));
|
||||
// one more step than vanilla
|
||||
TagCompound.merge(FastNMS.INSTANCE.field$ItemStack$getOrCreateTag(itemStack), FastNMS.INSTANCE.field$ItemStack$getOrCreateTag(item2.getLiteralObject()), true, true);
|
||||
return new LegacyItemWrapper(new RtagItem(FastNMS.INSTANCE.method$CraftItemStack$asCraftMirror(itemStack)));
|
||||
@@ -318,7 +318,7 @@ public class UniversalItemFactory extends BukkitItemFactory<LegacyItemWrapper> {
|
||||
protected void merge(LegacyItemWrapper item1, LegacyItemWrapper item2) {
|
||||
// load previous changes on nms items
|
||||
item1.load();
|
||||
TagCompound.merge(ItemObject.getCustomDataTag(item1.getLiteralObject()), ItemObject.getCustomDataTag(item2.getLiteralObject()), true, true);
|
||||
TagCompound.merge(FastNMS.INSTANCE.field$ItemStack$getOrCreateTag(item1.getLiteralObject()), FastNMS.INSTANCE.field$ItemStack$getOrCreateTag(item2.getLiteralObject()), true, true);
|
||||
// update wrapped item
|
||||
item1.update();
|
||||
}
|
||||
@@ -326,7 +326,14 @@ public class UniversalItemFactory extends BukkitItemFactory<LegacyItemWrapper> {
|
||||
@Override
|
||||
protected LegacyItemWrapper transmuteCopy(LegacyItemWrapper item, Key newItem, int amount) {
|
||||
Object newItemStack = FastNMS.INSTANCE.constructor$ItemStack(FastNMS.INSTANCE.method$Registry$getValue(MBuiltInRegistries.ITEM, KeyUtils.toResourceLocation(newItem)), amount);
|
||||
ItemObject.setCustomDataTag(newItemStack, TagCompound.clone(FastNMS.INSTANCE.field$ItemStack$getOrCreateTag(item.getLiteralObject())));
|
||||
FastNMS.INSTANCE.method$ItemStack$setTag(newItemStack, TagCompound.clone(FastNMS.INSTANCE.field$ItemStack$getOrCreateTag(item.getLiteralObject())));
|
||||
return new LegacyItemWrapper(new RtagItem(ItemObject.asCraftMirror(newItemStack)));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected LegacyItemWrapper unsafeTransmuteCopy(LegacyItemWrapper item, Object newItem, int amount) {
|
||||
Object newItemStack = FastNMS.INSTANCE.constructor$ItemStack(newItem, amount);
|
||||
FastNMS.INSTANCE.method$ItemStack$setTag(newItemStack, TagCompound.clone(FastNMS.INSTANCE.field$ItemStack$getOrCreateTag(item.getLiteralObject())));
|
||||
return new LegacyItemWrapper(new RtagItem(ItemObject.asCraftMirror(newItemStack)));
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package net.momirealms.craftengine.bukkit.item.listener;
|
||||
|
||||
import io.papermc.paper.event.block.CompostItemEvent;
|
||||
import net.momirealms.craftengine.bukkit.api.event.CustomBlockInteractEvent;
|
||||
import net.momirealms.craftengine.bukkit.block.BukkitBlockManager;
|
||||
import net.momirealms.craftengine.bukkit.plugin.BukkitCraftEngine;
|
||||
@@ -26,6 +27,7 @@ import net.momirealms.craftengine.core.world.BlockPos;
|
||||
import net.momirealms.craftengine.core.world.Vec3d;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
import org.bukkit.entity.EntityType;
|
||||
@@ -35,13 +37,18 @@ import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.Action;
|
||||
import org.bukkit.event.block.BlockIgniteEvent;
|
||||
import org.bukkit.event.enchantment.EnchantItemEvent;
|
||||
import org.bukkit.event.enchantment.PrepareItemEnchantEvent;
|
||||
import org.bukkit.event.entity.EntityDamageEvent;
|
||||
import org.bukkit.event.entity.FoodLevelChangeEvent;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.event.inventory.InventoryDragEvent;
|
||||
import org.bukkit.event.player.PlayerInteractEntityEvent;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.bukkit.event.player.PlayerItemConsumeEvent;
|
||||
import org.bukkit.inventory.EquipmentSlot;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.PlayerInventory;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
@@ -404,7 +411,7 @@ public class ItemEventListener implements Listener {
|
||||
|
||||
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)
|
||||
public void onEntityDamage(EntityDamageEvent event) {
|
||||
if (event.getEntityType() == EntityType.ITEM && event.getEntity() instanceof org.bukkit.entity.Item item) {
|
||||
if (event.getEntity() instanceof org.bukkit.entity.Item item) {
|
||||
Optional.ofNullable(this.plugin.itemManager().wrap(item.getItemStack()))
|
||||
.flatMap(Item::getCustomItem)
|
||||
.ifPresent(it -> {
|
||||
@@ -414,4 +421,34 @@ public class ItemEventListener implements Listener {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)
|
||||
public void onEnchant(PrepareItemEnchantEvent event) {
|
||||
ItemStack itemToEnchant = event.getItem();
|
||||
Item<ItemStack> wrapped = this.plugin.itemManager().wrap(itemToEnchant);
|
||||
Optional<CustomItem<ItemStack>> optionalCustomItem = wrapped.getCustomItem();
|
||||
if (optionalCustomItem.isEmpty()) return;
|
||||
CustomItem<ItemStack> customItem = optionalCustomItem.get();
|
||||
if (!customItem.settings().canEnchant()) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onCompost(CompostItemEvent event) {
|
||||
ItemStack itemToCompost = event.getItem();
|
||||
Item<ItemStack> wrapped = this.plugin.itemManager().wrap(itemToCompost);
|
||||
Optional<CustomItem<ItemStack>> optionalCustomItem = wrapped.getCustomItem();
|
||||
if (optionalCustomItem.isEmpty()) return;
|
||||
event.setWillRaiseLevel(RandomUtils.generateRandomFloat(0, 1) < optionalCustomItem.get().settings().compostProbability());
|
||||
}
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onDragItem(InventoryClickEvent event) {
|
||||
// Player player = (Player) event.getWhoClicked();
|
||||
// plugin.scheduler().sync().runLater(() -> {
|
||||
// System.out.println(1);
|
||||
// player.updateInventory();
|
||||
// }, 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package net.momirealms.craftengine.bukkit.plugin.gui;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.momirealms.craftengine.bukkit.nms.FastNMS;
|
||||
import net.momirealms.craftengine.bukkit.plugin.BukkitCraftEngine;
|
||||
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.CoreReflections;
|
||||
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.NetworkReflections;
|
||||
@@ -69,7 +70,7 @@ public class BukkitGuiManager implements GuiManager, Listener {
|
||||
public void updateInventoryTitle(net.momirealms.craftengine.core.entity.player.Player player, Component component) {
|
||||
Object nmsPlayer = player.serverPlayer();
|
||||
try {
|
||||
Object containerMenu = CoreReflections.field$Player$containerMenu.get(nmsPlayer);
|
||||
Object containerMenu = FastNMS.INSTANCE.field$Player$containerMenu(nmsPlayer);
|
||||
int containerId = CoreReflections.field$AbstractContainerMenu$containerId.getInt(containerMenu);
|
||||
Object menuType = CoreReflections.field$AbstractContainerMenu$menuType.get(containerMenu);
|
||||
Object packet = NetworkReflections.constructor$ClientboundOpenScreenPacket.newInstance(containerId, menuType, ComponentUtils.adventureToMinecraft(component));
|
||||
|
||||
@@ -12,6 +12,7 @@ import net.kyori.adventure.text.TranslationArgument;
|
||||
import net.momirealms.craftengine.bukkit.api.CraftEngineFurniture;
|
||||
import net.momirealms.craftengine.bukkit.api.event.FurnitureBreakEvent;
|
||||
import net.momirealms.craftengine.bukkit.api.event.FurnitureInteractEvent;
|
||||
import net.momirealms.craftengine.bukkit.api.event.FurnitureAttemptBreakEvent;
|
||||
import net.momirealms.craftengine.bukkit.block.BukkitBlockManager;
|
||||
import net.momirealms.craftengine.bukkit.entity.furniture.BukkitFurniture;
|
||||
import net.momirealms.craftengine.bukkit.entity.furniture.BukkitFurnitureManager;
|
||||
@@ -1539,9 +1540,14 @@ public class PacketConsumers {
|
||||
mainThreadTask = () -> {
|
||||
// todo 冒险模式破坏工具白名单
|
||||
if (serverPlayer.isAdventureMode() ||
|
||||
!furniture.isValid() ||
|
||||
!BukkitCraftEngine.instance().antiGrief().canBreak(platformPlayer, location)
|
||||
) return;
|
||||
!furniture.isValid()) return;
|
||||
|
||||
FurnitureAttemptBreakEvent preBreakEvent = new FurnitureAttemptBreakEvent(serverPlayer.platformPlayer(), furniture);
|
||||
if (EventUtils.fireAndCheckCancel(preBreakEvent))
|
||||
return;
|
||||
|
||||
if (!BukkitCraftEngine.instance().antiGrief().canBreak(platformPlayer, location))
|
||||
return;
|
||||
|
||||
FurnitureBreakEvent breakEvent = new FurnitureBreakEvent(serverPlayer.platformPlayer(), furniture);
|
||||
if (EventUtils.fireAndCheckCancel(breakEvent))
|
||||
@@ -2102,6 +2108,23 @@ public class PacketConsumers {
|
||||
FriendlyByteBuf buf = event.getBuffer();
|
||||
Object friendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf);
|
||||
ItemStack itemStack = FastNMS.INSTANCE.method$FriendlyByteBuf$readItem(friendlyBuf);
|
||||
if (VersionHelper.isOrAbove1_21_5()) {
|
||||
Item<ItemStack> wrapped = BukkitItemManager.instance().wrap(itemStack);
|
||||
if (wrapped != null && wrapped.isCustomItem()) {
|
||||
Object containerMenu = FastNMS.INSTANCE.field$Player$containerMenu(serverPlayer.serverPlayer());
|
||||
if (containerMenu != null) {
|
||||
ItemStack carried = FastNMS.INSTANCE.method$CraftItemStack$asCraftMirror(FastNMS.INSTANCE.method$AbstractContainerMenu$getCarried(containerMenu));
|
||||
if (ItemUtils.isEmpty(carried)) {
|
||||
event.setChanged(true);
|
||||
buf.clear();
|
||||
buf.writeVarInt(event.packetID());
|
||||
Object newFriendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf);
|
||||
FastNMS.INSTANCE.method$FriendlyByteBuf$writeItem(newFriendlyBuf, carried);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
BukkitItemManager.instance().s2c(itemStack, serverPlayer).ifPresent((newItemStack) -> {
|
||||
event.setChanged(true);
|
||||
buf.clear();
|
||||
|
||||
Reference in New Issue
Block a user