From 00525845076cc04f50648d6a6b4ab2ee1a59633e Mon Sep 17 00:00:00 2001 From: Pyrbu Date: Mon, 19 Jun 2023 00:20:18 +0200 Subject: [PATCH] Improve api a lot --- .../api/entity/EntityPropertyRegistry.java | 3 +++ .../lol/pyr/znpcsplus/api/npc/NpcRegistry.java | 4 +++- .../pyr/znpcsplus/api/npc/NpcTypeRegistry.java | 3 +++ .../main/java/lol/pyr/znpcsplus/ZNpcsPlus.java | 2 +- .../lol/pyr/znpcsplus/commands/CreateCommand.java | 2 +- .../lol/pyr/znpcsplus/commands/TypeCommand.java | 2 +- .../entity/EntityPropertyRegistryImpl.java | 12 ++++++++++++ .../lol/pyr/znpcsplus/npc/NpcRegistryImpl.java | 15 ++++++++++++++- .../pyr/znpcsplus/npc/NpcTypeRegistryImpl.java | 7 ++++++- 9 files changed, 44 insertions(+), 6 deletions(-) diff --git a/api/src/main/java/lol/pyr/znpcsplus/api/entity/EntityPropertyRegistry.java b/api/src/main/java/lol/pyr/znpcsplus/api/entity/EntityPropertyRegistry.java index 1dc26bb..0009ee9 100644 --- a/api/src/main/java/lol/pyr/znpcsplus/api/entity/EntityPropertyRegistry.java +++ b/api/src/main/java/lol/pyr/znpcsplus/api/entity/EntityPropertyRegistry.java @@ -1,6 +1,9 @@ package lol.pyr.znpcsplus.api.entity; +import java.util.Collection; + public interface EntityPropertyRegistry { + Collection> getAll(); EntityProperty getByName(String name); EntityProperty getByName(String name, Class type); } diff --git a/api/src/main/java/lol/pyr/znpcsplus/api/npc/NpcRegistry.java b/api/src/main/java/lol/pyr/znpcsplus/api/npc/NpcRegistry.java index 4047f93..a5d4a23 100644 --- a/api/src/main/java/lol/pyr/znpcsplus/api/npc/NpcRegistry.java +++ b/api/src/main/java/lol/pyr/znpcsplus/api/npc/NpcRegistry.java @@ -7,7 +7,9 @@ import java.util.Collection; public interface NpcRegistry { Collection getAll(); - Collection getIds(); + Collection getAllIds(); + Collection getAllPlayerMade(); + Collection getAllPlayerMadeIds(); NpcEntry create(String id, World world, NpcType type, NpcLocation location); NpcEntry get(String id); void delete(String id); diff --git a/api/src/main/java/lol/pyr/znpcsplus/api/npc/NpcTypeRegistry.java b/api/src/main/java/lol/pyr/znpcsplus/api/npc/NpcTypeRegistry.java index 3847504..9382869 100644 --- a/api/src/main/java/lol/pyr/znpcsplus/api/npc/NpcTypeRegistry.java +++ b/api/src/main/java/lol/pyr/znpcsplus/api/npc/NpcTypeRegistry.java @@ -1,5 +1,8 @@ package lol.pyr.znpcsplus.api.npc; +import java.util.Collection; + public interface NpcTypeRegistry { NpcType getByName(String name); + Collection getAll(); } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java index a69bb05..5706fb3 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java @@ -158,7 +158,7 @@ public class ZNpcsPlus extends JavaPlugin { World world = Bukkit.getWorld("world"); if (world == null) world = Bukkit.getWorlds().get(0); int i = 0; - for (NpcTypeImpl type : typeRegistry.getAll()) { + for (NpcTypeImpl type : typeRegistry.getAllImpl()) { NpcEntryImpl entry = npcRegistry.create("debug_npc_" + i, world, type, new NpcLocation(i * 3, 200, 0, 0, 0)); entry.setProcessed(true); NpcImpl npc = entry.getNpc(); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/CreateCommand.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/CreateCommand.java index 930fdc2..cf6e4a6 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/commands/CreateCommand.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/CreateCommand.java @@ -42,7 +42,7 @@ public class CreateCommand implements CommandHandler { @Override public List suggest(CommandContext context) throws CommandExecutionException { if (context.argSize() == 1) return context.suggestCollection(npcRegistry.getModifiableIds()); - if (context.argSize() == 2) return context.suggestStream(typeRegistry.getAll().stream().map(NpcTypeImpl::getName)); + if (context.argSize() == 2) return context.suggestStream(typeRegistry.getAllImpl().stream().map(NpcTypeImpl::getName)); return Collections.emptyList(); } } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/TypeCommand.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/TypeCommand.java index 8ed36fb..4d57468 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/commands/TypeCommand.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/TypeCommand.java @@ -31,7 +31,7 @@ public class TypeCommand implements CommandHandler { @Override public List suggest(CommandContext context) throws CommandExecutionException { if (context.argSize() == 1) return context.suggestCollection(registry.getModifiableIds()); - if (context.argSize() == 2) return context.suggestStream(typeRegistry.getAll().stream().map(NpcTypeImpl::getName)); + if (context.argSize() == 2) return context.suggestStream(typeRegistry.getAllImpl().stream().map(NpcTypeImpl::getName)); return Collections.emptyList(); } } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java b/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java index 92ac7bf..f105696 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java @@ -1,6 +1,7 @@ package lol.pyr.znpcsplus.entity; import com.github.retrooper.packetevents.protocol.item.ItemStack; +import lol.pyr.znpcsplus.api.entity.EntityProperty; import lol.pyr.znpcsplus.api.entity.EntityPropertyRegistry; import lol.pyr.znpcsplus.api.skin.SkinDescriptor; import lol.pyr.znpcsplus.entity.serializers.*; @@ -8,8 +9,11 @@ import lol.pyr.znpcsplus.skin.cache.SkinCache; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; +import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.Map; +import java.util.stream.Collectors; @SuppressWarnings("unchecked") public class EntityPropertyRegistryImpl implements EntityPropertyRegistry { @@ -213,6 +217,14 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry { byName.put(name.toLowerCase(), property); } + @Override + public Collection> getAll() { + return Collections.unmodifiableCollection( + byName.values().stream() + .map(property -> (EntityProperty) property) + .collect(Collectors.toSet())); + } + public EntityPropertyImpl getByName(String name, Class type) { return (EntityPropertyImpl) getByName(name); } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcRegistryImpl.java b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcRegistryImpl.java index 2527d4a..7e9879f 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcRegistryImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcRegistryImpl.java @@ -1,6 +1,7 @@ package lol.pyr.znpcsplus.npc; import lol.pyr.znpcsplus.ZNpcsPlus; +import lol.pyr.znpcsplus.api.npc.NpcEntry; import lol.pyr.znpcsplus.api.npc.NpcRegistry; import lol.pyr.znpcsplus.api.npc.NpcType; import lol.pyr.znpcsplus.config.ConfigManager; @@ -80,10 +81,22 @@ public class NpcRegistryImpl implements NpcRegistry { return getAll().stream().filter(entry -> entry.getNpc().getEntity().getEntityId() == id).findFirst().orElse(null); } - public Collection getIds() { + public Collection getAllIds() { return Collections.unmodifiableSet(npcMap.keySet()); } + @Override + public Collection getAllPlayerMade() { + return getAllModifiable(); + } + + @Override + public Collection getAllPlayerMadeIds() { + return getAllModifiable().stream() + .map(NpcEntryImpl::getId) + .collect(Collectors.toSet()); + } + public Collection getModifiableIds() { return Collections.unmodifiableSet(npcMap.entrySet().stream() .filter(entry -> entry.getValue().isAllowCommandModification()) diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeRegistryImpl.java b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeRegistryImpl.java index 82a38e4..cb61b19 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeRegistryImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeRegistryImpl.java @@ -4,6 +4,7 @@ import com.github.retrooper.packetevents.PacketEventsAPI; import com.github.retrooper.packetevents.manager.server.ServerVersion; import com.github.retrooper.packetevents.protocol.entity.type.EntityType; import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes; +import lol.pyr.znpcsplus.api.npc.NpcType; import lol.pyr.znpcsplus.api.npc.NpcTypeRegistry; import lol.pyr.znpcsplus.entity.EntityPropertyRegistryImpl; import org.bukkit.plugin.Plugin; @@ -319,7 +320,11 @@ public class NpcTypeRegistryImpl implements NpcTypeRegistry { .setHologramOffset(0.25)); } - public Collection getAll() { + public Collection getAll() { + return Collections.unmodifiableList(types); + } + + public Collection getAllImpl() { return Collections.unmodifiableList(types); }