Archived
0

Moved "util" package contents into correct "utils" package

This commit is contained in:
Pepich
2017-05-30 11:52:34 +02:00
parent 8c59af9ec3
commit c0fab2a72e
8 changed files with 61 additions and 44 deletions

View File

@@ -1,5 +1,16 @@
package com.redstoner.modules.blockplacemods;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import com.nemez.cmdmgr.Command;
import com.redstoner.annotations.AutoRegisterListener;
import com.redstoner.annotations.Version;
@@ -9,17 +20,11 @@ import com.redstoner.modules.Module;
import com.redstoner.modules.blockplacemods.mods.Mod;
import com.redstoner.modules.blockplacemods.mods.ModAbstract;
import com.redstoner.modules.blockplacemods.mods.ModToggledAbstract;
import com.redstoner.modules.blockplacemods.util.CommandException;
import com.redstoner.modules.blockplacemods.util.CommandMap;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import java.util.*;
import com.redstoner.utils.CommandException;
import com.redstoner.utils.CommandMap;
@AutoRegisterListener
@Version(major = 3, minor = 2, revision = 3, compatible = 3)
@Version(major = 3, minor = 2, revision = 4, compatible = 3)
public final class BlockPlaceMods implements Module, Listener
{
public static String PREFIX = ChatColor.GRAY + "[" + ChatColor.DARK_GREEN + "BPM" + ChatColor.GRAY + "]"
@@ -90,7 +95,6 @@ public final class BlockPlaceMods implements Module, Listener
* "}\n" +
* "}\n";
* } */
@Command(hook = "mod_empty")
public void onModEmptyCommand(CommandSender sender)
{
@@ -104,34 +108,46 @@ public final class BlockPlaceMods implements Module, Listener
.filter(x -> x != null && !x.trim().isEmpty()).toArray(String[]::new);
String prefix = PREFIX;
String message;
try {
if (args.length > 0) {
try
{
if (args.length > 0)
{
Mod target = ModAbstract.getMod(args[0].toLowerCase());
if (target != null) {
if (target != null)
{
prefix += "&7[&2" + capitalize(target.getName()) + "&7]&a";
if (!(sender instanceof Player)) {
if (!(sender instanceof Player))
{
message = "&cYou must be a player to use any block place mod";
} else {
}
else
{
message = target.runCommand((Player) sender, Arrays.copyOfRange(args, 1, args.length));
}
} else if (args[0].equalsIgnoreCase("help")) {
}
else if (args[0].equalsIgnoreCase("help"))
{
message = commandHelp(sender, args);
} else {
}
else
{
message = "&cThat argument could not be recognized";
}
} else {
}
else
{
message = commandHelp(sender, args);
}
} catch (CommandException ex) {
}
catch (CommandException ex)
{
message = " &c" + ex.getMessage();
} catch (Throwable t) {
}
catch (Throwable t)
{
message = " &cAn unexpected error occurred while executing this command.";
t.printStackTrace();
}
Utils.sendMessage(sender, prefix, message, '&');
}
@@ -139,36 +155,35 @@ public final class BlockPlaceMods implements Module, Listener
{
StringBuilder result = new StringBuilder("§7BlockPlaceMods adds some redstone-centric utilities");
result.append("\n").append(ChatColor.GRAY.toString()).append("Available mods:");
List<Mod> mods = new ArrayList<>(ModAbstract.getMods().values());
mods.sort(Comparator.<Mod>comparingInt(m -> ModToggledAbstract.class.isInstance(m) ? 1 : -1).thenComparing(Mod::getName));
mods.sort(Comparator.<Mod> comparingInt(m -> ModToggledAbstract.class.isInstance(m) ? 1 : -1)
.thenComparing(Mod::getName));
for (Mod mod : mods)
{
result.append("\n").append(ChatColor.AQUA.toString()).append("/mod ").append(ChatColor.ITALIC.toString())
.append(mod.getName());
for (String alias : mod.getAliases()) {
for (String alias : mod.getAliases())
{
result.append('|').append(alias);
}
result.append(ChatColor.GRAY.toString()).append(" - ")
.append(mod.getDescription());
result.append(ChatColor.GRAY.toString()).append(" - ").append(mod.getDescription());
}
return result.toString();
}
private static String capitalize(String modName) {
if (modName.isEmpty()) {
private static String capitalize(String modName)
{
if (modName.isEmpty())
{
return modName;
}
char first = modName.charAt(0);
if (first != (first = Character.toUpperCase(first))) {
if (first != (first = Character.toUpperCase(first)))
{
char[] result = modName.toCharArray();
result[0] = first;
return String.valueOf(result);
}
return modName;
}
}

View File

@@ -1,8 +1,9 @@
package com.redstoner.modules.blockplacemods.mods;
import com.redstoner.modules.blockplacemods.util.CommandException;
import org.bukkit.entity.Player;
import com.redstoner.utils.CommandException;
import java.util.Set;
public interface Mod

View File

@@ -1,8 +1,9 @@
package com.redstoner.modules.blockplacemods.mods;
import com.redstoner.modules.blockplacemods.util.CommandException;
import com.redstoner.modules.blockplacemods.util.ItemProperties;
import com.redstoner.modules.datamanager.DataManager;
import com.redstoner.utils.CommandException;
import com.redstoner.utils.ItemProperties;
import org.bukkit.Material;
import org.bukkit.block.BlockState;
import org.bukkit.entity.Player;

View File

@@ -2,8 +2,8 @@ package com.redstoner.modules.blockplacemods.mods;
import org.bukkit.entity.Player;
import com.redstoner.modules.blockplacemods.util.CommandException;
import com.redstoner.modules.datamanager.DataManager;
import com.redstoner.utils.CommandException;
public abstract class ModToggledAbstract extends ModAbstract
{

View File

@@ -1,23 +0,0 @@
package com.redstoner.modules.blockplacemods.util;
public class CommandException extends Exception {
public CommandException(String message, Throwable cause) {
super(message, cause);
}
public CommandException(Throwable cause) {
super(cause);
}
public CommandException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
public CommandException() {
}
public CommandException(String message) {
super(message);
}
}

View File

@@ -1,22 +0,0 @@
package com.redstoner.modules.blockplacemods.util;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.SimpleCommandMap;
import org.bukkit.plugin.SimplePluginManager;
import java.lang.reflect.Field;
import java.util.Map;
public class CommandMap {
public static Map<String, Command> getCommandMap() throws ReflectiveOperationException, ClassCastException {
Field field = SimplePluginManager.class.getDeclaredField("commandMap");
field.setAccessible(true);
Object map = field.get(Bukkit.getPluginManager());
field = SimpleCommandMap.class.getDeclaredField("knownCommands");
field.setAccessible(true);
return (Map<String, Command>) field.get(map);
}
}

View File

@@ -1,270 +0,0 @@
package com.redstoner.modules.blockplacemods.util;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.BiConsumer;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
/** Save and load {@link ItemStack} in json format
* Any additional NBT data not included by {@link ItemMeta} is discarded. */
public class ItemProperties
{
private int id = 0;
private byte data = 0;
private int amount = 1;
private Map<Enchantment, Integer> enchantments;
private List<String> lore;
private String displayName;
private boolean unbreakable = false;
public ItemProperties()
{}
public ItemProperties(ItemStack item)
{
if (item == null)
return;
id = item.getTypeId();
data = item.getData().getData();
amount = item.getAmount();
enchantments = new HashMap<>();
ItemMeta meta = item.getItemMeta();
if (meta == null)
return;
if (meta.hasEnchants())
{
enchantments.putAll(meta.getEnchants());
}
if (meta.hasLore())
{
lore = meta.getLore();
}
if (meta.hasDisplayName())
{
displayName = meta.getDisplayName();
}
unbreakable = meta.isUnbreakable();
}
public ItemStack toItemStack()
{
ItemStack result = new ItemStack(id, amount, data);
ItemMeta meta = result.getItemMeta();
if (meta == null)
return result;
if (enchantments != null)
{
enchantments.forEach(new BiConsumer<Enchantment, Integer>()
{
@Override
public void accept(Enchantment ench, Integer level)
{
meta.addEnchant(ench, level, true);
}
});
}
if (lore != null)
{
meta.setLore(lore);
}
if (displayName != null)
{
meta.setDisplayName(displayName);
}
meta.setUnbreakable(unbreakable);
result.setItemMeta(meta);
return result;
}
@SuppressWarnings("unchecked")
public JSONObject toJSONObject()
{
JSONObject object = new JSONObject();
object.put("id", id + "");
object.put("data", data + "");
object.put("amount", amount + "");
if (displayName != null)
{
object.put("displayName", displayName);
}
if (enchantments != null)
{
Map<Enchantment, Integer> enchantments = this.enchantments;
JSONObject stringKeys = new JSONObject();
for (Map.Entry<Enchantment, Integer> entry : enchantments.entrySet())
{
stringKeys.put(entry.getKey().getName(), entry.getValue());
}
object.put("enchantments", stringKeys);
}
if (lore != null)
{
object.put("lore", JSONArray.toJSONString(lore));
}
if (unbreakable)
{
object.put("unbreakable", true);
}
return object;
}
@Override
public String toString()
{
return toJSONObject().toString();
}
@SuppressWarnings("unchecked")
public ItemProperties loadFrom(JSONObject object)
{
for (Object obj : object.entrySet())
{
Entry<String, Object> entry = (Entry<String, Object>) obj;
final String key = entry.getKey();
switch (key)
{
case "id":
id = Integer.parseInt((String) entry.getValue());
break;
case "data":
data = Byte.parseByte((String) entry.getValue());
break;
case "amount":
amount = Integer.parseInt((String) entry.getValue());
break;
case "unbreakable":
unbreakable = (boolean) entry.getValue();
break;
case "enchantments":
{
if (enchantments == null)
{
enchantments = new HashMap<>();
}
else if (!enchantments.isEmpty())
{
enchantments.clear();
}
JSONObject read = (JSONObject) entry.getValue();
if (read != null)
{
for (Object obj2 : read.entrySet())
{
Entry<String, Integer> entry2 = (Entry<String, Integer>) obj2;
Enchantment ench = Enchantment.getByName(entry2.getKey());
if (ench != null)
{
enchantments.put(ench, entry2.getValue());
}
}
}
break;
}
case "lore":
JSONParser parser = new JSONParser();
Object rawObject;
try
{
rawObject = parser.parse((String) entry.getValue());
}
catch (ParseException e)
{
rawObject = new JSONArray();
}
JSONArray jsonArray = (JSONArray) rawObject;
lore = jsonArray;
break;
case "displayName":
displayName = (String) entry.getValue();
default:
}
}
return this;
}
public int getId()
{
return id;
}
public byte getData()
{
return data;
}
public int getAmount()
{
return amount;
}
public Map<Enchantment, Integer> getEnchantments()
{
return enchantments;
}
public List<String> getLore()
{
return lore;
}
public String getDisplayName()
{
return displayName;
}
public boolean isUnbreakable()
{
return unbreakable;
}
public ItemProperties setId(int id)
{
this.id = id;
return this;
}
public ItemProperties setData(byte data)
{
this.data = data;
return this;
}
public ItemProperties setAmount(int amount)
{
this.amount = amount;
return this;
}
public ItemProperties setEnchantments(Map<Enchantment, Integer> enchantments)
{
this.enchantments = enchantments;
return this;
}
public ItemProperties setLore(List<String> lore)
{
this.lore = lore;
return this;
}
public ItemProperties setDisplayName(String displayName)
{
this.displayName = displayName;
return this;
}
public ItemProperties setUnbreakable(boolean unbreakable)
{
this.unbreakable = unbreakable;
return this;
}
}

View File

@@ -1,12 +0,0 @@
package com.redstoner.modules.blockplacemods.util;
/**
* A supplier with a throws declaration.
* Once again, I have more solid alternatives, but if you want it in your utils... be my guest :D
*
* @param <T> The type of object computed by this supplier.
*/
@FunctionalInterface
public interface ThrowingSupplier<T> {
T get() throws Throwable;
}