Skip to content

Commit

Permalink
Version 1.3.5 (update to script system)
Browse files Browse the repository at this point in the history
Mathematical expressions and Else action included to script system
  • Loading branch information
Draww committed Feb 10, 2018
1 parent a732e79 commit 9ea064a
Show file tree
Hide file tree
Showing 12 changed files with 300 additions and 235 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

335 changes: 132 additions & 203 deletions .idea/workspace.xml

Large diffs are not rendered by default.

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.drawwdev</groupId>
<artifactId>Raffle</artifactId>
<version>1.3.4a</version>
<version>1.3.5</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
79 changes: 62 additions & 17 deletions src/main/java/com/drawwdev/raffle/utils/ScriptSystem.java

Large diffs are not rendered by default.

87 changes: 87 additions & 0 deletions src/main/java/com/drawwdev/raffle/utils/arithmeticExpression.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.drawwdev.raffle.utils;

public class arithmeticExpression {

public static double eval(final String str) {
return new Object() {
int pos = -1, ch;

void nextChar() {
ch = (++pos < str.length()) ? str.charAt(pos) : -1;
}

boolean eat(int charToEat) {
while (ch == ' ') nextChar();
if (ch == charToEat) {
nextChar();
return true;
}
return false;
}

double parse() {
nextChar();
double x = parseExpression();
if (pos < str.length()) throw new RuntimeException("Unexpected: " + (char) ch);
return x;
}

// Grammar:
// expression = term | expression `+` term | expression `-` term
// term = factor | term `*` factor | term `/` factor
// factor = `+` factor | `-` factor | `(` expression `)`
// | number | functionName factor | factor `^` factor

double parseExpression() {
double x = parseTerm();
for (; ; ) {
if (eat('+')) x += parseTerm(); // addition
else if (eat('-')) x -= parseTerm(); // subtraction
else return x;
}
}

double parseTerm() {
double x = parseFactor();
for (; ; ) {
if (eat('*')) x *= parseFactor(); // multiplication
else if (eat('/')) x /= parseFactor(); // division
else if (eat('^'))
x = Math.pow(x, parseFactor()); //exponentiation -> Moved in to here. So the problem is fixed
else return x;
}
}

double parseFactor() {
if (eat('+')) return parseFactor(); // unary plus
if (eat('-')) return -parseFactor(); // unary minus

double x;
int startPos = this.pos;
if (eat('(')) { // parentheses
x = parseExpression();
eat(')');
} else if ((ch >= '0' && ch <= '9') || ch == '.') { // numbers
while ((ch >= '0' && ch <= '9') || ch == '.') nextChar();
x = Double.parseDouble(str.substring(startPos, this.pos));
} else if (ch >= 'a' && ch <= 'z') { // functions
while (ch >= 'a' && ch <= 'z') nextChar();
String func = str.substring(startPos, this.pos);
x = parseFactor();
if (func.equals("sqrt")) x = Math.sqrt(x);
else if (func.equals("sin")) x = Math.sin(Math.toRadians(x));
else if (func.equals("cos")) x = Math.cos(Math.toRadians(x));
else if (func.equals("tan")) x = Math.tan(Math.toRadians(x));
else throw new RuntimeException("Unknown function: " + func);
} else {
throw new RuntimeException("Unexpected: " + (char) ch);
}

//if (eat('^')) x = Math.pow(x, parseFactor()); // exponentiation -> This is causing a bit of problem

return x;
}
}.parse();
}

}
5 changes: 3 additions & 2 deletions src/main/resources/custom.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
CustomRaffles:
example:
name: 'example1'
name: example1
datatype: 'Numeral'
time: 5
predicate:
- '#Script#[IF]$arg1.type=NUMBER'
- '#Script#[IF]%player%==Draww'
- '#Script#[IF]$math($arg1 + 1)==6.0 [ElseAction] [Message] example1;[Message] example2'
actions:
- '[Broadcast] %prefix% &7Hello, &a%player%'
- '[Broadcast] %prefix% &7Your World, &a%player_world%'
- '[Broadcast] %prefix% &7Example, &6$arg1'
- '[Broadcast] %prefix% &7Example, &6$math($arg1 + 1)'
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Raffle
main: com.drawwdev.raffle.Main
version: 1.3.4a
version: 1.3.5
author: drawwdev
commands:
raffle:
Expand Down
5 changes: 3 additions & 2 deletions target/classes/custom.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
CustomRaffles:
example:
name: 'example1'
name: example1
datatype: 'Numeral'
time: 5
predicate:
- '#Script#[IF]$arg1.type=NUMBER'
- '#Script#[IF]%player%==Draww'
- '#Script#[IF]$math($arg1 + 1)==6.0 [ElseAction] [Message] example1;[Message] example2'
actions:
- '[Broadcast] %prefix% &7Hello, &a%player%'
- '[Broadcast] %prefix% &7Your World, &a%player_world%'
- '[Broadcast] %prefix% &7Example, &6$arg1'
- '[Broadcast] %prefix% &7Example, &6$math($arg1 + 1)'
2 changes: 1 addition & 1 deletion target/classes/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Raffle
main: com.drawwdev.raffle.Main
version: 1.3.4
version: 1.3.5
author: drawwdev
commands:
raffle:
Expand Down
4 changes: 2 additions & 2 deletions target/maven-archiver/pom.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Generated by Maven
#Fri Feb 09 08:51:35 EET 2018
version=1.3.4
#Sat Feb 10 09:17:21 EET 2018
version=1.3.5
groupId=com.drawwdev
artifactId=Raffle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ com\drawwdev\raffle\utils\StringUtil.class
com\drawwdev\raffle\RaffleBuilder.class
com\drawwdev\raffle\depend\EconomyDepend.class
com\drawwdev\raffle\CustomRaffle.class
com\drawwdev\raffle\utils\arithmeticExpression.class
com\drawwdev\raffle\RaffleLoader.class
com\drawwdev\raffle\RaffleStorage.class
com\drawwdev\raffle\depend\DependType.class
Expand All @@ -16,6 +17,7 @@ com\drawwdev\raffle\RaffleConsumer.class
com\drawwdev\raffle\Raffle.class
com\drawwdev\raffle\RaffleType.class
com\drawwdev\raffle\utils\ScriptSystem.class
com\drawwdev\raffle\utils\arithmeticExpression$1.class
com\drawwdev\raffle\RaffleException.class
com\drawwdev\raffle\nms\Compatability_1_12_R1.class
com\drawwdev\raffle\utils\InventoryUtils.class
Expand Down

0 comments on commit 9ea064a

Please sign in to comment.