Inventory Menu updates and Plugin Annotations

Added Plugin annotations for plugin.yml generation.
Fixed renaming bug overwriting current groups.
Fixed sort permissions
Added inventory menu reopen after inv close.
Inventory menu now shows the most common item.
This commit is contained in:
jameslfc19 2020-04-27 12:22:51 +01:00
parent ddeb1c0200
commit 47a14aed0f
11 changed files with 128 additions and 68 deletions

3
.gitignore vendored
View File

@ -2,4 +2,5 @@
Server/
Server_1_14_4/
target/
*.log
*.log
.idea/

View File

@ -57,6 +57,12 @@
<version>3.4</version>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>plugin-annotations</artifactId>
<version>1.2.2-SNAPSHOT</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>fr.minuskube.inv</groupId>-->
<!-- <artifactId>smart-invs</artifactId>-->

View File

@ -5,6 +5,7 @@ import com.jamesdpeters.minecraft.chests.listeners.ChestLinkListener;
import com.jamesdpeters.minecraft.chests.listeners.HopperListener;
import com.jamesdpeters.minecraft.chests.listeners.InventoryListener;
import com.jamesdpeters.minecraft.chests.misc.Config;
import com.jamesdpeters.minecraft.chests.misc.Permissions;
import com.jamesdpeters.minecraft.chests.misc.Settings;
import com.jamesdpeters.minecraft.chests.misc.Stats;
import com.jamesdpeters.minecraft.chests.serialize.InventoryStorage;
@ -15,9 +16,29 @@ import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.configuration.serialization.ConfigurationSerialization;
import org.bukkit.permissions.PermissionDefault;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.plugin.java.annotation.command.Command;
import org.bukkit.plugin.java.annotation.command.Commands;
import org.bukkit.plugin.java.annotation.permission.Permission;
import org.bukkit.plugin.java.annotation.plugin.ApiVersion;
import org.bukkit.plugin.java.annotation.plugin.Description;
import org.bukkit.plugin.java.annotation.plugin.Plugin;
import org.bukkit.plugin.java.annotation.plugin.author.Author;
@Plugin(name = "ChestsPlusPlus", version = "1.15 v1.3")
@ApiVersion(ApiVersion.Target.v1_13)
@Description(value = "Minecraft Spigot mod that enhances chests and hoppers, with ChestLinks and Hopper filters!")
@Author(value = "jameslfc19")
@Commands(@Command(name = "chestlink", desc = "Chests++ Commands.", aliases = {"cl"}, usage = "Use /chestlink help for more info."))
@Permission(name = Permissions.ADD, desc = "Gives permission to add ChestLinks!", defaultValue = PermissionDefault.TRUE)
@Permission(name = Permissions.OPEN, desc = "Gives permission to open ChestLinks!", defaultValue = PermissionDefault.TRUE)
@Permission(name = Permissions.MENU, desc = "Gives permission to open the ChestLink menu!", defaultValue = PermissionDefault.TRUE)
@Permission(name = Permissions.REMOVE, desc = "Gives permission to remove a ChestLink!", defaultValue = PermissionDefault.TRUE)
@Permission(name = Permissions.OPEN_ANY, desc = "Gives permission to open all chests, for admin use.", defaultValue = PermissionDefault.OP)
@Permission(name = Permissions.MEMBER, desc = "Gives permission to add/remove a member to/from their chestlink.", defaultValue = PermissionDefault.TRUE)
@Permission(name = Permissions.SORT, desc = "Set the sorting option for the given ChestLink.", defaultValue = PermissionDefault.TRUE)
public class ChestsPlusPlus extends JavaPlugin {
public static JavaPlugin PLUGIN;
@ -58,7 +79,7 @@ public class ChestsPlusPlus extends JavaPlugin {
switch (versionResponse) {
case FOUND_NEW:
getLogger().warning("New version of the plugin has been found: " + version);
getLogger().warning("Download at: https://www.spigotmc.org/resources/chests-chest-linking-hopper-filtering-remote-chests-menus.71355/");
getLogger().warning("Download at: "+SPIGOT_URL);
Bukkit.broadcastMessage(ChatColor.RED + "[Chests++] New version of the plugin was found: " + version);
break;
case LATEST:

View File

@ -142,11 +142,12 @@ public class RemoteChestCommand extends ServerCommand {
}
case SORT:
if(args.length > 1) {
if (sender.hasPermission(Permissions.REMOVE)) {
if (sender.hasPermission(Permissions.SORT)) {
InventoryStorage storage = Config.getInventoryStorage(player.getUniqueId(),args[1]);
if(storage != null) {
storage.setSortMethod(Enum.valueOf(SortMethod.class, args[2]));
storage.sort();
Messages.SORT(player,storage);
}
return true;
} else {
@ -219,7 +220,10 @@ public class RemoteChestCommand extends ServerCommand {
if(args.length > 2){
String group = args[1];
String newIdentifier = args[2];
Config.renameInventoryStorage(player,group,newIdentifier);
if(!Config.renameInventoryStorage(player,group,newIdentifier)){
player.sendMessage(ChatColor.RED + OPTIONS.RENAME.commandHelp);
player.sendMessage(ChatColor.RED + OPTIONS.RENAME.description);
}
return true;
}
}

View File

@ -8,6 +8,9 @@ public class VirtualInventoryHolder implements InventoryHolder {
InventoryStorage storage;
private Runnable openPreviousInventory;
private boolean isPrevInvRunning = false;
public VirtualInventoryHolder(InventoryStorage storage){
this.storage = storage;
}
@ -20,4 +23,26 @@ public class VirtualInventoryHolder implements InventoryHolder {
public InventoryStorage getStorage(){
return storage;
}
public void setPreviousInventory(Runnable runnable){
openPreviousInventory = runnable;
isPrevInvRunning = false;
}
/**
* @return true if previous inventory existed.
*/
public boolean openPreviousInventory(){
if(isPrevInvRunning){
isPrevInvRunning = false;
return false;
}
if(openPreviousInventory != null){
isPrevInvRunning = true;
openPreviousInventory.run();
openPreviousInventory = null;
return true;
}
return false;
}
}

View File

@ -13,6 +13,7 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.*;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.scheduler.BukkitScheduler;
public class InventoryListener implements Listener {
@ -47,11 +48,14 @@ public class InventoryListener implements Listener {
@EventHandler
public void onInventoryClose(InventoryCloseEvent event){
try {
if (event.getInventory().getHolder() instanceof VirtualInventoryHolder) {
Config.save();
InventoryHolder holder = event.getInventory().getHolder();
if (holder instanceof VirtualInventoryHolder) {
VirtualInventoryHolder vHolder = (VirtualInventoryHolder) holder;
vHolder.openPreviousInventory();
if (event.getInventory().getLocation() == null) {
Utils.closeInventorySound((Player) event.getPlayer(), event.getInventory());
}
Config.save();
}
} catch (NullPointerException ignore){} //Essentials does something weird with enderchests - shit fix but works :)
}

View File

@ -198,13 +198,22 @@ public class Config {
return null;
}
public static void renameInventoryStorage(Player player, String oldIdentifier, String newIdentifier){
public static boolean renameInventoryStorage(Player player, String oldIdentifier, String newIdentifier){
HashMap<String,InventoryStorage> map = getInventoryStorageMap(player.getUniqueId());
if(!map.containsKey(oldIdentifier)){
Messages.CANNOT_RENAME_GROUP_DOESNT_EXIST(player,oldIdentifier);
return false;
}
if(map.containsKey(newIdentifier)){
Messages.CANNOT_RENAME_ALREADY_EXISTS(player,newIdentifier);
return false;
}
InventoryStorage storage = map.get(oldIdentifier);
storage.rename(newIdentifier);
map.remove(oldIdentifier);
map.put(newIdentifier,storage);
save();
return true;
}
}

View File

@ -95,4 +95,16 @@ public class Messages {
target.sendMessage(ChatColor.RED+"Invalid ChestLink ID! Must not contain a colon ':' unless you are referencing another players group that you are a member off");
target.sendMessage(ChatColor.RED+"/chestlink add <owner>:<group>");
}
public static void SORT(Player target, InventoryStorage storage){
target.sendMessage(ChatColor.GREEN+"Sort method for "+ChatColor.WHITE+storage.getIdentifier()+ChatColor.GREEN+" has been set to "+ChatColor.WHITE+storage.getSortMethod().toString());
}
public static void CANNOT_RENAME_ALREADY_EXISTS(Player target, String newidentifier){
target.sendMessage(ChatColor.RED+"Error renaming chest! "+ChatColor.WHITE+newidentifier+ChatColor.RED+" already exists!");
}
public static void CANNOT_RENAME_GROUP_DOESNT_EXIST(Player target, String oldidentifier){
target.sendMessage(ChatColor.RED+"Error renaming chest! "+ChatColor.WHITE+oldidentifier+ChatColor.RED+" doesn't exist!");
}
}

View File

@ -1,15 +1,20 @@
package com.jamesdpeters.minecraft.chests.serialize;
import com.jamesdpeters.minecraft.chests.ChestsPlusPlus;
import com.jamesdpeters.minecraft.chests.interfaces.VirtualInventoryHolder;
import com.jamesdpeters.minecraft.chests.inventories.ChestLinkMenu;
import com.jamesdpeters.minecraft.chests.misc.Config;
import com.jamesdpeters.minecraft.chests.misc.Permissions;
import com.jamesdpeters.minecraft.chests.misc.Utils;
import com.jamesdpeters.minecraft.chests.interfaces.VirtualInventoryHolder;
import com.jamesdpeters.minecraft.chests.runnables.VirtualChestToHopper;
import com.jamesdpeters.minecraft.chests.sort.InventorySorter;
import com.jamesdpeters.minecraft.chests.sort.SortMethod;
import fr.minuskube.inv.ClickableItem;
import org.bukkit.*;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.Chest;
@ -17,11 +22,16 @@ import org.bukkit.block.Sign;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.material.Directional;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
public class InventoryStorage implements ConfigurationSerializable {
@ -140,13 +150,10 @@ public class InventoryStorage implements ConfigurationSerializable {
}
public ItemStack getIventoryIcon(Player player){
ItemStack toReturn = null;
for(ItemStack item : inventory.getContents()){
if(item != null){
toReturn = item.clone();
}
}
if(toReturn == null) toReturn = new ItemStack(Material.CHEST);
Material mostCommon = InventorySorter.getMostCommonItem(inventory);
ItemStack toReturn;
if(mostCommon == null) toReturn = new ItemStack(Material.CHEST);
else toReturn = new ItemStack(mostCommon);
ItemMeta meta = toReturn.getItemMeta();
if(meta != null) {
@ -169,6 +176,12 @@ public class InventoryStorage implements ConfigurationSerializable {
public ClickableItem getClickableItem(Player player) {
return ClickableItem.from(getIventoryIcon(player), event -> {
InventoryHolder inventoryHolder = inventory.getHolder();
if(inventoryHolder instanceof VirtualInventoryHolder){
((VirtualInventoryHolder) inventoryHolder).setPreviousInventory(() -> {
Bukkit.getScheduler().scheduleSyncDelayedTask(ChestsPlusPlus.PLUGIN, () -> ChestLinkMenu.getMenu(player).open(player), 1);
});
}
Utils.openInventory(player,getInventory());
});
}
@ -253,6 +266,10 @@ public class InventoryStorage implements ConfigurationSerializable {
this.sortMethod = sortMethod;
}
public SortMethod getSortMethod(){
return sortMethod;
}
public void sort(){
InventorySorter.sort(inventory, sortMethod);
}

View File

@ -2,12 +2,10 @@ package com.jamesdpeters.minecraft.chests.sort;
import org.bukkit.Material;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.*;
public class InventorySorter {
@ -89,4 +87,11 @@ public class InventorySorter {
});
return condensedItems;
}
public static Material getMostCommonItem(Inventory inventory){
return getItemAmounts(inventory.getContents()).entrySet().stream()
.max(Comparator.comparing(Map.Entry::getValue))
.map(Map.Entry::getKey)
.orElse(null);
}
}

View File

@ -1,44 +0,0 @@
name: ChestsPlusPlus
version: 1.15 v1.3
main: com.jamesdpeters.minecraft.chests.ChestsPlusPlus
api-version: "1.14"
commands:
chestlink:
description: Chest++ Commands.
usage: Use /chestlink help for more info.
aliases:
- cl
permissions:
chestlink.*:
description: Gives permission for all ChestLink commands.
default: op
children:
chestlink.add: true
chestlink.open: true
chestlink.menu: true
chestlink.remove : true
chestlink.openall: true
chestlink.addmember: true
chestlink.add:
description: Gives permission to add ChestLinks!
default: true
chestlink.open:
description: Gives permission to open ChestLinks!
default: true
chestlink.menu:
description: Gives permission to open the ChestLink menu!
default: true
chestlink.remove:
description: Gives permission to remove a ChestLink!
default: false
chestlink.openall:
description: Gives permission to open all chests, for admin use.
default: false
chestlink.member:
description: Gives permission to add/remove a member to/from their chestlink.
default: true
chestlink.sort:
description: Set the sorting option for the given ChestLink.
default: true