Ability Checking

This commit is contained in:
Kam committed 2026-03-05 03:15:47 -05:00
1 parent 3919831857
commit 37d4b97a44
6 files changed
+79 -167

No files matched your search

@@ -81,6 +81,7 @@ public class AbilityController implements IAbilityHandler {
private final List<IAbilityExtender> extenders = new ArrayList<>();
private final Map<String, Supplier<AbilityCondition>> conditionTypes = new HashMap<>();
private final List<Predicate<EntityPlayer>> flightCheckers = new ArrayList<>();
private final List<Predicate<EntityPlayer>> activationCheckers = new ArrayList<>();
// ── Chained Abilities ──────────────────────────────────────────────────
private final Map<String, ChainedAbility> chainedAbilities = new LinkedHashMap<>(); // name → ChainedAbility
@@ -1019,6 +1020,17 @@ public class AbilityController implements IAbilityHandler {
return false;
}
public void registerActivationChecker(Predicate<EntityPlayer> checker) {
activationCheckers.add(checker);
}
public boolean canPlayerActivate(EntityPlayer player) {
for (Predicate<EntityPlayer> checker : activationCheckers) {
if (!checker.test(player)) return false;
}
return true;
}
/**
* Fire onAbilityStart on all extenders. Returns false if ANY extender cancels.
*/
@@ -64,6 +64,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
public abstract class Ability implements IAbility, IAbilityAction {
@@ -156,6 +157,15 @@ public abstract class Ability implements IAbility, IAbilityAction {
protected transient int defaultIconWidth = 48;
protected transient int defaultIconHeight = 48;
/**
* Optional player requirement predicate. When set, the ability is only visible
* and usable by players for whom this predicate returns true.
* Evaluated on-demand (no polling). Null means no requirement (always available).
* <p>
* This is transient — set programmatically by addons, not saved to NBT.
*/
protected transient Predicate<EntityPlayer> playerRequirement;
/**
* A single layer of a default ability icon. Each layer has a texture,
* an optional dynamic color source, and optional per-state texture variants.
@@ -1774,7 +1784,10 @@ public abstract class Ability implements IAbility, IAbilityAction {
}
public Ability deepCopy() {
return AbilityController.Instance.fromNBT(this.writeNBT(false));
Ability copy = AbilityController.Instance.fromNBT(this.writeNBT(false));
if (copy != null)
copy.playerRequirement = this.playerRequirement;
return copy;
}
@Override
@@ -2358,6 +2371,22 @@ public abstract class Ability implements IAbility, IAbilityAction {
this.allowedBy = allowedBy;
}
public Predicate<EntityPlayer> getPlayerRequirement() {
return playerRequirement;
}
public void setPlayerRequirement(Predicate<EntityPlayer> requirement) {
this.playerRequirement = requirement;
}
/**
* Check if this ability is available for the given player.
* Returns true if no requirement is set, or if the requirement predicate passes.
*/
public boolean isAvailableFor(EntityPlayer player) {
return playerRequirement == null || playerRequirement.test(player);
}
public boolean isIgnoreCooldown() {
return ignoreCooldown;
}
@@ -5,6 +5,7 @@ import kamkeel.npcs.controllers.data.ability.AbilityAction;
import kamkeel.npcs.controllers.data.ability.enums.UserType;
import kamkeel.npcs.controllers.data.ability.conditions.AbilityCondition;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import java.util.List;
@@ -40,6 +41,15 @@ public interface IAbilityAction {
boolean checkConditionsForPlayer(EntityLivingBase caster);
/**
* Check if this action is available for the given player.
* Used for visibility filtering (HUD, selection) and activation gating.
* Returns true by default (no requirement).
*/
default boolean isAvailableFor(EntityPlayer player) {
return true;
}
/**
* Whether this action is a chained ability (sequence of abilities) vs an individual ability.
*/
@@ -8,7 +8,7 @@ import kamkeel.npcs.controllers.data.ability.data.IAbilityAction;
import kamkeel.npcs.network.PacketHandler;
import kamkeel.npcs.network.packets.player.ability.AbilityHotbarSelectPacket;
import net.minecraft.client.Minecraft;
import noppes.npcs.client.ClientProxy;
import net.minecraft.entity.player.EntityPlayer;
import noppes.npcs.client.ClientAbilityState;
import noppes.npcs.client.KeyPressHandler;
import net.minecraft.client.gui.FontRenderer;
@@ -527,19 +527,34 @@ public class AbilityHotbarComponent extends HudComponent {
PlayerAbilityHotbarData hotbarData = playerData.hotbarData;
if (hotbarData == null) return;
EntityPlayer clientPlayer = Minecraft.getMinecraft().thePlayer;
boolean globalBlock = clientPlayer != null && AbilityController.Instance != null
&& !AbilityController.Instance.canPlayerActivate(clientPlayer);
for (int i = 0; i < TOTAL_SLOTS; i++) {
AbilityHotbarData slotData = hotbarData.slots[i];
String key = slotData.isEmpty() ? null : slotData.abilityKey;
Ability ability = null;
IAbilityAction action = null;
if (key != null && AbilityController.Instance != null) {
if (slotData.isChainKey()) {
action = AbilityController.Instance.resolveChainedAbility(slotData.getResolveKey());
if (globalBlock) {
key = null;
} else {
ability = AbilityController.Instance.resolveAbility(key);
action = ability;
if (slotData.isChainKey()) {
action = AbilityController.Instance.resolveChainedAbility(slotData.getResolveKey());
} else {
ability = AbilityController.Instance.resolveAbility(key);
action = ability;
}
if (ability == null && action == null) key = null;
// Hide abilities whose player requirement is not met
if (action != null && clientPlayer != null && !action.isAvailableFor(clientPlayer)) {
key = null;
ability = null;
action = null;
}
}
if (ability == null && action == null) key = null;
}
slots[i].setAbility(key, ability, action);
}
@@ -1,160 +0,0 @@
package noppes.npcs.client.gui.player.inventory;
import kamkeel.npcs.controllers.AbilityController;
import kamkeel.npcs.controllers.data.ability.Ability;
import kamkeel.npcs.controllers.data.ability.data.ChainedAbility;
import net.minecraft.client.gui.GuiButton;
import noppes.npcs.client.ClientCacheHandler;
import noppes.npcs.client.gui.util.GuiCustomScroll;
import noppes.npcs.client.gui.util.GuiNpcButton;
import noppes.npcs.client.gui.util.GuiNpcTextField;
import noppes.npcs.client.gui.util.ICustomScrollListener;
import noppes.npcs.client.gui.util.ITextfieldListener;
import noppes.npcs.client.gui.util.SubGuiInterface;
import noppes.npcs.controllers.data.AbilityHotbarData;
import noppes.npcs.controllers.data.PlayerData;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* SubGui for selecting an ability to assign to a hotbar slot.
* Uses ability keys internally to avoid duplicate display name collisions.
*/
public class SubGuiSelectAbilityForSlot extends SubGuiInterface implements ICustomScrollListener, ITextfieldListener {
private LinkedHashMap<String, String> keyToDisplay = new LinkedHashMap<>();
private List<String> scrollKeys = new ArrayList<>();
private GuiCustomScroll scrollAbilities;
private int selectedIndex = -1;
private String search = "";
public boolean confirmed = false;
public String selectedAbilityKey = null;
public int slotIndex;
public boolean removeAbility = false;
public SubGuiSelectAbilityForSlot(int slotIndex) {
this.slotIndex = slotIndex;
this.closeOnEsc = true;
this.drawDefaultBackground = true;
xSize = 256 + 10;
this.setBackground("menubg.png");
}
@Override
public void initGui() {
super.initGui();
guiTop = (this.height - ySize) / 2 + 10;
if (scrollAbilities == null) {
scrollAbilities = new GuiCustomScroll(this, 0, 0);
scrollAbilities.setSize(177, 185);
}
scrollAbilities.guiLeft = guiLeft + 4;
scrollAbilities.guiTop = guiTop + 4;
addScroll(scrollAbilities);
loadAbilities();
scrollAbilities.setList(getSearchList());
addTextField(new GuiNpcTextField(55, this, fontRendererObj, guiLeft + 4, guiTop + 192, 177, 20, search));
addButton(new GuiNpcButton(0, guiLeft + 183, guiTop + 4, 79, 20, "gui.add"));
addButton(new GuiNpcButton(1, guiLeft + 183, guiTop + 26, 79, 20, "gui.cancel"));
addButton(new GuiNpcButton(2, guiLeft + 183, guiTop + 88, 79, 20, "gui.remove"));
}
private void loadAbilities() {
keyToDisplay.clear();
PlayerData playerData = ClientCacheHandler.playerData;
if (playerData == null || playerData.abilityData == null) {
return;
}
List<String> abilities = playerData.abilityData.getUnlockedAbilityList();
for (String key : abilities) {
String displayName;
if (key.startsWith(AbilityHotbarData.CHAIN_PREFIX)) {
String chainKey = key.substring(AbilityHotbarData.CHAIN_PREFIX.length());
ChainedAbility chain = AbilityController.Instance != null ?
AbilityController.Instance.resolveChainedAbility(chainKey) : null;
displayName = chain != null ? "\u00A76\u2726 " + chain.getDisplayName() : key;
} else {
Ability ability = AbilityController.Instance != null ?
AbilityController.Instance.resolveAbility(key) : null;
displayName = ability != null ? ability.getDisplayName() : key;
}
keyToDisplay.put(key, displayName);
}
}
@Override
public void actionPerformed(GuiButton button) {
int id = button.id;
if (id == 0 && selectedIndex >= 0 && selectedIndex < scrollKeys.size()) {
confirmed = true;
selectedAbilityKey = scrollKeys.get(selectedIndex);
this.close();
}
if (id == 1) {
this.close();
}
if (id == 2) {
this.removeAbility = true;
this.close();
}
}
@Override
public void customScrollClicked(int i, int i1, int i2, GuiCustomScroll guiCustomScroll) {
if (guiCustomScroll == scrollAbilities) {
selectedIndex = scrollAbilities.selected;
}
}
@Override
public void customScrollDoubleClicked(String selection, GuiCustomScroll guiCustomScroll) {
if (guiCustomScroll == scrollAbilities && selectedIndex >= 0 && selectedIndex < scrollKeys.size()) {
confirmed = true;
selectedAbilityKey = scrollKeys.get(selectedIndex);
this.close();
}
}
@Override
public void unFocused(GuiNpcTextField guiNpcTextField) {
}
@Override
public void keyTyped(char c, int i) {
super.keyTyped(c, i);
if (getTextField(55) != null) {
if (getTextField(55).isFocused()) {
if (search.equals(getTextField(55).getText()))
return;
search = getTextField(55).getText().toLowerCase();
scrollAbilities.resetScroll();
selectedIndex = -1;
scrollAbilities.setList(getSearchList());
}
}
}
private List<String> getSearchList() {
scrollKeys.clear();
List<String> displayNames = new ArrayList<>();
for (Map.Entry<String, String> entry : keyToDisplay.entrySet()) {
String displayName = entry.getValue();
if (search.isEmpty() || displayName.toLowerCase().contains(search)) {
scrollKeys.add(entry.getKey());
displayNames.add(displayName);
}
}
return displayNames;
}
}
@@ -381,6 +381,9 @@ public class PlayerAbilityData extends AbstractDataAbilities implements IPlayerA
public boolean activateAbility(EntityPlayer player, String key) {
if (player.worldObj.isRemote || key == null || key.isEmpty()) return false;
// Global activation check (e.g., addon-registered restrictions)
if (!AbilityController.Instance.canPlayerActivate(player)) return false;
// Can't activate if already executing
if (currentAbility != null && currentAbility.isExecuting()) return false;
if (isExecutingChain()) return false;
@@ -395,6 +398,9 @@ public class PlayerAbilityData extends AbstractDataAbilities implements IPlayerA
// Check conditions (skip target-requiring ones)
if (!action.checkConditionsForPlayer(player)) return false;
// Check player requirement (visibility/availability gate)
if (!action.isAvailableFor(player)) return false;
// Dispatch: chain vs ability
if (action.isChain()) {
if (isOnCooldown()) return false;