Add sort command

This commit is contained in:
GeorgH93 2020-02-25 01:13:32 +01:00
parent fbf76b19dc
commit 88d79baad6
No known key found for this signature in database
GPG Key ID: D1630D37F9E4B3C8
7 changed files with 170 additions and 19 deletions

View File

@ -24,6 +24,8 @@ Language:
Cooldown: "&2Bitte warte noch {TimeLeft} Sekunden bis du deinen Rucksack wieder öffnest." Cooldown: "&2Bitte warte noch {TimeLeft} Sekunden bis du deinen Rucksack wieder öffnest."
#Parameter: {CurrentGameMode}, {AllowedGameModes} #Parameter: {CurrentGameMode}, {AllowedGameModes}
WrongGameMode: "Du darfst deinen Rucksack in deinem aktuellem Gamemode nicht öffnen." WrongGameMode: "Du darfst deinen Rucksack in deinem aktuellem Gamemode nicht öffnen."
Sort:
Sorted: "Dein Rucksack wurde sortiert."
Help: Help:
Header: "&6### Minepacks Commands ###" Header: "&6### Minepacks Commands ###"
Footer: "&6#############################" Footer: "&6#############################"
@ -62,6 +64,7 @@ Language:
PlayerNameVariable: "player_name" PlayerNameVariable: "player_name"
Description: Description:
Backpack: "Öffnet deinen Rucksack." Backpack: "Öffnet deinen Rucksack."
Sort: "Sortiert deinen Rucksack"
Clean: "Leert deinen Rucksack." Clean: "Leert deinen Rucksack."
CleanOthers: "Leert den Rucksack eines anderen Spielers." CleanOthers: "Leert den Rucksack eines anderen Spielers."
OpenOthers: "Öffnet den Rucksack eines anderen Spielers." OpenOthers: "Öffnet den Rucksack eines anderen Spielers."
@ -82,6 +85,9 @@ Command:
Open: Open:
- open - open
- öffnen - öffnen
Sort:
- sort
- sortieren
Clean: Clean:
- clean - clean
- clear - clear

View File

@ -25,6 +25,8 @@ Language:
BackpackCleaned: "Backpack cleared." BackpackCleaned: "Backpack cleared."
BackpackCleanedBy: "Your backpack has been cleared by {DisplayName}&r." BackpackCleanedBy: "Your backpack has been cleared by {DisplayName}&r."
BackpackCleanedOther: "{DisplayName}'s&r backpack has been cleared." BackpackCleanedOther: "{DisplayName}'s&r backpack has been cleared."
Sort:
Sorted: "Backpack sorted."
Help: Help:
Header: "&6### Minepacks Commands ###" Header: "&6### Minepacks Commands ###"
Footer: "&6#############################" Footer: "&6#############################"
@ -63,6 +65,7 @@ Language:
PlayerNameVariable: "player_name" PlayerNameVariable: "player_name"
Description: Description:
Backpack: "Opens your backpack." Backpack: "Opens your backpack."
Sort: "Sorts your backpack."
Clean: "Cleans your backpack." Clean: "Cleans your backpack."
CleanOthers: "Cleans the backpack of another players." CleanOthers: "Cleans the backpack of another players."
OpenOthers: "Shows the backpack of another player." OpenOthers: "Shows the backpack of another player."
@ -81,6 +84,8 @@ Command:
- bp - bp
Open: Open:
- open - open
Sort:
- sort
Clean: Clean:
- clean - clean
- clear - clear

View File

@ -120,7 +120,7 @@ public Backpack(final OfflinePlayer owner, ItemStack[] backpack, final int ID)
{ // Try to optimize space usage to compress items into only 6 rows { // Try to optimize space usage to compress items into only 6 rows
InventoryCompressor compressor = new InventoryCompressor(backpack, 54); InventoryCompressor compressor = new InventoryCompressor(backpack, 54);
final List<ItemStack> toMuch = compressor.compress(); final List<ItemStack> toMuch = compressor.compress();
backpack = compressor.getTargetStack(); backpack = compressor.getTargetStacks();
if(!toMuch.isEmpty()) if(!toMuch.isEmpty())
{ {
Minepacks.getInstance().getLogger().warning(owner.getName() + "'s backpack has to many items."); Minepacks.getInstance().getLogger().warning(owner.getName() + "'s backpack has to many items.");

View File

@ -18,6 +18,7 @@
package at.pcgamingfreaks.Minepacks.Bukkit.Command; package at.pcgamingfreaks.Minepacks.Bukkit.Command;
import at.pcgamingfreaks.Bukkit.Command.CommandExecutorWithSubCommandsGeneric; import at.pcgamingfreaks.Bukkit.Command.CommandExecutorWithSubCommandsGeneric;
import at.pcgamingfreaks.Bukkit.MCVersion;
import at.pcgamingfreaks.Bukkit.Message.Message; import at.pcgamingfreaks.Bukkit.Message.Message;
import at.pcgamingfreaks.Bukkit.RegisterablePluginCommand; import at.pcgamingfreaks.Bukkit.RegisterablePluginCommand;
import at.pcgamingfreaks.Command.HelpData; import at.pcgamingfreaks.Command.HelpData;
@ -72,6 +73,7 @@ public CommandManager(@NotNull Minepacks plugin)
// Init backpack commands // Init backpack commands
defaultSubCommand = new OpenCommand(plugin); defaultSubCommand = new OpenCommand(plugin);
registerSubCommand(defaultSubCommand); registerSubCommand(defaultSubCommand);
if(MCVersion.isNewerOrEqualThan(MCVersion.MC_1_8)) registerSubCommand(new SortCommand(plugin));
registerSubCommand(new ClearCommand(plugin)); registerSubCommand(new ClearCommand(plugin));
registerSubCommand(new ReloadCommand(plugin)); registerSubCommand(new ReloadCommand(plugin));
registerSubCommand(new UpdateCommand(plugin)); registerSubCommand(new UpdateCommand(plugin));

View File

@ -0,0 +1,73 @@
/*
* 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.Minepacks.Bukkit.API.Backpack;
import at.pcgamingfreaks.Minepacks.Bukkit.API.Callback;
import at.pcgamingfreaks.Minepacks.Bukkit.API.MinepacksCommand;
import at.pcgamingfreaks.Minepacks.Bukkit.Database.Helper.InventoryCompressor;
import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks;
import at.pcgamingfreaks.Minepacks.Bukkit.Permissions;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public class SortCommand extends MinepacksCommand
{
private final Message messageSorted;
public SortCommand(Minepacks plugin)
{
super(plugin, "sort", plugin.getLanguage().getTranslated("Commands.Description.Sort"), Permissions.SORT, true, plugin.getLanguage().getCommandAliases("Sort"));
messageSorted = plugin.getLanguage().getMessage("Ingame.Sort.Sorted");
}
@Override
public void execute(final @NotNull CommandSender commandSender, @NotNull String mainCommandAlias, @NotNull String alias, @NotNull String[] args)
{
final Player player = (Player) commandSender;
getMinepacksPlugin().getBackpack(player, new Callback<Backpack>()
{
@Override
public void onResult(Backpack backpack)
{
InventoryCompressor compressor = new InventoryCompressor(backpack.getInventory().getContents());
if(!compressor.sort().isEmpty())
{
plugin.getLogger().warning("Failed to sort backpack!"); //this should not happen
return;
}
backpack.getInventory().setContents(compressor.getTargetStacks());
messageSorted.send(player);
}
@Override
public void onFail() {} // This should not happen
});
}
@Override
public List<String> tabComplete(@NotNull CommandSender commandSender, @NotNull String mainCommandAlias, @NotNull String alias, @NotNull String[] args)
{
return null;
}
}

View File

@ -20,36 +20,105 @@
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import lombok.Getter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class InventoryCompressor public class InventoryCompressor
{ {
private ItemStack[] targetStack, inputStack; @Getter private ItemStack[] targetStacks, inputStacks;
private int filled = 0; private int filled = 0;
private ItemStack lastStack = null;
private List<ItemStack> toMuch;
public InventoryCompressor(ItemStack[] stack, int targetSize) public InventoryCompressor(ItemStack[] stacks)
{ {
inputStack = stack; this(stacks, stacks.length);
targetStack = new ItemStack[targetSize]; }
public InventoryCompressor(ItemStack[] stacks, int targetSize)
{
inputStacks = stacks;
targetStacks = new ItemStack[targetSize];
toMuch = new ArrayList<>(inputStacks.length - targetStacks.length);
}
public InventoryCompressor(ItemStack[] input, ItemStack[] output)
{
inputStacks = input;
targetStacks = output;
toMuch = new ArrayList<>(inputStacks.length - targetStacks.length);
}
public List<ItemStack> sort()
{
for(int i = 0; i < inputStacks.length; i++)
{
ItemStack stack = inputStacks[i];
if(stack == null || stack.getType() == Material.AIR || stack.getAmount() < 1) continue;
add(stack);
move(stack, i+1);
}
return toMuch;
}
private void move(ItemStack stack, int start)
{ // Search items that are the same
int differentMetaStart = -1;
for(int i = start; i < inputStacks.length; i++)
{
ItemStack stack2 = inputStacks[i];
if(stack2 == null || stack2.getType() == Material.AIR || stack2.getAmount() < 1) continue;
if(stack.isSimilar(stack2))
{
add(stack2); // Add item to sorted array
inputStacks[i] = null; // Remove item from input
}
else if(differentMetaStart == -1 && stack.getType() == stack2.getType())
{ // Same material but different meta
differentMetaStart = i;
}
}
if(differentMetaStart >= 0)
move(inputStacks[differentMetaStart], differentMetaStart);
}
private void add(ItemStack stack)
{
if(stack.isSimilar(lastStack) && lastStack.getAmount() < lastStack.getMaxStackSize())
{ // There is still space on the last stack, try to add it
int free = lastStack.getMaxStackSize() - lastStack.getAmount();
int place = Math.min(free, stack.getAmount());
lastStack.setAmount(lastStack.getAmount() + place);
stack.setAmount(stack.getAmount() - place);
}
if(stack.getAmount() < 1) return;
if(filled == targetStacks.length)
{ // The new item stack is full, add it to overfill list
toMuch.add(stack);
}
else
{ // Add the rest to the new inventory
targetStacks[filled++] = stack;
lastStack = stack;
}
} }
public List<ItemStack> compress() public List<ItemStack> compress()
{ {
List<ItemStack> toMuch = new ArrayList<>(inputStack.length - targetStack.length); for(ItemStack stack : inputStacks)
filled = 0;
for(ItemStack stack : inputStack)
{ {
if(stack == null || stack.getType() == Material.AIR) continue; if(stack == null || stack.getType() == Material.AIR || stack.getAmount() < 1) continue;
tryToStack(stack); tryToStack(stack);
if(stack.getAmount() == 0) continue; if(stack.getAmount() == 0) continue;
if(filled == targetStack.length) if(filled == targetStacks.length)
{ {
toMuch.add(stack); toMuch.add(stack);
} }
else else
{ {
targetStack[filled++] = stack; targetStacks[filled++] = stack;
} }
} }
return toMuch; return toMuch;
@ -60,18 +129,13 @@ private void tryToStack(ItemStack stack)
if(stack.getAmount() >= stack.getMaxStackSize()) return; if(stack.getAmount() >= stack.getMaxStackSize()) return;
for(int i = 0; i < filled && stack.getAmount() > 0; i++) for(int i = 0; i < filled && stack.getAmount() > 0; i++)
{ {
if(stack.isSimilar(targetStack[i]) && targetStack[i].getAmount() < targetStack[i].getMaxStackSize()) if(stack.isSimilar(targetStacks[i]) && targetStacks[i].getAmount() < targetStacks[i].getMaxStackSize())
{ // Same material and none full stack { // Same material and none full stack
int move = targetStack[i].getMaxStackSize() - targetStack[i].getAmount(); int move = targetStacks[i].getMaxStackSize() - targetStacks[i].getAmount();
move = Math.min(stack.getAmount(), move); move = Math.min(stack.getAmount(), move);
targetStack[i].setAmount(targetStack[i].getAmount() + move); targetStacks[i].setAmount(targetStacks[i].getAmount() + move);
stack.setAmount(stack.getAmount() - move); stack.setAmount(stack.getAmount() - move);
} }
} }
} }
public ItemStack[] getTargetStack()
{
return targetStack;
}
} }

View File

@ -21,6 +21,7 @@ public class Permissions
{ {
public static final String BASE = "backpack."; public static final String BASE = "backpack.";
public static final String USE = BASE + "use"; public static final String USE = BASE + "use";
public static final String SORT = BASE + "sort";
public static final String CLEAN = BASE + "clean"; public static final String CLEAN = BASE + "clean";
public static final String CLEAN_OTHER = BASE + "clean.other"; public static final String CLEAN_OTHER = BASE + "clean.other";
public static final String FULL_PICKUP = BASE + "fullpickup"; public static final String FULL_PICKUP = BASE + "fullpickup";