Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v5
uses: actions/checkout@v7

- name: Setup java
uses: actions/setup-java@v5
Expand All @@ -27,18 +27,26 @@ jobs:
distribution: 'zulu'

- name: Setup Gradle
uses: gradle/actions/setup-gradle@v5
uses: gradle/actions/setup-gradle@v6
with:
add-job-summary: always
cache-provider: enhanced
cache-cleanup: on-success
cache-read-only: ${{ github.ref != 'refs/heads/v3' }}
cache-encryption-key: ${{ secrets.GRADLE_ENCRYPTION_KEY }}
# automatically publishes build scans on main branch
build-scan-publish: ${{ github.ref == 'refs/heads/v3' }}
build-scan-terms-of-use-agree: yes
build-scan-terms-of-use-url: 'https://gradle.com/terms-of-service'

- name: Setup Loom cache
uses: actions/cache@v4
uses: actions/cache@v6
with:
path: '.gradle/loom-cache'
path: |
.gradle/loom-cache
fabric/.gradle/loom-cache
restore-keys: ${{ runner.os }}-gradle-loom-
key: ${{ runner.os }}-gradle-loom-${{ hashFiles('gradle/libs.versions.toml', 'gradle/wrapper/gradle-wrapper.properties') }}
key: ${{ runner.os }}-gradle-loom-${{ hashFiles('gradle/libs.versions.toml') }}

- name: Execute build
run: ./gradlew clean build --stacktrace
10 changes: 0 additions & 10 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,17 @@ public interface PlatformVersionAccessor {

int patch();

boolean atLeast(int major, int minor, int patch);
default boolean atLeast(int major, int minor, int patch) {
int majorCompare = Integer.compare(this.major(), major);
if (majorCompare != 0) {
return majorCompare > 0;
}

int minorCompare = Integer.compare(this.minor(), minor);
if (minorCompare != 0) {
return minorCompare > 0;
}

return this.patch() >= patch;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,4 @@ public int minor() {
public int patch() {
return this.patch;
}

@Override
public boolean atLeast(int major, int minor, int patch) {
if (this.major != major) {
return this.major > major;
}

if (this.minor != minor) {
return this.minor > minor;
}

return this.patch >= patch;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import com.github.retrooper.packetevents.util.TimeStampMode;
import com.github.retrooper.packetevents.util.adventure.AdventureSerializer;
import com.github.retrooper.packetevents.wrapper.PacketWrapper;
import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientAttack;
import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientInteractEntity;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerDestroyEntities;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEntityAnimation;
Expand Down Expand Up @@ -382,31 +383,49 @@ public NpcUsePacketAdapter(@NotNull Platform<World, Player, ItemStack, Plugin> p

@Override
public void onPacketPlayReceive(@NotNull PacketPlayReceiveEvent event) {
// check for an entity use packet
Object player = event.getPlayer();
if (event.getPacketType() == PacketType.Play.Client.INTERACT_ENTITY) {

int entityId;
InteractNpcEvent.Hand hand;
WrapperPlayClientInteractEntity.InteractAction action;

if (event.getPacketType() == PacketType.Play.Client.ATTACK) {
WrapperPlayClientAttack packet = new WrapperPlayClientAttack(event);
entityId = packet.getEntityId();
hand = InteractNpcEvent.Hand.MAIN_HAND;
action = WrapperPlayClientInteractEntity.InteractAction.ATTACK;
} else if (event.getPacketType() == PacketType.Play.Client.INTERACT_ENTITY) {
WrapperPlayClientInteractEntity packet = new WrapperPlayClientInteractEntity(event);
entityId = packet.getEntityId();
hand = Lazy.HAND_CONVERTER.get(packet.getHand());
action = packet.getAction();

// special case for 26.1+: attack is now a separate packet, so this has to be interact
if (event.getServerVersion().isNewerThanOrEquals(ServerVersion.V_26_1)) {
action = WrapperPlayClientInteractEntity.InteractAction.INTERACT;
}
} else {
return;
}

// get the associated npc from the tracked entities
Npc<World, Player, ItemStack, Plugin> npc = this.platform.npcTracker().npcById(packet.getEntityId());
if (npc != null) {
// call the event
switch (packet.getAction()) {
case ATTACK:
this.platform.eventManager().post(DefaultAttackNpcEvent.attackNpc(npc, player));
break;
case INTERACT:
InteractNpcEvent.Hand hand = Lazy.HAND_CONVERTER.get(packet.getHand());
this.platform.eventManager().post(DefaultInteractNpcEvent.interactNpc(npc, player, hand));
break;
default:
// we don't handle INTERACT_AT as the client sends it alongside the interact packet (duplicate event call)
break;
}

// don't pass the packet to the server
event.setCancelled(true);
// get the associated npc from the tracked entities
Npc<World, Player, ItemStack, Plugin> npc = this.platform.npcTracker().npcById(entityId);
if (npc != null) {
// call the event
switch (action) {
case ATTACK:
this.platform.eventManager().post(DefaultAttackNpcEvent.attackNpc(npc, player));
break;
case INTERACT:
this.platform.eventManager().post(DefaultInteractNpcEvent.interactNpc(npc, player, hand));
break;
default:
// we don't handle INTERACT_AT as the client sends it alongside the interact packet
break;
}

// don't pass the packet to the server
event.setCancelled(true);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,10 @@ private static final class NpcUsePacketAdapter extends PacketAdapter {
private final Platform<World, Player, ItemStack, Plugin> platform;

public NpcUsePacketAdapter(@NotNull Platform<World, Player, ItemStack, Plugin> platform) {
super(PacketAdapter.params(platform.extension(), PacketType.Play.Client.USE_ENTITY).optionAsync());
super(PacketAdapter.params(
platform.extension(),
PacketType.Play.Client.USE_ENTITY,
PacketType.Play.Client.ATTACK).optionAsync());
this.platform = platform;
}

Expand All @@ -590,7 +593,15 @@ public void onPacketReceiving(@NotNull PacketEvent event) {
EnumWrappers.EntityUseAction action;
EnumWrappers.Hand hand = EnumWrappers.Hand.MAIN_HAND;

if (MinecraftVersion.CAVES_CLIFFS_1.atOrAbove()) {
if (MinecraftVersion.v26_1.atOrAbove()) {
// mc 26.1: action does not exist anymore as there are two separate packets
if (packet.getType() == PacketType.Play.Client.ATTACK) {
action = EnumWrappers.EntityUseAction.ATTACK;
} else {
action = EnumWrappers.EntityUseAction.INTERACT;
hand = packet.getHands().read(0);
}
} else if (MinecraftVersion.CAVES_CLIFFS_1.atOrAbove()) {
// mc 1.17: hand & action are now in an internal wrapper class
WrappedEnumEntityUseAction useAction = packet.getEnumEntityUseActions().read(0);
action = useAction.getAction();
Expand Down
1 change: 0 additions & 1 deletion checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@
<module name="OneStatementPerLine"/>
<module name="MultipleVariableDeclarations"/>
<module name="ArrayTypeStyle"/>
<module name="MissingSwitchDefault"/>
<module name="FallThrough"/>
<module name="UpperEll"/>
<module name="ModifierOrder"/>
Expand Down
14 changes: 4 additions & 10 deletions fabric/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@ configurations {

dependencies {
minecraft(libs.minecraft)
mappings(loom.officialMojangMappings())

modImplementation(libs.geantyref)
modImplementation(libs.fabricLoader)
modImplementation(platform(libs.fabricApiBom))
modImplementation(libs.fabricApiNetworkingV1)
implementation(libs.geantyref)
implementation(libs.fabricLoader)
implementation(platform(libs.fabricApiBom))
implementation(libs.fabricApiNetworkingV1)

"shaded"(projects.npcLibApi)
"shaded"(projects.npcLibCommon)
Expand All @@ -52,12 +51,7 @@ loom {
tasks.shadowJar {
exclude("META-INF/maven/**")
configurations = setOf(project.configurations["shaded"])
}

tasks.remapJar {
dependsOn(tasks.shadowJar)
archiveFileName = "npc-lib-mod.jar"
inputFile = tasks.shadowJar.flatMap { it.archiveFile }
}

tasks.compileJava {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,5 @@ public int minor() {
public int patch() {
return safeGetPart(2, 0);
}

@Override
public boolean atLeast(int major, int minor, int patch) {
return this.major() >= major && this.minor() >= major && this.patch() >= patch;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.minecraft.network.protocol.game.ServerboundInteractPacket;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
Expand Down Expand Up @@ -152,10 +151,6 @@ interface ServerPlayerMove {
@FunctionalInterface
interface ServerPlayerEntityInteract {

boolean interact(
int entityId,
@NotNull ServerPlayer player,
@NotNull ServerboundInteractPacket.ActionType actionType,
@NotNull InteractionHand hand);
boolean interact(int entityId, boolean isAttack, @NotNull ServerPlayer player, @NotNull InteractionHand hand);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package com.github.juliarn.npclib.fabric.mixins;

import com.github.juliarn.npclib.fabric.controller.FabricActionControllerEvents;
import net.minecraft.network.protocol.game.ServerboundAttackPacket;
import net.minecraft.network.protocol.game.ServerboundInteractPacket;
import net.minecraft.network.protocol.game.ServerboundMovePlayerPacket;
import net.minecraft.network.protocol.game.ServerboundMoveVehiclePacket;
Expand Down Expand Up @@ -80,7 +81,7 @@ public abstract class ServerGamePacketListenerImplMixin {
target = "Lnet/minecraft/network/protocol/game/ServerboundMovePlayerPacket;getYRot(F)F"
),
slice = @Slice(
from = @At(value = "INVOKE", target = "Lnet/minecraft/server/level/ServerPlayer;hasClientLoaded()Z"),
from = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerGamePacketListenerImpl;hasClientLoaded()Z"),
to = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerGamePacketListenerImpl;updateAwaitingTeleport()Z")
)
)
Expand Down Expand Up @@ -153,20 +154,25 @@ public abstract class ServerGamePacketListenerImplMixin {
public void npc_lib$handleInteract(ServerboundInteractPacket packet, CallbackInfo ci) {
var player = this.getPlayer();
var invoker = FabricActionControllerEvents.SERVER_PLAYER_ENTITY_INTERACT.invoker();
if (invoker.interact(packet.entityId(), false, player, packet.hand())) {
ci.cancel();
}
}

// call the interact event if the action is either interact or attack
var action = packet.action;
var actionType = action.getType();
if (action instanceof ServerboundInteractPacket.InteractionAction interactionAction) {
var hand = interactionAction.hand;
if (invoker.interact(packet.entityId, player, ServerboundInteractPacket.ActionType.INTERACT, hand)) {
ci.cancel();
}
} else if (actionType == ServerboundInteractPacket.ActionType.ATTACK) {
var hand = InteractionHand.MAIN_HAND;
if (invoker.interact(packet.entityId, player, ServerboundInteractPacket.ActionType.ATTACK, hand)) {
ci.cancel();
}
@Inject(
method = "handleAttack",
at = @At(
value = "INVOKE",
shift = At.Shift.AFTER,
target = "Lnet/minecraft/server/network/ServerGamePacketListenerImpl;hasClientLoaded()Z"
),
cancellable = true
)
public void npc_lib$handleAttack(ServerboundAttackPacket packet, CallbackInfo ci) {
var player = this.getPlayer();
var invoker = FabricActionControllerEvents.SERVER_PLAYER_ENTITY_INTERACT.invoker();
if (invoker.interact(packet.entityId(), true, player, InteractionHand.MAIN_HAND)) {
ci.cancel();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.Mth;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.EntityTypes;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.Pose;
import net.minecraft.world.item.ItemStack;
Expand Down Expand Up @@ -216,7 +216,7 @@ private FabricProtocolAdapter() {
@NotNull Object value,
int index
) {
// check if we need to convert the value before creating the meta object
// check if we need to convert the value before creating the metaobject
var converter = SERIALIZER_CONVERTERS.get(type);
if (converter != null) {
type = converter.getKey();
Expand Down Expand Up @@ -246,7 +246,7 @@ private FabricProtocolAdapter() {
pos.z(),
pos.pitch(),
pos.yaw(),
EntityType.PLAYER,
EntityTypes.PLAYER,
0,
Vec3.ZERO,
pos.yaw());
Expand Down Expand Up @@ -354,7 +354,7 @@ private FabricProtocolAdapter() {
var customPayload = new ByteArrayCustomPayload(payloadType, payload);

// ensure that the payload codec is registered for the payload type
var payloadTypeRegistry = PayloadTypeRegistryImpl.PLAY_S2C;
var payloadTypeRegistry = PayloadTypeRegistryImpl.CLIENTBOUND_PLAY;
var registered = payloadTypeRegistry.get(channelLocation);
if (registered == null) {
payloadTypeRegistry.register(payloadType, ByteArrayCustomPayload.CODEC);
Expand Down Expand Up @@ -397,24 +397,18 @@ private FabricProtocolAdapter() {

@Override
public void initialize(@NotNull Platform<ServerLevel, ServerPlayer, ItemStack, Object> platform) {
FabricActionControllerEvents.SERVER_PLAYER_ENTITY_INTERACT.register((entityId, player, actionType, hand) -> {
FabricActionControllerEvents.SERVER_PLAYER_ENTITY_INTERACT.register((entityId, isAttack, player, hand) -> {
var npc = platform.npcTracker().npcById(entityId);
if (npc != null) {
return switch (actionType) {
case ATTACK -> {
platform.eventManager().post(DefaultAttackNpcEvent.attackNpc(npc, player));
yield true;
}
case INTERACT -> {
var convertedHand = HAND_CONVERTER.get(hand);
platform.eventManager().post(DefaultInteractNpcEvent.interactNpc(npc, player, convertedHand));
yield true;
}
default -> false;
};
if (npc == null) {
return false;
}

return false;
var convertedHand = HAND_CONVERTER.get(hand);
var event = isAttack
? DefaultAttackNpcEvent.attackNpc(npc, player)
: DefaultInteractNpcEvent.interactNpc(npc, player, convertedHand);
platform.eventManager().post(event);
return true;
});
}
}
Loading
Loading