1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-20 11:45:51 +00:00

[CI skip] Some additional API work

This commit is contained in:
TheBusyBiscuit 2019-11-28 20:23:39 +01:00
parent 6312d6c431
commit d5482f5496
3 changed files with 64 additions and 22 deletions

View File

@ -30,16 +30,8 @@ public class ErrorReport {
private File file; private File file;
public ErrorReport(Throwable throwable, Consumer<PrintStream> printer) { public ErrorReport(Throwable throwable, Consumer<PrintStream> printer) {
SlimefunPlugin.instance.getServer().getScheduler().scheduleSyncDelayedTask(SlimefunPlugin.instance, () -> { Slimefun.runSync(() -> {
String path = "plugins/Slimefun/error-reports/" + new SimpleDateFormat("yyyy-MM-dd-HH-mm").format(new Date()); file = getNewFile();
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");
}
try (PrintStream stream = new PrintStream(file)) { try (PrintStream stream = new PrintStream(file)) {
stream.println(); stream.println();
@ -159,6 +151,20 @@ public class ErrorReport {
}); });
} }
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() { public File getFile() {
return file; return file;
} }

View File

@ -23,14 +23,14 @@ public abstract class JsonDataHolder {
dirty = true; dirty = true;
} }
public boolean isDirty() {
return dirty;
}
public void markClean() { public void markClean() {
dirty = false; dirty = false;
} }
public boolean isDirty() {
return dirty;
}
// Setters // Setters
public void setString(String key, String value) { public void setString(String key, String value) {
data.addProperty(key, value); data.addProperty(key, value);
@ -128,10 +128,7 @@ public abstract class JsonDataHolder {
protected <T> Optional<T[]> getArray(String key, IntFunction<T[]> constructor, Function<JsonElement, T> getter) { protected <T> Optional<T[]> getArray(String key, IntFunction<T[]> constructor, Function<JsonElement, T> getter) {
JsonElement element = data.get(key); JsonElement element = data.get(key);
if (element == null || !(element instanceof JsonArray)) { if (element instanceof JsonArray) {
return Optional.empty();
}
else {
JsonArray json = (JsonArray) element; JsonArray json = (JsonArray) element;
T[] array = constructor.apply(json.size()); T[] array = constructor.apply(json.size());
@ -141,16 +138,19 @@ public abstract class JsonDataHolder {
return Optional.of(array); return Optional.of(array);
} }
else {
return Optional.empty();
}
} }
protected <T> Optional<T> getPrimitive(String key, Function<JsonElement, T> getter) { protected <T> Optional<T> getPrimitive(String key, Function<JsonElement, T> getter) {
JsonElement element = data.get(key); JsonElement element = data.get(key);
if (element == null || !(element instanceof JsonPrimitive)) { if (element instanceof JsonPrimitive) {
return Optional.empty(); return Optional.of(getter.apply(element));
} }
else { else {
return Optional.of(getter.apply(element)); return Optional.empty();
} }
} }

View File

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