Skip to content

Commit b96b415

Browse files
committed
Change use entity event and fix gui fixes
1 parent 870cbe2 commit b96b415

5 files changed

Lines changed: 17 additions & 38 deletions

File tree

src/main/java/io/icker/factions/api/events/PlayerEvents.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,6 @@
1818

1919
/** Events related to player actions */
2020
public class PlayerEvents {
21-
/** Called when a player tries to interact with an entity */
22-
public static final Event<UseEntity> USE_ENTITY =
23-
EventFactory.createArrayBacked(
24-
UseEntity.class,
25-
callbacks ->
26-
(source, target, world) -> {
27-
for (UseEntity callback : callbacks) {
28-
InteractionResult result =
29-
callback.onUseEntity(source, target, world);
30-
if (result != InteractionResult.PASS) {
31-
return result;
32-
}
33-
}
34-
return InteractionResult.PASS;
35-
});
36-
3721
public static final Event<PlaceBlock> PLACE_BLOCK =
3822
EventFactory.createArrayBacked(
3923
PlaceBlock.class,

src/main/java/io/icker/factions/core/InteractionManager.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import net.fabricmc.fabric.api.event.player.AttackEntityCallback;
1313
import net.fabricmc.fabric.api.event.player.PlayerBlockBreakEvents;
1414
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
15+
import net.fabricmc.fabric.api.event.player.UseEntityCallback;
1516
import net.fabricmc.fabric.api.event.player.UseItemCallback;
1617
import net.minecraft.core.BlockPos;
1718
import net.minecraft.server.level.ServerPlayer;
@@ -37,6 +38,8 @@
3738
import net.minecraft.world.phys.BlockHitResult;
3839
import net.minecraft.world.phys.EntityHitResult;
3940

41+
import org.jspecify.annotations.Nullable;
42+
4043
public class InteractionManager {
4144
public static void register() {
4245
PlayerBlockBreakEvents.BEFORE.register(InteractionManager::onBreakBlock);
@@ -45,8 +48,8 @@ public static void register() {
4548
UseBlockCallback.EVENT.register(InteractionManager::onUseBlock);
4649
UseItemCallback.EVENT.register(InteractionManager::onUseBucket);
4750
AttackEntityCallback.EVENT.register(InteractionManager::onAttackEntity);
51+
UseEntityCallback.EVENT.register(InteractionManager::onUseEntity);
4852
PlayerEvents.IS_INVULNERABLE.register(InteractionManager::isInvulnerableTo);
49-
PlayerEvents.USE_ENTITY.register(InteractionManager::onUseEntity);
5053
PlayerEvents.USE_INVENTORY.register(InteractionManager::onUseInventory);
5154
PlayerEvents.PLACE_BLOCK.register(InteractionManager::onPlaceBlock);
5255
}
@@ -199,7 +202,8 @@ private static InteractionResult onUseBucket(Player player, Level world, Interac
199202
net.minecraft.world.level.ClipContext.Fluid handling =
200203
fluid == Fluids.EMPTY ? ClipContext.Fluid.SOURCE_ONLY : ClipContext.Fluid.NONE;
201204

202-
BlockHitResult raycastResult = ItemInvoker.getPlayerPOVHitResult(world, player, handling);
205+
BlockHitResult raycastResult =
206+
ItemInvoker.getPlayerPOVHitResult(world, player, handling);
203207

204208
if (raycastResult.getType() != BlockHitResult.Type.MISS) {
205209
BlockPos raycastPos = raycastResult.getBlockPos();
@@ -242,15 +246,20 @@ && checkPermissions(
242246
return InteractionResult.PASS;
243247
}
244248

245-
private static InteractionResult onUseEntity(Player player, Entity entity, Level world) {
249+
private static InteractionResult onUseEntity(
250+
Player player,
251+
Level level,
252+
InteractionHand hand,
253+
Entity entity,
254+
@Nullable EntityHitResult hitResult) {
246255
BlockPos pos;
247256
if (entity == null) {
248257
pos = player.blockPosition();
249258
} else {
250259
pos = entity.blockPosition();
251260
}
252261

253-
if (checkPermissions(player, pos, world, Permissions.USE_ENTITIES)
262+
if (checkPermissions(player, pos, level, Permissions.USE_ENTITIES)
254263
== InteractionResult.FAIL) {
255264
InteractionsUtil.warn((ServerPlayer) player, InteractionsUtilActions.USE_ENTITIES);
256265
return InteractionResult.FAIL;

src/main/java/io/icker/factions/mixin/ItemInvoker.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
@Mixin(Item.class)
1313
public interface ItemInvoker {
1414
@Invoker("getPlayerPOVHitResult")
15-
static BlockHitResult getPlayerPOVHitResult(Level world, Player player, ClipContext.Fluid fluidHandling) {
15+
static BlockHitResult getPlayerPOVHitResult(
16+
Level world, Player player, ClipContext.Fluid fluidHandling) {
1617
throw new AssertionError();
1718
}
1819
}

src/main/java/io/icker/factions/mixin/ServerGamePacketListenerImplMixin.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,9 @@
77

88
import net.minecraft.network.chat.Component;
99
import net.minecraft.network.chat.PlayerChatMessage;
10-
import net.minecraft.network.protocol.game.ServerboundInteractPacket;
1110
import net.minecraft.network.protocol.game.ServerboundMovePlayerPacket;
1211
import net.minecraft.server.level.ServerPlayer;
1312
import net.minecraft.server.network.ServerGamePacketListenerImpl;
14-
import net.minecraft.world.InteractionResult;
15-
import net.minecraft.world.entity.Entity;
16-
import net.minecraft.world.level.Level;
1713

1814
import org.spongepowered.asm.mixin.Mixin;
1915
import org.spongepowered.asm.mixin.Shadow;
@@ -53,16 +49,4 @@ public void broadcastChatMessage(PlayerChatMessage signedMessage, CallbackInfo c
5349
ci.cancel();
5450
}
5551
}
56-
57-
@Inject(method = "handleInteract", at = @At("HEAD"), cancellable = true)
58-
public void handleInteract(ServerboundInteractPacket packet, CallbackInfo ci) {
59-
Level world = player.level();
60-
Entity entity = world.getEntity(packet.entityId());
61-
if (entity == null) return;
62-
63-
if (PlayerEvents.USE_ENTITY.invoker().onUseEntity(player, entity, world)
64-
== InteractionResult.FAIL) {
65-
ci.cancel();
66-
}
67-
}
6852
}

src/main/java/io/icker/factions/ui/PagedGui.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import net.minecraft.sounds.SoundEvents;
1414
import net.minecraft.world.inventory.MenuType;
1515
import net.minecraft.world.inventory.Slot;
16+
import net.minecraft.world.item.ItemStack;
1617
import net.minecraft.world.item.Items;
1718

1819
import org.jetbrains.annotations.ApiStatus;
@@ -126,7 +127,7 @@ protected DisplayElement getNavElement(int id) {
126127

127128
public record DisplayElement(@Nullable GuiElement element, @Nullable Slot slot) {
128129
private static final DisplayElement EMPTY =
129-
DisplayElement.of(new GuiElementBuilder().build());
130+
DisplayElement.of(GuiElementBuilder.from(ItemStack.EMPTY).build());
130131
private static final DisplayElement FILLER =
131132
DisplayElement.of(
132133
new GuiElementBuilder(Items.WHITE_STAINED_GLASS_PANE)

0 commit comments

Comments
 (0)