Skip to content

Commit

Permalink
cleanup and core impl
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Dec 5, 2020
1 parent eb7b99a commit f2534d3
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 71 deletions.
@@ -1,6 +1,5 @@
package com.denizenscript.denizen.events;

import com.denizenscript.denizen.flags.FlagManager;
import com.denizenscript.denizen.objects.*;
import com.denizenscript.denizen.objects.notable.NotableManager;
import com.denizenscript.denizen.tags.BukkitTagContext;
Expand Down Expand Up @@ -515,7 +514,7 @@ public boolean runFlaggedCheck(ScriptPath path, String switchName, PlayerTag pla
return false;
}
for (String flag : CoreUtilities.split(flagged, '|')) {
if (!FlagManager.playerHasFlag(player, flag)) {
if (!player.getFlagTracker().hasFlag(flag)) {
return false;
}
}
Expand Down
Expand Up @@ -2,7 +2,6 @@

import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.events.entity.EntityDespawnScriptEvent;
import com.denizenscript.denizen.flags.FlagManager;
import com.denizenscript.denizen.npc.actions.ActionHandler;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.NPCTag;
Expand Down Expand Up @@ -164,7 +163,6 @@ public void despawn(NPCDespawnEvent event) {
public void onRemove(NPCRemoveEvent event) {
NPC npc = event.getNPC();
new NPCTag(npc).action("remove", null);
FlagManager.clearNPCFlags(npc.getId());
}

@EventHandler
Expand Down
@@ -1,10 +1,9 @@
package com.denizenscript.denizen.npc.traits;

import com.denizenscript.denizen.flags.FlagManager;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.NPCTag;
import com.denizenscript.denizen.utilities.DenizenAPI;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import net.citizensnpcs.api.event.NPCTraitCommandAttachEvent;
import net.citizensnpcs.api.trait.Trait;
import org.bukkit.ChatColor;
Expand All @@ -29,9 +28,6 @@ public MobproxTrait() {
int timerBounce = 0;
LivingEntity liveEnt;
NPCTag dnpc;
FlagManager.Flag frange;
FlagManager.Flag facceptnpc;
FlagManager.Flag ftimer;
List<Entity> inrange = new ArrayList<>();

@Override
Expand All @@ -40,14 +36,11 @@ public void run() {
if (checkTimer == 10) {
checkTimer = 0;
timerBounce++;
if (timerBounce >= ftimer.getLast().asInteger()) {
ftimer.rebuild();
frange.rebuild();
facceptnpc.rebuild();
if (timerBounce >= getTimer()) {
timerBounce = 0;
if (getNPC().isSpawned()) {
int range = frange.getLast().asInteger();
boolean acceptnpc = facceptnpc.getLast().asBoolean();
int range = getRange();
boolean acceptnpc = acceptNpcs();
List<Entity> nearby = liveEnt.getNearbyEntities(range, range, range);
List<Entity> removeme = new ArrayList<>(inrange);
for (Entity ent : nearby) {
Expand Down Expand Up @@ -128,21 +121,34 @@ public void onTraitAttachEvent(NPCTraitCommandAttachEvent event) {
}
}

public int getRange() {
// TODO: Make this not flag based.
ObjectTag range = dnpc.getFlagTracker().getFlagValue("mobprox_range");
if (range == null) {
return 10;
}
return new ElementTag(range.toString()).asInt();
}

public int getTimer() {
ObjectTag range = dnpc.getFlagTracker().getFlagValue("mobprox_timer");
if (range == null) {
return 4;
}
return new ElementTag(range.toString()).asInt();
}

public boolean acceptNpcs() {
ObjectTag range = dnpc.getFlagTracker().getFlagValue("mobprox_acceptnpcs");
if (range == null) {
return false;
}
return new ElementTag(range.toString()).asBoolean();
}

@Override
public void onSpawn() {
liveEnt = (LivingEntity) getNPC().getEntity();
dnpc = new NPCTag(getNPC());
frange = DenizenAPI.getCurrentInstance().flagManager().getNPCFlag(dnpc.getId(), "mobprox_range");
if (frange.isEmpty()) {
frange.set("10");
}
facceptnpc = DenizenAPI.getCurrentInstance().flagManager().getNPCFlag(dnpc.getId(), "mobprox_acceptnpcs");
if (facceptnpc.isEmpty()) {
facceptnpc.set("false");
}
ftimer = DenizenAPI.getCurrentInstance().flagManager().getNPCFlag(dnpc.getId(), "mobprox_timer");
if (ftimer.isEmpty()) {
ftimer.set("4");
}
}
}
Expand Up @@ -142,6 +142,14 @@ public AbstractFlagTracker getFlagTracker() {
return npc.getOrAddTrait(DenizenFlagsTrait.class).fullFlagData;
}

public boolean hasFlag(String flag) {
DenizenFlagsTrait flagTrait = npc.getTraitNullable(DenizenFlagsTrait.class);
if (flagTrait == null) {
return false;
}
return flagTrait.fullFlagData.hasFlag(flag);
}

@Override
public void reapplyTracker(AbstractFlagTracker tracker) {
npc.getOrAddTrait(DenizenFlagsTrait.class).fullFlagData = (MapTagFlagTracker) tracker;
Expand Down
Expand Up @@ -4,7 +4,6 @@
import com.denizenscript.denizen.utilities.DenizenAPI;
import com.denizenscript.denizen.utilities.FormattedTextHelper;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.flags.FlagManager;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
Expand Down Expand Up @@ -148,7 +147,8 @@ else if (type == AnnounceType.TO_OPS) {
}
else if (type == AnnounceType.TO_FLAGGED) {
for (Player player : Bukkit.getOnlinePlayers()) {
if (FlagManager.playerHasFlag(PlayerTag.mirrorBukkitPlayer(player), flag.asString())) {
PlayerTag plTag = new PlayerTag(player);
if (plTag.getFlagTracker().hasFlag(flag.asString())) {
player.spigot().sendMessage(FormattedTextHelper.parse(message));
}
}
Expand Down
Expand Up @@ -1559,8 +1559,9 @@ else if (CoreUtilities.toLowerCase(entry.getKey()).contains(matchInput) && match
String flag = attribute.getContext(1);
ListTag players = new ListTag();
for (Player player : Bukkit.getOnlinePlayers()) {
if (DenizenAPI.getCurrentInstance().flagManager().getPlayerFlag(new PlayerTag(player), flag).size() > 0) {
players.addObject(new PlayerTag(player));
PlayerTag plTag = new PlayerTag(player);
if (plTag.getFlagTracker().hasFlag(flag)) {
players.addObject(plTag);
}
}
event.setReplacedObject(players.getObjectAttribute(attribute.fulfill(1)));
Expand All @@ -1579,8 +1580,9 @@ else if (CoreUtilities.toLowerCase(entry.getKey()).contains(matchInput) && match
String flag = attribute.getContext(1);
ListTag players = new ListTag();
for (Map.Entry<String, UUID> entry : PlayerTag.getAllPlayers().entrySet()) {
if (FlagManager.playerHasFlag(new PlayerTag(entry.getValue()), flag)) {
players.addObject(new PlayerTag(entry.getValue()));
PlayerTag plTag = new PlayerTag(entry.getValue());
if (plTag.getFlagTracker().hasFlag(flag)) {
players.addObject(plTag);
}
}
event.setReplacedObject(players.getObjectAttribute(attribute.fulfill(1)));
Expand All @@ -1600,7 +1602,7 @@ else if (CoreUtilities.toLowerCase(entry.getKey()).contains(matchInput) && match
ListTag npcs = new ListTag();
for (NPC npc : CitizensAPI.getNPCRegistry()) {
NPCTag dNpc = new NPCTag(npc);
if (dNpc.isSpawned() && FlagManager.npcHasFlag(dNpc, flag)) {
if (dNpc.isSpawned() && dNpc.hasFlag(flag)) {
npcs.addObject(dNpc);
}
}
Expand All @@ -1621,7 +1623,7 @@ else if (CoreUtilities.toLowerCase(entry.getKey()).contains(matchInput) && match
ListTag npcs = new ListTag();
for (NPC npc : CitizensAPI.getNPCRegistry()) {
NPCTag dNpc = new NPCTag(npc);
if (FlagManager.npcHasFlag(dNpc, flag)) {
if (dNpc.hasFlag(flag)) {
npcs.addObject(dNpc);
}
}
Expand Down
Expand Up @@ -7,7 +7,6 @@
import com.denizenscript.denizencore.objects.ObjectFetcher;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.utilities.CoreUtilities;

public class CommonRegistries {
Expand Down Expand Up @@ -120,9 +119,6 @@ public boolean canBecome(ObjectTag inp) {
if (inpType == EntityTag.class || inpType == PlayerTag.class || inpType == NPCTag.class) {
return true;
}
if (inpType == ListTag.class && ((ListTag) inp).flag != null) {
inpType = ElementTag.class;
}
if (inpType == ElementTag.class) {
String simple = inp.identifySimple();
int atIndex = simple.indexOf('@');
Expand Down
Expand Up @@ -51,7 +51,7 @@ public static AbstractMap.SimpleEntry<Integer, InventoryTag> getInventory(Argume
}

public static AbstractMap.SimpleEntry<Integer, InventoryTag> getInventory(Argument arg, TagContext context) {
boolean isElement = arg.object instanceof ElementTag || (arg.object instanceof ListTag && ((ListTag) arg.object).flag != null);
boolean isElement = arg.object instanceof ElementTag;
if (arg.object instanceof InventoryTag || (isElement && InventoryTag.matches(arg.getValue()))) {
InventoryTag inv = arg.object instanceof InventoryTag ? (InventoryTag) arg.object : InventoryTag.valueOf(arg.getValue(), context);
if (inv != null) {
Expand Down
Expand Up @@ -3,7 +3,6 @@
import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.utilities.Settings;
import com.denizenscript.denizen.events.bukkit.ScriptReloadEvent;
import com.denizenscript.denizen.flags.FlagManager;
import com.denizenscript.denizen.objects.*;
import com.denizenscript.denizen.scripts.containers.core.*;
import com.denizenscript.denizen.tags.BukkitTagContext;
Expand All @@ -15,6 +14,7 @@
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.utilities.maps.DenizenMapManager;
import com.denizenscript.denizencore.DenizenImplementation;
import com.denizenscript.denizencore.flags.AbstractFlagTracker;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
Expand Down Expand Up @@ -249,36 +249,8 @@ public String scriptQueueSpeed() {
}

@Override
public ListTag valueOfFlagListTag(String string) {
FlagManager.Flag flag = DenizenAPI.getCurrentInstance().getFlag(string);
if (flag == null) {
return null;
}
return new ListTag(flag.toString(), true, flag.values());
}

@Override
public boolean matchesFlagListTag(String arg) {
boolean flag = false;
if (arg.startsWith("fl")) {
if (arg.indexOf('[') == 2) {
int cb = arg.indexOf(']');
if (cb > 4 && arg.indexOf('@') == (cb + 1)) {
String owner = arg.substring(3, cb);
flag = arg.substring(cb + 2).length() > 0 && (PlayerTag.matches(owner)
|| (Depends.citizens != null && NPCTag.matches(owner)));
}
}
else if (arg.indexOf('@') == 2) {
flag = arg.substring(3).length() > 0;
}
}
return flag;
}

@Override
public boolean serverHasFlag(String flag) {
return FlagManager.serverHasFlag(flag);
public AbstractFlagTracker getServerFlags() {
return DenizenAPI.getCurrentInstance().serverFlagMap;
}

@Override
Expand Down

0 comments on commit f2534d3

Please sign in to comment.