Skip to content
Permalink
Browse files

0.2, entity name targets!

  • Loading branch information...
mcmonkey4eva committed Mar 27, 2016
1 parent e852a9d commit 3c6c7147c0d6ca6b9288c6fef34ecd7f056e8299
@@ -51,12 +51,12 @@ Created by mcmonkey4eva on behalf of the Citizens and Denizen teams.
- Ride a horse or whatever other NPC
- maybe support this in base Citizens rather than Sentinel?
- Targets:
- Player by name: `player:NAME` (Regex!)
- NPC by name: `npc:NAME` (Regex!)
- Items (EG, "Sword" or "Diamond_Sword") being held: `helditem:ITEM_MATERIAL_NAME` (Regex!)
- Commands:
- `/sentinel stats`

- `/sentinel fightback`
- Other:
- old Sentry saves importer?

### Need help using Sentinel? Try one of these places:

@@ -7,7 +7,7 @@
<groupId>org.mcmonkey</groupId>
<artifactId>sentinel</artifactId>
<packaging>jar</packaging>
<version>0.1</version>
<version>0.2</version>
<name>Sentinel</name>
<description>Combat NPCs for Spigot</description>

@@ -15,6 +15,7 @@

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

public class SentinelPlugin extends JavaPlugin {

@@ -65,6 +66,8 @@ public SentinelTrait getSentinelFor(CommandSender sender) {
return null;
}

private long ignoreMe = 0;

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
String arg0 = args.length > 0 ? args[0].toLowerCase(): "help";
@@ -76,12 +79,45 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
else if (arg0.equals("addtarget") && sender.hasPermission("sentinel.addtarget") && args.length > 1) {
SentinelTarget target = SentinelTarget.forName(args[1].toUpperCase());
if (target == null) {
String[] info = args[1].split(":", 2);
if (info.length > 1) {
List<String> names = null;
if (info[0].equalsIgnoreCase("player")) {
names = sentinel.playerNameTargets;
}
else if (info[0].equalsIgnoreCase("npc")) {
names = sentinel.npcNameTargets;
}
else if (info[0].equalsIgnoreCase("entityname")) {
names = sentinel.entityNameTargets;
}
try {
if ("Sentinel".matches(info[1])) {
ignoreMe++;
}
}
catch (Exception e) {
names = null;
sender.sendMessage(prefixBad + "Bad regular expression!");
}
if (names != null) {
if (names.contains(info[1])) {
sender.sendMessage(prefixBad + "Already tracking that name target!");
}
else {
names.add(info[1]);
sender.sendMessage(prefixGood + "Tracking new target!");
}
return true;
}
}
sender.sendMessage(prefixBad + "Invalid target!");
StringBuilder valid = new StringBuilder();
for (SentinelTarget poss: SentinelTarget.values()) {
valid.append(poss.name()).append(", ");
}
sender.sendMessage(prefixGood + "Valid targets: " + valid.substring(0, valid.length() - 2));
sender.sendMessage(prefixGood + "Also allowed: player:NAME(REGEX), npc:NAME(REGEX), entityname:NAME(REGEX)");
}
else {
if (sentinel.targets.add(target)) {
@@ -96,12 +132,39 @@ else if (arg0.equals("addtarget") && sender.hasPermission("sentinel.addtarget")
else if (arg0.equals("removetarget") && sender.hasPermission("sentinel.removetarget") && args.length > 1) {
SentinelTarget target = SentinelTarget.forName(args[1].toUpperCase());
if (target == null) {
sender.sendMessage(prefixBad + "Invalid target!");
StringBuilder valid = new StringBuilder();
for (SentinelTarget poss: SentinelTarget.values()) {
valid.append(poss.name()).append(", ");
String[] info = args[1].split(":", 2);
if (info.length > 1) {
List<String> names = null;
if (info[0].equalsIgnoreCase("player")) {
names = sentinel.playerNameTargets;
}
else if (info[0].equalsIgnoreCase("npc")) {
names = sentinel.npcNameTargets;
}
else if (info[0].equalsIgnoreCase("entityname")) {
names = sentinel.entityNameTargets;
}
try {
if ("Sentinel".matches(info[1])) {
ignoreMe++;
}
}
catch (Exception e) {
names = null;
sender.sendMessage(prefixBad + "Bad regular expression!");
}
if (names != null) {
if (!names.remove(info[1])) {
sender.sendMessage(prefixBad + "Not tracking that name target!");
}
else {
sender.sendMessage(prefixGood + "No longer tracking that target!");
}
return true;
}
}
sender.sendMessage(prefixGood + "Valid targets: " + valid.substring(0, valid.length() - 2));
sender.sendMessage(prefixBad + "Invalid target!");
sender.sendMessage(prefixGood + "See '/sentinel addtarget' to view valid targets!");
}
else {
if (sentinel.targets.remove(target)) {
@@ -116,12 +179,40 @@ else if (arg0.equals("removetarget") && sender.hasPermission("sentinel.removetar
else if (arg0.equals("addignore") && sender.hasPermission("sentinel.addignore") && args.length > 1) {
SentinelTarget target = SentinelTarget.forName(args[1].toUpperCase());
if (target == null) {
sender.sendMessage(prefixBad + "Invalid ignore target!");
StringBuilder valid = new StringBuilder();
for (SentinelTarget poss: SentinelTarget.values()) {
valid.append(poss.name()).append(", ");
String[] info = args[1].split(":", 2);
if (info.length > 1) {
List<String> names = null;
if (info[0].equalsIgnoreCase("player")) {
names = sentinel.playerNameIgnores;
}
else if (info[0].equalsIgnoreCase("npc")) {
names = sentinel.npcNameIgnores;
}
else if (info[0].equalsIgnoreCase("entityname")) {
names = sentinel.entityNameIgnores;
}
try {
if ("Sentinel".matches(info[1])) {
ignoreMe++;
}
}
catch (Exception e) {
names = null;
sender.sendMessage(prefixBad + "Bad regular expression!");
}
if (names != null) {
if (!names.contains(info[1])) {
sender.sendMessage(prefixBad + "Already ignoring that name target!");
}
else {
names.add(info[1]);
sender.sendMessage(prefixGood + "Ignoring new target!");
}
return true;
}
}
sender.sendMessage(prefixGood + "Valid ignore targets: " + valid.substring(0, valid.length() - 2));
sender.sendMessage(prefixBad + "Invalid ignore target!");
sender.sendMessage(prefixGood + "See '/sentinel addtarget' to view valid targets!");
}
else {
if (sentinel.ignores.add(target)) {
@@ -136,12 +227,39 @@ else if (arg0.equals("addignore") && sender.hasPermission("sentinel.addignore")
else if (arg0.equals("removeignore") && sender.hasPermission("sentinel.removeignore") && args.length > 1) {
SentinelTarget target = SentinelTarget.forName(args[1].toUpperCase());
if (target == null) {
sender.sendMessage(prefixBad + "Invalid ignore target!");
StringBuilder valid = new StringBuilder();
for (SentinelTarget poss: SentinelTarget.values()) {
valid.append(poss.name()).append(", ");
String[] info = args[1].split(":", 2);
if (info.length > 1) {
List<String> names = null;
if (info[0].equalsIgnoreCase("player")) {
names = sentinel.playerNameIgnores;
}
else if (info[0].equalsIgnoreCase("npc")) {
names = sentinel.npcNameIgnores;
}
else if (info[0].equalsIgnoreCase("entityname")) {
names = sentinel.entityNameIgnores;
}
try {
if ("Sentinel".matches(info[1])) {
ignoreMe++;
}
}
catch (Exception e) {
names = null;
sender.sendMessage(prefixBad + "Bad regular expression!");
}
if (names != null) {
if (!names.remove(info[1])) {
sender.sendMessage(prefixBad + "Was not ignoring that name target!");
}
else {
sender.sendMessage(prefixGood + "Not ignoring that target along longer!");
}
return true;
}
}
sender.sendMessage(prefixGood + "Valid ignore targets: " + valid.substring(0, valid.length() - 2));
sender.sendMessage(prefixBad + "Invalid ignore target!");
sender.sendMessage(prefixGood + "See '/sentinel addtarget' to view valid targets!");
}
else {
if (sentinel.ignores.remove(target)) {
@@ -247,7 +365,13 @@ else if (arg0.equals("info") && sender.hasPermission("sentinel.info")) {
sender.sendMessage(prefixGood + ChatColor.RESET + sentinel.getNPC().getFullName() + ColorBasic
+ ": owned by " + ChatColor.RESET + getOwner(sentinel.getNPC()));
sender.sendMessage(prefixGood + "Targets: " + ChatColor.AQUA + getTargetString(sentinel.targets));
sender.sendMessage(prefixGood + "Ignored targets: " + ChatColor.AQUA + getTargetString(sentinel.ignores));
sender.sendMessage(prefixGood + "Player Name Targets: " + ChatColor.AQUA + getNameTargetString(sentinel.playerNameTargets));
sender.sendMessage(prefixGood + "NPC Name Targets: " + ChatColor.AQUA + getNameTargetString(sentinel.npcNameTargets));
sender.sendMessage(prefixGood + "Entity Name Targets: " + ChatColor.AQUA + getNameTargetString(sentinel.entityNameTargets));
sender.sendMessage(prefixGood + "Ignored Targets: " + ChatColor.AQUA + getTargetString(sentinel.ignores));
sender.sendMessage(prefixGood + "Ignored Player Name Targets: " + ChatColor.AQUA + getNameTargetString(sentinel.playerNameIgnores));
sender.sendMessage(prefixGood + "Ignored NPC Name Targets: " + ChatColor.AQUA + getNameTargetString(sentinel.npcNameIgnores));
sender.sendMessage(prefixGood + "Ignored Entity Name Targets: " + ChatColor.AQUA + getNameTargetString(sentinel.entityNameIgnores));
sender.sendMessage(prefixGood + "Damage: " + ChatColor.AQUA + sentinel.damage);
sender.sendMessage(prefixGood + "Armor: " + ChatColor.AQUA + sentinel.armor);
sender.sendMessage(prefixGood + "Health: " + ChatColor.AQUA +
@@ -276,6 +400,14 @@ else if (arg0.equals("info") && sender.hasPermission("sentinel.info")) {
}
}

public String getNameTargetString(List<String> strs) {
StringBuilder targets = new StringBuilder();
for (String str: strs) {
targets.append(str).append(", ");
}
return targets.length() > 0 ? targets.substring(0, targets.length() - 2): targets.toString();
}

public String getTargetString(HashSet<SentinelTarget> sentinel) {
StringBuilder targets = new StringBuilder();
for (SentinelTarget target: sentinel) {
@@ -1,5 +1,6 @@
package org.mcmonkey.sentinel;

import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.trait.Owner;
@@ -17,9 +18,7 @@
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.*;

public class SentinelTrait extends Trait {

@@ -51,6 +50,27 @@ public SentinelTrait() {
@Persist("targets")
public HashSet<SentinelTarget> targets = new HashSet<SentinelTarget>();

@Persist("ignores")
public HashSet<SentinelTarget> ignores = new HashSet<SentinelTarget>();

@Persist("playerNameTargets")
public List<String> playerNameTargets = new ArrayList<String>();

@Persist("playerNameIgnores")
public List<String> playerNameIgnores = new ArrayList<String>();

@Persist("npcNameTargets")
public List<String> npcNameTargets = new ArrayList<String>();

@Persist("npcNameIgnores")
public List<String> npcNameIgnores = new ArrayList<String>();

@Persist("entityNameTargets")
public List<String> entityNameTargets = new ArrayList<String>();

@Persist("entityNameIgnores")
public List<String> entityNameIgnores = new ArrayList<String>();

@Persist("range")
public double range = 20;

@@ -63,9 +83,6 @@ public SentinelTrait() {
@Persist("health")
public double health = 20;

@Persist("ignores")
public HashSet<SentinelTarget> ignores = new HashSet<SentinelTarget>();

@Persist("ranged_chase")
public boolean rangedChase = false;

@@ -370,13 +387,31 @@ public boolean shouldTarget(LivingEntity entity) {
if (entity.getUniqueId().equals(getLivingEntity().getUniqueId())) {
return false;
}
if (entity.hasMetadata("NPC")) {
return targets.contains(SentinelTarget.NPCS);
}
return isTargeted(entity) && !isIgnored(entity);
}

public boolean isRegexTargeted(String name, List<String> regexes) {
for (String str: regexes) {
if (name.matches(str)) {
return true;
}
}
return false;
}

public boolean isIgnored(LivingEntity entity) {
if (entity.hasMetadata("NPC")) {
return ignores.contains(SentinelTarget.NPCS) ||
isRegexTargeted(CitizensAPI.getNPCRegistry().getNPC(entity).getName(), npcNameIgnores);
}
else if (entity instanceof Player) {
if (isRegexTargeted(((Player) entity).getName(), playerNameIgnores)) {
return true;
}
}
else if (isRegexTargeted(entity.getCustomName() == null ? entity.getType().name(): entity.getCustomName(), entityNameIgnores)) {
return true;
}
if (ignores.contains(SentinelTarget.OWNER) && entity.getUniqueId().equals(npc.getTrait(Owner.class).getOwnerId())) {
return true;
}
@@ -390,6 +425,18 @@ public boolean isIgnored(LivingEntity entity) {
}

public boolean isTargeted(LivingEntity entity) {
if (entity.hasMetadata("NPC")) {
return targets.contains(SentinelTarget.NPCS) ||
isRegexTargeted(CitizensAPI.getNPCRegistry().getNPC(entity).getName(), npcNameTargets);
}
if (entity instanceof Player) {
if (isRegexTargeted(((Player) entity).getName(), playerNameTargets)) {
return true;
}
}
else if (isRegexTargeted(entity.getCustomName() == null ? entity.getType().name(): entity.getCustomName(), entityNameTargets)) {
return true;
}
if (targets.contains(SentinelTarget.OWNER) && entity.getUniqueId().equals(npc.getTrait(Owner.class).getOwnerId())) {
return true;
}

0 comments on commit 3c6c714

Please sign in to comment.
You can’t perform that action at this time.