From ba31f5ca1b525be4e32ff29f332e751a083e8659 Mon Sep 17 00:00:00 2001 From: Misat11 <20199703+Misat11@users.noreply.github.com> Date: Sat, 30 Nov 2024 21:59:42 +0100 Subject: [PATCH] feat: port changes from SBW 0.2.35 --- .../bedwars/api/game/ItemSpawner.java | 4 +- .../bedwars/api/game/ItemSpawnerType.java | 5 ++ .../api/game/ItemSpawnerTypeHolder.java | 39 ++++++++++++ .../bedwars/api/upgrades/UpgradeStorage.java | 3 +- .../bedwars/api/variants/Variant.java | 2 + .../bedwars/commands/DumpCommand.java | 2 +- .../bedwars/commands/admin/InfoCommand.java | 5 +- .../commands/admin/SpawnerCommand.java | 45 ++++++------- .../bedwars/game/ItemSpawnerImpl.java | 63 +++++++++++++------ .../game/ItemSpawnerTypeHolderImpl.java | 33 ++++++++++ .../bedwars/game/ItemSpawnerTypeImpl.java | 5 ++ .../bedwars/listener/BungeeMotdListener.java | 31 +++++---- .../bedwars/listener/PlayerListener.java | 2 + plugin/universal/build.gradle | 2 +- 14 files changed, 184 insertions(+), 57 deletions(-) create mode 100644 api/src/main/java/org/screamingsandals/bedwars/api/game/ItemSpawnerTypeHolder.java create mode 100644 plugin/common/src/main/java/org/screamingsandals/bedwars/game/ItemSpawnerTypeHolderImpl.java diff --git a/api/src/main/java/org/screamingsandals/bedwars/api/game/ItemSpawner.java b/api/src/main/java/org/screamingsandals/bedwars/api/game/ItemSpawner.java index 3b5663105..8b0c3f4f7 100644 --- a/api/src/main/java/org/screamingsandals/bedwars/api/game/ItemSpawner.java +++ b/api/src/main/java/org/screamingsandals/bedwars/api/game/ItemSpawner.java @@ -33,9 +33,9 @@ public interface ItemSpawner extends Upgrade { /** * @return */ - ItemSpawnerType getItemSpawnerType(); + ItemSpawnerTypeHolder getItemSpawnerType(); - void setItemSpawnerType(ItemSpawnerType spawnerType); + void setItemSpawnerType(ItemSpawnerTypeHolder spawnerType); /** * @return diff --git a/api/src/main/java/org/screamingsandals/bedwars/api/game/ItemSpawnerType.java b/api/src/main/java/org/screamingsandals/bedwars/api/game/ItemSpawnerType.java index b3ca9bcbc..d0d96088a 100644 --- a/api/src/main/java/org/screamingsandals/bedwars/api/game/ItemSpawnerType.java +++ b/api/src/main/java/org/screamingsandals/bedwars/api/game/ItemSpawnerType.java @@ -79,4 +79,9 @@ public interface ItemSpawnerType { * @return */ ItemStackHolder getItem(int amount); + + /** + * @since 0.3.0 + */ + ItemSpawnerTypeHolder toHolder(); } diff --git a/api/src/main/java/org/screamingsandals/bedwars/api/game/ItemSpawnerTypeHolder.java b/api/src/main/java/org/screamingsandals/bedwars/api/game/ItemSpawnerTypeHolder.java new file mode 100644 index 000000000..0741662f6 --- /dev/null +++ b/api/src/main/java/org/screamingsandals/bedwars/api/game/ItemSpawnerTypeHolder.java @@ -0,0 +1,39 @@ +package org.screamingsandals.bedwars.api.game; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.screamingsandals.bedwars.api.variants.Variant; + +/** + * @since 0.3.0 + */ +public interface ItemSpawnerTypeHolder { + /** + * @since 0.3.0 + */ + @NotNull String configKey(); + + /** + * @since 0.3.0 + */ + @Nullable ItemSpawnerType toSpawnerType(@NotNull LocalGame variant); + + /** + * @since 0.3.0 + */ + @Nullable ItemSpawnerType toSpawnerType(@Nullable Variant variant); + + /** + * @since 0.3.0 + */ + default boolean isValid(@NotNull LocalGame game) { + return toSpawnerType(game) != null; + } + + /** + * @since 0.3.0 + */ + default boolean isValid(@Nullable Variant variant) { + return toSpawnerType(variant) != null; + } +} diff --git a/api/src/main/java/org/screamingsandals/bedwars/api/upgrades/UpgradeStorage.java b/api/src/main/java/org/screamingsandals/bedwars/api/upgrades/UpgradeStorage.java index e90a86551..2146ee79a 100644 --- a/api/src/main/java/org/screamingsandals/bedwars/api/upgrades/UpgradeStorage.java +++ b/api/src/main/java/org/screamingsandals/bedwars/api/upgrades/UpgradeStorage.java @@ -218,7 +218,8 @@ public List findItemSpawnerUpgrades(LocalGame game, Team team, ItemSpaw } final var name = itemSpawner.getTeam(); - if (name != null && team.getName().equals(name.getName()) && itemSpawnerType.getName().equals(itemSpawner.getItemSpawnerType().getName())) { + final var type = itemSpawner.getItemSpawnerType().toSpawnerType(game); + if (name != null && type != null && team.getName().equals(name.getName()) && itemSpawnerType.getName().equals(type.getName())) { upgrades.add(upgrade); } } diff --git a/api/src/main/java/org/screamingsandals/bedwars/api/variants/Variant.java b/api/src/main/java/org/screamingsandals/bedwars/api/variants/Variant.java index 6786abb7e..1858b47bc 100644 --- a/api/src/main/java/org/screamingsandals/bedwars/api/variants/Variant.java +++ b/api/src/main/java/org/screamingsandals/bedwars/api/variants/Variant.java @@ -19,6 +19,7 @@ package org.screamingsandals.bedwars.api.variants; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.screamingsandals.bedwars.api.config.GameConfigurationContainer; @@ -32,6 +33,7 @@ * @author ScreamingSandals * @since 0.3.0 */ +@ApiStatus.NonExtendable public interface Variant { /** diff --git a/plugin/common/src/main/java/org/screamingsandals/bedwars/commands/DumpCommand.java b/plugin/common/src/main/java/org/screamingsandals/bedwars/commands/DumpCommand.java index 2b12a8bc1..679e140ea 100644 --- a/plugin/common/src/main/java/org/screamingsandals/bedwars/commands/DumpCommand.java +++ b/plugin/common/src/main/java/org/screamingsandals/bedwars/commands/DumpCommand.java @@ -170,7 +170,7 @@ protected void construct(Command.Builder commandSenderWrapperBuil "pos2", locationToMap(game.getPos2()), "weather", game.getArenaWeather() != null ? game.getArenaWeather().location().asString() : null, "spawners", game.getSpawners().stream().map(itemSpawner -> nullValuesAllowingMap( - "type", itemSpawner.getItemSpawnerType().getConfigKey(), + "type", itemSpawner.getItemSpawnerType().configKey(), "location", locationToMap(itemSpawner.getLocation()), "maxSpawnedResources", itemSpawner.getMaxSpawnedResources(), "startLevel", itemSpawner.getBaseAmountPerSpawn(), diff --git a/plugin/common/src/main/java/org/screamingsandals/bedwars/commands/admin/InfoCommand.java b/plugin/common/src/main/java/org/screamingsandals/bedwars/commands/admin/InfoCommand.java index f7ca51355..01f1cc11a 100644 --- a/plugin/common/src/main/java/org/screamingsandals/bedwars/commands/admin/InfoCommand.java +++ b/plugin/common/src/main/java/org/screamingsandals/bedwars/commands/admin/InfoCommand.java @@ -27,6 +27,7 @@ import org.screamingsandals.bedwars.lang.LangKeys; import org.screamingsandals.lib.lang.Message; import org.screamingsandals.lib.sender.CommandSender; +import org.screamingsandals.lib.spectator.Color; import org.screamingsandals.lib.spectator.Component; import org.screamingsandals.lib.spectator.event.ClickEvent; import org.screamingsandals.lib.utils.annotations.Service; @@ -240,9 +241,11 @@ public void construct(CommandManager manager, Command.Builder manager, Command.Builder manager, Command.Builder manager, Command.Builder manager, Command.Builder manager, Command.Builder manager, Command.Builder manager, Command.Builder manager, Command.Builder manager, Command.Builder manager, Command.Builder manager, Command.Builder manager, Command.Builder changeSettingCommand(commandContext, (sender, game, itemSpawner) -> { itemSpawner.setInitialInterval(null); sender.sendMessage(Message.of(LangKeys.ADMIN_ARENA_EDIT_SUCCESS_SPAWNER_INITIAL_INTERVAL_RESET) - .placeholder("type", itemSpawner.getItemSpawnerType().getItemName()) + .placeholder("type", itemSpawner.getItemSpawnerType().toSpawnerType(game).getItemName()) .defaultPrefix()); })) ); @@ -367,7 +368,7 @@ public void construct(CommandManager manager, Command.Builder manager, Command.Builder changeSettingCommand(commandContext, (sender, game, itemSpawner) -> { itemSpawner.setCustomSpread(null); sender.sendMessage(Message.of(LangKeys.ADMIN_ARENA_EDIT_SUCCESS_SPAWNER_SPREAD_RESET) - .placeholder("type", itemSpawner.getItemSpawnerType().getItemName()) + .placeholder("type", itemSpawner.getItemSpawnerType().toSpawnerType(game).getItemName()) .defaultPrefix()); })) ); @@ -424,7 +425,7 @@ private void changeSettingCommand(CommandContext commandContext, .filter(spawner -> spawner.getLocation().getBlock().equals(loc.getBlock())) .collect(Collectors.toList()); - if (spawners.size() == 0) { + if (spawners.isEmpty()) { sender.sendMessage(Message.of(LangKeys.ADMIN_ARENA_EDIT_ERRORS_NO_SPAWNER).defaultPrefix()); } else if (spawners.size() == 1) { var itemSpawner = spawners.get(0); @@ -441,8 +442,9 @@ private void changeSettingCommand(CommandContext commandContext, var rawInput = new ArrayList<>(commandContext.getRawInput()); rawInput.remove(rawInput.size() - 1); var command = String.join(" ", rawInput) + " " + number.getAndIncrement(); + var type = itemSpawner.getItemSpawnerType().toSpawnerType(game); Message.of(LangKeys.ADMIN_ARENA_EDIT_ERRORS_MULTIPLE_SPAWNERS_LINE) - .placeholder("type", itemSpawner.getItemSpawnerType().getItemName()) + .placeholder("type", type == null ? Component.text(itemSpawner.getItemSpawnerType().configKey(), Color.RED) : type.getItemName()) .placeholder("command", Component.text().content(command).clickEvent(ClickEvent.runCommand(command))) .send(sender); }); @@ -452,8 +454,9 @@ private void changeSettingCommand(CommandContext commandContext, AtomicInteger number = new AtomicInteger(1); spawners.forEach(itemSpawner -> { var command = commandContext.getRawInputJoined() + " " + number.getAndIncrement(); + var type = itemSpawner.getItemSpawnerType().toSpawnerType(game); Message.of(LangKeys.ADMIN_ARENA_EDIT_ERRORS_MULTIPLE_SPAWNERS_LINE) - .placeholder("type", itemSpawner.getItemSpawnerType().getItemName()) + .placeholder("type", type == null ? Component.text(itemSpawner.getItemSpawnerType().configKey(), Color.RED) : type.getItemName()) .placeholder("command", Component.text().content(command).clickEvent(ClickEvent.runCommand(command))) .send(sender); }); diff --git a/plugin/common/src/main/java/org/screamingsandals/bedwars/game/ItemSpawnerImpl.java b/plugin/common/src/main/java/org/screamingsandals/bedwars/game/ItemSpawnerImpl.java index 593bf453f..a12a08038 100644 --- a/plugin/common/src/main/java/org/screamingsandals/bedwars/game/ItemSpawnerImpl.java +++ b/plugin/common/src/main/java/org/screamingsandals/bedwars/game/ItemSpawnerImpl.java @@ -25,10 +25,11 @@ import lombok.experimental.Tolerate; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.screamingsandals.bedwars.BedWarsPlugin; import org.screamingsandals.bedwars.api.Team; import org.screamingsandals.bedwars.api.config.GameConfigurationContainer; import org.screamingsandals.bedwars.api.game.ItemSpawner; -import org.screamingsandals.bedwars.api.game.ItemSpawnerType; +import org.screamingsandals.bedwars.api.game.ItemSpawnerTypeHolder; import org.screamingsandals.bedwars.config.MainConfig; import org.screamingsandals.bedwars.events.ResourceSpawnEventImpl; import org.screamingsandals.bedwars.lang.LangKeys; @@ -61,7 +62,7 @@ @Setter public class ItemSpawnerImpl implements ItemSpawner, SerializableGameComponent { private final Location location; - private ItemSpawnerTypeImpl itemSpawnerType; + private ItemSpawnerTypeHolderImpl itemSpawnerType; private String customName = null; private boolean hologramEnabled = true; private double baseAmountPerSpawn = 1; @@ -111,8 +112,11 @@ public class ItemSpawnerImpl implements ItemSpawner, SerializableGameComponent { @Getter(AccessLevel.NONE) @Setter(AccessLevel.NONE) private Task runningTask; + @Getter(AccessLevel.NONE) + @Setter(AccessLevel.NONE) + private ItemSpawnerTypeImpl cachedType; - public ItemSpawnerImpl(Location location, ItemSpawnerTypeImpl itemSpawnerType) { + public ItemSpawnerImpl(Location location, ItemSpawnerTypeHolderImpl itemSpawnerType) { this.location = location; this.itemSpawnerType = itemSpawnerType; } @@ -209,11 +213,12 @@ public int getSpawnedItemsCount() { @Override @Tolerate - public void setItemSpawnerType(ItemSpawnerType spawnerType) { - if (!(spawnerType instanceof ItemSpawnerTypeImpl)) { + public void setItemSpawnerType(ItemSpawnerTypeHolder spawnerType) { + if (!(spawnerType instanceof ItemSpawnerTypeHolderImpl)) { throw new IllegalArgumentException("Provided instance of item spawner type is not created by BedWars plugin!"); } - this.itemSpawnerType = (ItemSpawnerTypeImpl) spawnerType; + this.itemSpawnerType = (ItemSpawnerTypeHolderImpl) spawnerType; + this.cachedType = null; } @Override @@ -227,7 +232,7 @@ public void setTeam(Team team) { @Override public long getInitialIntervalTicks() { - return initialInterval != null ? initialInterval.second().getBukkitTime(initialInterval.first()) : itemSpawnerType.getIntervalTicks(); + return initialInterval != null ? initialInterval.second().getBukkitTime(initialInterval.first()) : (cachedType != null ? cachedType.getIntervalTicks() : 1); } @Override @@ -269,9 +274,9 @@ private void prepareHolograms(List viewers, boolean countdownHolo .hologram(loc); if (certainPopularServerHolo) { hologram.firstLine(Message.of(LangKeys.IN_GAME_SPAWNER_TIER).placeholder("tier", this.tier)); - hologram.bottomLine(TextEntry.of(itemSpawnerType.getItemBoldName())); + hologram.bottomLine(TextEntry.of(cachedType.getItemBoldName())); } else { - hologram.firstLine(TextEntry.of(itemSpawnerType.getItemBoldName())); + hologram.firstLine(TextEntry.of(cachedType.getItemBoldName())); } if (countdownHologram) { @@ -286,13 +291,13 @@ private void prepareHolograms(List viewers, boolean countdownHolo if (floatingBlockEnabled && MainConfig.getInstance().node("floating-generator", "enabled").getBoolean(true)) { - var materialName = itemSpawnerType.getItemType().location().asString(); + var materialName = cachedType.getItemType().location().asString(); var indexOfUnderscore = materialName.indexOf("_"); hologram .item( Objects.requireNonNullElseGet(ItemStackFactory .build(materialName.substring(0, (indexOfUnderscore != -1 ? indexOfUnderscore : materialName.length())) + "_BLOCK"), - () -> ItemStackFactory.build(itemSpawnerType.getItemType())) + () -> ItemStackFactory.build(cachedType.getItemType())) ) .itemPosition(Hologram.ItemPosition.BELOW) .rotationMode(rotationMode) @@ -331,6 +336,11 @@ public void start(GameImpl game) { return; } + this.cachedType = itemSpawnerType.toSpawnerType(game); + if (this.cachedType == null) { + return; + } + this.amountPerSpawn = this.baseAmountPerSpawn; this.tier = 0; this.certainPopularServerHolo = hologramType == HologramType.CERTAIN_POPULAR_SERVER || (hologramType == HologramType.DEFAULT && game.getConfigurationContainer().getOrDefault(GameConfigurationContainer.USE_CERTAIN_POPULAR_SERVER_LIKE_HOLOGRAMS_FOR_SPAWNERS, false)); @@ -354,7 +364,7 @@ public void start(GameImpl game) { return; } - this.currentInterval = Objects.requireNonNullElseGet(this.initialInterval, this.itemSpawnerType::getInterval); + this.currentInterval = Objects.requireNonNullElseGet(this.initialInterval, this.cachedType::getInterval); if (runningTask != null) { runningTask.cancel(); @@ -378,6 +388,13 @@ public void start(GameImpl game) { this.elapsedTime++; return; } + // Someone changed the ItemSpawnerType while the game was running. That is unsupported and may lead to weird behaviour + if (this.cachedType == null) { + this.cachedType = itemSpawnerType.toSpawnerType(game); + if (this.cachedType == null) { + return; // WHAT?? + } + } if (team != null && !game.isTeamActive(team) && stopTeamSpawnersOnDie) { taskItself.cancel(); @@ -441,7 +458,7 @@ public void start(GameImpl game) { } } - var resourceSpawnEvent = new ResourceSpawnEventImpl(game, this, itemSpawnerType, itemSpawnerType.getItem(calculatedStack)); + var resourceSpawnEvent = new ResourceSpawnEventImpl(game, this, cachedType, cachedType.getItem(calculatedStack)); EventManager.fire(resourceSpawnEvent); if (resourceSpawnEvent.isCancelled()) { @@ -455,7 +472,7 @@ public void start(GameImpl game) { if (resource.getAmount() > 0) { var loc = this.location.add(0, 0.05, 0); var item = Objects.requireNonNull(Entities.dropItem(resource, loc)); - var spread = customSpread != null ? customSpread : itemSpawnerType.getSpread(); + var spread = customSpread != null ? customSpread : cachedType.getSpread(); if (spread != 1.0) { item.setVelocity(item.getVelocity().multiply(spread)); } @@ -477,7 +494,7 @@ public void changeInterval(Pair time) { @Override public void saveTo(@NotNull ConfigurationNode node) throws SerializationException { node.node("location").set(MiscUtils.writeLocationToString(location)); - node.node("type").set(itemSpawnerType.getConfigKey()); + node.node("type").set(itemSpawnerType.configKey()); node.node("customName").set(customName); node.node("startLevel").set(baseAmountPerSpawn); node.node("hologramEnabled").set(hologramEnabled); @@ -507,12 +524,19 @@ public Optional load(@NotNull GameImpl game, @NotNull Configura if (spawnerType == null) { throw new UnsupportedOperationException("Wrongly configured spawner type!"); } - var type = game.getGameVariant().getItemSpawnerType(spawnerType); + var typeHolder = new ItemSpawnerTypeHolderImpl(spawnerType); + var type = typeHolder.toSpawnerType(game); + if (type == null) { - throw new UnsupportedOperationException("Wrongly configured spawner type!"); + BedWarsPlugin.getInstance().getLogger().warn( + "There is an unknown spawner type {} defined in game {} on location {}", + spawnerType, + game.getName(), + node.node("location").getString() + ); } - var spawner = new ItemSpawnerImpl(MiscUtils.readLocationFromString(game.getWorld(), Objects.requireNonNull(node.node("location").getString())), type); + var spawner = new ItemSpawnerImpl(MiscUtils.readLocationFromString(game.getWorld(), Objects.requireNonNull(node.node("location").getString())), typeHolder); spawner.setCustomName(node.node("customName").getString()); spawner.setHologramEnabled(node.node("hologramEnabled").getBoolean(true)); spawner.setBaseAmountPerSpawn(node.node("startLevel").getDouble(1)); @@ -521,6 +545,7 @@ public Optional load(@NotNull GameImpl game, @NotNull Configura spawner.setFloatingBlockEnabled(node.node("floatingEnabled").getBoolean()); spawner.setRotationMode(Hologram.RotationMode.valueOf(node.node("rotationMode").getString("Y"))); spawner.setHologramType(ItemSpawner.HologramType.valueOf(node.node("hologramType").getString("DEFAULT"))); + spawner.cachedType = type; var initialIntervalValueNode = node.node("initialInterval", "value"); var initialIntervalUnitNode = node.node("initialInterval", "unit"); @@ -530,7 +555,7 @@ public Optional load(@NotNull GameImpl game, @NotNull Configura var customSpreadNode = node.node("customSpread"); if (!customSpreadNode.empty()) { - spawner.setCustomSpread(customSpreadNode.getDouble(type.getSpread())); + spawner.setCustomSpread(customSpreadNode.getDouble(type != null ? type.getSpread() : 1)); } return Optional.of(spawner); diff --git a/plugin/common/src/main/java/org/screamingsandals/bedwars/game/ItemSpawnerTypeHolderImpl.java b/plugin/common/src/main/java/org/screamingsandals/bedwars/game/ItemSpawnerTypeHolderImpl.java new file mode 100644 index 000000000..31db44a26 --- /dev/null +++ b/plugin/common/src/main/java/org/screamingsandals/bedwars/game/ItemSpawnerTypeHolderImpl.java @@ -0,0 +1,33 @@ +package org.screamingsandals.bedwars.game; + +import lombok.Data; +import lombok.experimental.Accessors; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.screamingsandals.bedwars.api.game.ItemSpawnerTypeHolder; +import org.screamingsandals.bedwars.api.game.LocalGame; +import org.screamingsandals.bedwars.api.variants.Variant; +import org.screamingsandals.bedwars.variants.VariantImpl; +import org.screamingsandals.bedwars.variants.VariantManagerImpl; + +@Data +@Accessors(fluent = true) +public class ItemSpawnerTypeHolderImpl implements ItemSpawnerTypeHolder { + private final @NotNull String configKey; + + @Override + public @Nullable ItemSpawnerTypeImpl toSpawnerType(@NotNull LocalGame game) { + return toSpawnerType(game.getGameVariant()); + } + + @Override + public @Nullable ItemSpawnerTypeImpl toSpawnerType(@Nullable Variant variant) { + if (variant == null) { + variant = VariantManagerImpl.getInstance().getDefaultVariant(); + } + if (!(variant instanceof VariantImpl)) { + throw new IllegalArgumentException("Invalid variant type: " + variant); + } + return ((VariantImpl) variant).getItemSpawnerType(configKey); + } +} diff --git a/plugin/common/src/main/java/org/screamingsandals/bedwars/game/ItemSpawnerTypeImpl.java b/plugin/common/src/main/java/org/screamingsandals/bedwars/game/ItemSpawnerTypeImpl.java index 67e7162bb..e01937094 100644 --- a/plugin/common/src/main/java/org/screamingsandals/bedwars/game/ItemSpawnerTypeImpl.java +++ b/plugin/common/src/main/java/org/screamingsandals/bedwars/game/ItemSpawnerTypeImpl.java @@ -78,6 +78,11 @@ public ItemStack getItem(int amount) { return Objects.requireNonNull(ItemStackFactory.build(itemType, builder -> builder.name(getItemName()).amount(amount))); } + @Override + public ItemSpawnerTypeHolderImpl toHolder() { + return new ItemSpawnerTypeHolderImpl(configKey); + } + public static ItemSpawnerTypeImpl deserialize(String spawnerKey, ConfigurationNode node) { spawnerKey = spawnerKey.toLowerCase(Locale.ROOT); diff --git a/plugin/common/src/main/java/org/screamingsandals/bedwars/listener/BungeeMotdListener.java b/plugin/common/src/main/java/org/screamingsandals/bedwars/listener/BungeeMotdListener.java index fb781ece2..9bb939f01 100644 --- a/plugin/common/src/main/java/org/screamingsandals/bedwars/listener/BungeeMotdListener.java +++ b/plugin/common/src/main/java/org/screamingsandals/bedwars/listener/BungeeMotdListener.java @@ -19,6 +19,7 @@ package org.screamingsandals.bedwars.listener; +import lombok.RequiredArgsConstructor; import org.jetbrains.annotations.NotNull; import org.screamingsandals.bedwars.api.game.Game; import org.screamingsandals.bedwars.config.MainConfig; @@ -31,26 +32,34 @@ import org.screamingsandals.lib.utils.annotations.methods.ShouldRunControllable; @Service +@RequiredArgsConstructor public class BungeeMotdListener { + private final GameManagerImpl gameManager; + private final MainConfig mainConfig; + @ShouldRunControllable public boolean isEnabled() { - return MainConfig.getInstance().node("bungee", "enabled").getBoolean() && MainConfig.getInstance().node("bungee", "motd", "enabled").getBoolean(); + return mainConfig.node("bungee", "enabled").getBoolean() && mainConfig.node("bungee", "motd", "enabled").getBoolean(); } @OnEvent public void onServerListPing(@NotNull ServerListPingEvent slpe) { - var games = GameManagerImpl.getInstance().getLocalGames(); + var games = gameManager.getLocalGames(); if (games.isEmpty()) { return; } Game gameA = null; - if (GameManagerImpl.getInstance().isDoGamePreselection()) { - gameA = GameManagerImpl.getInstance().getPreselectedGame(); + if (gameManager.isDoGamePreselection()) { + gameA = gameManager.getPreselectedGame(); } if (gameA == null) { - if (MainConfig.getInstance().node("bungee", "random-game-selection", "enabled").getBoolean()) { - gameA = GameManagerImpl.getInstance().getGameWithHighestPlayers().orElse(null); + if (mainConfig.node("bungee", "random-game-selection", "enabled").getBoolean()) { + gameA = gameManager.getGameWithHighestPlayers().orElse(null); + + if (gameA == null) { + gameA = games.get(0); // seems like there are no waiting games, let's just show one of the game in running state + } } else { gameA = games.get(0); } @@ -66,20 +75,20 @@ public void onServerListPing(@NotNull ServerListPingEvent slpe) { switch (game.getStatus()) { case DISABLED: - string = MainConfig.getInstance().node("bungee", "motd", "disabled").getString(); + string = mainConfig.node("bungee", "motd", "disabled").getString(); break; case GAME_END_CELEBRATING: case RUNNING: - string = MainConfig.getInstance().node("bungee", "motd", "running").getString(); + string = mainConfig.node("bungee", "motd", "running").getString(); break; case REBUILDING: - string = MainConfig.getInstance().node("bungee", "motd", "rebuilding").getString(); + string = mainConfig.node("bungee", "motd", "rebuilding").getString(); break; case WAITING: if (game.countPlayers() >= game.getMaxPlayers()) { - string = MainConfig.getInstance().node("bungee", "motd", "waiting_full").getString(); + string = mainConfig.node("bungee", "motd", "waiting_full").getString(); } else { - string = MainConfig.getInstance().node("bungee", "motd", "waiting").getString(); + string = mainConfig.node("bungee", "motd", "waiting").getString(); } break; } diff --git a/plugin/common/src/main/java/org/screamingsandals/bedwars/listener/PlayerListener.java b/plugin/common/src/main/java/org/screamingsandals/bedwars/listener/PlayerListener.java index f724dce44..cb0c01a8e 100644 --- a/plugin/common/src/main/java/org/screamingsandals/bedwars/listener/PlayerListener.java +++ b/plugin/common/src/main/java/org/screamingsandals/bedwars/listener/PlayerListener.java @@ -90,6 +90,8 @@ public class PlayerListener { public void onPlayerDeath(PlayerDeathEvent event) { final var victim = event.player(); + // TODO: check for cancellation state if possible (need adjustments in slib) + if (PlayerManagerImpl.getInstance().isPlayerInGame(victim)) { Debug.info(victim.getName() + " died in a BedWars game, processing his death..."); final var gVictim = victim.as(BedWarsPlayer.class); diff --git a/plugin/universal/build.gradle b/plugin/universal/build.gradle index ddbf50e97..fa16f0942 100644 --- a/plugin/universal/build.gradle +++ b/plugin/universal/build.gradle @@ -1,5 +1,5 @@ prepareTestTask() - .versions('1.21', '1.20.6', '1.20.4', '1.20.2', '1.20.1', '1.19.4', '1.19.3', '1.18.2', '1.17.1', '1.16.5', '1.15.2', '1.14.4', '1.13.2', '1.12.2', '1.11.2', '1.10.2', '1.9.4', '1.8.8') + .versions('1.21.3', '1.21.1', '1.20.6', '1.20.4', '1.20.2', '1.20.1', '1.19.4', '1.19.3', '1.18.2', '1.17.1', '1.16.5', '1.15.2', '1.14.4', '1.13.2', '1.12.2', '1.11.2', '1.10.2', '1.9.4', '1.8.8') .setSubdirectory('paper-master') .jvmArgs('-Dio.papermc.paper.suppress.sout.nags=true', '-DPaper.IgnoreJavaVersion=true') // suppress System.out.println nag .onlineMode(false)