From ee0f702748bcfb1e168b68676075e5eb8d58f9df Mon Sep 17 00:00:00 2001 From: XiaoMoMi Date: Mon, 17 Feb 2025 01:09:07 +0800 Subject: [PATCH] Improve API --- .../bukkit/api/CraftEngineFurniture.java | 40 ++++++++++------ .../event/AsyncResourcePackGenerateEvent.java | 3 +- .../event/CustomBlockAttemptPlaceEvent.java | 44 +++++++++++++++-- .../api/event/CustomBlockBreakEvent.java | 34 ++++++++++++-- .../api/event/CustomBlockInteractEvent.java | 31 ++++++++++-- .../api/event/CustomBlockPlaceEvent.java | 35 ++++++++++++-- .../api/event/FurnitureAttemptPlaceEvent.java | 47 ++++++++++++++++--- .../bukkit/api/event/FurnitureBreakEvent.java | 10 +++- .../api/event/FurnitureInteractEvent.java | 29 ++++++++++-- .../bukkit/api/event/FurniturePlaceEvent.java | 16 +++++-- .../bukkit/block/BlockEventListener.java | 4 +- .../bukkit/item/ItemEventListener.java | 2 + .../item/behavior/BlockItemBehavior.java | 6 ++- .../item/behavior/FurnitureItemBehavior.java | 7 ++- .../plugin/network/PacketConsumers.java | 25 ++++++++-- .../craftengine/bukkit/util/Reflections.java | 18 +++++++ gradle.properties | 2 +- 17 files changed, 295 insertions(+), 58 deletions(-) diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/CraftEngineFurniture.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/CraftEngineFurniture.java index e6a2e849b..1aed8a2b2 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/CraftEngineFurniture.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/CraftEngineFurniture.java @@ -3,7 +3,6 @@ package net.momirealms.craftengine.bukkit.api; import net.momirealms.craftengine.bukkit.entity.furniture.BukkitFurnitureManager; import net.momirealms.craftengine.bukkit.entity.furniture.LoadedFurniture; import net.momirealms.craftengine.bukkit.plugin.BukkitCraftEngine; -import net.momirealms.craftengine.bukkit.plugin.user.BukkitServerPlayer; import net.momirealms.craftengine.bukkit.util.LocationUtils; import net.momirealms.craftengine.bukkit.world.BukkitWorld; import net.momirealms.craftengine.core.entity.furniture.AnchorType; @@ -41,13 +40,13 @@ public class CraftEngineFurniture { /** * Places furniture at the certain location * + * @param location location * @param furnitureId furniture to place - * @param location location - * @param anchorType anchor type + * @param anchorType anchor type * @return the loaded furniture */ @Nullable - public static LoadedFurniture place(Key furnitureId, Location location, AnchorType anchorType) { + public static LoadedFurniture place(Location location, Key furnitureId, AnchorType anchorType) { CustomFurniture furniture = byId(furnitureId); if (furniture == null) return null; return BukkitFurnitureManager.instance().place(furniture, location, anchorType, true); @@ -56,27 +55,27 @@ public class CraftEngineFurniture { /** * Places furniture at the certain location * - * @param furniture furniture to place - * @param location location + * @param location location + * @param furniture furniture to place * @param anchorType anchor type * @return the loaded furniture */ @NotNull - public static LoadedFurniture place(CustomFurniture furniture, Location location, AnchorType anchorType) { + public static LoadedFurniture place(Location location, CustomFurniture furniture, AnchorType anchorType) { return BukkitFurnitureManager.instance().place(furniture, location, anchorType, true); } /** * Places furniture at the certain location * + * @param location location * @param furnitureId furniture to place - * @param location location - * @param anchorType anchor type - * @param playSound whether to play place sounds + * @param anchorType anchor type + * @param playSound whether to play place sounds * @return the loaded furniture */ @Nullable - public static LoadedFurniture place(Key furnitureId, Location location, AnchorType anchorType, boolean playSound) { + public static LoadedFurniture place(Location location, Key furnitureId, AnchorType anchorType, boolean playSound) { CustomFurniture furniture = byId(furnitureId); if (furniture == null) return null; return BukkitFurnitureManager.instance().place(furniture, location, anchorType, playSound); @@ -85,14 +84,14 @@ public class CraftEngineFurniture { /** * Places furniture at the certain location * - * @param furniture furniture to place - * @param location location + * @param location location + * @param furniture furniture to place * @param anchorType anchor type - * @param playSound whether to play place sounds + * @param playSound whether to play place sounds * @return the loaded furniture */ @NotNull - public static LoadedFurniture place(CustomFurniture furniture, Location location, AnchorType anchorType, boolean playSound) { + public static LoadedFurniture place(Location location, CustomFurniture furniture, AnchorType anchorType, boolean playSound) { return BukkitFurnitureManager.instance().place(furniture, location, anchorType, playSound); } @@ -118,6 +117,17 @@ public class CraftEngineFurniture { return baseEntityId != null; } + /** + * Gets the base furniture by the base entity + * + * @param baseEntity base entity + * @return the loaded furniture + */ + @Nullable + public static LoadedFurniture getLoadedFurnitureByBaseEntity(@NotNull Entity baseEntity) { + return BukkitFurnitureManager.instance().getLoadedFurnitureByBaseEntityId(baseEntity.getEntityId()); + } + /** * Gets the base furniture by the seat entity * diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/event/AsyncResourcePackGenerateEvent.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/event/AsyncResourcePackGenerateEvent.java index 0a891b2aa..efca827ba 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/event/AsyncResourcePackGenerateEvent.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/event/AsyncResourcePackGenerateEvent.java @@ -11,7 +11,8 @@ public class AsyncResourcePackGenerateEvent extends Event { private final Path generatedPackPath; private final Path zipFilePath; - public AsyncResourcePackGenerateEvent(@NotNull Path generatedPackPath, @NotNull Path zipFilePath) { + public AsyncResourcePackGenerateEvent(@NotNull Path generatedPackPath, + @NotNull Path zipFilePath) { super(true); this.generatedPackPath = generatedPackPath; this.zipFilePath = zipFilePath; diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/event/CustomBlockAttemptPlaceEvent.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/event/CustomBlockAttemptPlaceEvent.java index 762fcda8e..3a15366d0 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/event/CustomBlockAttemptPlaceEvent.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/event/CustomBlockAttemptPlaceEvent.java @@ -2,7 +2,10 @@ package net.momirealms.craftengine.bukkit.api.event; import net.momirealms.craftengine.core.block.CustomBlock; import net.momirealms.craftengine.core.block.ImmutableBlockState; +import net.momirealms.craftengine.core.entity.player.InteractionHand; import org.bukkit.Location; +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; import org.bukkit.entity.Player; import org.bukkit.event.Cancellable; import org.bukkit.event.HandlerList; @@ -12,20 +15,51 @@ import org.jetbrains.annotations.NotNull; public class CustomBlockAttemptPlaceEvent extends PlayerEvent implements Cancellable { private static final HandlerList HANDLER_LIST = new HandlerList(); private boolean cancelled; - private final CustomBlock block; + private final CustomBlock customBlock; private final ImmutableBlockState state; private final Location location; + private final BlockFace clickedFace; + private final Block clickedBlock; + private final InteractionHand hand; - public CustomBlockAttemptPlaceEvent(@NotNull Player player, @NotNull Location location, @NotNull ImmutableBlockState state) { + public CustomBlockAttemptPlaceEvent(@NotNull Player player, + @NotNull Location location, + @NotNull ImmutableBlockState state, + @NotNull BlockFace clickedFace, + @NotNull Block clickedBlock, + @NotNull InteractionHand hand) { super(player); - this.block = state.owner().value(); + this.customBlock = state.owner().value(); this.state = state; this.location = location; + this.clickedFace = clickedFace; + this.clickedBlock = clickedBlock; + this.hand = hand; } @NotNull - public CustomBlock block() { - return this.block; + public Player player() { + return getPlayer(); + } + + @NotNull + public InteractionHand hand() { + return hand; + } + + @NotNull + public Block clickedBlock() { + return clickedBlock; + } + + @NotNull + public BlockFace clickedFace() { + return clickedFace; + } + + @NotNull + public CustomBlock customBlock() { + return this.customBlock; } @NotNull diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/event/CustomBlockBreakEvent.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/event/CustomBlockBreakEvent.java index 591fb078c..6d23290eb 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/event/CustomBlockBreakEvent.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/event/CustomBlockBreakEvent.java @@ -3,6 +3,7 @@ package net.momirealms.craftengine.bukkit.api.event; import net.momirealms.craftengine.core.block.CustomBlock; import net.momirealms.craftengine.core.block.ImmutableBlockState; import org.bukkit.Location; +import org.bukkit.block.Block; import org.bukkit.entity.Player; import org.bukkit.event.Cancellable; import org.bukkit.event.HandlerList; @@ -12,20 +13,45 @@ import org.jetbrains.annotations.NotNull; public class CustomBlockBreakEvent extends PlayerEvent implements Cancellable { private static final HandlerList HANDLER_LIST = new HandlerList(); private boolean cancelled; - private final CustomBlock block; + private final CustomBlock customBlock; private final ImmutableBlockState state; private final Location location; + private final Block bukkitBlock; + private boolean dropItems; - public CustomBlockBreakEvent(@NotNull Player player, @NotNull Location location, @NotNull ImmutableBlockState state) { + public CustomBlockBreakEvent(@NotNull Player player, + @NotNull Location location, + @NotNull Block bukkitBlock, + @NotNull ImmutableBlockState state) { super(player); - this.block = state.owner().value(); + this.customBlock = state.owner().value(); this.state = state; + this.bukkitBlock = bukkitBlock; this.location = location; + this.dropItems = true; + } + + public boolean dropItems() { + return dropItems; + } + + public void setDropItems(boolean dropItems) { + this.dropItems = dropItems; + } + + @NotNull + public Block bukkitBlock() { + return bukkitBlock; + } + + @NotNull + public Player player() { + return getPlayer(); } @NotNull public CustomBlock block() { - return this.block; + return this.customBlock; } @NotNull diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/event/CustomBlockInteractEvent.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/event/CustomBlockInteractEvent.java index 8cf66634f..6c7407690 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/event/CustomBlockInteractEvent.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/event/CustomBlockInteractEvent.java @@ -4,6 +4,8 @@ import net.momirealms.craftengine.core.block.CustomBlock; import net.momirealms.craftengine.core.block.ImmutableBlockState; import net.momirealms.craftengine.core.entity.player.InteractionHand; import org.bukkit.Location; +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; import org.bukkit.entity.Player; import org.bukkit.event.Cancellable; import org.bukkit.event.HandlerList; @@ -14,26 +16,37 @@ import org.jetbrains.annotations.Nullable; public class CustomBlockInteractEvent extends PlayerEvent implements Cancellable { private static final HandlerList HANDLER_LIST = new HandlerList(); private boolean cancelled; - private final CustomBlock block; + private final CustomBlock customBlock; + private final Block bukkitBlock; private final ImmutableBlockState state; private final Location location; private final Location interactionPoint; private final InteractionHand hand; private final Action action; + private final BlockFace clickedFace; public CustomBlockInteractEvent(@NotNull Player player, @NotNull Location location, @Nullable Location interactionPoint, @NotNull ImmutableBlockState state, + @NotNull Block bukkitBlock, + @NotNull BlockFace clickedFace, @NotNull InteractionHand hand, @NotNull Action action) { super(player); - this.block = state.owner().value(); + this.customBlock = state.owner().value(); + this.bukkitBlock = bukkitBlock; this.state = state; this.location = location; this.interactionPoint = interactionPoint; this.hand = hand; this.action = action; + this.clickedFace = clickedFace; + } + + @NotNull + public BlockFace clickedFace() { + return clickedFace; } @Nullable @@ -52,8 +65,18 @@ public class CustomBlockInteractEvent extends PlayerEvent implements Cancellable } @NotNull - public CustomBlock block() { - return this.block; + public Block bukkitBlock() { + return bukkitBlock; + } + + @NotNull + public CustomBlock customBlock() { + return this.customBlock; + } + + @NotNull + public Player player() { + return getPlayer(); } @NotNull diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/event/CustomBlockPlaceEvent.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/event/CustomBlockPlaceEvent.java index 1c8619e91..c154b9a2c 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/event/CustomBlockPlaceEvent.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/event/CustomBlockPlaceEvent.java @@ -2,7 +2,9 @@ package net.momirealms.craftengine.bukkit.api.event; import net.momirealms.craftengine.core.block.CustomBlock; import net.momirealms.craftengine.core.block.ImmutableBlockState; +import net.momirealms.craftengine.core.entity.player.InteractionHand; import org.bukkit.Location; +import org.bukkit.block.Block; import org.bukkit.entity.Player; import org.bukkit.event.HandlerList; import org.bukkit.event.player.PlayerEvent; @@ -10,20 +12,38 @@ import org.jetbrains.annotations.NotNull; public class CustomBlockPlaceEvent extends PlayerEvent { private static final HandlerList HANDLER_LIST = new HandlerList(); - private final CustomBlock block; + private final CustomBlock customBlock; private final ImmutableBlockState state; private final Location location; + private final InteractionHand hand; + private final Block bukkitBlock; - public CustomBlockPlaceEvent(@NotNull Player player, @NotNull Location location, @NotNull ImmutableBlockState state) { + public CustomBlockPlaceEvent(@NotNull Player player, + @NotNull Location location, + @NotNull ImmutableBlockState state, + @NotNull Block bukkitBlock, + @NotNull InteractionHand hand) { super(player); - this.block = state.owner().value(); + this.customBlock = state.owner().value(); this.state = state; this.location = location; + this.hand = hand; + this.bukkitBlock = bukkitBlock; } @NotNull - public CustomBlock block() { - return this.block; + public Block bukkitBlock() { + return bukkitBlock; + } + + @NotNull + public InteractionHand hand() { + return hand; + } + + @NotNull + public CustomBlock customBlock() { + return this.customBlock; } @NotNull @@ -36,6 +56,11 @@ public class CustomBlockPlaceEvent extends PlayerEvent { return this.state; } + @NotNull + public Player player() { + return getPlayer(); + } + @NotNull public static HandlerList getHandlerList() { return HANDLER_LIST; diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/event/FurnitureAttemptPlaceEvent.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/event/FurnitureAttemptPlaceEvent.java index 3bac60742..dffa1872c 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/event/FurnitureAttemptPlaceEvent.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/event/FurnitureAttemptPlaceEvent.java @@ -1,8 +1,11 @@ package net.momirealms.craftengine.bukkit.api.event; -import net.momirealms.craftengine.bukkit.item.behavior.FurnitureItemBehavior; import net.momirealms.craftengine.core.entity.furniture.AnchorType; +import net.momirealms.craftengine.core.entity.furniture.CustomFurniture; +import net.momirealms.craftengine.core.entity.player.InteractionHand; import org.bukkit.Location; +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; import org.bukkit.entity.Player; import org.bukkit.event.Cancellable; import org.bukkit.event.HandlerList; @@ -12,15 +15,47 @@ import org.jetbrains.annotations.NotNull; public class FurnitureAttemptPlaceEvent extends PlayerEvent implements Cancellable { private static final HandlerList HANDLER_LIST = new HandlerList(); private boolean cancelled; - private final FurnitureItemBehavior behavior; + private final CustomFurniture furniture; private final Location location; private final AnchorType anchorType; + private final BlockFace clickedFace; + private final Block clickedBlock; + private final InteractionHand hand; - public FurnitureAttemptPlaceEvent(@NotNull Player player, @NotNull FurnitureItemBehavior behavior, @NotNull AnchorType anchorType, @NotNull Location location) { + public FurnitureAttemptPlaceEvent(@NotNull Player player, + @NotNull CustomFurniture furniture, + @NotNull AnchorType anchorType, + @NotNull Location location, + @NotNull BlockFace clickedFace, + @NotNull InteractionHand hand, + @NotNull Block clickedBlock) { super(player); - this.behavior = behavior; + this.furniture = furniture; this.location = location; this.anchorType = anchorType; + this.clickedFace = clickedFace; + this.clickedBlock = clickedBlock; + this.hand = hand; + } + + @NotNull + public Block clickedBlock() { + return clickedBlock; + } + + @NotNull + public InteractionHand hand() { + return hand; + } + + @NotNull + public BlockFace clickedFace() { + return clickedFace; + } + + @NotNull + public Player player() { + return getPlayer(); } @NotNull @@ -34,8 +69,8 @@ public class FurnitureAttemptPlaceEvent extends PlayerEvent implements Cancellab } @NotNull - public FurnitureItemBehavior behavior() { - return behavior; + public CustomFurniture furniture() { + return furniture; } @NotNull diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/event/FurnitureBreakEvent.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/event/FurnitureBreakEvent.java index f25480192..82cf7db71 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/event/FurnitureBreakEvent.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/event/FurnitureBreakEvent.java @@ -13,12 +13,18 @@ public class FurnitureBreakEvent extends PlayerEvent implements Cancellable { private boolean cancelled; private final LoadedFurniture furniture; - public FurnitureBreakEvent(@NotNull Player player, @Nullable LoadedFurniture furniture) { + public FurnitureBreakEvent(@NotNull Player player, + @NotNull LoadedFurniture furniture) { super(player); this.furniture = furniture; } - @Nullable + @NotNull + public Player player() { + return getPlayer(); + } + + @NotNull public LoadedFurniture furniture() { return this.furniture; } diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/event/FurnitureInteractEvent.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/event/FurnitureInteractEvent.java index b83900ff5..10b553242 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/event/FurnitureInteractEvent.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/event/FurnitureInteractEvent.java @@ -1,24 +1,47 @@ package net.momirealms.craftengine.bukkit.api.event; import net.momirealms.craftengine.bukkit.entity.furniture.LoadedFurniture; +import net.momirealms.craftengine.core.entity.player.InteractionHand; +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; -import org.jetbrains.annotations.Nullable; public class FurnitureInteractEvent extends PlayerEvent implements Cancellable { private static final HandlerList HANDLER_LIST = new HandlerList(); private boolean cancelled; private final LoadedFurniture furniture; + private final InteractionHand hand; + private final Location interactionPoint; - public FurnitureInteractEvent(@NotNull Player player, @Nullable LoadedFurniture furniture) { + public FurnitureInteractEvent(@NotNull Player player, + @NotNull LoadedFurniture furniture, + @NotNull InteractionHand hand, + @NotNull Location interactionPoint) { super(player); this.furniture = furniture; + this.hand = hand; + this.interactionPoint = interactionPoint; } - @Nullable + @NotNull + public Location interactionPoint() { + return interactionPoint; + } + + @NotNull + public Player player() { + return getPlayer(); + } + + @NotNull + public InteractionHand hand() { + return hand; + } + + @NotNull public LoadedFurniture furniture() { return this.furniture; } diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/event/FurniturePlaceEvent.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/event/FurniturePlaceEvent.java index 8191c9a09..0c016cdab 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/event/FurniturePlaceEvent.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/event/FurniturePlaceEvent.java @@ -1,6 +1,7 @@ package net.momirealms.craftengine.bukkit.api.event; import net.momirealms.craftengine.bukkit.entity.furniture.LoadedFurniture; +import net.momirealms.craftengine.core.entity.player.InteractionHand; import org.bukkit.Location; import org.bukkit.entity.Player; import org.bukkit.event.HandlerList; @@ -11,12 +12,21 @@ public class FurniturePlaceEvent extends PlayerEvent { private static final HandlerList HANDLER_LIST = new HandlerList(); private final Location location; private final LoadedFurniture furniture; + private final InteractionHand hand; - public FurniturePlaceEvent(@NotNull Player player, @NotNull LoadedFurniture furniture, @NotNull Location location) { + public FurniturePlaceEvent(@NotNull Player player, + @NotNull LoadedFurniture furniture, + @NotNull Location location, + @NotNull InteractionHand hand) { super(player); - this.player = player; this.location = location; this.furniture = furniture; + this.hand = hand; + } + + @NotNull + public InteractionHand hand() { + return hand; } @NotNull @@ -31,7 +41,7 @@ public class FurniturePlaceEvent extends PlayerEvent { @NotNull public Player player() { - return this.player; + return getPlayer(); } @NotNull diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/BlockEventListener.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/BlockEventListener.java index 6221827a6..091fb551d 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/BlockEventListener.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/BlockEventListener.java @@ -103,7 +103,7 @@ public class BlockEventListener implements Listener { Location location = block.getLocation(); // trigger event - CustomBlockBreakEvent customBreakEvent = new CustomBlockBreakEvent(event.getPlayer(), location, state); + CustomBlockBreakEvent customBreakEvent = new CustomBlockBreakEvent(event.getPlayer(), location, block, state); boolean isCancelled = EventUtils.fireAndCheckCancel(customBreakEvent); if (isCancelled) { event.setCancelled(true); @@ -133,7 +133,7 @@ public class BlockEventListener implements Listener { Item itemInHand = serverPlayer.getItemInHand(InteractionHand.MAIN_HAND); 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)) { + if (!state.settings().isCorrectTool(itemId) || !customBreakEvent.dropItems()) { return; } // drop items diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/item/ItemEventListener.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/item/ItemEventListener.java index 0efe4427e..42af1dd76 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/item/ItemEventListener.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/item/ItemEventListener.java @@ -67,6 +67,8 @@ public class ItemEventListener implements Listener { block.getLocation(), event.getInteractionPoint(), BukkitBlockManager.instance().getImmutableBlockStateUnsafe(stateId), + block, + event.getBlockFace(), event.getHand() == EquipmentSlot.HAND ? InteractionHand.MAIN_HAND : InteractionHand.OFF_HAND, action == Action.RIGHT_CLICK_BLOCK ? CustomBlockInteractEvent.Action.RIGHT_CLICK : CustomBlockInteractEvent.Action.LEFT_CLICK ); diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/item/behavior/BlockItemBehavior.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/item/behavior/BlockItemBehavior.java index 387447e3c..1f2beb89a 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/item/behavior/BlockItemBehavior.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/item/behavior/BlockItemBehavior.java @@ -4,6 +4,7 @@ import net.momirealms.craftengine.bukkit.api.CraftEngineBlocks; import net.momirealms.craftengine.bukkit.api.event.CustomBlockAttemptPlaceEvent; import net.momirealms.craftengine.bukkit.api.event.CustomBlockPlaceEvent; import net.momirealms.craftengine.bukkit.block.BukkitBlockManager; +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.bukkit.util.Reflections; @@ -75,7 +76,8 @@ public class BlockItemBehavior extends ItemBehavior { Location placeLocation = new Location(world, pos.x(), pos.y(), pos.z()); // trigger event - CustomBlockAttemptPlaceEvent attemptPlaceEvent = new CustomBlockAttemptPlaceEvent((org.bukkit.entity.Player) player.platformPlayer(), placeLocation.clone(), blockStateToPlace); + CustomBlockAttemptPlaceEvent attemptPlaceEvent = new CustomBlockAttemptPlaceEvent((org.bukkit.entity.Player) player.platformPlayer(), placeLocation.clone(), blockStateToPlace, + DirectionUtils.toBlockFace(context.getClickedFace()), world.getBlockAt(context.getClickedPos().x(), context.getClickedPos().y(), context.getClickedPos().z()), context.getHand()); if (EventUtils.fireAndCheckCancel(attemptPlaceEvent)) { return InteractionResult.FAIL; } @@ -85,7 +87,7 @@ public class BlockItemBehavior extends ItemBehavior { // TODO Make place event cancellable. Needs to get the previous block state from #0 // TODO Add Bukkit block argument - CustomBlockPlaceEvent placeEvent = new CustomBlockPlaceEvent((org.bukkit.entity.Player) player.platformPlayer(), placeLocation.clone(), blockStateToPlace); + CustomBlockPlaceEvent placeEvent = new CustomBlockPlaceEvent((org.bukkit.entity.Player) player.platformPlayer(), placeLocation.clone(), blockStateToPlace, world.getBlockAt(placeLocation), context.getHand()); EventUtils.fireAndForget(placeEvent); if (!player.isCreativeMode()) { diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/item/behavior/FurnitureItemBehavior.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/item/behavior/FurnitureItemBehavior.java index 4a5056605..4135aa75a 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/item/behavior/FurnitureItemBehavior.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/item/behavior/FurnitureItemBehavior.java @@ -4,6 +4,7 @@ import net.momirealms.craftengine.bukkit.api.event.FurnitureAttemptPlaceEvent; import net.momirealms.craftengine.bukkit.api.event.FurniturePlaceEvent; import net.momirealms.craftengine.bukkit.entity.furniture.BukkitFurnitureManager; import net.momirealms.craftengine.bukkit.entity.furniture.LoadedFurniture; +import net.momirealms.craftengine.bukkit.util.DirectionUtils; import net.momirealms.craftengine.bukkit.util.EntityUtils; import net.momirealms.craftengine.bukkit.util.EventUtils; import net.momirealms.craftengine.core.block.BlockSounds; @@ -80,6 +81,7 @@ public class FurnitureItemBehavior extends ItemBehavior { } Vec3d clickedPosition = context.getClickLocation(); + // trigger event org.bukkit.entity.Player bukkitPlayer = (org.bukkit.entity.Player) player.platformPlayer(); World world = (World) context.getLevel().getHandle(); @@ -103,7 +105,8 @@ public class FurnitureItemBehavior extends ItemBehavior { } Location furnitureLocation = new Location(world, finalPlacePosition.x(), finalPlacePosition.y(), finalPlacePosition.z(), (float) furnitureYaw, 0); - FurnitureAttemptPlaceEvent attemptPlaceEvent = new FurnitureAttemptPlaceEvent(bukkitPlayer, this, anchorType,furnitureLocation.clone()); + FurnitureAttemptPlaceEvent attemptPlaceEvent = new FurnitureAttemptPlaceEvent(bukkitPlayer, customFurniture, anchorType, furnitureLocation.clone(), + DirectionUtils.toBlockFace(clickedFace), context.getHand(), world.getBlockAt(context.getClickedPos().x(), context.getClickedPos().y(), context.getClickedPos().z())); if (EventUtils.fireAndCheckCancel(attemptPlaceEvent)) { return InteractionResult.FAIL; } @@ -116,7 +119,7 @@ public class FurnitureItemBehavior extends ItemBehavior { } player.swingHand(context.getHand()); - FurniturePlaceEvent placeEvent = new FurniturePlaceEvent(bukkitPlayer, loadedFurniture, furnitureLocation); + FurniturePlaceEvent placeEvent = new FurniturePlaceEvent(bukkitPlayer, loadedFurniture, furnitureLocation, context.getHand()); EventUtils.fireAndForget(placeEvent); return InteractionResult.SUCCESS; } diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/network/PacketConsumers.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/network/PacketConsumers.java index 0cea5f540..07fa075fc 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/network/PacketConsumers.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/network/PacketConsumers.java @@ -13,6 +13,7 @@ import net.momirealms.craftengine.bukkit.plugin.BukkitCraftEngine; import net.momirealms.craftengine.bukkit.plugin.user.BukkitServerPlayer; import net.momirealms.craftengine.bukkit.util.*; import net.momirealms.craftengine.core.block.ImmutableBlockState; +import net.momirealms.craftengine.core.entity.player.InteractionHand; import net.momirealms.craftengine.core.plugin.CraftEngine; import net.momirealms.craftengine.core.plugin.config.ConfigManager; import net.momirealms.craftengine.core.plugin.network.ConnectionState; @@ -28,6 +29,7 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; import org.bukkit.util.RayTraceResult; +import org.jetbrains.annotations.NotNull; import java.lang.reflect.Method; import java.util.Arrays; @@ -563,13 +565,25 @@ public class PacketConsumers { CraftEngineFurniture.remove(furniture, serverPlayer, !serverPlayer.isCreativeMode(), true); } } else if (actionType == Reflections.instance$ServerboundInteractPacket$ActionType$INTERACT_AT) { - FurnitureInteractEvent interactEvent = new FurnitureInteractEvent(serverPlayer.platformPlayer(), furniture); + InteractionHand hand; + Location interactionPoint; + try { + Object interactionHand = Reflections.field$ServerboundInteractPacket$InteractionAtLocationAction$hand.get(action); + hand = interactionHand == Reflections.instance$InteractionHand$MAIN_HAND ? InteractionHand.MAIN_HAND : InteractionHand.OFF_HAND; + Object vec3 = Reflections.field$ServerboundInteractPacket$InteractionAtLocationAction$location.get(action); + double x = (double) Reflections.field$Vec3$x.get(vec3); + double y = (double) Reflections.field$Vec3$y.get(vec3); + double z = (double) Reflections.field$Vec3$z.get(vec3); + interactionPoint = new Location(location.getWorld(), x, y, z); + } catch (ReflectiveOperationException e) { + throw new RuntimeException("Failed to get interaction hand from interact packet", e); + } + FurnitureInteractEvent interactEvent = new FurnitureInteractEvent(serverPlayer.platformPlayer(), furniture, hand, interactionPoint); if (EventUtils.fireAndCheckCancel(interactEvent)) { return; } - if (player.isSneaking()) { + if (player.isSneaking()) return; - } furniture.getAvailableSeat(entityId).ifPresent(seatPos -> { if (furniture.occupySeat(seatPos)) { furniture.mountSeat(Objects.requireNonNull(player.getPlayer()), seatPos); @@ -582,6 +596,11 @@ public class PacketConsumers { } }; + private static @NotNull InteractionHand getInteractionHand(Object action) { + + return hand; + } + public static final TriConsumer SOUND = (user, event, packet) -> { try { Object sound = Reflections.field$ClientboundSoundPacket$sound.get(packet); diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/util/Reflections.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/util/Reflections.java index 11bbdca80..f621dc426 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/util/Reflections.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/util/Reflections.java @@ -571,6 +571,24 @@ public class Reflections { ) ); + public static final Field field$Vec3$x = requireNonNull( + ReflectionUtils.getInstanceDeclaredField( + clazz$Vec3, double.class, 0 + ) + ); + + public static final Field field$Vec3$y = requireNonNull( + ReflectionUtils.getInstanceDeclaredField( + clazz$Vec3, double.class, 1 + ) + ); + + public static final Field field$Vec3$z = requireNonNull( + ReflectionUtils.getInstanceDeclaredField( + clazz$Vec3, double.class, 2 + ) + ); + public static final Constructor constructor$Vec3 = requireNonNull( ReflectionUtils.getConstructor( clazz$Vec3, double.class, double.class, double.class diff --git a/gradle.properties b/gradle.properties index a311e923e..e2934f3fd 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ # Project settings # Rule: [major update].[feature update].[bug fix] -project_version=0.0.13 +project_version=0.0.15 config_version=1 project_group=net.momirealms legacy_model_templates_version=1.21.3