Skip to content

Commit

Permalink
Refactor command handling in ManageMobs and simplify utility methods
Browse files Browse the repository at this point in the history
A major overhaul of the ManageMobs command handling has resulted in more flexible and concise code. New utility methods 'changeButton' and 'changeableValue' have been introduced, making command options manipulation simpler. Unnecessary import of ILocalizer has also been removed for a cleaner, non-redundant codebase.
  • Loading branch information
rainbowdashlabs committed Feb 25, 2024
1 parent 321299f commit c006c59
Show file tree
Hide file tree
Showing 11 changed files with 338 additions and 401 deletions.
3 changes: 3 additions & 0 deletions BloodNight-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ dependencies {
implementation("de.eldoria.util", "inventory", "2.0.4-SNAPSHOT")
implementation("de.eldoria.util", "conversation", "2.0.4-SNAPSHOT")
implementation("de.eldoria.util", "threading", "2.0.4-SNAPSHOT")
implementation("de.eldoria.util", "crossversion", "2.0.4-SNAPSHOT")
implementation("de.eldoria.util", "entities", "2.0.4-SNAPSHOT")
implementation("de.eldoria.util", "legacy-serialization", "2.0.4-SNAPSHOT")
implementation("net.kyori", "adventure-platform-bukkit", "4.3.1")
testImplementation("org.junit.jupiter", "junit-jupiter-api", "5.10.1")
testImplementation("junit", "junit", "4.13.2")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package de.eldoria.bloodnight.command;

import de.eldoria.bloodnight.command.bloodnight.About;
import de.eldoria.bloodnight.command.bloodnight.CancelNight;
import de.eldoria.bloodnight.command.bloodnight.ForceNight;
import de.eldoria.bloodnight.command.bloodnight.Help;
Expand All @@ -20,7 +19,6 @@
import de.eldoria.eldoutilities.commands.command.CommandMeta;
import de.eldoria.eldoutilities.commands.defaultcommands.DefaultAbout;
import de.eldoria.eldoutilities.commands.defaultcommands.DefaultDebug;
import de.eldoria.eldoutilities.simplecommands.commands.DefaultDebug;
import org.bukkit.plugin.Plugin;

public class BloodNightCommand extends AdvancedCommand {
Expand All @@ -35,15 +33,16 @@ public BloodNightCommand(Configuration configuration, Plugin plugin,
.withSubCommand(new DefaultAbout(plugin, "https://bn.discord.eldoria.de"))
.withSubCommand(new SpawnMob(plugin, nightManager, mobManager))
.withSubCommand(new CancelNight(plugin, nightManager, configuration))
.withSubCommand("forceNight", new ForceNight(plugin, nightManager, configuration))
.withSubCommand("manageWorlds", new ManageWorlds(plugin, configuration))
.withSubCommand("manageMob", new ManageMob(plugin, configuration, inventoryListener))
.withSubCommand("manageNight", new ManageNight(plugin, configuration))
.withSubCommand("manageMobs", new ManageMobs(plugin, configuration, inventoryListener))
.withSubCommand("nightSelection", new ManageNightSelection(plugin, configuration, inventoryListener))
.withSubCommand(new ForceNight(plugin, nightManager, configuration))
.withSubCommand(new ManageWorlds(plugin, configuration))
.withSubCommand(new ManageMob(plugin, configuration, inventoryListener))
.withSubCommand(new ManageNight(plugin, configuration))
.withSubCommand(new ManageMobs(plugin, configuration, inventoryListener))
.withSubCommand(new ManageNightSelection(plugin, configuration, inventoryListener))
.withSubCommand(new ManageDeathActions(plugin, configuration))
.withSubCommand(new Reload(plugin))
.withSubCommand(new DefaultDebug(plugin, Permissions.Admin.RELOAD))
.build()
);
}
}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected void reloadConfigs() {
BloodNight.logger().info("§2Migration of config to v2 done.");
}

generalSettings = getMainConfig().getConfig().getObject("generalSettings", GeneralSettings.class);
generalSettings = getMainConfig(plugin).getConfig().getObject("generalSettings", GeneralSettings.class);

for (World world : Bukkit.getWorlds()) {
loadWorldSettings(world.getName(), true);
Expand All @@ -82,8 +82,8 @@ private void initV2() {

private void migrateToV2() {
setVersion(2, false);
getMainConfig().getConfig().set("updateReminder", null);
List<WorldSettings> worldList = ObjUtil.nonNull((List<WorldSettings>) getMainConfig().getConfig().get("worldSettings", new ArrayList<>()), new ArrayList<>());
getMainConfig(plugin).getConfig().set("updateReminder", null);
List<WorldSettings> worldList = ObjUtil.nonNull((List<WorldSettings>) getMainConfig(plugin).getConfig().get("worldSettings", new ArrayList<>()), new ArrayList<>());

Path worldSettings = Paths.get(plugin.getDataFolder().toPath().toString(), "worldSettings");

Expand All @@ -94,7 +94,7 @@ private void migrateToV2() {
}, true);
BloodNight.logger().info("§2Migrated settings for " + settings.getWorldName());
}
getMainConfig().getConfig().set("worldSettings", null);
getMainConfig(plugin).getConfig().set("worldSettings", null);
}

private @NotNull
Expand All @@ -119,7 +119,7 @@ WorldSettings loadWorldSettings(String world, boolean reload) {

@Override
protected void saveConfigs() {
getMainConfig().getConfig().set("generalSettings", generalSettings);
getMainConfig(plugin).getConfig().set("generalSettings", generalSettings);

for (Map.Entry<String, WorldSettings> entry : worldSettings.entrySet()) {
ObjUtil.nonNull(loadConfig(getWorldConfigPath(entry.getKey()), null, false),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
import de.eldoria.bloodnight.events.BloodNightEndEvent;
import de.eldoria.bloodnight.hooks.HookService;
import de.eldoria.eldoutilities.localization.ILocalizer;
import de.eldoria.eldoutilities.localization.Replacement;
import de.eldoria.eldoutilities.messages.MessageSender;
import de.eldoria.eldoutilities.messages.Replacement;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Player;
Expand All @@ -20,6 +21,9 @@

import java.util.Collection;

import static java.time.Duration.ofSeconds;
import static net.kyori.adventure.title.Title.Times.times;

public class NotificationManager implements Listener {
private final ILocalizer localizer;
private final NightManager nightManager;
Expand All @@ -38,20 +42,20 @@ public NotificationManager(Configuration configuration, NightManager nightManage
@EventHandler(priority = EventPriority.MONITOR)
public void onBloodNightEnd(BloodNightEndEvent event) {
dispatchBroadcast(event.getWorld(),
localizer.getMessage("notify.nightEnd",
Replacement.create("WORLD", getAlias(event.getWorld())).addFormatting('6'))
"notify.nightEnd",
Replacement.create("WORLD", getAlias(event.getWorld()))
);
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBloodNightStart(BloodNightBeginEvent event) {
dispatchBroadcast(event.getWorld(),
localizer.getMessage("notify.nightStart",
Replacement.create("WORLD", getAlias(event.getWorld())).addFormatting('6'))
"notify.nightStart",
Replacement.create("WORLD", getAlias(event.getWorld()))
);
}

private void dispatchBroadcast(World world, String message) {
private void dispatchBroadcast(World world, String message, TagResolver... tagResolver) {
Collection<? extends Player> players;
switch (configuration.getGeneralSettings().getBroadcastLevel()) {
case SERVER:
Expand All @@ -68,16 +72,18 @@ private void dispatchBroadcast(World world, String message) {


for (Player player : players) {
sendBroadcast(player, message);
sendBroadcast(player, message, tagResolver);
}
}

private void sendBroadcast(Player player, String message) {
private void sendBroadcast(Player player, String message, TagResolver... tagResolver) {
String m = "§a" + message.replace("§r", "§r§a");
switch (configuration.getGeneralSettings().getBroadcastMethod()) {
case CHAT -> messageSender.sendMessage(player, message);
case TITLE -> player.sendTitle(m, "", 10, 70, 20);
case SUBTITLE -> player.sendTitle("", m, 10, 70, 20);
case CHAT -> messageSender.sendMessage(player, message, tagResolver);
case TITLE ->
messageSender.sendTitle(player, m, "", times(ofSeconds(1), ofSeconds(5), ofSeconds(1)), tagResolver);
case SUBTITLE ->
messageSender.sendTitle(player, "", m, times(ofSeconds(1), ofSeconds(5), ofSeconds(1)), tagResolver);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
import de.eldoria.bloodnight.hooks.mythicmobs.MythicMobUtil;
import de.eldoria.bloodnight.specialmobs.SpecialMob;
import de.eldoria.bloodnight.specialmobs.SpecialMobUtil;
import de.eldoria.eldoutilities.entityutils.ProjectileSender;
import de.eldoria.eldoutilities.entityutils.ProjectileUtil;
import de.eldoria.eldoutilities.entities.projectiles.ProjectileSender;
import de.eldoria.eldoutilities.entities.projectiles.ProjectileUtil;
import de.eldoria.eldoutilities.pdc.DataContainerUtil;
import de.eldoria.eldoutilities.scheduling.DelayedActions;
import de.eldoria.eldoutilities.threading.IteratingTask;
import de.eldoria.eldoutilities.utils.DataContainerUtil;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.World;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import de.eldoria.bloodnight.hooks.mythicmobs.MythicMobUtil;
import de.eldoria.bloodnight.specialmobs.SpecialMob;
import de.eldoria.bloodnight.specialmobs.SpecialMobUtil;
import de.eldoria.eldoutilities.entityutils.ProjectileSender;
import de.eldoria.eldoutilities.entityutils.ProjectileUtil;
import de.eldoria.eldoutilities.entities.projectiles.ProjectileSender;
import de.eldoria.eldoutilities.entities.projectiles.ProjectileUtil;
import de.eldoria.eldoutilities.threading.IteratingTask;
import lombok.NonNull;
import org.bukkit.World;
Expand All @@ -20,10 +20,23 @@
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.*;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.entity.EntityTargetEvent;
import org.bukkit.event.entity.EntityTeleportEvent;
import org.bukkit.event.entity.ExplosionPrimeEvent;
import org.bukkit.event.entity.ProjectileHitEvent;
import org.bukkit.event.entity.ProjectileLaunchEvent;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.*;
import java.util.ArrayDeque;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Queue;
import java.util.UUID;

public class SpecialMobManager extends BukkitRunnable implements Listener {
private final Map<String, WorldMobs> mobRegistry = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void onCommandPreprocessEvent(PlayerCommandPreprocessEvent event) {
if (!nightManager.isBloodNightActive(event.getPlayer().getWorld())) return;

if (isBlocked(event.getMessage()) && !event.getPlayer().hasPermission(Permissions.Bypass.COMMAND_BLOCK)) {
sender.sendLocalizedError(event.getPlayer(), "error.commandBlocked");
sender.sendError(event.getPlayer(), "error.commandBlocked");
event.setCancelled(true);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
import de.eldoria.bloodnight.events.BloodNightEndEvent;
import de.eldoria.bloodnight.specialmobs.SpecialMobUtil;
import de.eldoria.bloodnight.util.C;
import de.eldoria.eldoutilities.core.EldoUtilities;
import de.eldoria.eldoutilities.localization.ILocalizer;
import de.eldoria.eldoutilities.messages.MessageSender;
import de.eldoria.eldoutilities.scheduling.DelayedActions;
import de.eldoria.eldoutilities.utils.ObjUtil;
import org.bukkit.Bukkit;
import org.bukkit.GameRule;
Expand All @@ -30,14 +30,27 @@
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.*;
import org.bukkit.event.player.PlayerBedEnterEvent;
import org.bukkit.event.player.PlayerChangedWorldEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.*;
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;

public class NightManager extends BukkitRunnable implements Listener {
Expand All @@ -57,6 +70,7 @@ public class NightManager extends BukkitRunnable implements Listener {
private final PluginManager pluginManager = Bukkit.getPluginManager();
private final ILocalizer localizer;
private final MessageSender messageSender;
private final DelayedActions delayedActions;
private TimeManager timeManager;
private SoundManager soundManager;

Expand All @@ -67,6 +81,7 @@ public NightManager(Plugin plugin, Configuration configuration) {
this.configuration = configuration;
this.localizer = ILocalizer.getPluginLocalizer(BloodNight.class);
this.messageSender = MessageSender.getPluginMessageSender(BloodNight.class);
this.delayedActions = DelayedActions.start(plugin);
reload();
}

Expand Down Expand Up @@ -315,7 +330,7 @@ public void onPlayerDeath(PlayerDeathEvent event) {
event.setDroppedExp(0);
}

EldoUtilities.getDelayedActions().schedule(() -> {
delayedActions.schedule(() -> {
for (String deathCommand : actions.getDeathCommands()) {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), deathCommand.replace("{player}", event.getEntity().getName()));
}
Expand All @@ -333,7 +348,7 @@ public void onPlayerRespawn(PlayerRespawnEvent event) {
.getDeathActionSettings()
.getPlayerDeathActions();

EldoUtilities.getDelayedActions().schedule(() -> {
delayedActions.schedule(() -> {
for (PotionEffectSettings value : actions.getRespawnEffects().values()) {
player.addPotionEffect(new PotionEffect(value.getEffectType(), value.getDuration() * 20, 1, false));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import de.eldoria.bloodnight.core.BloodNight;
import de.eldoria.bloodnight.specialmobs.SpecialMobUtil;
import de.eldoria.bloodnight.specialmobs.mobs.ExtendedSpecialMob;
import de.eldoria.eldoutilities.container.Triple;
import de.eldoria.eldoutilities.crossversion.ServerVersion;
import de.eldoria.eldoutilities.utils.Version;
import org.bukkit.entity.Creeper;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Vex;
Expand All @@ -13,26 +13,14 @@
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.Optional;

public class GhostCreeper extends ExtendedSpecialMob<Vex, Creeper> {

private boolean legacy = false;

public GhostCreeper(Creeper creeper) {
super(EntityType.VEX, creeper);
Optional<Triple<Integer, Integer, Integer>> optVersion = ServerVersion.extractVersion();
Version optVersion = ServerVersion.getVersion();
// Entites can be invisible since 1.16.3. Hotfix for backwards compatibiliy to spigot 1.16.2
if (optVersion.isPresent()) {
Triple<Integer, Integer, Integer> version = optVersion.get();
if (version.second >= 16 && version.third > 2) {
getBaseEntity().setInvisible(true);
} else {
legacy = true;
}
} else {
legacy = true;
}
getBaseEntity().setInvulnerable(true);
new BukkitRunnable() {
@Override
Expand Down

0 comments on commit c006c59

Please sign in to comment.