code refactoring

This commit is contained in:
unurled 2024-03-18 22:26:42 +01:00
parent ad919e76d5
commit f706a7e91a
Signed by: unurled
GPG key ID: FDBC9CBE1F82423F
4 changed files with 16 additions and 11 deletions

View file

@ -11,12 +11,15 @@ import org.bukkit.Bukkit;
import org.bukkit.event.inventory.InventoryType; import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
public class InventoryDeserializer implements JsonDeserializer<Inventory> { public class InventoryDeserializer implements JsonDeserializer<Inventory> {
@Override @Override
public Inventory deserialize( public Inventory deserialize(
JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) @NotNull JsonElement jsonElement,
Type type,
JsonDeserializationContext jsonDeserializationContext)
throws JsonParseException { throws JsonParseException {
Inventory inv = Bukkit.createInventory(null, InventoryType.PLAYER); Inventory inv = Bukkit.createInventory(null, InventoryType.PLAYER);
// parse from a string, to a list of string // parse from a string, to a list of string

View file

@ -7,18 +7,18 @@ import com.google.gson.JsonSerializer;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.jetbrains.annotations.NotNull;
public class InventorySerializer implements JsonSerializer<Inventory> { public class InventorySerializer implements JsonSerializer<Inventory> {
@Override @Override
public JsonElement serialize( public JsonElement serialize(
Inventory itemStacks, Type type, JsonSerializationContext jsonSerializationContext) { @NotNull Inventory itemStacks, Type type, JsonSerializationContext jsonSerializationContext) {
List<String> collect = List<String> collect =
Arrays.stream(itemStacks.getContents()) Arrays.stream(itemStacks.getContents())
.map(item -> item == null ? "null" : Arrays.toString(item.serializeAsBytes())) .map(item -> item == null ? "null" : Arrays.toString(item.serializeAsBytes()))
.collect(Collectors.toList()); .toList();
String s = String.join(";", collect); String s = String.join(";", collect);
return new JsonPrimitive(s); return new JsonPrimitive(s);

View file

@ -1,7 +1,7 @@
package me.unurled.sacredrealms.sr.gui.entitytype; package me.unurled.sacredrealms.sr.gui.entitytype;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
import me.unurled.sacredrealms.sr.components.entity.EntityManager; import me.unurled.sacredrealms.sr.components.entity.EntityManager;
import me.unurled.sacredrealms.sr.components.entity.SREntityType; import me.unurled.sacredrealms.sr.components.entity.SREntityType;
import me.unurled.sacredrealms.sr.gui.BackItem; import me.unurled.sacredrealms.sr.gui.BackItem;
@ -21,11 +21,11 @@ public class EntityTypeGUI {
public static @NotNull Gui listGui() { public static @NotNull Gui listGui() {
EntityManager em = Manager.getInstance(EntityManager.class); EntityManager em = Manager.getInstance(EntityManager.class);
List<Item> items = List<Item> list = new ArrayList<>();
em.getTypes().stream() for (SREntityType type : em.getTypes()) {
.map(type -> new SimpleItem(new ItemBuilder(type.getItem()))) SimpleItem simpleItem = new SimpleItem(new ItemBuilder(type.getItem()));
.collect(Collectors.toList()); list.add(simpleItem);
}
return PagedGui.items() return PagedGui.items()
.setStructure( .setStructure(
"# # # # # # # # #", "# # # # # # # # #",
@ -36,7 +36,7 @@ public class EntityTypeGUI {
.addIngredient('X', Markers.CONTENT_LIST_SLOT_HORIZONTAL) .addIngredient('X', Markers.CONTENT_LIST_SLOT_HORIZONTAL)
.addIngredient('<', new BackItem()) .addIngredient('<', new BackItem())
.addIngredient('>', new ForwardItem()) .addIngredient('>', new ForwardItem())
.setContent(items) .setContent(list)
.build(); .build();
} }

View file

@ -5,6 +5,8 @@ import net.kyori.adventure.text.Component;
public class Logger { public class Logger {
private Logger() {}
public static void log(String message) { public static void log(String message) {
SR.getInstance().getLogger().info(message); SR.getInstance().getLogger().info(message);
} }