From debc925bd0f72f88427c7c666e28bf845664e330 Mon Sep 17 00:00:00 2001 From: TheBusyBiscuit Date: Sat, 13 Jun 2020 21:24:28 +0200 Subject: [PATCH] Fixes #1976 --- CHANGELOG.md | 1 + ...ndroidConsumer.java => AndroidAction.java} | 2 +- .../items/androids/Instruction.java | 4 +- .../items/androids/ProgrammableAndroid.java | 13 ++++- .../implementation/items/androids/Script.java | 56 ++++++++++++------- 5 files changed, 49 insertions(+), 27 deletions(-) rename src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/{AndroidConsumer.java => AndroidAction.java} (91%) diff --git a/CHANGELOG.md b/CHANGELOG.md index fb86cf163..31158984d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,7 @@ * Fixed #1943 * Fixed Nuclear Reactors accepting Lava as coolant * Fixed #1971 +* Fixed #1976 ## Release Candidate 12 (27 May 2020) https://thebusybiscuit.github.io/builds/TheBusyBiscuit/Slimefun4/stable/#12 diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/AndroidConsumer.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/AndroidAction.java similarity index 91% rename from src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/AndroidConsumer.java rename to src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/AndroidAction.java index 73467c972..296452b6b 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/AndroidConsumer.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/AndroidAction.java @@ -6,7 +6,7 @@ import org.bukkit.block.BlockFace; import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu; @FunctionalInterface -interface AndroidConsumer { +interface AndroidAction { void perform(ProgrammableAndroid android, Block b, BlockMenu inventory, BlockFace face); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/Instruction.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/Instruction.java index 07745e6fa..8635eb8b0 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/Instruction.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/Instruction.java @@ -141,9 +141,9 @@ enum Instruction { private final ItemStack item; private final AndroidType type; - private final AndroidConsumer method; + private final AndroidAction method; - Instruction(AndroidType type, String texture, AndroidConsumer method) { + Instruction(AndroidType type, String texture, AndroidAction method) { this.type = type; this.item = SlimefunUtils.getCustomHead(texture); this.method = method; diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/ProgrammableAndroid.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/ProgrammableAndroid.java index c8ced8646..313a96c9d 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/ProgrammableAndroid.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/ProgrammableAndroid.java @@ -61,6 +61,7 @@ public abstract class ProgrammableAndroid extends SlimefunItem implements Invent private static final List POSSIBLE_ROTATIONS = Arrays.asList(BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST); private static final int[] BORDER = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 18, 24, 25, 26, 27, 33, 35, 36, 42, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 }; private static final int[] OUTPUT_BORDER = { 10, 11, 12, 13, 14, 19, 23, 28, 32, 37, 38, 39, 40, 41 }; + private static final String DEFAULT_SCRIPT = "START-TURN_LEFT-REPEAT"; protected final Set fuelTypes = new HashSet<>(); protected final String texture; @@ -126,7 +127,7 @@ public abstract class ProgrammableAndroid extends SlimefunItem implements Invent @Override public void onPlace(Player p, Block b, SlimefunItem item) { BlockStorage.addBlockInfo(b, "owner", p.getUniqueId().toString()); - BlockStorage.addBlockInfo(b, "script", "START-TURN_LEFT-REPEAT"); + BlockStorage.addBlockInfo(b, "script", DEFAULT_SCRIPT); BlockStorage.addBlockInfo(b, "index", "0"); BlockStorage.addBlockInfo(b, "fuel", "0"); BlockStorage.addBlockInfo(b, "rotation", p.getFacing().getOppositeFace().toString()); @@ -379,6 +380,11 @@ public abstract class ProgrammableAndroid extends SlimefunItem implements Invent private void uploadScript(Player p, Block b, int page) { String code = getScript(b.getLocation()); + + if (code == null) { + return; + } + int nextId = 1; for (Script script : Script.getUploadedScripts(getAndroidType())) { @@ -414,7 +420,7 @@ public abstract class ProgrammableAndroid extends SlimefunItem implements Invent menu.addItem(3, new CustomItem(SlimefunUtils.getCustomHead("171d8979c1878a05987a7faf21b56d1b744f9d068c74cffcde1ea1edad5852"), "&4> Create new Script", "", "&cDeletes your current Script", "&cand creates a blank one")); menu.addMenuClickHandler(3, (pl, slot, item, action) -> { - openScript(pl, b, "START-TURN_LEFT-REPEAT"); + openScript(pl, b, DEFAULT_SCRIPT); return false; }); @@ -510,7 +516,8 @@ public abstract class ProgrammableAndroid extends SlimefunItem implements Invent } protected String getScript(Location l) { - return BlockStorage.getLocationInfo(l, "script"); + String script = BlockStorage.getLocationInfo(l, "script"); + return script != null ? script : DEFAULT_SCRIPT; } protected void setScript(Location l, String script) { diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/Script.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/Script.java index cb479ef9f..8b0daf56b 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/Script.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/androids/Script.java @@ -6,13 +6,16 @@ import java.util.Collections; import java.util.Comparator; import java.util.LinkedList; import java.util.List; +import java.util.logging.Level; +import org.apache.commons.lang.Validate; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; import io.github.thebusybiscuit.cscorelib2.config.Config; import io.github.thebusybiscuit.slimefun4.utils.ChatUtils; +import me.mrCookieSlime.Slimefun.api.Slimefun; public final class Script { @@ -21,11 +24,16 @@ public final class Script { private final String author; private final String code; - protected Script(Config config) { + private Script(Config config) { + Validate.notNull(config); + this.config = config; this.name = config.getString("name"); this.code = config.getString("code"); + Validate.notNull(name); + Validate.notNull(code); + OfflinePlayer player = Bukkit.getOfflinePlayer(config.getUUID("author")); this.author = player.getName() != null ? player.getName() : config.getString("author_name"); } @@ -152,34 +160,40 @@ public final class Script { public static List