Skip to content

Commit

Permalink
fix issues from flag commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Mar 12, 2019
1 parent 94d1c2c commit 1bbbb84
Showing 1 changed file with 63 additions and 29 deletions.
92 changes: 63 additions & 29 deletions plugin/src/main/java/net/aufdemrand/denizen/flags/FlagManager.java
Expand Up @@ -200,7 +200,7 @@ public boolean StillValid() {
*/
public List<String> values() {
checkExpired();
return value.values;
return value.asList();
}

/**
Expand Down Expand Up @@ -241,7 +241,7 @@ public void clear() {
String OldOwner = flagOwner;
String OldName = flagName;
dObject OldValue = FlagSmartEvent.isActive() ? (value.size() > 1
? new dList(denizen.getSaves().getStringList(flagPath))
? value.asList()
: value.size() == 1 ? new Element(value.get(0).asString()) : new Element("null")) : null;

denizen.getSaves().set(flagPath, null);
Expand Down Expand Up @@ -323,21 +323,24 @@ public void set(Object obj, int index) {

// No index? Clear the flag and set the whole thing.
if (index < 0) {
value.values.clear();
value.values.add((String) obj);
value.values = null;
value.size = 1;
value.firstValue = (String) obj;
}
else if (size() == 0) {
value.values.add((String) obj);
value.firstValue = (String) obj;
value.size = 1;
}
else if (index > 0) {
value.mustBeList();
if (value.values.size() > index - 1) {
value.values.remove(index - 1);
value.values.add(index - 1, (String) obj);
value.values.set(index - 1, (String) obj);

// Index higher than currently exists? Add the item to the end of the list.
}
else {
value.values.add((String) obj);
value.size++;
}
}
valid = true;
Expand All @@ -352,7 +355,9 @@ else if (index > 0) {
*/
public int add(Object obj) {
checkExpired();
value.mustBeList();
value.values.add((String) obj);
value.size++;
valid = true;
save();
rebuild();
Expand All @@ -367,9 +372,11 @@ public int split(Object obj) {
checkExpired();
dList split = dList.valueOf(obj.toString());
if (split.size() > 0) {
value.mustBeList();
for (String val : split) {
if (val.length() > 0) {
value.values.add(val);
value.size++;
}
}
save();
Expand All @@ -382,10 +389,13 @@ public int splitNew(Object obj) {
checkExpired();
dList split = dList.valueOf(obj.toString());
if (split.size() > 0) {
value.mustBeList();
value.values.clear();
value.size = 0;
for (String val : split) {
if (val.length() > 0) {
value.values.add(val);
value.size++;
}
}
save();
Expand Down Expand Up @@ -417,6 +427,7 @@ public void remove(Object obj, int index) {
checkExpired();
boolean isDouble = aH.matchesDouble((String) obj);

value.mustBeList();
// No index? Match object and remove it.
if (index <= 0 && obj != null) {
int x = 0;
Expand All @@ -426,13 +437,15 @@ public void remove(Object obj, int index) {
if (val.equalsIgnoreCase(String.valueOf(obj))) {

value.values.remove(x);
value.size--;
break;
}

// Evaluate as number
try {
if (isDouble && aH.matchesDouble(val) && Double.valueOf(val).equals(Double.valueOf((String) obj))) {
value.values.remove(x);
value.size--;
break;
}
}
Expand All @@ -447,6 +460,7 @@ public void remove(Object obj, int index) {
}
else if (index <= size()) {
value.values.remove(index - 1);
value.size--;
}

valid = true;
Expand Down Expand Up @@ -513,12 +527,17 @@ public void save() {
String oldName = flagName;
dObject oldValue = null;
if (FlagSmartEvent.isActive()) {
List<String> oldValueList = denizen.getSaves().getStringList(flagPath);
oldValue = oldValueList.size() > 1 ? new dList(oldValueList)
dList oldValueList = value.asList();
oldValue = oldValueList.size() > 1 ? oldValueList
: oldValueList.size() == 1 ? new Element(oldValueList.get(0)) : new Element("null");
}

denizen.getSaves().set(flagPath, value.values);
if (value.values != null) {
denizen.getSaves().set(flagPath, value.values);
}
else {
denizen.getSaves().set(flagPath, value.size == 0 ? null : value.firstValue);
}
denizen.getSaves().set(flagPath + "-expiration", (expiration > 0 ? expiration : null));

if (FlagSmartEvent.isActive()) {
Expand Down Expand Up @@ -555,9 +574,9 @@ else if (entity != null) {
world_script_events.add(type + " flag changed");
world_script_events.add(type + " flag " + oldName + " changed");

context.put("owner", Element.valueOf(oldOwner));
context.put("name", Element.valueOf(oldName));
context.put("type", Element.valueOf(type));
context.put("owner", new Element(oldOwner));
context.put("name", new Element(oldName));
context.put("type", new Element(type));
context.put("old_value", oldValue);

world_script_events.add("flag changed");
Expand Down Expand Up @@ -608,11 +627,11 @@ public boolean checkExpired() {
rebuild();
if (denizen.getSaves().contains(flagPath + "-expiration")) {
if (expiration > 1 && expiration < DenizenCore.currentTimeMillis) {
String OldOwner = flagOwner;
String OldName = flagName;
dObject OldValue = FlagSmartEvent.isActive() ? (value.size() > 1
? new dList(denizen.getSaves().getStringList(flagPath))
: value.size() == 1 ? new Element(value.get(0).asString()) : Element.valueOf("null")) : null;
String oldOwner = flagOwner;
String oldName = flagName;
dObject oldValue = FlagSmartEvent.isActive() ? (value.size() > 1
? value.asList()
: value.size() == 1 ? new Element(value.get(0).asString()) : new Element("null")) : null;
denizen.getSaves().set(flagPath + "-expiration", null);
denizen.getSaves().set(flagPath, null);
valid = false;
Expand All @@ -623,16 +642,16 @@ public boolean checkExpired() {

Map<String, dObject> context = new HashMap<>();
dPlayer player = null;
if (dPlayer.matches(OldOwner)) {
player = dPlayer.valueOf(OldOwner);
if (dPlayer.matches(oldOwner)) {
player = dPlayer.valueOf(oldOwner);
}
dNPC npc = null;
if (Depends.citizens != null && dNPC.matches(OldOwner)) {
npc = dNPC.valueOf(OldOwner);
if (Depends.citizens != null && dNPC.matches(oldOwner)) {
npc = dNPC.valueOf(oldOwner);
}
dEntity entity = null;
if (dEntity.matches(OldOwner)) {
entity = dEntity.valueOf(OldOwner);
if (dEntity.matches(oldOwner)) {
entity = dEntity.valueOf(oldOwner);
}

String type;
Expand All @@ -650,12 +669,12 @@ else if (entity != null) {
type = "server";
}
world_script_events.add(type + " flag expires");
world_script_events.add(type + " flag " + OldName + " expires");
world_script_events.add(type + " flag " + oldName + " expires");

context.put("owner", Element.valueOf(OldOwner));
context.put("name", Element.valueOf(OldName));
context.put("type", Element.valueOf(type));
context.put("old_value", OldValue);
context.put("owner", new Element(oldOwner));
context.put("name", new Element(oldName));
context.put("type", new Element(type));
context.put("old_value", oldValue);

world_script_events.add("flag expires");

Expand Down Expand Up @@ -837,6 +856,21 @@ public Value() {
index = 0;
}

public void mustBeList() {
if (values == null) {
values = new ArrayList<>();
if (size != 0) {
values.add(firstValue);
}
}
}

public void fixSize() {
if (values != null) {
size = values.size();
}
}

public Value(String oneValue) {
this.firstValue = oneValue;
size = 1;
Expand Down

0 comments on commit 1bbbb84

Please sign in to comment.