Skip to content

Commit

Permalink
add any_click trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverSchlueter committed Sep 16, 2024
1 parent db23897 commit 3b6640c
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package de.oliver.fancynpcs.api.actions;

public enum ActionTrigger {
/**
* represents any click interaction by a player.
*/
ANY_CLICK,
/**
* represents a left click interaction by a player.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@ public void onPacketReceived(final PacketReceivedEvent event) {
? EquipmentSlot.HAND
: EquipmentSlot.OFF_HAND;
// This can optionally be ALSO called for OFF-HAND slot. Making sure to run logic only ONCE.
if (hand == EquipmentSlot.HAND)
if (hand == EquipmentSlot.HAND) {
// This packet can be sent multiple times for interactions that are NOT attacks, making sure to run logic only ONCE.
if (isAttack || !isInteract || npc.getData().getType() == EntityType.ARMOR_STAND)
// Further interaction handling is done by Npc#interact method...
if (isAttack || !isInteract || npc.getData().getType() == EntityType.ARMOR_STAND) {
npc.interact(event.getPlayer(), ActionTrigger.ANY_CLICK);
npc.interact(event.getPlayer(), isAttack ? ActionTrigger.LEFT_CLICK : ActionTrigger.RIGHT_CLICK);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,13 @@ public void onPacketReceived(final PacketReceivedEvent event) {
? EquipmentSlot.HAND
: EquipmentSlot.OFF_HAND;
// This can optionally be ALSO called for OFF-HAND slot. Making sure to run logic only ONCE.
if (hand == EquipmentSlot.HAND)
if (hand == EquipmentSlot.HAND) {
// This packet can be sent multiple times for interactions that are NOT attacks, making sure to run logic only ONCE.
if (isAttack || !isInteract || npc.getData().getType() == EntityType.ARMOR_STAND)
// Further interaction handling is done by Npc#interact method...
if (isAttack || !isInteract || npc.getData().getType() == EntityType.ARMOR_STAND) {
npc.interact(event.getPlayer(), ActionTrigger.ANY_CLICK);
npc.interact(event.getPlayer(), isAttack ? ActionTrigger.LEFT_CLICK : ActionTrigger.RIGHT_CLICK);
}
}
}

}
3 changes: 1 addition & 2 deletions src/main/java/de/oliver/fancynpcs/NpcManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,7 @@ public void loadNpcs() {
}
if (!migrateActionList.isEmpty()) {
takeBackup(npcConfig);
actions.put(ActionTrigger.LEFT_CLICK, migrateActionList);
actions.put(ActionTrigger.RIGHT_CLICK, migrateActionList);
actions.put(ActionTrigger.ANY_CLICK, migrateActionList);
}

ConfigurationSection actiontriggerSection = npcConfig.getConfigurationSection("npcs." + id + ".actions");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public ActionTrigger parse(final CommandContext<CommandSender> context, final Co
@Suggestions("action_trigger")
public List<String> suggestions(final CommandContext<CommandSender> context, final CommandInput input) {
return List.of(
ActionTrigger.ANY_CLICK.name().toLowerCase(),
ActionTrigger.LEFT_CLICK.name().toLowerCase(),
ActionTrigger.RIGHT_CLICK.name().toLowerCase()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ public void onPlayerUseUnknownEntity(final PlayerUseUnknownEntityEvent event) {
if (npc == null)
return;
// PlayerUseUnknownEntityEvent can optionally be ALSO called for OFF-HAND slot. Making sure to run logic only ONCE.
if (event.getHand() == EquipmentSlot.HAND)
if (event.getHand() == EquipmentSlot.HAND) {
// PlayerUseUnknownEntityEvent can be called multiple times for interactions that are NOT attacks, making sure to run logic only ONCE.
if (event.isAttack() || event.getClickedRelativePosition() == null || npc.getData().getType() == EntityType.ARMOR_STAND)
// Further interaction handling is done by Npc#interact method...
if (event.isAttack() || event.getClickedRelativePosition() == null || npc.getData().getType() == EntityType.ARMOR_STAND) {
npc.interact(event.getPlayer(), ActionTrigger.ANY_CLICK);
npc.interact(event.getPlayer(), event.isAttack() ? ActionTrigger.LEFT_CLICK : ActionTrigger.RIGHT_CLICK);
}
}
}

}

0 comments on commit 3b6640c

Please sign in to comment.