SteamWar/BauSystem2.0
Archiviert
12
0

Fix script commands

Add BlockPlace, BlockBreak, RightClick and LeftClick to script event system

Signed-off-by: yoyosource <yoyosource@nidido.de>
Dieser Commit ist enthalten in:
yoyosource 2021-09-21 12:53:00 +02:00
Ursprung 841921d458
Commit 0373408f26
24 geänderte Dateien mit 373 neuen und 123 gelöschten Zeilen

Datei anzeigen

@ -24,8 +24,9 @@ import de.steamwar.inventory.SWItem;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.experimental.UtilityClass; import lombok.experimental.UtilityClass;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerEvent;
import org.bukkit.inventory.meta.BookMeta; import org.bukkit.inventory.meta.BookMeta;
import yapion.hierarchy.types.YAPIONArray; import yapion.hierarchy.types.YAPIONArray;
import yapion.hierarchy.types.YAPIONMap; import yapion.hierarchy.types.YAPIONMap;
@ -47,7 +48,7 @@ public class CustomScript {
public interface CustomEvent extends Script { public interface CustomEvent extends Script {
String eventName(); String eventName();
boolean execute(PlayerEvent e, Map<String, Value> variables); boolean execute(Event e, Player p, Map<String, Value> variables);
} }
@RequiredArgsConstructor @RequiredArgsConstructor
@ -61,8 +62,8 @@ public class CustomScript {
} }
@Override @Override
public boolean execute(PlayerEvent e, Map<String, Value> variables) { public boolean execute(Event e, Player p, Map<String, Value> variables) {
new ScriptExecutor(bookMeta, e.getPlayer(), variables); new ScriptExecutor(bookMeta, p, variables);
return true; return true;
} }
} }
@ -78,8 +79,8 @@ public class CustomScript {
} }
@Override @Override
public boolean execute(PlayerEvent e, Map<String, Value> variables) { public boolean execute(Event e, Player p, Map<String, Value> variables) {
new ScriptExecutor(pages, e.getPlayer(), variables); new ScriptExecutor(pages, p, variables);
return false; return false;
} }

Datei anzeigen

@ -33,8 +33,13 @@ import lombok.Getter;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.player.*; import org.bukkit.event.player.*;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -95,13 +100,13 @@ public class CustomScriptListener implements Listener {
YAPIONObject yapionObject; YAPIONObject yapionObject;
if (s == null) { if (s == null) {
yapionObject = new YAPIONObject(); yapionObject = new YAPIONObject();
yapionObject.getYAPIONMapOrSetDefault("events", new YAPIONMap()).add("FF", new YAPIONArray().add("#!EVENT FF\ngui")); yapionObject.getYAPIONMapOrSetDefault("events", new YAPIONMap()).add("FF", new YAPIONArray().add("#!EVENT FF /gui Kürzel\ngui"));
} else { } else {
yapionObject = YAPIONParser.parse(s); yapionObject = YAPIONParser.parse(s);
if (yapionObject.containsKey("")) { if (yapionObject.containsKey("")) {
yapionObject.add("commands", yapionObject.getMap("")); yapionObject.add("commands", yapionObject.getMap(""));
yapionObject.remove(""); yapionObject.remove("");
yapionObject.getYAPIONMapOrSetDefault("events", new YAPIONMap()).add("FF", new YAPIONArray().add("#!EVENT FF\ngui")); yapionObject.getYAPIONMapOrSetDefault("events", new YAPIONMap()).add("FF", new YAPIONArray().add("#!EVENT FF /gui Kürzel\ngui"));
} }
} }
@ -251,28 +256,66 @@ public class CustomScriptListener implements Listener {
openCommandsMenu(p); openCommandsMenu(p);
} }
@AllArgsConstructor
@Getter @Getter
public enum EventType { public enum EventType {
FF(PlayerSwapHandItemsEvent.class, event -> null); FF(PlayerSwapHandItemsEvent.class, event -> null),
PlaceBlock(BlockPlaceEvent.class, event -> {
Map<String, Value> valueMap = new HashMap<>();
valueMap.put("blockX", new Value.LongValue(event.getBlockPlaced().getX()));
valueMap.put("blockY", new Value.LongValue(event.getBlockPlaced().getY()));
valueMap.put("blockZ", new Value.LongValue(event.getBlockPlaced().getZ()));
valueMap.put("blockType", new Value.StringValue(event.getBlockPlaced().getType().name()));
return valueMap;
}),
BreakBlock(BlockBreakEvent.class, event -> {
Map<String, Value> valueMap = new HashMap<>();
valueMap.put("blockX", new Value.LongValue(event.getBlock().getX()));
valueMap.put("blockY", new Value.LongValue(event.getBlock().getY()));
valueMap.put("blockZ", new Value.LongValue(event.getBlock().getZ()));
valueMap.put("blockType", new Value.StringValue(event.getBlock().getType().name()));
return valueMap;
}),
RightClick(PlayerInteractEvent.class, event -> {
Map<String, Value> valueMap = new HashMap<>();
valueMap.put("blockInHand", new Value.BooleanValue(event.isBlockInHand()));
return valueMap;
}),
LeftClick(PlayerInteractEvent.class, event -> {
Map<String, Value> valueMap = new HashMap<>();
valueMap.put("blockInHand", new Value.BooleanValue(event.isBlockInHand()));
return valueMap;
});
private Class<? extends PlayerEvent> eventType; private Class<? extends Event> eventType;
private Function<PlayerEvent, Map<String, Value>> eventValues; private Function<Event, Map<String, Value>> eventValues;
<T extends Event> EventType(Class<T> eventType, Function<T, Map<String, Value>> eventValues) {
this.eventType = eventType;
this.eventValues = event -> {
return eventValues.apply((T) event);
};
}
} }
private void callEvent(EventType eventType, PlayerEvent e) { private void callEvent(EventType eventType, Player p, Event e) {
if (!eventType.getEventType().equals(e.getClass())) { if (!eventType.getEventType().equals(e.getClass())) {
return; return;
} }
List<CustomScript.CustomEvent> customEvents = playerMap.getOrDefault(e.getPlayer(), new ArrayList<>()).stream().filter(CustomScript.CustomEvent.class::isInstance).map(CustomScript.CustomEvent.class::cast).collect(Collectors.toList()); List<CustomScript.CustomEvent> customEvents = playerMap.getOrDefault(p, new ArrayList<>()).stream().filter(CustomScript.CustomEvent.class::isInstance).map(CustomScript.CustomEvent.class::cast).collect(Collectors.toList());
for (CustomScript.CustomEvent customEvent : customEvents) { for (CustomScript.CustomEvent customEvent : customEvents) {
if (customEvent.eventName().equals(eventType.name())) { if (customEvent.eventName().equals(eventType.name())) {
Map<String, Value> variables = eventType.getEventValues().apply(e); Map<String, Value> variables = eventType.getEventValues().apply(e);
if (variables == null) { if (variables == null) {
variables = new HashMap<>(); variables = new HashMap<>();
} }
customEvent.execute(e, variables); customEvent.execute(e, p, variables);
if (e instanceof Cancellable && variables.containsKey("cancel")) {
Value value = variables.get("cancel");
if (value.asBoolean()) {
((Cancellable) e).setCancelled(true);
}
}
return; return;
} }
} }
@ -307,9 +350,29 @@ public class CustomScriptListener implements Listener {
@EventHandler @EventHandler
public void onPlayerSwapHandItems(PlayerSwapHandItemsEvent event) { public void onPlayerSwapHandItems(PlayerSwapHandItemsEvent event) {
if (LAST_FS.contains(event.getPlayer())) { if (LAST_FS.contains(event.getPlayer())) {
callEvent(EventType.FF, event); callEvent(EventType.FF, event.getPlayer(), event);
} else { } else {
LAST_FS.add(event.getPlayer()); LAST_FS.add(event.getPlayer());
} }
} }
@EventHandler
public void onBlockPlace(BlockPlaceEvent event) {
callEvent(EventType.PlaceBlock, event.getPlayer(), event);
}
@EventHandler
public void onBlockBreak(BlockBreakEvent event) {
callEvent(EventType.BreakBlock, event.getPlayer(), event);
}
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
callEvent(EventType.RightClick, event.getPlayer(), event);
}
if (event.getAction() == Action.LEFT_CLICK_AIR || event.getAction() == Action.LEFT_CLICK_BLOCK) {
callEvent(EventType.RightClick, event.getPlayer(), event);
}
}
} }

Datei anzeigen

@ -43,7 +43,7 @@ public class ScriptCommand extends SWCommand {
} }
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, "§eCustom Commands", Arrays.asList("§7Schreibe§8: §7#!CMD 'COMMAND'", "§7an den Anfang eines Script Buches um", "§7ein Custom Command zu nutzen. Der", "§7Befehl startet immer mit / und kann dann so", "§7aufgebaut sein wie du willst. Alles was in Spitzen", "§7Klammern steht '<>' wird als Parameter und somit", "§7als Variable gewertet.", "§7Parameter, welche in runden Klammern", "§7stehen sind Optional. Einfache", "§7Texte als Parameter bekommen", "§7eine gleichnamige Variable mit", "§7true/false als Wert je nachdem", "§7ob dieser angegeben wurde oder nicht"), false, clickType -> { swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, "§eCustom Commands", Arrays.asList("§7Schreibe§8: §7#!CMD 'COMMAND'", "§7an den Anfang eines Script Buches um", "§7ein Custom Command zu nutzen. Der", "§7Befehl startet immer mit / und kann dann so", "§7aufgebaut sein wie du willst. Alles was in Spitzen", "§7Klammern steht '<>' wird als Parameter und somit", "§7als Variable gewertet.", "§7Parameter, welche in runden Klammern", "§7stehen sind Optional. Einfache", "§7Texte als Parameter bekommen", "§7eine gleichnamige Variable mit", "§7true/false als Wert je nachdem", "§7ob dieser angegeben wurde oder nicht"), false, clickType -> {
}), null)); }), null));
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, "§eCustom Events", Arrays.asList("§7Schreibe§8: §7#!EVENT 'EventName'", "§7an den Anfang eines Script Buches um", "§7ein Custom Event zu nutzen.", "§7Nutzbare Events sind:", "§eFF"), false, clickType -> { swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, "§eCustom Events", Arrays.asList("§7Schreibe§8: §7#!EVENT 'EventName'", "§7an den Anfang eines Script Buches um", "§7ein Custom Event zu nutzen. Jedes Event kann durch", "§7'var cancel true' gecancelt werden.", "§7Hinter dem Event Namen stehen die Variablen,", "§7welche im Script durch das Event nutztbar sind.", "§7Nutzbare Events sind:", "§eFF", "§ePlaceBlock §8-§7 blockX, blockY, blockZ, blockType", "§eBreakBlock §8-§7 blockX, blockY, blockZ, blockType", "§eRightClick §8-§7 blockInHand", "§eLeftClick §8-§7 blockInHand"), false, clickType -> {
}), null)); }), null));
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, "§eOther", Arrays.asList("§7Kommentare fangen mit §e#§7 an.", "§7Jump_Points fangen mit §e.§7 an.", "§7Eine Variablen Namen in '<>'", "§7eingeschlossen wird ersetzt, bis zu zwei mal.", "§7Eine Variable in '<>' kann mit 'const.'", "§7oder 'local.' oder 'global.' prefixed werden", "§7für den genauen type wo nach geguckt werden soll"), false, clickType -> { swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, "§eOther", Arrays.asList("§7Kommentare fangen mit §e#§7 an.", "§7Jump_Points fangen mit §e.§7 an.", "§7Eine Variablen Namen in '<>'", "§7eingeschlossen wird ersetzt, bis zu zwei mal.", "§7Eine Variable in '<>' kann mit 'const.'", "§7oder 'local.' oder 'global.' prefixed werden", "§7für den genauen type wo nach geguckt werden soll"), false, clickType -> {
}), null)); }), null));
@ -80,7 +80,7 @@ public class ScriptCommand extends SWCommand {
}); });
swItems.add(new SWListInv.SWListEntry<>(swItem, specialCommand)); swItems.add(new SWListInv.SWListEntry<>(swItem, specialCommand));
}); });
for (int i = 0; i < 9 + 4; i++) { for (int i = 0; i < 9 + 3; i++) {
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.GRAY_STAINED_GLASS_PANE, "§7", new ArrayList<>(), false, clickType -> { swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.GRAY_STAINED_GLASS_PANE, "§7", new ArrayList<>(), false, clickType -> {
}), null)); }), null));
} }
@ -104,17 +104,21 @@ public class ScriptCommand extends SWCommand {
}), null)); }), null));
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.OBSIDIAN, "§7Constant §eprotect", Arrays.asList("§etrue§7 wenn Protect angeschaltet ist."), false, clickType -> { swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.OBSIDIAN, "§7Constant §eprotect", Arrays.asList("§etrue§7 wenn Protect angeschaltet ist."), false, clickType -> {
}), null)); }), null));
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.PLAYER_HEAD, "§7Constant §ex", Arrays.asList("§ex§7 Position des Spielers."), false, clickType -> { swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.PLAYER_HEAD, "§7Constant §ex", Arrays.asList("§ex§7 Position des Spielers.", "§eÜberschreibbar"), false, clickType -> {
}), null)); }), null));
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.PLAYER_HEAD, "§7Constant §ey", Arrays.asList("§ey§7 Position des Spielers."), false, clickType -> { swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.PLAYER_HEAD, "§7Constant §ey", Arrays.asList("§ey§7 Position des Spielers.", "§eÜberschreibbar"), false, clickType -> {
}), null)); }), null));
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.PLAYER_HEAD, "§7Constant §ez", Arrays.asList("§ez§7 Position des Spielers."), false, clickType -> { swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.PLAYER_HEAD, "§7Constant §ez", Arrays.asList("§ez§7 Position des Spielers.", "§eÜberschreibbar"), false, clickType -> {
}), null)); }), null));
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.NAME_TAG, "§7Constant §ename", Arrays.asList("§eDisplay§7 Name des Spielers."), false, clickType -> { swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.NAME_TAG, "§7Constant §ename", Arrays.asList("§eDisplay§7 Name des Spielers."), false, clickType -> {
}), null)); }), null));
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.IRON_BOOTS, "§7Constant §esneak", Arrays.asList("§etrue§7 wenn der Spieler gerade sneakt."), false, clickType -> { swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.IRON_BOOTS, "§7Constant §esneak", Arrays.asList("§etrue§7 wenn der Spieler gerade sneakt."), false, clickType -> {
}), null)); }), null));
for (int i = 0; i < 6 + 2 * 9; i++) { swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.ARROW, "§7Constant §eslot", Arrays.asList("§e0-8§7 für den ausgewählten slot.", "§eÜberschreibbar"), false, clickType -> {
}), null));
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.GRASS_BLOCK, "§7Constant §eslotmaterial", Arrays.asList("§eMaterial§7 des Items im Slot"), false, clickType -> {
}), null));
for (int i = 0; i < 4 + 2 * 9; i++) {
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.GRAY_STAINED_GLASS_PANE, "§7", new ArrayList<>(), false, clickType -> { swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.GRAY_STAINED_GLASS_PANE, "§7", new ArrayList<>(), false, clickType -> {
}), null)); }), null));
} }

Datei anzeigen

@ -44,11 +44,11 @@ public class Add implements SpecialCommand {
Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]);
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
if (v1.getClass() != Value.LongValue.class) { if (!(v1 instanceof Value.LongValue)) {
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können addiert werden"); scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können addiert werden");
return true; return true;
} }
if (v2.getClass() != Value.LongValue.class) { if (!(v2 instanceof Value.LongValue)) {
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können addiert werden"); scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können addiert werden");
return true; return true;
} }

Datei anzeigen

@ -44,11 +44,11 @@ public class Div implements SpecialCommand {
Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]);
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
if (v1.getClass() != Value.LongValue.class) { if (!(v1 instanceof Value.LongValue)) {
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können dividiert werden"); scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können dividiert werden");
return true; return true;
} }
if (v2.getClass() != Value.LongValue.class) { if (!(v2 instanceof Value.LongValue)) {
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können dividiert werden"); scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können dividiert werden");
return true; return true;
} }

Datei anzeigen

@ -44,11 +44,11 @@ public class Mul implements SpecialCommand {
Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]);
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
if (v1.getClass() != Value.LongValue.class) { if (!(v1 instanceof Value.LongValue)) {
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können multipliziert werden"); scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können multipliziert werden");
return true; return true;
} }
if (v2.getClass() != Value.LongValue.class) { if (!(v2 instanceof Value.LongValue)) {
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können multipliziert werden"); scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können multipliziert werden");
return true; return true;
} }

Datei anzeigen

@ -44,11 +44,11 @@ public class Sub implements SpecialCommand {
Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]);
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
if (v1.getClass() != Value.LongValue.class) { if (!(v1 instanceof Value.LongValue)) {
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können subtrahiert werden"); scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können subtrahiert werden");
return true; return true;
} }
if (v2.getClass() != Value.LongValue.class) { if (!(v2 instanceof Value.LongValue)) {
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können subtrahiert werden"); scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können subtrahiert werden");
return true; return true;
} }

Datei anzeigen

@ -44,11 +44,11 @@ public class And implements SpecialCommand {
Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]);
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
if (v1.getClass() != Value.BooleanValue.class) { if (!(v1 instanceof Value.BooleanValue)) {
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Booleans können verglichen werden"); scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Booleans können verglichen werden");
return true; return true;
} }
if (v2.getClass() != Value.BooleanValue.class) { if (!(v2 instanceof Value.BooleanValue)) {
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Booleans können verglichen werden"); scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Booleans können verglichen werden");
return true; return true;
} }

Datei anzeigen

@ -45,7 +45,7 @@ public class Equal implements SpecialCommand {
Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]);
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
Value result; Value result;
if (v1.getClass() != v2.getClass()) { if (!v1.getClass().isInstance(v2.getClass()) && !v2.getClass().isInstance(v1.getClass())) {
result = new Value.BooleanValue(false); result = new Value.BooleanValue(false);
} else if (v1.asString().equals(v2.asString())) { } else if (v1.asString().equals(v2.asString())) {
result = new Value.BooleanValue(true); result = new Value.BooleanValue(true);

Datei anzeigen

@ -44,11 +44,11 @@ public class Greater implements SpecialCommand {
Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]);
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
if (v1.getClass() != Value.LongValue.class) { if (!(v1 instanceof Value.LongValue)) {
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können verglichen werden"); scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können verglichen werden");
return true; return true;
} }
if (v2.getClass() != Value.LongValue.class) { if (!(v2 instanceof Value.LongValue)) {
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können verglichen werden"); scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können verglichen werden");
return true; return true;
} }

Datei anzeigen

@ -44,11 +44,11 @@ public class Less implements SpecialCommand {
Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]);
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
if (v1.getClass() != Value.LongValue.class) { if (!(v1 instanceof Value.LongValue)) {
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können verglichen werden"); scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können verglichen werden");
return true; return true;
} }
if (v2.getClass() != Value.LongValue.class) { if (!(v2 instanceof Value.LongValue)) {
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können verglichen werden"); scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können verglichen werden");
return true; return true;
} }

Datei anzeigen

@ -39,7 +39,7 @@ public class Not implements SpecialCommand {
} }
Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 1]); Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
if (v1.getClass() != Value.BooleanValue.class) { if (!(v1 instanceof Value.BooleanValue)) {
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Booleans können genichtet werden"); scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Booleans können genichtet werden");
return true; return true;
} }

Datei anzeigen

@ -44,11 +44,11 @@ public class Or implements SpecialCommand {
Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]);
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
if (v1.getClass() != Value.BooleanValue.class) { if (!(v1 instanceof Value.BooleanValue)) {
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Booleans können verglichen werden"); scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Booleans können verglichen werden");
return true; return true;
} }
if (v2.getClass() != Value.BooleanValue.class) { if (!(v2 instanceof Value.BooleanValue)) {
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Booleans können verglichen werden"); scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Booleans können verglichen werden");
return true; return true;
} }

Datei anzeigen

@ -70,15 +70,15 @@ public class Insert implements SpecialCommand {
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 2]);
Value v3 = scriptExecutor.getOrItselfValue(command[command.length - 1]); Value v3 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
if (v1.getClass() != Value.StringValue.class) { if (!(v1 instanceof Value.StringValue)) {
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden"); scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden");
return true; return true;
} }
if (v2.getClass() != Value.StringValue.class) { if (!(v2 instanceof Value.StringValue)) {
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden"); scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden");
return true; return true;
} }
if (v3.getClass() != Value.LongValue.class) { if (!(v3 instanceof Value.StringValue)) {
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können verwendet werden"); scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können verwendet werden");
return true; return true;
} }

Datei anzeigen

@ -64,7 +64,7 @@ public class Length implements SpecialCommand {
String resultName = command[1]; String resultName = command[1];
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
if (v2.getClass() != Value.StringValue.class) { if (!(v2 instanceof Value.StringValue)) {
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden"); scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden");
return true; return true;
} }

Datei anzeigen

@ -65,11 +65,11 @@ public class Remove implements SpecialCommand {
Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]);
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
if (v1.getClass() != Value.StringValue.class) { if (!(v1 instanceof Value.StringValue)) {
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden"); scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden");
return true; return true;
} }
if (v2.getClass() != Value.StringValue.class) { if (!(v2 instanceof Value.StringValue)) {
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden"); scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden");
return true; return true;
} }

Datei anzeigen

@ -70,15 +70,15 @@ public class Replace implements SpecialCommand {
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 2]);
Value v3 = scriptExecutor.getOrItselfValue(command[command.length - 1]); Value v3 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
if (v1.getClass() != Value.StringValue.class) { if (!(v1 instanceof Value.StringValue)) {
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden"); scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden");
return true; return true;
} }
if (v2.getClass() != Value.StringValue.class) { if (!(v2 instanceof Value.StringValue)) {
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden"); scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden");
return true; return true;
} }
if (v3.getClass() != Value.StringValue.class) { if (!(v3 instanceof Value.StringValue)) {
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden"); scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden");
return true; return true;
} }

Datei anzeigen

@ -65,11 +65,11 @@ public class Substring implements SpecialCommand {
Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]);
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
if (v1.getClass() != Value.StringValue.class) { if (!(v1 instanceof Value.StringValue)) {
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden"); scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden");
return true; return true;
} }
if (v2.getClass() != Value.LongValue.class) { if (!(v2 instanceof Value.LongValue)) {
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können verwendet werden"); scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können verwendet werden");
return true; return true;
} }

Datei anzeigen

@ -0,0 +1,73 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bausystem.features.script.command.variable;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.features.script.ScriptExecutor;
import de.steamwar.bausystem.features.script.SpecialCommand;
import de.steamwar.bausystem.features.script.variables.Constants;
import de.steamwar.bausystem.linkage.LinkageType;
import de.steamwar.bausystem.linkage.Linked;
import org.bukkit.Material;
@Linked(LinkageType.SCRIPT_COMMAND)
public class Const implements SpecialCommand {
@Override
public String[] description() {
return new String[]{
"§econst §8<§7Variable§8> §8[§7Value§8(§7s§8)§8]",
"",
"Schreibt in eine Konstante einen Wert rein, welcher eine Zahl sein kann, ein Boolscher Wert oder ein Text."
};
}
@Override
public Material material() {
return Material.STRUCTURE_BLOCK;
}
@Override
public String command() {
return "const";
}
@Override
public boolean execute(String[] command, ScriptExecutor scriptExecutor) {
if (command.length <= 1) {
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cDas erste Argument fehlt und sollte eine Variable sein");
return true;
}
if (command.length <= 2) {
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cDas zweite Argument fehlt und sollte ein Wert sein");
return true;
}
String varName = command[1];
StringBuilder varValue = new StringBuilder();
for (int i = 2; i < command.length; i++) {
if (varValue.length() != 0) {
varValue.append(" ");
}
varValue.append(command[i]);
}
Constants.getConstant(varName, scriptExecutor.getPlayer()).fromValue(scriptExecutor.parse(varValue.toString()));
return true;
}
}

Datei anzeigen

@ -3,7 +3,6 @@ package de.steamwar.bausystem.features.script.command.variable;
import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.features.script.ScriptExecutor; import de.steamwar.bausystem.features.script.ScriptExecutor;
import de.steamwar.bausystem.features.script.SpecialCommand; import de.steamwar.bausystem.features.script.SpecialCommand;
import de.steamwar.bausystem.features.script.variables.Value;
import de.steamwar.bausystem.linkage.LinkageType; import de.steamwar.bausystem.linkage.LinkageType;
import de.steamwar.bausystem.linkage.Linked; import de.steamwar.bausystem.linkage.Linked;
import org.bukkit.Material; import org.bukkit.Material;
@ -48,17 +47,7 @@ public class Global implements SpecialCommand {
} }
varValue.append(command[i]); varValue.append(command[i]);
} }
try { scriptExecutor.getGlobalVariables().putValue(varName, scriptExecutor.parse(varValue.toString()));
long value = Long.parseLong(varValue.toString());
scriptExecutor.getLocalVariables().putValue(varName, new Value.LongValue(value));
} catch (NumberFormatException e) {
String s = varValue.toString();
if (s.equalsIgnoreCase("true") || s.equalsIgnoreCase("false")) {
scriptExecutor.getGlobalVariables().putValue(varName, new Value.BooleanValue(s.equalsIgnoreCase("true")));
} else {
scriptExecutor.getGlobalVariables().putValue(varName, new Value.StringValue(s));
}
}
return true; return true;
} }
} }

Datei anzeigen

@ -3,6 +3,7 @@ package de.steamwar.bausystem.features.script.command.variable;
import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.features.script.ScriptExecutor; import de.steamwar.bausystem.features.script.ScriptExecutor;
import de.steamwar.bausystem.features.script.SpecialCommand; import de.steamwar.bausystem.features.script.SpecialCommand;
import de.steamwar.bausystem.features.script.variables.Constants;
import de.steamwar.bausystem.features.script.variables.Value; import de.steamwar.bausystem.features.script.variables.Value;
import de.steamwar.bausystem.linkage.LinkageType; import de.steamwar.bausystem.linkage.LinkageType;
import de.steamwar.bausystem.linkage.Linked; import de.steamwar.bausystem.linkage.Linked;

Datei anzeigen

@ -8,54 +8,206 @@ import de.steamwar.bausystem.region.flags.flagvalues.FreezeMode;
import de.steamwar.bausystem.region.flags.flagvalues.ProtectMode; import de.steamwar.bausystem.region.flags.flagvalues.ProtectMode;
import de.steamwar.bausystem.region.flags.flagvalues.TNTMode; import de.steamwar.bausystem.region.flags.flagvalues.TNTMode;
import lombok.experimental.UtilityClass; import lombok.experimental.UtilityClass;
import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Supplier;
@UtilityClass @UtilityClass
public class Constants { public class Constants {
private final Map<String, Function<Player, Value>> CONSTANTS = new HashMap<>(); private final Map<String, Function<Player, Value>> CONSTANTS = new HashMap<>();
private static class ConstantLongValue extends Value.LongValue {
private Supplier<Long> longSupplier;
private Consumer<Long> longConsumer = ignored -> {};
public ConstantLongValue(Supplier<Long> longSupplier) {
super(longSupplier.get());
this.longSupplier = longSupplier;
}
public ConstantLongValue(Supplier<Long> longSupplier, Consumer<Long> longConsumer) {
super(longSupplier.get());
this.longSupplier = longSupplier;
this.longConsumer = longConsumer;
}
@Override
public long asLong() {
value = longSupplier.get();
return super.asLong();
}
@Override
public boolean asBoolean() {
value = longSupplier.get();
return super.asBoolean();
}
@Override
public String asString() {
value = longSupplier.get();
return super.asString();
}
@Override
public void fromValue(Value value) {
super.fromValue(value);
longConsumer.accept(this.value);
}
}
private static class ConstantBooleanValue extends Value.BooleanValue {
private Supplier<Boolean> booleanSupplier;
private Consumer<Boolean> booleanConsumer = ignored -> {};
public ConstantBooleanValue(Supplier<Boolean> booleanSupplier) {
super(booleanSupplier.get());
this.booleanSupplier = booleanSupplier;
}
public ConstantBooleanValue(Supplier<Boolean> booleanSupplier, Consumer<Boolean> booleanConsumer) {
super(booleanSupplier.get());
this.booleanSupplier = booleanSupplier;
this.booleanConsumer = booleanConsumer;
}
@Override
public long asLong() {
value = booleanSupplier.get();
return super.asLong();
}
@Override
public boolean asBoolean() {
value = booleanSupplier.get();
return super.asBoolean();
}
@Override
public String asString() {
value = booleanSupplier.get();
return super.asString();
}
@Override
public void fromValue(Value value) {
super.fromValue(value);
booleanConsumer.accept(this.value);
}
}
private static class ConstantStringValue extends Value.StringValue {
private Supplier<String> stringSupplier;
private Consumer<String> stringConsumer = ignored -> {};
public ConstantStringValue(Supplier<String> stringSupplier) {
super(stringSupplier.get());
this.stringSupplier = stringSupplier;
}
public ConstantStringValue(Supplier<String> stringSupplier, Consumer<String> stringConsumer) {
super(stringSupplier.get());
this.stringSupplier = stringSupplier;
this.stringConsumer = stringConsumer;
}
@Override
public long asLong() {
value = stringSupplier.get();
return super.asLong();
}
@Override
public boolean asBoolean() {
value = stringSupplier.get();
return super.asBoolean();
}
@Override
public String asString() {
value = stringSupplier.get();
return super.asString();
}
@Override
public void fromValue(Value value) {
super.fromValue(value);
stringConsumer.accept(this.value);
}
}
static { static {
CONSTANTS.put("trace", player -> { CONSTANTS.put("trace", player -> {
return new Value.BooleanValue(RecordStateMachine.getRecordStatus().isTracing()); return new ConstantBooleanValue(RecordStateMachine.getRecordStatus()::isTracing);
}); });
CONSTANTS.put("autotrace", player -> { CONSTANTS.put("autotrace", player -> {
return new Value.BooleanValue(RecordStateMachine.getRecordStatus().isAutoTrace()); return new ConstantBooleanValue(RecordStateMachine.getRecordStatus()::isAutoTrace);
}); });
CONSTANTS.put("tnt", player -> { CONSTANTS.put("tnt", player -> {
return new Value.BooleanValue(Region.getRegion(player.getLocation()).getPlain(Flag.TNT, TNTMode.class) != TNTMode.DENY); return new ConstantBooleanValue(() -> Region.getRegion(player.getLocation()).getPlain(Flag.TNT, TNTMode.class) != TNTMode.DENY);
}); });
CONSTANTS.put("tnt-onlytb", player -> { CONSTANTS.put("tnt-onlytb", player -> {
return new Value.BooleanValue(Region.getRegion(player.getLocation()).getPlain(Flag.TNT, TNTMode.class) != TNTMode.ONLY_TB); return new ConstantBooleanValue(() -> Region.getRegion(player.getLocation()).getPlain(Flag.TNT, TNTMode.class) != TNTMode.ONLY_TB);
}); });
CONSTANTS.put("freeze", player -> { CONSTANTS.put("freeze", player -> {
return new Value.BooleanValue(Region.getRegion(player.getLocation()).getPlain(Flag.FREEZE, FreezeMode.class) == FreezeMode.ACTIVE); return new ConstantBooleanValue(() -> Region.getRegion(player.getLocation()).getPlain(Flag.FREEZE, FreezeMode.class) == FreezeMode.ACTIVE);
}); });
CONSTANTS.put("fire", player -> { CONSTANTS.put("fire", player -> {
return new Value.BooleanValue(Region.getRegion(player.getLocation()).getPlain(Flag.FIRE, FireMode.class) == FireMode.ALLOW); return new ConstantBooleanValue(() -> Region.getRegion(player.getLocation()).getPlain(Flag.FIRE, FireMode.class) == FireMode.ALLOW);
}); });
CONSTANTS.put("protect", player -> { CONSTANTS.put("protect", player -> {
return new Value.BooleanValue(Region.getRegion(player.getLocation()).getPlain(Flag.PROTECT, ProtectMode.class) == ProtectMode.ACTIVE); return new ConstantBooleanValue(() -> Region.getRegion(player.getLocation()).getPlain(Flag.PROTECT, ProtectMode.class) == ProtectMode.ACTIVE);
}); });
CONSTANTS.put("x", player -> { CONSTANTS.put("x", player -> {
return new Value.LongValue(player.getLocation().getBlockX()); return new ConstantLongValue(() -> (long) player.getLocation().getBlockX(), aLong -> {
Location location = player.getLocation();
location.setX((double) aLong);
player.teleport(location);
});
}); });
CONSTANTS.put("y", player -> { CONSTANTS.put("y", player -> {
return new Value.LongValue(player.getLocation().getBlockY()); return new ConstantLongValue(() -> (long) player.getLocation().getBlockY(), aLong -> {
Location location = player.getLocation();
location.setY((double) aLong);
player.teleport(location);
});
}); });
CONSTANTS.put("z", player -> { CONSTANTS.put("z", player -> {
return new Value.LongValue(player.getLocation().getBlockZ()); return new ConstantLongValue(() -> (long) player.getLocation().getBlockZ(), aLong -> {
Location location = player.getLocation();
location.setZ((double) aLong);
player.teleport(location);
});
}); });
CONSTANTS.put("name", player -> { CONSTANTS.put("name", player -> {
return new Value.StringValue(player.getDisplayName()); return new ConstantStringValue(player::getDisplayName);
}); });
CONSTANTS.put("sneaking", player -> { CONSTANTS.put("sneaking", player -> {
return new Value.BooleanValue(player.isSneaking()); return new ConstantBooleanValue(player::isSneaking);
});
CONSTANTS.put("slot", player -> {
return new ConstantLongValue(() -> (long) player.getInventory().getHeldItemSlot(), slot -> {
if (slot > 8) {
slot = 8L;
}
if (slot < 0) {
slot = 0L;
}
player.getInventory().setHeldItemSlot((int) (long) slot);
});
});
CONSTANTS.put("slotmaterial", player -> {
return new ConstantStringValue(() -> player.getInventory().getItemInMainHand().getType().name());
}); });
} }
@ -70,5 +222,4 @@ public class Constants {
public Value getConstant(String variableName, Player player) { public Value getConstant(String variableName, Player player) {
return CONSTANTS.get(variableName).apply(player); return CONSTANTS.get(variableName).apply(player);
} }
} }

Datei anzeigen

@ -9,7 +9,11 @@ public class Context {
private Map<String, Value> variables = new HashMap<>(); private Map<String, Value> variables = new HashMap<>();
public void putValue(String variableName, Value value) { public void putValue(String variableName, Value value) {
variables.put(variableName, value); if (variables.containsKey(variableName)) {
variables.get(variableName).fromValue(value);
} else {
variables.put(variableName, value);
}
} }
public void removeValue(String variableName) { public void removeValue(String variableName) {

Datei anzeigen

@ -8,15 +8,13 @@ public interface Value {
boolean asBoolean(); boolean asBoolean();
String asString(); String asString();
void fromLong(long value); void fromValue(Value value);
void fromBoolean(boolean value);
void fromString(String value);
@AllArgsConstructor @AllArgsConstructor
@ToString @ToString
class LongValue implements Value { class LongValue implements Value {
private long value; protected long value;
@Override @Override
public long asLong() { public long asLong() {
@ -34,22 +32,8 @@ public interface Value {
} }
@Override @Override
public void fromLong(long value) { public void fromValue(Value value) {
this.value = value; this.value = value.asLong();
}
@Override
public void fromBoolean(boolean value) {
this.value = value ? 1 : 0;
}
@Override
public void fromString(String value) {
try {
this.value = Long.parseLong(value);
} catch (NumberFormatException e) {
this.value = 0;
}
} }
} }
@ -57,7 +41,7 @@ public interface Value {
@ToString @ToString
class BooleanValue implements Value { class BooleanValue implements Value {
private boolean value; protected boolean value;
@Override @Override
public long asLong() { public long asLong() {
@ -75,18 +59,8 @@ public interface Value {
} }
@Override @Override
public void fromLong(long value) { public void fromValue(Value value) {
this.value = value != 0; this.value = value.asBoolean();
}
@Override
public void fromBoolean(boolean value) {
this.value = value;
}
@Override
public void fromString(String value) {
this.value = value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes");
} }
} }
@ -94,7 +68,7 @@ public interface Value {
@ToString @ToString
class StringValue implements Value { class StringValue implements Value {
private String value; protected String value;
@Override @Override
public long asLong() { public long asLong() {
@ -116,18 +90,8 @@ public interface Value {
} }
@Override @Override
public void fromLong(long value) { public void fromValue(Value value) {
this.value = value + ""; this.value = value.asString();
}
@Override
public void fromBoolean(boolean value) {
this.value = value + "";
}
@Override
public void fromString(String value) {
this.value = value;
} }
} }