1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-19 19:25:48 +00:00
This commit is contained in:
TheBusyBiscuit 2020-06-13 21:24:28 +02:00
parent c915b3375b
commit debc925bd0
5 changed files with 49 additions and 27 deletions

View File

@ -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

View File

@ -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);

View File

@ -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;

View File

@ -61,6 +61,7 @@ public abstract class ProgrammableAndroid extends SlimefunItem implements Invent
private static final List<BlockFace> 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<MachineFuel> 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) {

View File

@ -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<Script> getUploadedScripts(AndroidType androidType) {
List<Script> scripts = new LinkedList<>();
File directory = new File("plugins/Slimefun/scripts/" + androidType.name());
if (!directory.exists()) {
directory.mkdirs();
}
for (File script : directory.listFiles()) {
if (script.getName().endsWith("sfs")) {
scripts.add(new Script(new Config(script)));
}
}
loadScripts(scripts, androidType);
if (androidType != AndroidType.NONE) {
File mainDirectory = new File("plugins/Slimefun/scripts/NONE");
if (!mainDirectory.exists()) {
mainDirectory.mkdirs();
}
for (File script : mainDirectory.listFiles()) {
if (script.getName().endsWith(".sfs")) {
scripts.add(new Script(new Config(script)));
}
}
loadScripts(scripts, AndroidType.NONE);
}
Collections.sort(scripts, Comparator.comparingInt(script -> -script.getUpvotes() + 1 - script.getDownvotes()));
return scripts;
}
private static void loadScripts(List<Script> scripts, AndroidType type) {
File directory = new File("plugins/Slimefun/scripts/" + type.name());
if (!directory.exists()) {
directory.mkdirs();
}
for (File file : directory.listFiles()) {
if (file.getName().endsWith(".sfs")) {
try {
Config config = new Config(file);
// Some older versions somehow allowed null values to slip in here sometimes
// So we need this check for compatibility with older scripts
if (config.contains("code") && config.contains("author")) {
scripts.add(new Script(config));
}
}
catch (Exception x) {
Slimefun.getLogger().log(Level.SEVERE, x, () -> "An Exception occured while trying to load Android Script '" + file.getName() + "'");
}
}
}
}
public static void upload(Player p, AndroidType androidType, int id, String name, String code) {
Config config = new Config("plugins/Slimefun/scripts/" + androidType.name() + '/' + p.getName() + ' ' + String.valueOf(id) + ".sfs");