aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/re/jag/parquet/commands/Calculator.java
blob: ee8bc53ba4852e8e7d5d093b5b314b6e8b7e51d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package re.jag.parquet.commands;

import static net.minecraft.server.command.CommandManager.argument;
import static net.minecraft.server.command.CommandManager.literal;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;

import net.minecraft.command.argument.MessageArgumentType;
import net.minecraft.entity.Entity;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text;

import java.util.ArrayList;

public class Calculator {
	public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
		LiteralArgumentBuilder<ServerCommandSource> r = literal("r").executes( (c) -> help(c.getSource()) ).then(
				argument("expression", MessageArgumentType.message()).executes((c) -> calc(c.getSource(), MessageArgumentType.getMessage(c, "expression")))
		);

		dispatcher.register(r);
	}

	private static int calc(ServerCommandSource _source, Text _expression) {
		String expression = _expression.asString();
		expression = expression.replace(" ", "");
		char expression_array[] = expression.toCharArray();
		ArrayList<String> parts = new ArrayList<String>();
		ArrayList<String> operators = new ArrayList<String>();

		int last_break=-1;
		String element="";
		for(int i = 0; i < expression_array.length; i++) {
			int op_weight = get_operator_weight(expression_array[i]);

			if (Math.abs(last_break - i) > 1 && op_weight > 0) {
				parts.add(element);
				operators.add(String.copyValueOf(expression_array, i, 1));

				element = "";
				last_break = i;
			} else {
				element+=expression_array[i];
			}
		}

		if(element.length() > 0)
			parts.add(element);

		int current_level = 2;
		while(current_level > 0) {
			int level_count = 0;
			for (int o = 0; o < operators.size(); o++) {
				String operator = operators.get(o);
				if (get_operator_weight(operator) == current_level) {
					level_count++;

					String result = execute_operation(operator, parts.get(o), parts.get(o+1), _source);
					parts.set(o, result);
					parts.remove(o+1);
					operators.remove(o);
					break;
				}
			}

			if (level_count == 0)
				current_level--;
		}

		_source.sendFeedback(new LiteralText(parts.get(0)), false);

		return 0;
	}

	private static Integer parse_operand(String _operand, ServerCommandSource _source) {
		Integer operand = null;

		try {
			operand = Integer.parseInt(_operand);
		} catch (NumberFormatException e) {
			Entity player = _source.getEntity();
			switch (_operand) {
				case "x":
					if (player != null)
						operand = (int)player.getPos().x;
					break;
				case "y":
					if (player != null)
						operand = (int)player.getPos().y;
					break;
				case "z":
					if (player != null)
						operand = (int)player.getPos().z;
					break;
				default:
			}
		}

		if (operand == null) {
			String msg = "Parsing failed for Operand \"" + _operand + "\"";
			_source.sendError(new LiteralText(msg));
		}

		return operand;
	}

	private static int get_operator_weight(char _c) {
		switch(_c) {
			case '+':
			case '-':
				return 1;
			case '*':
			case '/':
				return 2;
		}
		return 0;
	}

	private static int get_operator_weight(String _s) {
		if (_s.length() != 1)
			return 0;
		return get_operator_weight(_s.toCharArray()[0]);
	}

	private static String execute_operation(String _operator, String _left, String _right, ServerCommandSource _source) {
		String result = null;

		Integer left = parse_operand(_left, _source);
		Integer right = parse_operand(_right, _source);

		if (left == null || right == null)
			return null;

		switch (_operator) {
			case "+":
				result = String.valueOf(left + right);
				break;
			case "-":
				result = String.valueOf(left - right);
				break;
			case "*":
				result = String.valueOf(left * right);
				break;
			case "/":
				result = String.valueOf(left / right);
				break;
		}

		return result;
	}

	private static int help(ServerCommandSource _source){
		_source.sendFeedback(new LiteralText("RTFM"), false);
		return 0;
	}
}