latest
AKL City ยท akl-city.git
Branches > main > latest > files
Optional nonsense?
Commit: 918770cfe231a973b56f08f1677933357306a5a0
Parent: 3f7228d
2 changed files:
src/main/java/dev/grahp/aklcity/Signs.java view file
@@ -43,6 +43,7 @@ return "BuildingRegistry[buildings=" + buildings + ", houses=" + houses + ", jobs=" + jobs + "]"; } + // Don't just clear... public void clear() { buildings.clear(); houses.clear();@@ -132,13 +133,11 @@ public void onEditSign(SignChangeEvent e) { var sign = sign(e); // TODO building is the OLD building, not the new one!- var building = getSignBuilding(sign);- if (building.present) {- var b = building.val();+ getSignBuilding(sign).then(b -> { buildings.put(blockLocation(sign), b); var address = b.address(); e.getPlayer().sendMessage(Component.text("Saved Building '" + address.number().toString() + " " + address.street() + "'", NamedTextColor.GREEN));- }+ }); } // On breaking a block, send a message that the player broke a building@@ -148,27 +147,21 @@ @EventHandler public void onBreakSign(BlockBreakEvent e) { var sign = sign(e);- var building = getSignBuilding(sign);- if (building.present) {- // Require a shift or cancel the event?- var b = building.val();+ getSignBuilding(sign).then(b -> { buildings.remove(blockLocation(sign)); var address = b.address(); e.getPlayer().sendMessage(Component.text("Deleted Building '" + address.number().toString() + " " + address.street() + "'", NamedTextColor.RED));- }+ }); } @EventHandler public void onPlaceSign(BlockPlaceEvent e) { var sign = sign(e);- var building = getSignBuilding(sign);- if (building.present) {- // Require a shift or cancel the event?- var b = building.val();+ getSignBuilding(sign).then(b -> { buildings.put(blockLocation(sign), b); var address = b.address(); e.getPlayer().sendMessage(Component.text("Added Building '" + address.number().toString() + " " + address.street() + "'", NamedTextColor.GREEN));- }+ }); } // On block place, add the sign as well@@ -184,13 +177,9 @@ // TODO are signs still tile entities? After 26.2 or something. for (var block : chunk.getTileEntities()) { if (block instanceof Sign sign) {-- var building = getSignBuilding(sign);- if (building.present) {- buildings.put(blockLocation(sign), building.val());- } else if (isPopulationSign(sign)) {- populationSigns.add(sign);- }+ getSignBuilding(sign)+ .then(b -> { buildings.put(blockLocation(sign), b); },+ () -> { populationSigns.add(sign); }); } } }src/main/java/dev/grahp/option/Optional.java view file
@@ -1,6 +1,7 @@ package dev.grahp.option; import java.lang.RuntimeException;+import java.util.function.*; // Make IBox and make this Box to avoid name collision public class Optional<T> implements Box {@@ -26,11 +27,67 @@ } } - // public R with(Function<T, R> f) {- // if (present) {- // return f.apply(value);- // }- // }+ public <R> R then(Function<T, R> f) {+ if (present) {+ return f.apply(value);+ }++ return null;+ }++ public void then(Consumer<T> f) {+ if (present) {+ f.accept(value);+ }+ }++ public <R> R otherwise(Supplier<R> f) {+ if (!present) {+ return f.get();+ }++ return null;+ }++ public void otherwise(Runnable f) {+ if (!present) {+ f.run();+ }+ }++ public <R> R then(Function<T, R> f, Supplier<R> otherwise) {+ if (present) {+ return f.apply(value);+ }++ return otherwise.get();+ }++ public void then(Consumer<T> f, Runnable otherwise) {+ if (present) {+ f.accept(value);+ } else {+ otherwise.run();+ }+ }++ public <R> R then(Consumer<T> f, Supplier<R> otherwise) {+ if (present) {+ f.accept(value);+ return null;+ }++ return otherwise.get();+ }++ public <R> R then(Function<T, R> f, Runnable otherwise) {+ if (present) {+ return f.apply(value);+ }++ otherwise.run();+ return null;+ } @Override public String toString() {