first commit

This commit is contained in:
BuildTools 2018-08-12 15:33:54 +02:00
commit 15b494b4ba
10 changed files with 354 additions and 0 deletions

11
.classpath Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="lib" path="J:/BuildTools/Spigot/Spigot-API/target/spigot-api-1.13-R0.1-SNAPSHOT-shaded.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

17
.project Normal file
View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ChestSort</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8

21
config.yml Normal file
View File

@ -0,0 +1,21 @@
# when set to false, new players will have
# to run /chestsort once to enable automatic
# chest sorting.
sorting-enabled-by-default: false
# when set to true, players with sorting
# disabled will be shown a message on how
# to enable automatic chest sorting when
# they use a chest for the first time.
show-message-when-using-chest: true
# when set to true, the message is shown again
# when a player logs out and back in and then
# uses a chest again.
show-message-again-after-logout: true
# localization
message-when-using-chest: "&7Hint: Type &6/chestsort&7 to enable automatic chest sorting."
message-sorting-disabled: "&7Automatic chest sorting has been &cdisabled&7.&r"
message-sorting-enabled: "&7Automatic chest sorting has been &aenabled&7.&r"
message-error-players-only: "&cError: This command can only be run by players.&r"

13
plugin.yml Normal file
View File

@ -0,0 +1,13 @@
main: de.jeffclan.JeffChestSort.JeffChestSortPlugin
name: ChestSort
version: 1.3.1
api-version: 1.13
description: Allows automatic chest sorting
author: mfnalex
website: www.jeff-media.de
prefix: [ChestSort]
commands:
chestsort:
description: Toggle automatic chest sorting
usage: /<command>
aliases: sort

View File

@ -0,0 +1,50 @@
package de.jeffclan.JeffChestSort;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class JeffChestSortCommandExecutor implements CommandExecutor {
JeffChestSortPlugin plugin;
JeffChestSortCommandExecutor(JeffChestSortPlugin plugin)
{
this.plugin = plugin;
}
@Override
public boolean onCommand(CommandSender sender, Command arg1, String arg2, String[] arg3) {
if(!(sender instanceof Player))
{
return false;
}
if(arg1.getName().equalsIgnoreCase("chestsort"))
{
if ( !(sender instanceof Player)) {
sender.sendMessage(plugin.msg.MSG_PLAYERSONLY);
return true;
}
Player p = (Player) sender;
JeffChestSortPlayerSetting setting = plugin.PerPlayerSettings.get(p.getUniqueId().toString());
setting.sortingEnabled = !setting.sortingEnabled;
if(setting.sortingEnabled) {
p.sendMessage(plugin.msg.MSG_ACTIVATED);
} else {
p.sendMessage(plugin.msg.MSG_DEACTIVATED);
}
return true;
}
return false;
}
}

View File

@ -0,0 +1,157 @@
package de.jeffclan.JeffChestSort;
import java.util.Arrays;
import java.util.UUID;
import java.io.File;
import java.io.IOException;
import org.bukkit.block.Chest;
import org.bukkit.block.DoubleChest;
import org.bukkit.block.ShulkerBox;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
public class JeffChestSortListener implements Listener {
JeffChestSortPlugin plugin;
JeffChestSortListener(JeffChestSortPlugin plugin){
this.plugin = plugin;
}
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
UUID uniqueId=event.getPlayer().getUniqueId();
if(!plugin.PerPlayerSettings.containsKey(uniqueId.toString())) {
File playerFile = new File(plugin.getDataFolder() + File.separator + "playerdata" ,event.getPlayer().getUniqueId().toString() + ".yml");
YamlConfiguration playerConfig = YamlConfiguration.loadConfiguration(playerFile);
boolean activeForThisPlayer;
if(!playerFile.exists()) {
activeForThisPlayer = plugin.getConfig().getBoolean("sorting-enabled-by-default");
} else {
activeForThisPlayer = playerConfig.getBoolean("sortingEnabled");
}
JeffChestSortPlayerSetting newSettings = new JeffChestSortPlayerSetting(activeForThisPlayer);
if (!plugin.getConfig().getBoolean("show-message-again-after-logout")) {
newSettings.hasSeenMessage = playerConfig.getBoolean("hasSeenMessage");
}
plugin.PerPlayerSettings.put(uniqueId.toString(),newSettings);
}
}
@EventHandler
public void onPlayerQuit(PlayerQuitEvent event) {
UUID uniqueId=event.getPlayer().getUniqueId();
if(plugin.PerPlayerSettings.containsKey(uniqueId.toString())) {
JeffChestSortPlayerSetting setting = plugin.PerPlayerSettings.get(event.getPlayer().getUniqueId().toString());
File playerFile = new File(plugin.getDataFolder() + File.separator + "playerdata" ,event.getPlayer().getUniqueId().toString() + ".yml");
YamlConfiguration playerConfig = YamlConfiguration.loadConfiguration(playerFile);
playerConfig.set("sortingEnabled", setting.sortingEnabled);
playerConfig.set("hasSeenMessage", setting.hasSeenMessage);
try {
playerConfig.save(playerFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
plugin.PerPlayerSettings.remove(uniqueId.toString());
}
}
@EventHandler
public void onInventoryClose(InventoryCloseEvent event)
{
if(!(event.getPlayer() instanceof Player)) {
return;
}
Player p = (Player) event.getPlayer();
JeffChestSortPlayerSetting setting = plugin.PerPlayerSettings.get(p.getUniqueId().toString());
if(!(event.getInventory().getHolder() instanceof Chest)
&& !(event.getInventory().getHolder() instanceof DoubleChest)
&& !(event.getInventory().getHolder() instanceof ShulkerBox))
{
return;
}
if(!plugin.sortingEnabled(p)) {
if(!setting.hasSeenMessage) {
setting.hasSeenMessage = true;
if(plugin.getConfig().getBoolean("show-message-when-using-chest")) {
//p.sendMessage(JeffChestSortMessages.MSG_COMMANDMESSAGE);
p.sendMessage(plugin.msg.MSG_COMMANDMESSAGE);
}
}
return;
}
Inventory inv = event.getInventory();
ItemStack[] items = inv.getContents();
event.getInventory().clear();
String[] itemList = new String[inv.getSize()];
int i=0;
for(ItemStack item : items)
{
if(item!=null)
{
itemList[i] = item.getType().name() + "," + String.valueOf(item.hashCode());
i++;
}
}
// count all items that are not null
int count = 0;
for(String s: itemList)
{
if(s!=null)
{
count++;
}
}
// create new array with just the size we need
String[] shortenedArray = new String[count];
// fill new array with items
for(int j=0; j < count; j++)
{
shortenedArray[j] = itemList[j];
}
// sort array alphabetically
Arrays.sort(shortenedArray);
// put everything back in the inventory
for(String s : shortenedArray)
{
//System.out.println(s);
for(ItemStack item : items) {
if(item!=null && s != null) {
if(item.hashCode() == Integer.parseInt(s.split(",")[1])) {
inv.addItem(item);
item = null;
s = null;
}
}
}
}
}
}

View File

@ -0,0 +1,22 @@
package de.jeffclan.JeffChestSort;
import org.bukkit.ChatColor;
public class JeffChestSortMessages {
JeffChestSortPlugin plugin;
final String MSG_ACTIVATED, MSG_DEACTIVATED, MSG_COMMANDMESSAGE, MSG_PLAYERSONLY;
JeffChestSortMessages(JeffChestSortPlugin plugin) {
this.plugin=plugin;
MSG_ACTIVATED=ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("message-sorting-enabled"));
MSG_DEACTIVATED=ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("message-sorting-disabled"));
MSG_COMMANDMESSAGE=ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("message-when-using-chest"));
MSG_PLAYERSONLY=ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("message-error-players-only"));
}
}

View File

@ -0,0 +1,15 @@
package de.jeffclan.JeffChestSort;
public class JeffChestSortPlayerSetting {
// Sorting enabled for this player?
boolean sortingEnabled;
// Did we already show the message how to activate sorting?
boolean hasSeenMessage = false;
JeffChestSortPlayerSetting(boolean sortingEnabled) {
this.sortingEnabled = sortingEnabled;
}
}

View File

@ -0,0 +1,37 @@
package de.jeffclan.JeffChestSort;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
public class JeffChestSortPlugin extends JavaPlugin {
Map<String, JeffChestSortPlayerSetting> PerPlayerSettings = new HashMap<String, JeffChestSortPlayerSetting>();
FileConfiguration config = getConfig();
JeffChestSortMessages msg;
@Override
public void onEnable() {
createConfig();
msg = new JeffChestSortMessages(this);
getServer().getPluginManager().registerEvents(new JeffChestSortListener(this), this);
JeffChestSortCommandExecutor commandExecutor = new JeffChestSortCommandExecutor(this);
this.getCommand("chestsort").setExecutor(commandExecutor);
}
public boolean sortingEnabled(Player p) {
return PerPlayerSettings.get(p.getUniqueId().toString()).sortingEnabled;
}
void createConfig() {
this.saveDefaultConfig();
File playerDataFolder = new File(getDataFolder().getPath() + File.separator+"playerdata");
if(!playerDataFolder.exists())
playerDataFolder.mkdir();
}
}