|
| 1 | +package net.swofty.type.bedwarsconfigurator; |
| 2 | + |
| 3 | +import com.google.gson.Gson; |
| 4 | +import com.google.gson.GsonBuilder; |
| 5 | +import lombok.Getter; |
| 6 | +import net.minestom.server.MinecraftServer; |
| 7 | +import net.minestom.server.coordinate.Pos; |
| 8 | +import net.swofty.commons.CustomWorlds; |
| 9 | +import net.swofty.commons.ServerType; |
| 10 | +import net.swofty.commons.ServiceType; |
| 11 | +import net.swofty.commons.bedwars.map.BedWarsMapsConfig; |
| 12 | +import net.swofty.proxyapi.redis.ProxyToClient; |
| 13 | +import net.swofty.proxyapi.redis.ServiceToClient; |
| 14 | +import net.swofty.type.generic.HypixelGenericLoader; |
| 15 | +import net.swofty.type.generic.HypixelTypeLoader; |
| 16 | +import net.swofty.type.generic.command.HypixelCommand; |
| 17 | +import net.swofty.type.generic.entity.npc.HypixelNPC; |
| 18 | +import net.swofty.type.generic.event.HypixelEventClass; |
| 19 | +import net.swofty.type.generic.tab.EmptyTabModule; |
| 20 | +import net.swofty.type.generic.tab.TablistManager; |
| 21 | +import net.swofty.type.generic.tab.TablistModule; |
| 22 | +import org.jetbrains.annotations.Nullable; |
| 23 | +import org.tinylog.Logger; |
| 24 | + |
| 25 | +import java.io.InputStream; |
| 26 | +import java.nio.charset.StandardCharsets; |
| 27 | +import java.nio.file.Files; |
| 28 | +import java.nio.file.Path; |
| 29 | +import java.util.List; |
| 30 | + |
| 31 | +public class TypeBedWarsConfiguratorLoader implements HypixelTypeLoader { |
| 32 | + private static Gson gson; |
| 33 | + |
| 34 | + @Getter |
| 35 | + private static BedWarsMapsConfig mapsConfig; |
| 36 | + |
| 37 | + @Override |
| 38 | + public ServerType getType() { |
| 39 | + return ServerType.BEDWARS_CONFIGURATOR; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void onInitialize(MinecraftServer server) { |
| 44 | + gson = new GsonBuilder().create(); |
| 45 | + loadMapsConfig(); |
| 46 | + } |
| 47 | + |
| 48 | + private static void loadMapsConfig() { |
| 49 | + Path mapsPath = Path.of("./configuration/bedwars/maps.json"); |
| 50 | + if (!Files.exists(mapsPath)) { |
| 51 | + Logger.error("maps.json not found at {}", mapsPath.toAbsolutePath()); |
| 52 | + return; |
| 53 | + } |
| 54 | + try (InputStream in = Files.newInputStream(mapsPath)) { |
| 55 | + byte[] bytes = in.readAllBytes(); |
| 56 | + String json = new String(bytes, StandardCharsets.UTF_8); |
| 57 | + mapsConfig = gson.fromJson(json, BedWarsMapsConfig.class); |
| 58 | + } catch (Exception e) { |
| 59 | + Logger.error("Failed to load maps.json", e); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Reloads the maps configuration from file |
| 65 | + */ |
| 66 | + public static void reloadMapsConfig() { |
| 67 | + if (gson == null) { |
| 68 | + gson = new GsonBuilder().create(); |
| 69 | + } |
| 70 | + loadMapsConfig(); |
| 71 | + Logger.info("Reloaded maps.json configuration"); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void afterInitialize(MinecraftServer server) { |
| 76 | + HypixelGenericLoader.loopThroughPackage("net.swofty.type.bedwarsconfigurator.commands", HypixelCommand.class).forEach(command -> { |
| 77 | + try { |
| 78 | + MinecraftServer.getCommandManager().register(command.getCommand()); |
| 79 | + } catch (Exception e) { |
| 80 | + Logger.error(e, "Failed to register command " + command.getCommand().getName() + " in class " + command.getClass().getSimpleName()); |
| 81 | + } |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public List<ServiceType> getRequiredServices() { |
| 87 | + return List.of(); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public TablistManager getTablistManager() { |
| 92 | + return new TablistManager() { |
| 93 | + @Override |
| 94 | + public List<TablistModule> getModules() { |
| 95 | + return List.of( |
| 96 | + new EmptyTabModule(), |
| 97 | + new EmptyTabModule(), |
| 98 | + new EmptyTabModule(), |
| 99 | + new EmptyTabModule() |
| 100 | + ); |
| 101 | + } |
| 102 | + }; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public LoaderValues getLoaderValues() { |
| 107 | + return new LoaderValues( |
| 108 | + (type) -> new Pos(0, 100, 0, -90, 0), // Spawn position |
| 109 | + false // Announce death messages |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public List<HypixelEventClass> getTraditionalEvents() { |
| 115 | + return HypixelGenericLoader.loopThroughPackage( |
| 116 | + "net.swofty.type.bedwarsconfigurator.events", |
| 117 | + HypixelEventClass.class |
| 118 | + ).toList(); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public List<HypixelEventClass> getCustomEvents() { |
| 123 | + return HypixelGenericLoader.loopThroughPackage( |
| 124 | + "net.swofty.type.bedwarsconfigurator.events.custom", |
| 125 | + HypixelEventClass.class |
| 126 | + ).toList(); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public List<HypixelNPC> getNPCs() { |
| 131 | + return HypixelGenericLoader.loopThroughPackage( |
| 132 | + "net.swofty.type.bedwarsconfigurator.npcs", |
| 133 | + HypixelNPC.class |
| 134 | + ).toList(); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public List<ServiceToClient> getServiceRedisListeners() { |
| 139 | + return HypixelGenericLoader.loopThroughPackage( |
| 140 | + "net.swofty.type.bedwarsconfigurator.redis.service", |
| 141 | + ServiceToClient.class |
| 142 | + ).toList(); |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public List<ProxyToClient> getProxyRedisListeners() { |
| 147 | + return List.of(); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public @Nullable CustomWorlds getMainInstance() { |
| 152 | + return CustomWorlds.BEDWARS_LOBBY; |
| 153 | + } |
| 154 | + |
| 155 | +} |
0 commit comments