1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-20 03:35: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;
public ErrorReport(Throwable throwable, Consumer<PrintStream> 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;

View File

@ -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 <T> Optional<T[]> getArray(String key, IntFunction<T[]> constructor, Function<JsonElement, T> 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 <T> Optional<T> getPrimitive(String key, Function<JsonElement, T> 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();
}
}

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