diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/ErrorReport.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/ErrorReport.java index c21a7bd7b..1221be994 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/api/ErrorReport.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/ErrorReport.java @@ -30,16 +30,8 @@ public class ErrorReport { private File file; public ErrorReport(Throwable throwable, Consumer printer) { - SlimefunPlugin.instance.getServer().getScheduler().scheduleSyncDelayedTask(SlimefunPlugin.instance, () -> { - String path = "plugins/Slimefun/error-reports/" + new SimpleDateFormat("yyyy-MM-dd-HH-mm").format(new Date()); - file = new File(path + ".err"); - - if (file.exists()) { - IntStream stream = IntStream.iterate(1, i -> i + 1).filter(i -> !new File(path + " (" + i + ").err").exists()); - int id = stream.findFirst().getAsInt(); - - file = new File(path + " (" + id + ").err"); - } + Slimefun.runSync(() -> { + file = getNewFile(); try (PrintStream stream = new PrintStream(file)) { stream.println(); @@ -99,7 +91,7 @@ public class ErrorReport { } }); } - + public ErrorReport(Throwable throwable, TickerTask task, Location l, SlimefunItem item) { this(throwable, stream -> { stream.println("Block Info:"); @@ -158,6 +150,20 @@ public class ErrorReport { stream.println(); }); } + + private File getNewFile() { + String path = "plugins/Slimefun/error-reports/" + new SimpleDateFormat("yyyy-MM-dd-HH-mm").format(new Date()); + File file = new File(path + ".err"); + + if (file.exists()) { + IntStream stream = IntStream.iterate(1, i -> i + 1).filter(i -> !new File(path + " (" + i + ").err").exists()); + int id = stream.findFirst().getAsInt(); + + file = new File(path + " (" + id + ").err"); + } + + return file; + } public File getFile() { return file; diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/JsonDataHolder.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/JsonDataHolder.java index 7e43612c9..10b1f5026 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/api/JsonDataHolder.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/JsonDataHolder.java @@ -23,13 +23,13 @@ public abstract class JsonDataHolder { dirty = true; } - public boolean isDirty() { - return dirty; - } - public void markClean() { dirty = false; } + + public boolean isDirty() { + return dirty; + } // Setters public void setString(String key, String value) { @@ -128,10 +128,7 @@ public abstract class JsonDataHolder { protected Optional getArray(String key, IntFunction constructor, Function getter) { JsonElement element = data.get(key); - if (element == null || !(element instanceof JsonArray)) { - return Optional.empty(); - } - else { + if (element instanceof JsonArray) { JsonArray json = (JsonArray) element; T[] array = constructor.apply(json.size()); @@ -141,16 +138,19 @@ public abstract class JsonDataHolder { return Optional.of(array); } + else { + return Optional.empty(); + } } protected Optional getPrimitive(String key, Function getter) { JsonElement element = data.get(key); - if (element == null || !(element instanceof JsonPrimitive)) { - return Optional.empty(); + if (element instanceof JsonPrimitive) { + return Optional.of(getter.apply(element)); } else { - return Optional.of(getter.apply(element)); + return Optional.empty(); } } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/items/ItemRestriction.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/items/ItemRestriction.java new file mode 100644 index 000000000..d41bdffc9 --- /dev/null +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/items/ItemRestriction.java @@ -0,0 +1,36 @@ +package io.github.thebusybiscuit.slimefun4.api.items; + +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; + +import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; +import me.mrCookieSlime.Slimefun.api.PlayerProfile; + +public interface ItemRestriction { + + /** + * This method represents a check. + * The returned boolean will decide whether to allow the action. + * + * @param profile The Player's profile + * @param p The Player itself + * @param item The SlimefunItem that the {@link ItemStack} represents + * @param itemstack The ItemStack that is being tested. + */ + boolean isAllowed(PlayerProfile profile, Player p, SlimefunItem item, ItemStack itemstack); + + /** + * This method is executed if an ItemRestriction took affect. + * Override it to send a message to the Player telling them they cannot + * use that item, or do something else in there. + * + * @param profile The Player's profile + * @param p The Player to warn + * @param item The SlimefunItem that the {@link ItemStack} represents + * @param itemstack The ItemStack that was prevented from being used + */ + void warnPlayer(PlayerProfile profile, Player p, SlimefunItem item, ItemStack itemstack); + + + +}