Skip to content

Commit

Permalink
fix issues from testing previous 4 commits
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Mar 23, 2019
1 parent 0f4828b commit f910a06
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 90 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -170,7 +170,7 @@ These are all valid targets and ignores:
- Note that incompatible requirements don't work together (for example, `allinone:dolphin|parrot` doesn't work, because no single entity is both a dolphin *and* a parrot).
- Note that you can have `allinone` targets inside a `multi` target, but you cannot have `multi` targets inside an `allinone` target.
- Also, do not put `allinone` inside another `allinone`, and do not put a `multi` inside another `multi`.
- Note that to remove `allinone` and `multi` targets, you need to use the ID number (use `/sentinel targets` and related commands to find the ID).
- Note that to remove `allinone` and `multi` targets, you need to use the ID number (use `/sentinel targets` and related commands to find the ID), like `/sentinel removetarget allinone:0`.

### Some random supported things

Expand Down
Expand Up @@ -141,6 +141,9 @@ public String addable() {
* True for all non-multi targets.
*/
public boolean isValidMulti() {
if (prefix == null) {
return true;
}
if (prefix.equals("multi")) {
return getMulti(",").totalTargetsCount() > 0;
}
Expand All @@ -157,7 +160,7 @@ public SentinelTargetList getMulti(String splitter) {
SentinelTargetList newList = new SentinelTargetList();
for (String str : value.split(splitter)) {
SentinelTargetLabel label = new SentinelTargetLabel(str);
label.addToList(newList);
label.addToList(newList, false);
}
newList.recalculateCacheNoClear();
return newList;
Expand All @@ -167,21 +170,28 @@ public SentinelTargetList getMulti(String splitter) {
* Adds this target label to a list set.
*/
public boolean addToList(SentinelTargetList listSet) {
if (prefix.equals("multi")) {
return addToList(listSet, true);
}

/**
* Adds this target label to a list set.
*/
public boolean addToList(SentinelTargetList listSet, boolean doRecache) {
if (prefix != null && prefix.equals("multi")) {
listSet.byMultiple.add(getMulti(","));
return true;
}
if (prefix.equals("allinone")) {
listSet.byAllInOne.add(getMulti("|"));
if (prefix != null && prefix.equals("allinone")) {
listSet.byAllInOne.add(getMulti("\\|"));
return true;
}
Collection<String> list = getTargetsList(listSet);
String addable = addable();
if (list.contains(addable)) {
if (doRecache && list.contains(addable)) {
return false;
}
getTargetsList(listSet).add(addable());
if (list == listSet.targets || list == listSet.byOther) {
if (doRecache && (list == listSet.targets || list == listSet.byOther)) {
listSet.recalculateTargetsCache();
}
return true;
Expand All @@ -191,7 +201,7 @@ public boolean addToList(SentinelTargetList listSet) {
* Removes this target label from a list set.
*/
public boolean removeFromList(SentinelTargetList listSet) {
if (prefix.equals("multi")) {
if (prefix != null && prefix.equals("multi")) {
try {
int integerValue = Integer.parseInt(value);
if (integerValue >= 0 && integerValue < listSet.byMultiple.size()) {
Expand All @@ -204,7 +214,7 @@ public boolean removeFromList(SentinelTargetList listSet) {
return false;
}
}
if (prefix.equals("allinone")) {
if (prefix != null && prefix.equals("allinone")) {
try {
int integerValue = Integer.parseInt(value);
if (integerValue >= 0 && integerValue < listSet.byAllInOne.size()) {
Expand Down
108 changes: 56 additions & 52 deletions src/main/java/org/mcmonkey/sentinel/targeting/SentinelTargetList.java
Expand Up @@ -4,6 +4,7 @@
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.trait.Owner;
import net.citizensnpcs.api.util.DataKey;
import org.bukkit.ChatColor;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
Expand All @@ -12,6 +13,7 @@
import org.mcmonkey.sentinel.SentinelPlugin;
import org.mcmonkey.sentinel.SentinelTrait;
import org.mcmonkey.sentinel.SentinelUtilities;
import org.mcmonkey.sentinel.commands.SentinelCommand;

import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -32,6 +34,7 @@ public SentinelTargetList duplicate() {
result.byHeldItem.addAll(byHeldItem);
result.byGroup.addAll(byGroup);
result.byEvent.addAll(byEvent);
result.byOther.addAll(byOther);
result.byMultiple.addAll(byMultiple);
result.byAllInOne.addAll(byAllInOne);
return result;
Expand Down Expand Up @@ -67,17 +70,9 @@ public boolean isTarget(LivingEntity entity) {
* Explicitly does not reprocess the cache.
*/
public boolean isTargetNoCache(LivingEntity entity) {
if (SentinelTarget.v1_9) {
if (entity.getEquipment() != null && entity.getEquipment().getItemInMainHand() != null
&& SentinelUtilities.isRegexTargeted(entity.getEquipment().getItemInMainHand().getType().name(), byHeldItem)) {
return true;
}
}
else {
if (entity.getEquipment() != null && entity.getEquipment().getItemInHand() != null
&& SentinelUtilities.isRegexTargeted(entity.getEquipment().getItemInHand().getType().name(), byHeldItem)) {
return true;
}
if (entity.getEquipment() != null && SentinelUtilities.getHeldItem(entity) != null
&& SentinelUtilities.isRegexTargeted(SentinelUtilities.getHeldItem(entity).getType().name(), byHeldItem)) {
return true;
}
for (ArrayList<CachedOtherTarget> targets : otherTargetCache.values()) {
for (CachedOtherTarget target : targets) {
Expand All @@ -86,6 +81,22 @@ public boolean isTargetNoCache(LivingEntity entity) {
}
}
}
for (SentinelTargetList allInOne : byAllInOne) {
SentinelTargetList subList = allInOne.duplicate();
subList.recalculateCacheNoClear();
if (SentinelPlugin.debugMe) {
SentinelPlugin.instance.getLogger().info("All-In-One Debug: " + subList.totalTargetsCount() + " at start: " + subList.toMultiTargetString());
}
while (subList.ifIsTargetDeleteTarget(entity)) {
}
if (subList.totalTargetsCount() == 0) {
return true;
}
if (SentinelPlugin.debugMe) {
SentinelPlugin.instance.getLogger().info("All-In-One Debug: " + subList.totalTargetsCount() + " left: " + subList.toMultiTargetString());
}
}
// Any NPCs cause instant return - things below should be non-NPC only target types
if (entity.hasMetadata("NPC")) {
return targetsProcessed.contains(SentinelTarget.NPCS) ||
SentinelUtilities.isRegexTargeted(CitizensAPI.getNPCRegistry().getNPC(entity).getName(), byNpcName);
Expand All @@ -111,14 +122,6 @@ else if (SentinelUtilities.isRegexTargeted(entity.getCustomName() == null ? enti
return true;
}
}
for (SentinelTargetList allInOne : byAllInOne) {
SentinelTargetList subList = allInOne.duplicate();
while (subList.ifIsTargetDeleteTarget(entity)) {
}
if (subList.totalTargetsCount() == 0) {
return true;
}
}
return false;
}

Expand All @@ -127,22 +130,11 @@ else if (SentinelUtilities.isRegexTargeted(entity.getCustomName() == null ? enti
* Primarily for the multi-targets system.
*/
public boolean ifIsTargetDeleteTarget(LivingEntity entity) {
if (SentinelTarget.v1_9) {
if (entity.getEquipment() != null && entity.getEquipment().getItemInMainHand() != null) {
String match = SentinelUtilities.getRegexTarget(entity.getEquipment().getItemInMainHand().getType().name(), byHeldItem);
if (match != null) {
byHeldItem.remove(match);
return true;
}
}
}
else {
if (entity.getEquipment() != null && entity.getEquipment().getItemInHand() != null) {
String match = SentinelUtilities.getRegexTarget(entity.getEquipment().getItemInHand().getType().name(), byHeldItem);
if (match != null) {
byHeldItem.remove(match);
return true;
}
if (entity.getEquipment() != null && SentinelUtilities.getHeldItem(entity) != null) {
String match = SentinelUtilities.getRegexTarget(SentinelUtilities.getHeldItem(entity).getType().name(), byHeldItem);
if (match != null) {
byHeldItem.remove(match);
return true;
}
}
for (Map.Entry<String, ArrayList<CachedOtherTarget>> targets : otherTargetCache.entrySet()) {
Expand All @@ -154,6 +146,17 @@ public boolean ifIsTargetDeleteTarget(LivingEntity entity) {
}
}
}
for (SentinelTargetList allInOne : byAllInOne) {
SentinelTargetList subList = allInOne.duplicate();
subList.recalculateCacheNoClear();
while (subList.ifIsTargetDeleteTarget(entity)) {
}
if (subList.totalTargetsCount() == 0) {
byAllInOne.remove(allInOne);
return true;
}
}
// Any NPCs cause instant return - things below should be non-NPC only target types
if (entity.hasMetadata("NPC")) {
if (targetsProcessed.contains(SentinelTarget.NPCS)) {
for (String target : targets) {
Expand All @@ -171,6 +174,7 @@ public boolean ifIsTargetDeleteTarget(LivingEntity entity) {
byNpcName.remove(match);
return true;
}
return false;
}
if (entity instanceof Player) {
String match = SentinelUtilities.getRegexTarget(((Player) entity).getName(), byPlayerName);
Expand Down Expand Up @@ -208,15 +212,6 @@ public boolean ifIsTargetDeleteTarget(LivingEntity entity) {
return true;
}
}
for (SentinelTargetList allInOne : byAllInOne) {
SentinelTargetList subList = allInOne.duplicate();
while (subList.ifIsTargetDeleteTarget(entity)) {
}
if (subList.totalTargetsCount() == 0) {
byAllInOne.remove(allInOne);
return true;
}
}
return false;
}

Expand Down Expand Up @@ -398,16 +393,15 @@ private static void addList(StringBuilder builder, ArrayList<String> strs, Strin
if (prefix != null) {
builder.append(prefix).append(":");
}
builder.append(str).append(",");
builder.append(str).append(SentinelCommand.colorBasic).append(" ").append((char) 0x01).append(" ").append(ChatColor.AQUA);
}
builder.setLength(builder.length() - 1);
}
}

/**
* Forms a comma-separated list for multi-target output.
* Forms a \0x01-separated list for all-in-one-target output.
*/
public String toMultiTargetString() {
public String toComboString() {
StringBuilder sb = new StringBuilder();
addList(sb, targets, null);
addList(sb, byPlayerName, "player");
Expand All @@ -419,18 +413,28 @@ public String toMultiTargetString() {
addList(sb, byOther, null);
if (!byAllInOne.isEmpty()) {
for (SentinelTargetList list : byAllInOne) {
sb.append("allinone:").append(list.toAllInOneString()).append(",");
sb.append("allinone:").append(list.toAllInOneString()).append(SentinelCommand.colorBasic)
.append(" ").append((char) 0x01).append(" ").append(ChatColor.AQUA);
}
sb.setLength(sb.length() - 1);
}
return sb.toString();
if (sb.length() == 0) {
return "";
}
return sb.substring(0, sb.length() - (SentinelCommand.colorBasic + " . " + ChatColor.AQUA.toString()).length());
}

/**
* Forms a comma-separated list for multi-target output.
*/
public String toMultiTargetString() {
return toComboString().replace((char) 0x01, ',');
}

/**
* Forms a pipe-separated list for all-in-one-target output.
*/
public String toAllInOneString() {
return toMultiTargetString().replace(',', '|');
return toComboString().replace((char) 0x01, '|');
}

/**
Expand Down

0 comments on commit f910a06

Please sign in to comment.