From 705500bddd0dd5610c0e4644cfb125ebce7b30f3 Mon Sep 17 00:00:00 2001 From: ayunami2000 Date: Wed, 6 Apr 2022 21:48:51 -0400 Subject: [PATCH] autowipe + toggledrops yay! --- .../dev/plex/command/impl/ToggleDropsCMD.java | 47 +++++++++++++++++++ .../dev/plex/listener/impl/DropListener.java | 14 ++++++ .../dev/plex/services/ServiceManager.java | 2 + .../plex/services/impl/AutoWipeService.java | 40 ++++++++++++++++ src/main/resources/config.yml | 13 +++++ src/main/resources/messages.yml | 5 +- 6 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 src/main/java/dev/plex/command/impl/ToggleDropsCMD.java create mode 100644 src/main/java/dev/plex/listener/impl/DropListener.java create mode 100644 src/main/java/dev/plex/services/impl/AutoWipeService.java diff --git a/src/main/java/dev/plex/command/impl/ToggleDropsCMD.java b/src/main/java/dev/plex/command/impl/ToggleDropsCMD.java new file mode 100644 index 0000000..b0ac152 --- /dev/null +++ b/src/main/java/dev/plex/command/impl/ToggleDropsCMD.java @@ -0,0 +1,47 @@ +package dev.plex.command.impl; + +import dev.plex.command.PlexCommand; +import dev.plex.command.annotation.CommandParameters; +import dev.plex.command.annotation.CommandPermissions; +import dev.plex.command.source.RequiredCommandSource; +import dev.plex.rank.enums.Rank; +import dev.plex.services.ServiceManager; +import dev.plex.services.impl.AutoWipeService; +import dev.plex.util.PlexUtils; +import net.kyori.adventure.text.Component; +import org.bukkit.Bukkit; +import org.bukkit.World; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; + +@CommandPermissions(level = Rank.ADMIN, permission = "plex.toggledrops", source = RequiredCommandSource.ANY) +@CommandParameters(name = "toggledrops", description = "Toggle immediately removing drops.", usage = "/", aliases = "td") +public class ToggleDropsCMD extends PlexCommand +{ + @Override + protected Component execute(@NotNull CommandSender sender, @Nullable Player playerSender, @NotNull String[] args) + { + if (plugin.config.getBoolean("allowdrops")) + { + plugin.config.set("allowdrops", false); + plugin.config.save(); + sender.sendMessage(messageComponent("allowDropsDisabled")); + } + else + { + plugin.config.set("allowdrops", true); + plugin.config.save(); + sender.sendMessage(messageComponent("allowDropsEnabled")); + } + return null; + } +} \ No newline at end of file diff --git a/src/main/java/dev/plex/listener/impl/DropListener.java b/src/main/java/dev/plex/listener/impl/DropListener.java new file mode 100644 index 0000000..1aa2dfb --- /dev/null +++ b/src/main/java/dev/plex/listener/impl/DropListener.java @@ -0,0 +1,14 @@ +package dev.plex.listener.impl; + +import dev.plex.listener.PlexListener; +import org.bukkit.event.EventHandler; +import org.bukkit.event.player.PlayerDropItemEvent; + +public class DropListener extends PlexListener +{ + @EventHandler + public void onPlayerDropItem(PlayerDropItemEvent event) + { + if (!plugin.config.getBoolean("allowdrops")) event.setCancelled(true); + } +} diff --git a/src/main/java/dev/plex/services/ServiceManager.java b/src/main/java/dev/plex/services/ServiceManager.java index 7a43fb4..9f030c3 100644 --- a/src/main/java/dev/plex/services/ServiceManager.java +++ b/src/main/java/dev/plex/services/ServiceManager.java @@ -2,6 +2,7 @@ package dev.plex.services; import com.google.common.collect.Lists; import dev.plex.Plex; +import dev.plex.services.impl.AutoWipeService; import dev.plex.services.impl.BanService; import dev.plex.services.impl.GameRuleService; import dev.plex.services.impl.UpdateCheckerService; @@ -19,6 +20,7 @@ public class ServiceManager registerService(new BanService()); registerService(new GameRuleService()); registerService(new UpdateCheckerService()); + if (Plex.get().config.getBoolean("autowipe.enabled")) registerService(new AutoWipeService()); } public void startServices() diff --git a/src/main/java/dev/plex/services/impl/AutoWipeService.java b/src/main/java/dev/plex/services/impl/AutoWipeService.java new file mode 100644 index 0000000..4a6fe28 --- /dev/null +++ b/src/main/java/dev/plex/services/impl/AutoWipeService.java @@ -0,0 +1,40 @@ +package dev.plex.services.impl; + +import dev.plex.services.AbstractService; +import org.bukkit.Bukkit; +import org.bukkit.World; +import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; + +import java.util.List; + +public class AutoWipeService extends AbstractService +{ + public AutoWipeService() + { + super(true, false); + } + + @Override + public void run() + { + List entities = plugin.config.getStringList("autowipe.entities"); + + for (World world : Bukkit.getWorlds()) + { + for (Entity entity : world.getEntities()) + { + if (entities.stream().anyMatch(entityName -> entityName.equalsIgnoreCase(entity.getType().name()))) + { + entity.remove(); + } + } + } + } + + @Override + public int repeatInSeconds() + { + return Math.max(1, plugin.config.getInt("autowipe.interval")); + } +} \ No newline at end of file diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 2e4117a..8a0b567 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -141,6 +141,19 @@ entitywipe_list: - "ZOMBIFIED_PIGLIN" - "PUFFERFISH" +# Automatically wipe the specified entities +autowipe: + # Should we automatically wipe entities? + enabled: true + # How often, in seconds, to automatically wipe entities. Default is 5 minutes. + interval: 300 + # Entities to automatically wipe + entities: + - "DROPPED_ITEM" + +# Should we allow drops from players? +allowdrops: true + worlds: flatlands: name: "Flatlands" diff --git a/src/main/resources/messages.yml b/src/main/resources/messages.yml index a6f6b5d..7cbd1ef 100644 --- a/src/main/resources/messages.yml +++ b/src/main/resources/messages.yml @@ -159,4 +159,7 @@ invalidEntityType: "Notice: Entity type {0} is invalid!" noRemovedEntities: "No entities were removed." # 0 - The command sender # 1 - Number of mobs removed -removedMobs: "{0} - Removed {1} mobs" \ No newline at end of file +removedMobs: "{0} - Removed {1} mobs" +autoWipeDisabled: "Item wiping is currently disabled in the config!" +allowDropsDisabled: "No longer allowing drops from players." +allowDropsEnabled: "Now allowing drops from players." \ No newline at end of file