Add inventory clear command

This commit is contained in:
GeorgH93 2020-02-22 17:27:16 +01:00
parent ba3331d16a
commit ea09d5fb06
No known key found for this signature in database
GPG Key ID: D1630D37F9E4B3C8
12 changed files with 332 additions and 31 deletions

View File

@ -0,0 +1,59 @@
/*
* Copyright (C) 2020 GeorgH93
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package at.pcgamingfreaks.Minepacks.Bukkit.API.Events;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
@RequiredArgsConstructor()
public class InventoryClearEvent extends Event implements Cancellable
{
@Getter @Setter private boolean cancelled = false;
/**
* The player who's inventory should be cleared.
*/
@Getter final private Player player;
/**
* The command sender who has requested the inventory to be cleared.
*/
@Getter final private CommandSender sender;
// Bukkit handler stuff
private static final HandlerList handlers = new HandlerList();
@Override
public @NotNull HandlerList getHandlers()
{
return handlers;
}
public static HandlerList getHandlerList()
{
return handlers;
}
}

View File

@ -0,0 +1,56 @@
/*
* Copyright (C) 2020 GeorgH93
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package at.pcgamingfreaks.Minepacks.Bukkit.API.Events;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor()
public class InventoryClearedEvent extends Event
{
/**
* The player who's inventory has been cleared.
*/
@Getter final private Player player;
/**
* The command sender who has requested the inventory to be cleared.
*/
@Getter final private CommandSender sender;
// Bukkit handler stuff
private static final HandlerList handlers = new HandlerList();
@Override
public @NotNull HandlerList getHandlers()
{
return handlers;
}
@SuppressWarnings("unused")
public static HandlerList getHandlerList()
{
return handlers;
}
}

View File

@ -43,13 +43,6 @@
<artifactId>BadRabbit-Bukkit</artifactId>
<version>1.4</version>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
@ -72,20 +65,6 @@
</resource>
</resources>
<plugins>
<!-- Resolve lombok -->
<plugin>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-maven-plugin</artifactId>
<version>1.18.10.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>delombok</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Bundle the API into the JAR -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>

View File

@ -179,6 +179,12 @@ Sound:
# auto will play the chest sound for minecraft versions older than 1.11 and the shulker-box sound for newer MC versions
CloseSound: auto
# Settings for the inventory management
InventoryManagement:
ClearCommand:
# If enabled the plugin provides an inventory clear command that keeps the backpack item.
Enabled: true
Misc:
# Enables/Disables the auto-update function of the plugin.
AutoUpdate: true
@ -186,4 +192,4 @@ Misc:
UseBungeeCord: false
# Config file version. Don't touch it!
Version: 24
Version: 25

View File

@ -50,6 +50,11 @@ Language:
# No Json!!!
DateFormat: "dd.MM.yyyy HH:mm:ss"
Restored: "Backup wurde erfolgreich wiederhergestellt."
InventoryClear:
UnknownPlayer: "&cSpieler {Name} konnte nicht gefunden werden!"
Cleared: "Inventar gelöscht."
ClearedOther: "Das Inventar von {DisplayName}&r wurde gelöscht."
ClearedOtherTarget: "Dein Inventar wurde von {DisplayName}&r gelöscht."
Commands:
HelpFormat: "[\"\",{\"text\":\"/{MainCommand} {SubCommand} {Parameters}\",\"clickEvent\":{\"action\":\"suggest_command\",\"value\":\"/{MainCommand} {SubCommand}\"}},{\"text\":\" - \",\"color\":\"white\"},{\"text\":\"{Description}\",\"color\":\"aqua\"}]"
PlayerNameVariable: "player_name"
@ -97,10 +102,14 @@ Command:
Help:
- help
- hilfe
InventoryClear:
- clear
- inventoryclear
- clean
# Will be shown in the console during startup
LanguageName: "german"
Author: "GeorgH93"
# Language file version. Don't touch it!
Version: 17
Version: 18

View File

@ -51,6 +51,11 @@ Language:
# No Json!!!
DateFormat: "yyyy.MM.dd HH:mm:ss"
Restored: "Backup has been successful restored."
InventoryClear:
UnknownPlayer: "&cCould not find player {Name}!"
Cleared: "Inventory cleared."
ClearedOther: "{DisplayName}'s&r inventory has been cleared."
ClearedOtherTarget: "Your inventory has been cleared by {DisplayName}&r."
Commands:
HelpFormat: "[\"\",{\"text\":\"/{MainCommand} {SubCommand} {Parameters}\",\"clickEvent\":{\"action\":\"suggest_command\",\"value\":\"/{MainCommand} {SubCommand}\"}},{\"text\":\" - \",\"color\":\"white\"},{\"text\":\"{Description}\",\"color\":\"aqua\"}]"
PlayerNameVariable: "player_name"
@ -93,10 +98,14 @@ Command:
- version
Help:
- help
InventoryClear:
- clear
- inventoryclear
- clean
# Will be shown in the console during startup
LanguageName: "english"
Author: "GeorgH93"
# Language file version. Don't touch it!
Version: 17
Version: 18

View File

@ -0,0 +1,137 @@
/*
* Copyright (C) 2020 GeorgH93
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package at.pcgamingfreaks.Minepacks.Bukkit.Command;
import at.pcgamingfreaks.Bukkit.Message.Message;
import at.pcgamingfreaks.Bukkit.RegisterablePluginCommand;
import at.pcgamingfreaks.Bukkit.Utils;
import at.pcgamingfreaks.Minepacks.Bukkit.API.Events.InventoryClearEvent;
import at.pcgamingfreaks.Minepacks.Bukkit.API.Events.InventoryClearedEvent;
import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks;
import at.pcgamingfreaks.Minepacks.Bukkit.Permissions;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
public class InventoryClearCommand implements CommandExecutor, TabCompleter
{
private final Minepacks plugin;
private final RegisterablePluginCommand command;
private final Message messageUnknownPlayer, messageOwnInventoryCleared, messageOtherInventoryCleared, messageInventoryWasCleared;
public InventoryClearCommand(final @NotNull Minepacks plugin)
{
this.plugin = plugin;
command = new RegisterablePluginCommand(plugin, "clearinventory", plugin.getLanguage().getCommandAliases("InventoryClear"));
command.registerCommand();
command.setExecutor(this);
command.setTabCompleter(this);
// Load messages
messageUnknownPlayer = plugin.getLanguage().getMessage("Ingame.InventoryClear.UnknownPlayer").replaceAll("\\{Name}", "%s");
messageOwnInventoryCleared = plugin.getLanguage().getMessage("Ingame.InventoryClear.Cleared");
messageOtherInventoryCleared = plugin.getLanguage().getMessage("Ingame.InventoryClear.ClearedOther").replaceAll("\\{Name}", "%1\\$s").replaceAll("\\{DisplayName}", "%2\\$s");
messageInventoryWasCleared = plugin.getLanguage().getMessage("Ingame.InventoryClear.ClearedOtherTarget").replaceAll("\\{Name}", "%1\\$s").replaceAll("\\{DisplayName}", "%2\\$s");
}
public void close()
{
command.unregisterCommand();
}
private void clearInventory(Player player, CommandSender sender)
{
InventoryClearEvent clearEvent = new InventoryClearEvent(player, sender);
Bukkit.getPluginManager().callEvent(clearEvent);
if(clearEvent.isCancelled()) return;
player.getInventory().clear();
if(sender.equals(player))
{
messageOwnInventoryCleared.send(player);
Bukkit.getPluginManager().callEvent(new InventoryClearedEvent(player, sender));
}
else
{
messageInventoryWasCleared.send(player, sender.getName(), (sender instanceof Player) ? ((Player) sender).getDisplayName() : ChatColor.GRAY + sender.getName());
messageOtherInventoryCleared.send(sender, player.getName(), player.getDisplayName());
Bukkit.getPluginManager().callEvent(new InventoryClearedEvent(player, sender));
}
}
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args)
{
if(sender.hasPermission(Permissions.INVENTORY_CLEAR))
{
if(args.length > 0)
{
if(sender.hasPermission(Permissions.INVENTORY_CLEAR_OTHER))
{
for(String name : args)
{
Player player = plugin.getServer().getPlayer(name);
if(player == null)
{
messageUnknownPlayer.send(sender, name);
}
else
{
clearInventory(player, sender);
}
}
}
else
{
plugin.messageNoPermission.send(sender);
}
}
else if(sender instanceof Player)
{
clearInventory((Player) sender, sender);
}
else
{
sender.sendMessage("/clear <player_name>"); //TODO
}
}
else
{
plugin.messageNoPermission.send(sender);
}
return true;
}
@Override
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args)
{
if(args.length > 0 && (!(sender instanceof Player) || sender.hasPermission(Permissions.INVENTORY_CLEAR_OTHER)))
{
return Utils.getPlayerNamesStartingWith(args[args.length - 1], sender);
}
return null;
}
}

View File

@ -37,7 +37,7 @@
public class Config extends Configuration implements DatabaseConnectionConfiguration
{
private static final int CONFIG_VERSION = 24, UPGRADE_THRESHOLD = CONFIG_VERSION, PRE_V2_VERSION = 20;
private static final int CONFIG_VERSION = 25, UPGRADE_THRESHOLD = CONFIG_VERSION, PRE_V2_VERSION = 20;
public Config(JavaPlugin plugin)
{
@ -398,5 +398,12 @@ public Sound getCloseSound()
return getSound("CloseSound", MCVersion.isNewerOrEqualThan(MCVersion.MC_1_11) ? "BLOCK_SHULKER_BOX_CLOSE" : (MCVersion.isNewerOrEqualThan(MCVersion.MC_1_9_2) ? "BLOCK_CHEST_CLOSE" : "CHEST_CLOSE"));
}
//endregion
//region InventoryManagement settings
public boolean isInventoryManagementClearCommandEnabled()
{
return getConfigE().getBoolean("InventoryManagement.ClearCommand.Enabled", true);
}
//endregion
//endregion
}

View File

@ -28,7 +28,7 @@
public class Language extends at.pcgamingfreaks.Bukkit.Language
{
private static final int LANG_VERSION = 17, UPGRADE_THRESHOLD = 17;
private static final int LANG_VERSION = 18, UPGRADE_THRESHOLD = LANG_VERSION;
public Language(JavaPlugin plugin)
{

View File

@ -21,6 +21,7 @@
import at.pcgamingfreaks.Bukkit.MCVersion;
import at.pcgamingfreaks.Bukkit.Message.Message;
import at.pcgamingfreaks.Minepacks.Bukkit.API.Backpack;
import at.pcgamingfreaks.Minepacks.Bukkit.API.Events.InventoryClearedEvent;
import at.pcgamingfreaks.Minepacks.Bukkit.Database.Helper.WorldBlacklistMode;
import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks;
import at.pcgamingfreaks.Minepacks.Bukkit.Permissions;
@ -106,6 +107,13 @@ public void onWorldChange(PlayerChangedWorldEvent event)
if(plugin.isDisabled(event.getPlayer()) != WorldBlacklistMode.None) return;
addItem(event.getPlayer());
}
@EventHandler
public void onInventoryClear(InventoryClearedEvent event)
{
if(plugin.isDisabled(event.getPlayer()) != WorldBlacklistMode.None) return;
addItem(event.getPlayer());
}
//endregion
//region Prevent placing of backpack item

View File

@ -27,6 +27,7 @@
import at.pcgamingfreaks.Minepacks.Bukkit.API.MinepacksCommandManager;
import at.pcgamingfreaks.Minepacks.Bukkit.API.MinepacksPlugin;
import at.pcgamingfreaks.Minepacks.Bukkit.Command.CommandManager;
import at.pcgamingfreaks.Minepacks.Bukkit.Command.InventoryClearCommand;
import at.pcgamingfreaks.Minepacks.Bukkit.Database.Config;
import at.pcgamingfreaks.Minepacks.Bukkit.Database.Database;
import at.pcgamingfreaks.Minepacks.Bukkit.Database.Helper.WorldBlacklistMode;
@ -72,6 +73,7 @@ public class Minepacks extends JavaPlugin implements MinepacksPlugin
private WorldBlacklistMode worldBlacklistMode;
private ItemsCollector collector;
private CommandManager commandManager;
private InventoryClearCommand inventoryClearCommand;
private Collection<GameMode> gameModes;
private CooldownManager cooldownManager = null;
private ItemFilter itemFilter = null;
@ -184,6 +186,7 @@ private void load()
messageNotANumber = lang.getMessage("Ingame.NaN");
commandManager = new CommandManager(this);
if(config.isInventoryManagementClearCommandEnabled()) inventoryClearCommand = new InventoryClearCommand(this);
//region register events
PluginManager pluginManager = getServer().getPluginManager();
@ -209,6 +212,11 @@ private void load()
private void unload()
{
if(inventoryClearCommand != null)
{
inventoryClearCommand.close();
inventoryClearCommand = null;
}
if(collector != null) collector.close();
commandManager.close();
if(collector != null) collector.cancel();

33
pom.xml
View File

@ -6,6 +6,12 @@
<version>${revision}</version>
<packaging>pom</packaging>
<properties>
<revision>2.3-ALPHA-SNAPSHOT</revision>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<scm>
<connection>scm:git:git@github.com:GeorgH93/Minepacks.git</connection>
<developerConnection>scm:git:git@github.com:GeorgH93/Minepacks.git</developerConnection>
@ -31,11 +37,6 @@
<distribution>repo</distribution>
</license>
</licenses>
<properties>
<revision>2.2</revision>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<repositories>
<repository>
@ -56,6 +57,13 @@
<version>1.15.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
@ -69,6 +77,21 @@
<target>1.8</target>
</configuration>
</plugin>
<!-- Resolve lombok -->
<plugin>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-maven-plugin</artifactId>
<version>1.18.10.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>delombok</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Flatten pom for deployment -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>