mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-19 15:09:15 +00:00
添加捡起物品触发器
This commit is contained in:
@@ -103,4 +103,9 @@ public class BukkitEntity extends AbstractEntity {
|
|||||||
public <T> void setEntityData(EntityData<T> data, T value, boolean force) {
|
public <T> void setEntityData(EntityData<T> data, T value, boolean force) {
|
||||||
FastNMS.INSTANCE.method$SynchedEntityData$set(entityData(), data.entityDataAccessor(), value, force);
|
FastNMS.INSTANCE.method$SynchedEntityData$set(entityData(), data.entityDataAccessor(), value, force);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void remove() {
|
||||||
|
this.platformEntity().remove();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -576,16 +576,31 @@ public class ItemEventListener implements Listener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGH)
|
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
|
||||||
public void onPickUpItem(EntityPickupItemEvent event) {
|
public void onPickUpItem(EntityPickupItemEvent event) {
|
||||||
if (!Config.triggerUpdatePickUp()) return;
|
|
||||||
if (!(event.getEntity() instanceof Player player)) return;
|
if (!(event.getEntity() instanceof Player player)) return;
|
||||||
org.bukkit.entity.Item itemDrop = event.getItem();
|
org.bukkit.entity.Item itemDrop = event.getItem();
|
||||||
ItemStack itemStack = itemDrop.getItemStack();
|
ItemStack itemStack = itemDrop.getItemStack();
|
||||||
Item<ItemStack> wrapped = this.itemManager.wrap(itemStack);
|
Item<ItemStack> wrapped = this.itemManager.wrap(itemStack);
|
||||||
ItemUpdateResult result = this.itemManager.updateItem(wrapped, () -> ItemBuildContext.of(BukkitAdaptors.adapt(player)));
|
Optional<CustomItem<ItemStack>> optionalCustomItem = wrapped.getCustomItem();
|
||||||
if (result.updated()) {
|
if (optionalCustomItem.isEmpty()) return;
|
||||||
itemDrop.setItemStack((ItemStack) result.finalItem().getItem());
|
BukkitServerPlayer serverPlayer = BukkitAdaptors.adapt(player);
|
||||||
|
CustomItem<ItemStack> customItem = optionalCustomItem.get();
|
||||||
|
if (Config.triggerUpdatePickUp() && customItem.updater().isPresent()) {
|
||||||
|
ItemUpdateResult result = this.itemManager.updateItem(wrapped, () -> ItemBuildContext.of(serverPlayer));
|
||||||
|
if (result.updated()) {
|
||||||
|
itemDrop.setItemStack((ItemStack) result.finalItem().getItem());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Cancellable dummy = Cancellable.dummy();
|
||||||
|
customItem.execute(PlayerOptionalContext.of(serverPlayer, ContextHolder.builder()
|
||||||
|
.withParameter(DirectContextParameters.ENTITY, new BukkitEntity(itemDrop))
|
||||||
|
.withParameter(DirectContextParameters.POSITION, LocationUtils.toWorldPosition(itemDrop.getLocation()))
|
||||||
|
.withParameter(DirectContextParameters.EVENT, dummy)
|
||||||
|
), EventTrigger.PICK_UP);
|
||||||
|
if (dummy.isCancelled()) {
|
||||||
|
event.setCancelled(true);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -48,4 +48,6 @@ public interface Entity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
<T> void setEntityData(EntityData<T> data, T value, boolean force);
|
<T> void setEntityData(EntityData<T> data, T value, boolean force);
|
||||||
|
|
||||||
|
void remove();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -180,4 +180,8 @@ public abstract class Player extends AbstractEntity implements NetWorkUser {
|
|||||||
public abstract Locale selectedLocale();
|
public abstract Locale selectedLocale();
|
||||||
|
|
||||||
public abstract void setSelectedLocale(@Nullable Locale locale);
|
public abstract void setSelectedLocale(@Nullable Locale locale);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void remove() {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,6 +47,7 @@ public class EventFunctions {
|
|||||||
register(CommonFunctions.TOAST, new ToastFunction.FactoryImpl<>(EventConditions::fromMap));
|
register(CommonFunctions.TOAST, new ToastFunction.FactoryImpl<>(EventConditions::fromMap));
|
||||||
register(CommonFunctions.DAMAGE, new DamageFunction.FactoryImpl<>(EventConditions::fromMap));
|
register(CommonFunctions.DAMAGE, new DamageFunction.FactoryImpl<>(EventConditions::fromMap));
|
||||||
register(CommonFunctions.MERCHANT_TRADE, new MerchantTradeFunction.FactoryImpl<>(EventConditions::fromMap));
|
register(CommonFunctions.MERCHANT_TRADE, new MerchantTradeFunction.FactoryImpl<>(EventConditions::fromMap));
|
||||||
|
register(CommonFunctions.REMOVE_ENTITY, new RemoveEntityFunction.FactoryImpl<>(EventConditions::fromMap));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void register(Key key, FunctionFactory<PlayerOptionalContext> factory) {
|
public static void register(Key key, FunctionFactory<PlayerOptionalContext> factory) {
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ public enum EventTrigger {
|
|||||||
CONSUME("eat", "consume", "drink"),
|
CONSUME("eat", "consume", "drink"),
|
||||||
BREAK("break", "dig"),
|
BREAK("break", "dig"),
|
||||||
PLACE("place", "build"),
|
PLACE("place", "build"),
|
||||||
|
PICK_UP("pick_up", "pick"),
|
||||||
STEP("step"),;
|
STEP("step"),;
|
||||||
|
|
||||||
public static final Map<String, EventTrigger> BY_NAME = new HashMap<>();
|
public static final Map<String, EventTrigger> BY_NAME = new HashMap<>();
|
||||||
|
|||||||
@@ -36,4 +36,5 @@ public final class CommonFunctions {
|
|||||||
public static final Key SET_VARIABLE = Key.of("craftengine:set_variable");
|
public static final Key SET_VARIABLE = Key.of("craftengine:set_variable");
|
||||||
public static final Key DAMAGE = Key.of("craftengine:damage");
|
public static final Key DAMAGE = Key.of("craftengine:damage");
|
||||||
public static final Key MERCHANT_TRADE = Key.of("craftengine:merchant_trade");
|
public static final Key MERCHANT_TRADE = Key.of("craftengine:merchant_trade");
|
||||||
|
public static final Key REMOVE_ENTITY = Key.of("craftengine:remove_entity");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package net.momirealms.craftengine.core.plugin.context.function;
|
||||||
|
|
||||||
|
import net.momirealms.craftengine.core.entity.Entity;
|
||||||
|
import net.momirealms.craftengine.core.plugin.context.Condition;
|
||||||
|
import net.momirealms.craftengine.core.plugin.context.Context;
|
||||||
|
import net.momirealms.craftengine.core.plugin.context.parameter.DirectContextParameters;
|
||||||
|
import net.momirealms.craftengine.core.util.Key;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class RemoveEntityFunction<CTX extends Context> extends AbstractConditionalFunction<CTX> {
|
||||||
|
|
||||||
|
public RemoveEntityFunction(List<Condition<CTX>> predicates) {
|
||||||
|
super(predicates);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void runInternal(CTX ctx) {
|
||||||
|
ctx.getOptionalParameter(DirectContextParameters.ENTITY).ifPresent(Entity::remove);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Key type() {
|
||||||
|
return CommonFunctions.REMOVE_ENTITY;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class FactoryImpl<CTX extends Context> extends AbstractFactory<CTX> {
|
||||||
|
|
||||||
|
public FactoryImpl(java.util.function.Function<Map<String, Object>, Condition<CTX>> factory) {
|
||||||
|
super(factory);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Function<CTX> create(Map<String, Object> arguments) {
|
||||||
|
return new RemoveEntityFunction<>(getPredicates(arguments));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -65,4 +65,5 @@ public final class DirectContextParameters {
|
|||||||
public static final ContextKey<Boolean> IS_CUSTOM = ContextKey.direct("is_custom");
|
public static final ContextKey<Boolean> IS_CUSTOM = ContextKey.direct("is_custom");
|
||||||
public static final ContextKey<Boolean> IS_BLOCK_ITEM = ContextKey.direct("is_block_item");
|
public static final ContextKey<Boolean> IS_BLOCK_ITEM = ContextKey.direct("is_block_item");
|
||||||
public static final ContextKey<GameMode> GAMEMODE = ContextKey.direct("gamemode");
|
public static final ContextKey<GameMode> GAMEMODE = ContextKey.direct("gamemode");
|
||||||
|
public static final ContextKey<Integer> COUNT = ContextKey.direct("count");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ public class ItemParameterProvider implements ChainParameterProvider<Item<?>> {
|
|||||||
CONTEXT_FUNCTIONS.put(DirectContextParameters.ID, Item::id);
|
CONTEXT_FUNCTIONS.put(DirectContextParameters.ID, Item::id);
|
||||||
CONTEXT_FUNCTIONS.put(DirectContextParameters.CUSTOM_MODEL_DATA, i -> i.customModelData().orElse(null));
|
CONTEXT_FUNCTIONS.put(DirectContextParameters.CUSTOM_MODEL_DATA, i -> i.customModelData().orElse(null));
|
||||||
CONTEXT_FUNCTIONS.put(DirectContextParameters.IS_CUSTOM, Item::isCustomItem);
|
CONTEXT_FUNCTIONS.put(DirectContextParameters.IS_CUSTOM, Item::isCustomItem);
|
||||||
|
CONTEXT_FUNCTIONS.put(DirectContextParameters.COUNT, Item::count);
|
||||||
CONTEXT_FUNCTIONS.put(DirectContextParameters.IS_BLOCK_ITEM, Item::isBlockItem);
|
CONTEXT_FUNCTIONS.put(DirectContextParameters.IS_BLOCK_ITEM, Item::isBlockItem);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user