Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cffbdab
commit 96d0a79
Showing
6 changed files
with
233 additions
and
7 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
src/main/java/net/aufdemrand/denizen/objects/Adjustable.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,17 @@ | ||
package net.aufdemrand.denizen.objects; | ||
|
||
import net.aufdemrand.denizen.scripts.commands.core.AdjustCommand; | ||
import net.aufdemrand.denizen.tags.Attribute; | ||
|
||
public interface Adjustable { | ||
|
||
/** | ||
* Gets a specific attribute using this object to fetch the necessary data. | ||
* | ||
* @param mechanism the name of the mechanism | ||
* @param value the value of the mechanism | ||
* @return a string result of the fetched attribute | ||
*/ | ||
public void adjust(Mechanism mechanism, Element value); | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/net/aufdemrand/denizen/objects/Mechanism.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,27 @@ | ||
package net.aufdemrand.denizen.objects; | ||
|
||
public class Mechanism { | ||
|
||
boolean fulfilled; | ||
String raw_mechanism; | ||
String outcome = null; | ||
|
||
public Mechanism(String string) { | ||
fulfilled = false; | ||
raw_mechanism = string; | ||
} | ||
|
||
public void fulfill(String outcome) { | ||
fulfilled = true; | ||
outcome = outcome; | ||
} | ||
|
||
public boolean matches(String string) { | ||
return (string.equalsIgnoreCase(raw_mechanism)); | ||
} | ||
|
||
public boolean fulfilled() { | ||
return fulfilled; | ||
} | ||
|
||
} |
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
79 changes: 79 additions & 0 deletions
79
src/main/java/net/aufdemrand/denizen/scripts/commands/core/AdjustCommand.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,79 @@ | ||
package net.aufdemrand.denizen.scripts.commands.core; | ||
|
||
import net.aufdemrand.denizen.exceptions.CommandExecutionException; | ||
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException; | ||
import net.aufdemrand.denizen.objects.*; | ||
import net.aufdemrand.denizen.scripts.ScriptEntry; | ||
import net.aufdemrand.denizen.scripts.commands.AbstractCommand; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
|
||
|
||
public class AdjustCommand extends AbstractCommand { | ||
|
||
@Override | ||
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException { | ||
|
||
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) { | ||
|
||
if (!scriptEntry.hasObject("object")) { | ||
scriptEntry.addObject("object", arg.asElement()); | ||
|
||
} else if (!scriptEntry.hasObject("mechanism")) { | ||
if (arg.hasPrefix()) { | ||
scriptEntry.addObject("mechanism", new Element(arg.getPrefix().getValue())); | ||
scriptEntry.addObject("mechanism_value", arg.asElement()); | ||
} else | ||
scriptEntry.addObject("mechanism", arg.asElement()); | ||
|
||
} | ||
|
||
} | ||
|
||
// Check for valid object | ||
|
||
// Check for valid mechanism | ||
|
||
} | ||
|
||
|
||
@Override | ||
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException { | ||
|
||
String object = scriptEntry.getElement("object").asString(); | ||
|
||
Class object_class = net.aufdemrand.denizen.tags.ObjectFetcher.getObjectClass(object.split("@")[0]); | ||
|
||
if (object_class == null) | ||
throw new CommandExecutionException("Unfetchable object found '" + object + '\''); | ||
|
||
dObject fetched; | ||
|
||
try { | ||
// Check to make sure this is a valid constructor by checking the 'matches' static method | ||
if (!((Boolean) object_class.getMethod("matches", String.class) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
.invoke(null, object))) | ||
throw new CommandExecutionException('\'' + object + "' is returning null."); | ||
|
||
// Get the object with the 'valueOf' static method | ||
fetched = (dObject) object_class.getMethod("valueOf", String.class) | ||
.invoke(null, object); | ||
|
||
// Make sure this object is Adjustable | ||
if (!(fetched instanceof Adjustable)) | ||
throw new CommandExecutionException('\'' + object + "' is not adjustable."); | ||
|
||
// Do the adjustment! | ||
((Adjustable) fetched).adjust(new Mechanism(scriptEntry.getElement("mechanism").asString()), | ||
scriptEntry.getElement("mechanism_value")); | ||
|
||
} catch (NoSuchMethodException e) { } catch (SecurityException e) { | ||
} catch (InvocationTargetException e) { } catch (IllegalAccessException e) { } | ||
|
||
// :) | ||
|
||
} | ||
|
||
|
||
|
||
} |
Cache the expensive .getMethod call?