-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(initial) data action system, define command support
- Loading branch information
1 parent
37db2da
commit 8dafa00
Showing
8 changed files
with
402 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/java/net/aufdemrand/denizencore/utilities/data/ActionableDataProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package net.aufdemrand.denizencore.utilities.data; | ||
|
||
import net.aufdemrand.denizencore.objects.dObject; | ||
|
||
public abstract class ActionableDataProvider { | ||
|
||
/** | ||
* Return the value object at a key. | ||
* Result should generally be Element or dList. | ||
*/ | ||
public abstract dObject getValueAt(String keyName); | ||
|
||
/** | ||
* Set the valueu object to a key. | ||
* Value will be Element or dList. | ||
* null indicates to remove the key. | ||
*/ | ||
public abstract void setValueAt(String keyName, dObject value); | ||
} |
184 changes: 184 additions & 0 deletions
184
src/main/java/net/aufdemrand/denizencore/utilities/data/DataAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
package net.aufdemrand.denizencore.utilities.data; | ||
|
||
import net.aufdemrand.denizencore.objects.Element; | ||
import net.aufdemrand.denizencore.objects.aH; | ||
import net.aufdemrand.denizencore.objects.dList; | ||
import net.aufdemrand.denizencore.objects.dObject; | ||
import net.aufdemrand.denizencore.utilities.CoreUtilities; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.RoundingMode; | ||
|
||
public class DataAction { | ||
|
||
public ActionableDataProvider provider; | ||
|
||
public DataActionType type; | ||
|
||
public String key; | ||
|
||
/** | ||
* Zero = no index needed. | ||
* Positive number = list index (starting at 1). | ||
*/ | ||
public int index = 0; | ||
|
||
public dObject inputValue = null; | ||
|
||
public String debug() { | ||
return aH.debugObj("action", "(" + key + "[" + index + "]:" + type + ":" + inputValue + ")"); | ||
} | ||
|
||
public dList autoList(String key) { | ||
dObject obj = provider.getValueAt(key); | ||
if (obj == null) { | ||
return new dList(); | ||
} | ||
else { | ||
return autoList(dList.getListFor(obj)); | ||
} | ||
} | ||
|
||
public dList autoList(dList list) { | ||
return new dList(list); | ||
} | ||
|
||
public dObject autoDup(dObject object) { | ||
if (object == null) { | ||
return null; | ||
} | ||
if (object instanceof dList) { | ||
return autoList((dList) object); | ||
} | ||
return new Element(object.toString()); | ||
} | ||
|
||
public BigDecimal autoNumber() { | ||
dObject obj = provider.getValueAt(key); | ||
if (index != 0) { | ||
dList subList = dList.getListFor(obj); | ||
if (index < 0 || index > subList.size()) { | ||
return BigDecimal.ZERO; | ||
} | ||
obj = subList.getObject(index - 1); | ||
} | ||
return autoNumber(obj); | ||
} | ||
|
||
public BigDecimal autoNumber(dObject obj) { | ||
if (obj == null) { | ||
return BigDecimal.ZERO; | ||
} | ||
return new BigDecimal(obj.toString()); | ||
} | ||
|
||
public Element autoNumber(BigDecimal decimal) { | ||
return new Element(decimal); | ||
} | ||
|
||
public void autoSet(dObject value) { | ||
if (index != 0) { | ||
dObject obj = provider.getValueAt(key); | ||
dList subList = dList.getListFor(obj); | ||
subList.setObject(index - 1, value); | ||
value = subList; | ||
} | ||
provider.setValueAt(key, value); | ||
} | ||
|
||
public void requiresInputValue() { | ||
if (inputValue == null) { | ||
throw new DataActionException("Input value required for data action " + type + "."); | ||
} | ||
} | ||
|
||
public void execute() { | ||
switch (type) { | ||
case INCREMENT: { | ||
BigDecimal num = autoNumber(); | ||
num = num.add(BigDecimal.ONE); | ||
autoSet(autoNumber(num)); | ||
break; | ||
} | ||
case DECREMENT: { | ||
BigDecimal num = autoNumber(); | ||
num = num.subtract(BigDecimal.ONE); | ||
autoSet(autoNumber(num)); | ||
break; | ||
} | ||
case ADD: { | ||
requiresInputValue(); | ||
BigDecimal num = autoNumber(); | ||
num = num.add(autoNumber(inputValue)); | ||
autoSet(autoNumber(num)); | ||
break; | ||
} | ||
case SUBTRACT: { | ||
requiresInputValue(); | ||
BigDecimal num = autoNumber(); | ||
num = num.subtract(autoNumber(inputValue)); | ||
autoSet(autoNumber(num)); | ||
break; | ||
} | ||
case MULTIPLY: { | ||
requiresInputValue(); | ||
BigDecimal num = autoNumber(); | ||
num = num.multiply(autoNumber(inputValue)); | ||
autoSet(autoNumber(num)); | ||
break; | ||
} | ||
case DIVIDE: { | ||
requiresInputValue(); | ||
BigDecimal num = autoNumber(); | ||
num = num.setScale(15, RoundingMode.HALF_UP); | ||
num = num.divide(autoNumber(inputValue), RoundingMode.HALF_UP); | ||
autoSet(autoNumber(num)); | ||
break; | ||
} | ||
case INSERT: { | ||
requiresInputValue(); | ||
dList list = autoList(key); | ||
list.addObject(inputValue); | ||
provider.setValueAt(key, list); | ||
break; | ||
} | ||
case REMOVE: { | ||
dList list = autoList(key); | ||
if (index != 0) { | ||
list.remove(index - 1); | ||
} | ||
requiresInputValue(); | ||
String findValue = CoreUtilities.toLowerCase(inputValue.toString()); | ||
for (int i = 0; i < list.size(); i++) { | ||
if (CoreUtilities.toLowerCase(list.get(i)).equals(findValue)) { | ||
list.remove(i); | ||
break; | ||
} | ||
} | ||
provider.setValueAt(key, list); | ||
break; | ||
} | ||
case SPLIT: { | ||
requiresInputValue(); | ||
dList list = autoList(key); | ||
list.addObjects(dList.getListFor(inputValue).objectForms); | ||
provider.setValueAt(key, list); | ||
break; | ||
} | ||
case SPLIT_NEW: | ||
requiresInputValue(); | ||
provider.setValueAt(key, autoList(dList.getListFor(inputValue))); | ||
break; | ||
case SET: | ||
requiresInputValue(); | ||
provider.setValueAt(key, autoDup(inputValue)); | ||
break; | ||
case AUTO_SET: | ||
provider.setValueAt(key, new Element(true)); | ||
break; | ||
case CLEAR: | ||
provider.setValueAt(key, null); | ||
break; | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/net/aufdemrand/denizencore/utilities/data/DataActionException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package net.aufdemrand.denizencore.utilities.data; | ||
|
||
public class DataActionException extends RuntimeException { | ||
|
||
private static final long serialVersionUID = 3159123523457793068L; | ||
|
||
public DataActionException(String message) { | ||
super(message); | ||
} | ||
} |
Oops, something went wrong.