Skip to content

Commit

Permalink
Special functions
Browse files Browse the repository at this point in the history
  • Loading branch information
itsmaxymoo committed Mar 13, 2022
1 parent 1bad093 commit 9cca147
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 9 deletions.
35 changes: 30 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ Of course, `_showtext` supports [standard Minecraft formatting codes](https://mi

The `_showstring` command will display to the user a string defined in the plugin's `config.yml` under the `strings` section.
The config file can be reloaded at any time using the `aliasshowtext-reload` command (OP only or permission `aliasshowtext.reload`).
Advanced strings have the advantage of supporting arguments. An example string in config.yml:

### Arguments

Arguments are only supported by `_showstring` and use a `{{ argument number }}` format. For example:

```yaml
strings:
Expand All @@ -41,13 +44,35 @@ which would display `String name: argument-test, First argument: Apple` to the u
Some things to note:

* The 0th argument is always the string name
* Arguments are processed before functions
* Arguments follow the format `{{ n }}` where `n` is the number argument you wish to use
* If you try to access an argument that does not exist, the `{{ n }}` tag will **not** be replaced.
* If you try to access an argument that does not exist, the `{{ n }}` tag will **not** be replaced

### Functions

Functions are only supported by `_showstring` and use a `{% func_name arg1, arg2, etc... %}` format. For example:

```yaml
strings:
end: "End portal entrance: 1140, 117, 1808 ({% distance3d 1140 117 1801 %} blocks away)"
```

...will tell the user how far away they are from the server's end portal entrance.
**Please remember,** arguments are processed before functions!
This allows you to pass argument values as function parameters.

#### Function List

| **Function** | **Arguments** | **Usage** |
|--------------|---------------|-------------------------------------------------------------------------------------------------------------|
| `distance3d` | num, num, num | Returns the user's distance from the specified position. Player only. |
| `distance2d` | num, num | Returns the user's distance from the specified position, only considering X and Z coordinates. Player only. |

## To-Do

* ~~Support for storing text strings in a seperate file~~
* ~~Allow server operator to reload strings at runtime~~
* ~~Allow text strings to support arguments~~
* Support for special text functions
* ~~Allow server operator to reload strings at runtime~~
* ~~Allow text strings to support arguments~~
* ~~Support for special text functions~~
* Support for special cases (ex: joining the server)
* Strict/non-strict mode: Allow certain functionality based off of a boolean setting (ex: functions in _showtext)
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.itsmaxymoo</groupId>
<artifactId>AliasShowText</artifactId>
<version>0.1-SNAPSHOT</version>
<version>0.2-SNAPSHOT</version>
<packaging>jar</packaging>

<name>AliasShowText</name>
Expand Down
39 changes: 38 additions & 1 deletion src/main/java/com/itsmaxymoo/aliasshowtext/ArgString.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package com.itsmaxymoo.aliasshowtext;

import com.itsmaxymoo.aliasshowtext.textfunction.GetDistance2D;
import com.itsmaxymoo.aliasshowtext.textfunction.GetDistance3D;
import com.itsmaxymoo.aliasshowtext.textfunction.InvalidSenderTypeException;
import com.itsmaxymoo.aliasshowtext.textfunction.TextFunction;
import org.bukkit.command.CommandSender;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -10,9 +16,11 @@ public ArgString(String s) {
rawString = s;
}

public String getFormatted(String[] args) {
public String getFormatted(CommandSender sender, String[] args) {
String formattedString = rawString;

// *** Process args first

// Setup regex
Pattern patternArg = Pattern.compile("(\\{\\{ *[0-9]+ *}})");
Pattern patternArgNumber = Pattern.compile("[0-9]+");
Expand All @@ -36,6 +44,35 @@ public String getFormatted(String[] args) {
}
}

// *** Process functions
TextFunction[] textFunctions = {
new GetDistance3D(),
new GetDistance2D()
};
for (TextFunction tf : textFunctions) {
// Get instances of command (outer regex)
Pattern outerRegex = Pattern.compile("\\{% *" + tf.getRegex() + " *%}");
Matcher matcherCommandInstances = outerRegex.matcher(formattedString);
while (matcherCommandInstances.find()) {
String match = matcherCommandInstances.group();
// Get inner regex
Matcher matcherInner = tf.getRegex().matcher(match);
matcherInner.find();

String innerMatch = matcherInner.group();

// Set result
String result = "";
try {
result = tf.compute(sender, innerMatch.trim().split("\\s+"));
} catch (InvalidSenderTypeException e) {
return "You must be a player!";
}

formattedString = formattedString.replace(match, result);
}
}

return formattedString;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
ArgString aString = ConfigManager.getArgString(args[0]);

if (aString != null) {
sender.sendMessage(aString.getFormatted(args));
sender.sendMessage(aString.getFormatted(sender, args));

return true;
} else {
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/itsmaxymoo/aliasshowtext/CommandShowText.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.checkerframework.checker.units.qual.A;

public class CommandShowText implements CommandExecutor {
@Override
Expand All @@ -11,12 +12,19 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
return false;
}

// Create string from arguments

StringBuilder sb = new StringBuilder();

for (String token : args) {
/* for (String token : args) {
sb.append(token).append(" ");
}
// Create ArgString to process potential function calls
ArgString argString = new ArgString(sb.toString());
sender.sendMessage(argString.getFormatted(sender, new String[]{})); */

sender.sendMessage(sb.toString());

return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.itsmaxymoo.aliasshowtext.textfunction;

import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.regex.Pattern;

public class GetDistance2D extends TextFunction {
@Override
public Pattern getRegex() {
return Pattern.compile("(distance2d) +-?[0-9]+ +-?[0-9]+");
}

@Override
public String compute(CommandSender sender, String[] args) throws InvalidSenderTypeException {
if (!(sender instanceof Player)) {
throw new InvalidSenderTypeException("You must be a player!");
}

Player player = (Player) sender;

int tx, tz;

try {
tx = Integer.parseInt(args[1]);
tz = Integer.parseInt(args[2]);
} catch (NumberFormatException e) {
return "ERROR";
}

int px = player.getLocation().getBlockX();
int pz = player.getLocation().getBlockZ();

return "" + Math.round(
Math.sqrt(Math.pow(tx - px, 2) + Math.pow(tz - pz, 2))
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.itsmaxymoo.aliasshowtext.textfunction;

import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.regex.Pattern;

public class GetDistance3D extends TextFunction {
@Override
public Pattern getRegex() {
return Pattern.compile("(distance3d) +-?[0-9]+ +-?[0-9]+ +-?[0-9]+");
}

@Override
public String compute(CommandSender sender, String[] args) throws InvalidSenderTypeException {
if (!(sender instanceof Player)) {
throw new InvalidSenderTypeException("You must be a player!");
}

Player player = (Player) sender;

int tx, ty, tz;

try {
tx = Integer.parseInt(args[1]);
ty = Integer.parseInt(args[2]);
tz = Integer.parseInt(args[3]);
} catch (NumberFormatException e) {
return "ERROR";
}

int px = player.getLocation().getBlockX();
int py = player.getLocation().getBlockY();
int pz = player.getLocation().getBlockZ();

return "" + Math.round(
Math.sqrt(Math.pow(tx - px, 2) + Math.pow(ty - py, 2) + Math.pow(tz - pz, 2))
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.itsmaxymoo.aliasshowtext.textfunction;

public class InvalidSenderTypeException extends Exception{
public InvalidSenderTypeException(String message){
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.itsmaxymoo.aliasshowtext.textfunction;

import org.bukkit.command.CommandSender;

import java.util.regex.Pattern;

public abstract class TextFunction {
public abstract Pattern getRegex();

public abstract String compute(CommandSender sender, String[] args) throws InvalidSenderTypeException;
}

0 comments on commit 9cca147

Please sign in to comment.