mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-31 04:46:37 +00:00
改进上下文参数
This commit is contained in:
@@ -14,7 +14,6 @@ import net.momirealms.craftengine.core.item.Item;
|
||||
import net.momirealms.craftengine.core.plugin.context.ContextHolder;
|
||||
import net.momirealms.craftengine.core.plugin.context.parameter.DirectContextParameters;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import net.momirealms.craftengine.core.world.Vec3d;
|
||||
import net.momirealms.craftengine.core.world.World;
|
||||
import net.momirealms.craftengine.core.world.WorldEvents;
|
||||
import net.momirealms.craftengine.core.world.WorldPosition;
|
||||
|
||||
@@ -5,7 +5,6 @@ import net.momirealms.craftengine.bukkit.entity.furniture.LoadedFurniture;
|
||||
import net.momirealms.craftengine.bukkit.nms.CollisionEntity;
|
||||
import net.momirealms.craftengine.bukkit.nms.FastNMS;
|
||||
import net.momirealms.craftengine.bukkit.plugin.BukkitCraftEngine;
|
||||
import net.momirealms.craftengine.bukkit.util.LocationUtils;
|
||||
import net.momirealms.craftengine.bukkit.world.BukkitWorld;
|
||||
import net.momirealms.craftengine.core.entity.furniture.AnchorType;
|
||||
import net.momirealms.craftengine.core.entity.furniture.CustomFurniture;
|
||||
@@ -15,7 +14,6 @@ import net.momirealms.craftengine.core.loot.LootTable;
|
||||
import net.momirealms.craftengine.core.plugin.context.ContextHolder;
|
||||
import net.momirealms.craftengine.core.plugin.context.parameter.DirectContextParameters;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import net.momirealms.craftengine.core.world.Vec3d;
|
||||
import net.momirealms.craftengine.core.world.World;
|
||||
import net.momirealms.craftengine.core.world.WorldPosition;
|
||||
import org.bukkit.Location;
|
||||
|
||||
@@ -18,6 +18,7 @@ import net.momirealms.craftengine.core.plugin.context.ContextHolder;
|
||||
import net.momirealms.craftengine.core.plugin.context.PlayerOptionalContext;
|
||||
import net.momirealms.craftengine.core.plugin.context.parameter.DirectContextParameters;
|
||||
import net.momirealms.craftengine.core.plugin.event.EventTrigger;
|
||||
import net.momirealms.craftengine.core.util.Cancellable;
|
||||
import net.momirealms.craftengine.core.util.VersionHelper;
|
||||
import net.momirealms.craftengine.core.world.BlockPos;
|
||||
import net.momirealms.craftengine.core.world.Vec3d;
|
||||
@@ -25,7 +26,6 @@ import net.momirealms.craftengine.core.world.WorldPosition;
|
||||
import org.bukkit.*;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.BlockFace;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
@@ -131,7 +131,7 @@ public class BlockEventListener implements Listener {
|
||||
return;
|
||||
}
|
||||
|
||||
// trigger event
|
||||
// trigger api event
|
||||
CustomBlockBreakEvent customBreakEvent = new CustomBlockBreakEvent(serverPlayer, location, block, state);
|
||||
boolean isCancelled = EventUtils.fireAndCheckCancel(customBreakEvent);
|
||||
if (isCancelled) {
|
||||
@@ -139,12 +139,24 @@ public class BlockEventListener implements Listener {
|
||||
return;
|
||||
}
|
||||
|
||||
// execute functions
|
||||
Item<ItemStack> itemInHand = serverPlayer.getItemInHand(InteractionHand.MAIN_HAND);
|
||||
net.momirealms.craftengine.core.world.World world = new BukkitWorld(location.getWorld());
|
||||
WorldPosition position = new WorldPosition(world, location.getBlockX() + 0.5, location.getBlockY() + 0.5, location.getBlockZ() + 0.5);
|
||||
|
||||
// execute functions
|
||||
Cancellable cancellable = Cancellable.dummy();
|
||||
PlayerOptionalContext context = PlayerOptionalContext.of(serverPlayer, ContextHolder.builder()
|
||||
.withParameter(DirectContextParameters.BLOCK, new BukkitBlockInWorld(block))
|
||||
.withParameter(DirectContextParameters.BLOCK_STATE, state));
|
||||
.withParameter(DirectContextParameters.BLOCK_STATE, state)
|
||||
.withParameter(DirectContextParameters.EVENT, cancellable)
|
||||
.withParameter(DirectContextParameters.POSITION, position)
|
||||
.withOptionalParameter(DirectContextParameters.ITEM_IN_HAND, itemInHand)
|
||||
);
|
||||
state.owner().value().execute(context, EventTrigger.BREAK);
|
||||
if (cancellable.isCancelled()) {
|
||||
event.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
|
||||
// handle waterlogged blocks
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -157,13 +169,11 @@ public class BlockEventListener implements Listener {
|
||||
}
|
||||
|
||||
// play sound
|
||||
WorldPosition position = new WorldPosition(world, location.getBlockX() + 0.5, location.getBlockY() + 0.5, location.getBlockZ() + 0.5);
|
||||
world.playBlockSound(position, state.sounds().breakSound());
|
||||
if (player.getGameMode() == GameMode.CREATIVE || !customBreakEvent.dropItems()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Item<ItemStack> itemInHand = serverPlayer.getItemInHand(InteractionHand.MAIN_HAND);
|
||||
// do not drop if it's not the correct tool
|
||||
if (!BlockStateUtils.isCorrectTool(state, itemInHand)) {
|
||||
return;
|
||||
@@ -172,8 +182,9 @@ public class BlockEventListener implements Listener {
|
||||
// drop items
|
||||
ContextHolder.Builder builder = ContextHolder.builder()
|
||||
.withParameter(DirectContextParameters.POSITION, position)
|
||||
.withParameter(DirectContextParameters.PLAYER, serverPlayer);
|
||||
//mark item .withOptionalParameter(CommonParameters.MAIN_HAND_ITEM, itemInHand);
|
||||
.withParameter(DirectContextParameters.PLAYER, serverPlayer)
|
||||
.withParameter(DirectContextParameters.BLOCK, new BukkitBlockInWorld(block))
|
||||
.withOptionalParameter(DirectContextParameters.ITEM_IN_HAND, itemInHand);
|
||||
for (Item<Object> item : state.getDrops(builder, world, serverPlayer)) {
|
||||
world.dropItemNaturally(position, item);
|
||||
}
|
||||
@@ -191,8 +202,10 @@ public class BlockEventListener implements Listener {
|
||||
net.momirealms.craftengine.core.world.World world = new BukkitWorld(player.getWorld());
|
||||
WorldPosition position = new WorldPosition(world, location.getBlockX() + 0.5, location.getBlockY() + 0.5, location.getBlockZ() + 0.5);
|
||||
ContextHolder.Builder builder = ContextHolder.builder()
|
||||
.withParameter(DirectContextParameters.BLOCK, new BukkitBlockInWorld(block))
|
||||
.withParameter(DirectContextParameters.POSITION, position)
|
||||
.withParameter(DirectContextParameters.PLAYER, serverPlayer);
|
||||
.withParameter(DirectContextParameters.PLAYER, serverPlayer)
|
||||
.withOptionalParameter(DirectContextParameters.ITEM_IN_HAND, serverPlayer.getItemInHand(InteractionHand.MAIN_HAND));
|
||||
ContextHolder contextHolder = builder.build();
|
||||
for (LootTable<?> lootTable : it.lootTables()) {
|
||||
for (Item<?> item : lootTable.getRandomItems(contextHolder, world, serverPlayer)) {
|
||||
@@ -230,9 +243,9 @@ public class BlockEventListener implements Listener {
|
||||
Location location = block.getLocation();
|
||||
net.momirealms.craftengine.core.world.World world = new BukkitWorld(block.getWorld());
|
||||
WorldPosition position = new WorldPosition(world, location.getBlockX() + 0.5, location.getBlockY() + 0.5, location.getBlockZ() + 0.5);
|
||||
ContextHolder.Builder builder = ContextHolder.builder()
|
||||
.withParameter(DirectContextParameters.POSITION, position);
|
||||
for (Item<?> item : immutableBlockState.getDrops(builder, world, null)) {
|
||||
for (Item<?> item : immutableBlockState.getDrops(ContextHolder.builder()
|
||||
.withParameter(DirectContextParameters.POSITION, position)
|
||||
.withParameter(DirectContextParameters.BLOCK, new BukkitBlockInWorld(block)), world, null)) {
|
||||
world.dropItemNaturally(position, item);
|
||||
}
|
||||
}
|
||||
@@ -243,15 +256,14 @@ public class BlockEventListener implements Listener {
|
||||
event.getDrops().clear();
|
||||
event.setExpToDrop(0);
|
||||
}
|
||||
|
||||
Location location = block.getLocation();
|
||||
net.momirealms.craftengine.core.world.World world = new BukkitWorld(location.getWorld());
|
||||
WorldPosition position = new WorldPosition(world, location.getBlockX() + 0.5, location.getBlockY() + 0.5, location.getBlockZ() + 0.5);
|
||||
ContextHolder.Builder builder = ContextHolder.builder()
|
||||
.withParameter(DirectContextParameters.POSITION, position);
|
||||
ContextHolder contextHolder = builder.build();
|
||||
.withParameter(DirectContextParameters.POSITION, position)
|
||||
.withParameter(DirectContextParameters.BLOCK, new BukkitBlockInWorld(block));
|
||||
for (LootTable<?> lootTable : it.lootTables()) {
|
||||
for (Item<?> item : lootTable.getRandomItems(contextHolder, world, null)) {
|
||||
for (Item<?> item : lootTable.getRandomItems(builder.build(), world, null)) {
|
||||
world.dropItemNaturally(position, item);
|
||||
}
|
||||
}
|
||||
@@ -259,26 +271,37 @@ public class BlockEventListener implements Listener {
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOW)
|
||||
public void onStep(GenericGameEvent event) {
|
||||
if (event.getEvent() != GameEvent.STEP) return;
|
||||
Entity entity = event.getEntity();
|
||||
if (!(entity instanceof Player player)) return;
|
||||
BlockPos pos = EntityUtils.getOnPos(player);
|
||||
Location playerLocation = player.getLocation();
|
||||
BlockData blockData = player.getWorld().getBlockData(pos.x(), pos.y(), pos.z());
|
||||
Object blockState = BlockStateUtils.blockDataToBlockState(blockData);
|
||||
Block block = player.getWorld().getBlockAt(pos.x(), pos.y(), pos.z());
|
||||
Object blockState = BlockStateUtils.blockDataToBlockState(block.getBlockData());
|
||||
int stateId = BlockStateUtils.blockStateToId(blockState);
|
||||
if (!BlockStateUtils.isVanillaBlock(stateId)) {
|
||||
Location location = player.getLocation();
|
||||
ImmutableBlockState state = manager.getImmutableBlockStateUnsafe(stateId);
|
||||
player.playSound(playerLocation, state.sounds().stepSound().id().toString(), SoundCategory.BLOCKS, state.sounds().stepSound().volume(), state.sounds().stepSound().pitch());
|
||||
Cancellable cancellable = Cancellable.dummy();
|
||||
state.owner().value().execute(PlayerOptionalContext.of(this.plugin.adapt(player), ContextHolder.builder()
|
||||
.withParameter(DirectContextParameters.EVENT, cancellable)
|
||||
.withParameter(DirectContextParameters.POSITION, new WorldPosition(new BukkitWorld(event.getWorld()), LocationUtils.toVec3d(location)))
|
||||
.withParameter(DirectContextParameters.BLOCK, new BukkitBlockInWorld(block))
|
||||
.withParameter(DirectContextParameters.BLOCK_STATE, state)
|
||||
), EventTrigger.STEP);
|
||||
if (cancellable.isCancelled()) {
|
||||
event.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
player.playSound(location, state.sounds().stepSound().id().toString(), SoundCategory.BLOCKS, state.sounds().stepSound().volume(), state.sounds().stepSound().pitch());
|
||||
} else if (Config.enableSoundSystem()) {
|
||||
Object ownerBlock = BlockStateUtils.getBlockOwner(blockState);
|
||||
if (manager.isBlockSoundRemoved(ownerBlock)) {
|
||||
try {
|
||||
Object soundType = Reflections.field$BlockBehaviour$soundType.get(ownerBlock);
|
||||
Object stepSound = Reflections.field$SoundType$stepSound.get(soundType);
|
||||
player.playSound(playerLocation, FastNMS.INSTANCE.field$SoundEvent$location(stepSound).toString(), SoundCategory.BLOCKS, 0.15f, 1f);
|
||||
player.playSound(player.getLocation(), FastNMS.INSTANCE.field$SoundEvent$location(stepSound).toString(), SoundCategory.BLOCKS, 0.15f, 1f);
|
||||
} catch (ReflectiveOperationException e) {
|
||||
plugin.logger().warn("Failed to get sound type", e);
|
||||
}
|
||||
@@ -286,39 +309,6 @@ public class BlockEventListener implements Listener {
|
||||
}
|
||||
}
|
||||
|
||||
// Use BlockBreakBlock event
|
||||
// @EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
|
||||
// public void onPistonRetract(BlockPistonRetractEvent event) {
|
||||
// handlePistonEvent(event.getDirection(), event.getBlocks(), event.getBlock());
|
||||
// }
|
||||
//
|
||||
// @EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
|
||||
// public void onPistonExtend(BlockPistonExtendEvent event) {
|
||||
// handlePistonEvent(event.getDirection(), event.getBlocks(), event.getBlock());
|
||||
// }
|
||||
//
|
||||
// private void handlePistonEvent(BlockFace face, List<Block> blocksList, Block piston) {
|
||||
// int blocks = blocksList.size();
|
||||
// net.momirealms.craftengine.core.world.World world = new BukkitWorld(piston.getWorld());
|
||||
// for (int i = blocks - 1; i >= 0; --i) {
|
||||
// Location oldLocation = blocksList.get(i).getLocation();
|
||||
// BlockPos oldPos = new BlockPos(oldLocation.getBlockX(), oldLocation.getBlockY(), oldLocation.getBlockZ());
|
||||
// Block block = blocksList.get(i);
|
||||
// ImmutableBlockState blockState = manager.getImmutableBlockState(BlockStateUtils.blockDataToId(block.getBlockData()));
|
||||
// if (blockState != null && blockState.pushReaction() == PushReaction.DESTROY) {
|
||||
// // break actions
|
||||
// ContextHolder.Builder builder = ContextHolder.builder();
|
||||
// Vec3d vec3d = Vec3d.atCenterOf(oldPos);
|
||||
// builder.withParameter(LootParameters.LOCATION, vec3d);
|
||||
// builder.withParameter(LootParameters.WORLD, world);
|
||||
// for (Item<Object> item : blockState.getDrops(builder, world)) {
|
||||
// world.dropItemNaturally(vec3d, item);
|
||||
// }
|
||||
// world.playBlockSound(vec3d, blockState.sounds().breakSound());
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
|
||||
public void onEntityExplode(EntityExplodeEvent event) {
|
||||
if (VersionHelper.isOrAbove1_21()) {
|
||||
@@ -344,7 +334,8 @@ public class BlockEventListener implements Listener {
|
||||
if (state != null && !state.isEmpty()) {
|
||||
WorldPosition position = new WorldPosition(world, Vec3d.atCenterOf(blockPos));
|
||||
ContextHolder.Builder builder = ContextHolder.builder()
|
||||
.withParameter(DirectContextParameters.POSITION, position);
|
||||
.withParameter(DirectContextParameters.POSITION, position)
|
||||
.withParameter(DirectContextParameters.BLOCK, new BukkitBlockInWorld(block));
|
||||
if (yield < 1f) {
|
||||
builder.withParameter(DirectContextParameters.EXPLOSION_RADIUS, 1.0f / yield);
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ import net.momirealms.craftengine.core.item.Item;
|
||||
import net.momirealms.craftengine.core.plugin.CraftEngine;
|
||||
import net.momirealms.craftengine.core.plugin.context.ContextHolder;
|
||||
import net.momirealms.craftengine.core.plugin.context.parameter.DirectContextParameters;
|
||||
import net.momirealms.craftengine.core.world.Vec3d;
|
||||
import net.momirealms.craftengine.core.world.WorldPosition;
|
||||
import org.bukkit.entity.FallingBlock;
|
||||
import org.bukkit.event.EventHandler;
|
||||
|
||||
@@ -8,6 +8,7 @@ import net.momirealms.craftengine.bukkit.api.event.CustomBlockPlaceEvent;
|
||||
import net.momirealms.craftengine.bukkit.block.BukkitBlockManager;
|
||||
import net.momirealms.craftengine.bukkit.nms.FastNMS;
|
||||
import net.momirealms.craftengine.bukkit.util.*;
|
||||
import net.momirealms.craftengine.bukkit.world.BukkitBlockInWorld;
|
||||
import net.momirealms.craftengine.core.block.CustomBlock;
|
||||
import net.momirealms.craftengine.core.block.ImmutableBlockState;
|
||||
import net.momirealms.craftengine.core.block.UpdateOption;
|
||||
@@ -22,12 +23,17 @@ import net.momirealms.craftengine.core.item.context.UseOnContext;
|
||||
import net.momirealms.craftengine.core.pack.Pack;
|
||||
import net.momirealms.craftengine.core.plugin.CraftEngine;
|
||||
import net.momirealms.craftengine.core.plugin.config.Config;
|
||||
import net.momirealms.craftengine.core.plugin.context.ContextHolder;
|
||||
import net.momirealms.craftengine.core.plugin.context.PlayerOptionalContext;
|
||||
import net.momirealms.craftengine.core.plugin.context.parameter.DirectContextParameters;
|
||||
import net.momirealms.craftengine.core.plugin.event.EventTrigger;
|
||||
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
|
||||
import net.momirealms.craftengine.core.util.Cancellable;
|
||||
import net.momirealms.craftengine.core.util.Direction;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import net.momirealms.craftengine.core.util.MiscUtils;
|
||||
import net.momirealms.craftengine.core.world.BlockPos;
|
||||
import net.momirealms.craftengine.core.world.Vec3d;
|
||||
import net.momirealms.craftengine.core.world.WorldPosition;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.GameEvent;
|
||||
import org.bukkit.Location;
|
||||
@@ -137,6 +143,20 @@ public class BlockItemBehavior extends ItemBehavior {
|
||||
return InteractionResult.FAIL;
|
||||
}
|
||||
|
||||
WorldPosition position = new WorldPosition(context.getLevel(), pos.x() + 0.5, pos.y() + 0.5, pos.z() + 0.5);
|
||||
Cancellable dummy = Cancellable.dummy();
|
||||
PlayerOptionalContext functionContext = PlayerOptionalContext.of(player, ContextHolder.builder()
|
||||
.withParameter(DirectContextParameters.BLOCK, new BukkitBlockInWorld(bukkitBlock))
|
||||
.withParameter(DirectContextParameters.POSITION, position)
|
||||
.withParameter(DirectContextParameters.EVENT, dummy)
|
||||
.withParameter(DirectContextParameters.HAND, context.getHand())
|
||||
.withParameter(DirectContextParameters.ITEM_IN_HAND, context.getItem())
|
||||
);
|
||||
block.execute(functionContext, EventTrigger.PLACE);
|
||||
if (dummy.isCancelled()) {
|
||||
return InteractionResult.SUCCESS_AND_CANCEL;
|
||||
}
|
||||
|
||||
if (!player.isCreativeMode()) {
|
||||
Item<?> item = context.getItem();
|
||||
item.count(item.count() - 1);
|
||||
@@ -144,7 +164,7 @@ public class BlockItemBehavior extends ItemBehavior {
|
||||
}
|
||||
|
||||
player.swingHand(context.getHand());
|
||||
context.getLevel().playBlockSound(new Vec3d(pos.x() + 0.5, pos.y() + 0.5, pos.z() + 0.5), blockStateToPlace.sounds().placeSound());
|
||||
context.getLevel().playBlockSound(position, blockStateToPlace.sounds().placeSound());
|
||||
world.sendGameEvent(bukkitPlayer, GameEvent.BLOCK_PLACE, new Vector(pos.x(), pos.y(), pos.z()));
|
||||
return InteractionResult.SUCCESS;
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import net.momirealms.craftengine.bukkit.nms.FastNMS;
|
||||
import net.momirealms.craftengine.bukkit.plugin.BukkitCraftEngine;
|
||||
import net.momirealms.craftengine.bukkit.util.DirectionUtils;
|
||||
import net.momirealms.craftengine.bukkit.util.EventUtils;
|
||||
import net.momirealms.craftengine.bukkit.util.LocationUtils;
|
||||
import net.momirealms.craftengine.core.entity.furniture.AnchorType;
|
||||
import net.momirealms.craftengine.core.entity.furniture.CustomFurniture;
|
||||
import net.momirealms.craftengine.core.entity.furniture.FurnitureExtraData;
|
||||
@@ -20,6 +21,10 @@ import net.momirealms.craftengine.core.item.behavior.ItemBehaviorFactory;
|
||||
import net.momirealms.craftengine.core.item.context.UseOnContext;
|
||||
import net.momirealms.craftengine.core.pack.Pack;
|
||||
import net.momirealms.craftengine.core.plugin.CraftEngine;
|
||||
import net.momirealms.craftengine.core.plugin.context.ContextHolder;
|
||||
import net.momirealms.craftengine.core.plugin.context.PlayerOptionalContext;
|
||||
import net.momirealms.craftengine.core.plugin.context.parameter.DirectContextParameters;
|
||||
import net.momirealms.craftengine.core.plugin.event.EventTrigger;
|
||||
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
|
||||
import net.momirealms.craftengine.core.util.*;
|
||||
import net.momirealms.craftengine.core.world.Vec3d;
|
||||
@@ -143,6 +148,19 @@ public class FurnitureItemBehavior extends ItemBehavior {
|
||||
return InteractionResult.FAIL;
|
||||
}
|
||||
|
||||
Cancellable dummy = Cancellable.dummy();
|
||||
PlayerOptionalContext functionContext = PlayerOptionalContext.of(player, ContextHolder.builder()
|
||||
.withParameter(DirectContextParameters.FURNITURE, loadedFurniture)
|
||||
.withParameter(DirectContextParameters.POSITION, LocationUtils.toWorldPosition(furnitureLocation))
|
||||
.withParameter(DirectContextParameters.EVENT, dummy)
|
||||
.withParameter(DirectContextParameters.HAND, context.getHand())
|
||||
.withParameter(DirectContextParameters.ITEM_IN_HAND, item)
|
||||
);
|
||||
customFurniture.execute(functionContext, EventTrigger.PLACE);
|
||||
if (dummy.isCancelled()) {
|
||||
return InteractionResult.SUCCESS_AND_CANCEL;
|
||||
}
|
||||
|
||||
if (!player.isCreativeMode()) {
|
||||
item.count(item.count() - 1);
|
||||
item.load();
|
||||
|
||||
@@ -14,12 +14,11 @@ import net.momirealms.craftengine.core.item.CustomItem;
|
||||
import net.momirealms.craftengine.core.item.Item;
|
||||
import net.momirealms.craftengine.core.item.behavior.ItemBehavior;
|
||||
import net.momirealms.craftengine.core.item.context.UseOnContext;
|
||||
import net.momirealms.craftengine.core.plugin.context.CommonParameterProvider;
|
||||
import net.momirealms.craftengine.core.plugin.context.ContextHolder;
|
||||
import net.momirealms.craftengine.core.plugin.context.PlayerOptionalContext;
|
||||
import net.momirealms.craftengine.core.plugin.context.parameter.DirectContextParameters;
|
||||
import net.momirealms.craftengine.core.plugin.event.EventTrigger;
|
||||
import net.momirealms.craftengine.core.util.ClickType;
|
||||
import net.momirealms.craftengine.core.util.Cancellable;
|
||||
import net.momirealms.craftengine.core.util.Direction;
|
||||
import net.momirealms.craftengine.core.world.BlockHitResult;
|
||||
import net.momirealms.craftengine.core.world.BlockPos;
|
||||
@@ -75,6 +74,7 @@ public class ItemEventListener implements Listener {
|
||||
Object blockState = BlockStateUtils.blockDataToBlockState(blockData);
|
||||
ImmutableBlockState immutableBlockState = null;
|
||||
int stateId = BlockStateUtils.blockStateToId(blockState);
|
||||
Item<ItemStack> itemInHand = serverPlayer.getItemInHand(hand);
|
||||
|
||||
// 处理自定义方块
|
||||
if (!BlockStateUtils.isVanillaBlock(stateId)) {
|
||||
@@ -96,19 +96,25 @@ public class ItemEventListener implements Listener {
|
||||
return;
|
||||
}
|
||||
|
||||
Cancellable dummy = Cancellable.dummy();
|
||||
// run custom functions
|
||||
CustomBlock customBlock = immutableBlockState.owner().value();
|
||||
PlayerOptionalContext context = PlayerOptionalContext.of(serverPlayer, ContextHolder.builder()
|
||||
.withParameter(DirectContextParameters.BLOCK, new BukkitBlockInWorld(block))
|
||||
.withParameter(DirectContextParameters.BLOCK_STATE, immutableBlockState)
|
||||
.withParameter(DirectContextParameters.HAND, hand)
|
||||
.withParameter(DirectContextParameters.CLICK_TYPE, action.isRightClick() ? ClickType.RIGHT : ClickType.LEFT));
|
||||
customBlock.execute(context, EventTrigger.CLICK);
|
||||
.withParameter(DirectContextParameters.EVENT, dummy)
|
||||
.withParameter(DirectContextParameters.POSITION, LocationUtils.toWorldPosition(block.getLocation()))
|
||||
.withOptionalParameter(DirectContextParameters.ITEM_IN_HAND, itemInHand)
|
||||
);
|
||||
if (action.isRightClick()) customBlock.execute(context, EventTrigger.RIGHT_CLICK);
|
||||
else customBlock.execute(context, EventTrigger.LEFT_CLICK);
|
||||
if (dummy.isCancelled()) {
|
||||
event.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Item<ItemStack> itemInHand = serverPlayer.getItemInHand(hand);
|
||||
Optional<CustomItem<ItemStack>> optionalCustomItem = itemInHand == null ? Optional.empty() : itemInHand.getCustomItem();
|
||||
boolean hasItem = itemInHand != null;
|
||||
boolean hasCustomItem = optionalCustomItem.isPresent();
|
||||
@@ -157,16 +163,23 @@ public class ItemEventListener implements Listener {
|
||||
}
|
||||
}
|
||||
|
||||
// execute item functions
|
||||
// execute item right click functions
|
||||
if (hasCustomItem) {
|
||||
Cancellable dummy = Cancellable.dummy();
|
||||
PlayerOptionalContext context = PlayerOptionalContext.of(serverPlayer, ContextHolder.builder()
|
||||
.withParameter(DirectContextParameters.BLOCK, new BukkitBlockInWorld(block))
|
||||
.withOptionalParameter(DirectContextParameters.BLOCK_STATE, immutableBlockState)
|
||||
.withOptionalParameter(DirectContextParameters.ITEM_IN_HAND, itemInHand)
|
||||
.withParameter(DirectContextParameters.POSITION, LocationUtils.toWorldPosition(block.getLocation()))
|
||||
.withParameter(DirectContextParameters.HAND, hand)
|
||||
.withParameter(DirectContextParameters.CLICK_TYPE, ClickType.RIGHT));
|
||||
.withParameter(DirectContextParameters.EVENT, dummy)
|
||||
);
|
||||
CustomItem<ItemStack> customItem = optionalCustomItem.get();
|
||||
customItem.execute(context, EventTrigger.CLICK);
|
||||
customItem.execute(context, EventTrigger.RIGHT_CLICK);
|
||||
if (dummy.isCancelled()) {
|
||||
event.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 检查其他的物品行为,物品行为理论只在交互时处理
|
||||
@@ -196,14 +209,20 @@ public class ItemEventListener implements Listener {
|
||||
}
|
||||
|
||||
if (hasCustomItem && action == Action.LEFT_CLICK_BLOCK) {
|
||||
Cancellable dummy = Cancellable.dummy();
|
||||
PlayerOptionalContext context = PlayerOptionalContext.of(serverPlayer, ContextHolder.builder()
|
||||
.withParameter(DirectContextParameters.BLOCK, new BukkitBlockInWorld(block))
|
||||
.withOptionalParameter(DirectContextParameters.BLOCK_STATE, immutableBlockState)
|
||||
.withOptionalParameter(DirectContextParameters.ITEM_IN_HAND, itemInHand)
|
||||
.withParameter(DirectContextParameters.POSITION, LocationUtils.toWorldPosition(block.getLocation()))
|
||||
.withParameter(DirectContextParameters.HAND, hand)
|
||||
.withParameter(DirectContextParameters.CLICK_TYPE, ClickType.LEFT));
|
||||
);
|
||||
CustomItem<ItemStack> customItem = optionalCustomItem.get();
|
||||
customItem.execute(context, EventTrigger.CLICK);
|
||||
customItem.execute(context, EventTrigger.LEFT_CLICK);
|
||||
if (dummy.isCancelled()) {
|
||||
event.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -222,17 +241,18 @@ public class ItemEventListener implements Listener {
|
||||
// should never be null
|
||||
if (itemInHand == null) return;
|
||||
|
||||
if (cancelEventIfHasInteraction(event, serverPlayer, hand)) {
|
||||
return;
|
||||
}
|
||||
// todo 真的需要这个吗
|
||||
// if (cancelEventIfHasInteraction(event, serverPlayer, hand)) {
|
||||
// return;
|
||||
// }
|
||||
|
||||
Optional<CustomItem<ItemStack>> optionalCustomItem = itemInHand.getCustomItem();
|
||||
if (optionalCustomItem.isPresent()) {
|
||||
PlayerOptionalContext context = PlayerOptionalContext.of(serverPlayer, ContextHolder.builder()
|
||||
.withParameter(DirectContextParameters.HAND, hand)
|
||||
.withParameter(DirectContextParameters.CLICK_TYPE, action.isRightClick() ? ClickType.RIGHT : ClickType.LEFT));
|
||||
.withOptionalParameter(DirectContextParameters.ITEM_IN_HAND, itemInHand)
|
||||
);
|
||||
CustomItem<ItemStack> customItem = optionalCustomItem.get();
|
||||
customItem.execute(context, EventTrigger.CLICK);
|
||||
if (action.isRightClick()) customItem.execute(context, EventTrigger.RIGHT_CLICK);
|
||||
else customItem.execute(context, EventTrigger.LEFT_CLICK);
|
||||
}
|
||||
@@ -254,7 +274,7 @@ public class ItemEventListener implements Listener {
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOW)
|
||||
public void onConsumeItem(PlayerItemConsumeEvent event) {
|
||||
ItemStack consumedItem = event.getItem();
|
||||
if (ItemUtils.isEmpty(consumedItem)) return;
|
||||
@@ -263,12 +283,18 @@ public class ItemEventListener implements Listener {
|
||||
if (optionalCustomItem.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
Cancellable dummy = Cancellable.dummy();
|
||||
CustomItem<ItemStack> customItem = optionalCustomItem.get();
|
||||
PlayerOptionalContext context = PlayerOptionalContext.of(this.plugin.adapt(event.getPlayer()), ContextHolder.builder()
|
||||
.withParameter(DirectContextParameters.CONSUMED_ITEM, wrapped)
|
||||
.withParameter(DirectContextParameters.EVENT, dummy)
|
||||
.withParameter(DirectContextParameters.HAND, event.getHand() == EquipmentSlot.HAND ? InteractionHand.MAIN_HAND : InteractionHand.OFF_HAND)
|
||||
);
|
||||
customItem.execute(context, EventTrigger.CONSUME);
|
||||
if (dummy.isCancelled()) {
|
||||
event.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean cancelEventIfHasInteraction(PlayerInteractEvent event, BukkitServerPlayer player, InteractionHand hand) {
|
||||
|
||||
@@ -17,7 +17,6 @@ import net.momirealms.craftengine.core.plugin.context.ContextHolder;
|
||||
import net.momirealms.craftengine.core.plugin.context.parameter.DirectContextParameters;
|
||||
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
|
||||
import net.momirealms.craftengine.core.util.*;
|
||||
import net.momirealms.craftengine.core.world.Vec3d;
|
||||
import net.momirealms.craftengine.core.world.WorldPosition;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
|
||||
@@ -1695,9 +1695,8 @@ public class PacketConsumers {
|
||||
PlayerOptionalContext context = PlayerOptionalContext.of(serverPlayer, ContextHolder.builder()
|
||||
.withParameter(DirectContextParameters.FURNITURE, furniture)
|
||||
.withParameter(DirectContextParameters.POSITION, new WorldPosition(furniture.world(), furniture.position()))
|
||||
.withParameter(DirectContextParameters.CLICK_TYPE, ClickType.LEFT));
|
||||
);
|
||||
furniture.config().execute(context, EventTrigger.LEFT_CLICK);
|
||||
furniture.config().execute(context, EventTrigger.CLICK);
|
||||
furniture.config().execute(context, EventTrigger.BREAK);
|
||||
|
||||
CraftEngineFurniture.remove(furniture, serverPlayer, !serverPlayer.isCreativeMode(), true);
|
||||
@@ -1726,9 +1725,8 @@ public class PacketConsumers {
|
||||
PlayerOptionalContext context = PlayerOptionalContext.of(serverPlayer, ContextHolder.builder()
|
||||
.withParameter(DirectContextParameters.FURNITURE, furniture)
|
||||
.withParameter(DirectContextParameters.POSITION, new WorldPosition(furniture.world(), furniture.position()))
|
||||
.withParameter(DirectContextParameters.CLICK_TYPE, ClickType.RIGHT));
|
||||
furniture.config().execute(context, EventTrigger.RIGHT_CLICK);
|
||||
furniture.config().execute(context, EventTrigger.CLICK);
|
||||
);
|
||||
furniture.config().execute(context, EventTrigger.RIGHT_CLICK);;
|
||||
|
||||
if (player.isSneaking()) {
|
||||
// try placing another furniture above it
|
||||
|
||||
@@ -6,7 +6,10 @@ import net.momirealms.craftengine.bukkit.util.ItemUtils;
|
||||
import net.momirealms.craftengine.core.item.Item;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import net.momirealms.craftengine.core.util.VersionHelper;
|
||||
import net.momirealms.craftengine.core.world.*;
|
||||
import net.momirealms.craftengine.core.world.BlockInWorld;
|
||||
import net.momirealms.craftengine.core.world.Position;
|
||||
import net.momirealms.craftengine.core.world.World;
|
||||
import net.momirealms.craftengine.core.world.WorldHeight;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.SoundCategory;
|
||||
import org.bukkit.entity.EntityType;
|
||||
@@ -32,11 +35,7 @@ public class BukkitWorld implements World {
|
||||
|
||||
@Override
|
||||
public Object serverWorld() {
|
||||
try {
|
||||
return FastNMS.INSTANCE.field$CraftWorld$ServerLevel(platformWorld());
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Failed to get server world", e);
|
||||
}
|
||||
return FastNMS.INSTANCE.field$CraftWorld$ServerLevel(platformWorld());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user