forked from LogBlock/LogBlock
Added MaterialName class from guardian
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
package de.diddiz.LogBlock;
|
||||
|
||||
import static de.diddiz.util.BukkitUtils.materialName;
|
||||
import static de.diddiz.util.MaterialName.materialName;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
@@ -20,8 +20,6 @@ import org.bukkit.Material;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.permissions.PermissionDefault;
|
||||
import de.diddiz.util.BukkitUtils;
|
||||
import de.diddiz.util.Utils;
|
||||
|
||||
public class Config
|
||||
{
|
||||
@@ -220,16 +218,6 @@ public class Config
|
||||
if (wcfg.logEndermen)
|
||||
logEndermen = true;
|
||||
}
|
||||
final File matFile = new File(logblock.getDataFolder(), "materials.yml");
|
||||
final YamlConfiguration matCfg = YamlConfiguration.loadConfiguration(matFile);
|
||||
matCfg.options().header("Add block names you want to be overridden or names for custom blocks");
|
||||
final Map<Integer, String> materials = new HashMap<Integer, String>();
|
||||
for (final String material : matCfg.getKeys(false))
|
||||
if (Utils.isInt(material))
|
||||
materials.put(Integer.valueOf(material), matCfg.getString(material));
|
||||
if (materials.size() > 0)
|
||||
BukkitUtils.overrideMaterialNames(materials);
|
||||
matCfg.save(matFile);
|
||||
}
|
||||
|
||||
public static List<String> toStringList(List<?> list) {
|
||||
|
@@ -2,8 +2,8 @@ package de.diddiz.LogBlock;
|
||||
|
||||
import static de.diddiz.util.BukkitUtils.friendlyWorldname;
|
||||
import static de.diddiz.util.BukkitUtils.getBlockEquivalents;
|
||||
import static de.diddiz.util.BukkitUtils.materialName;
|
||||
import static de.diddiz.util.BukkitUtils.senderName;
|
||||
import static de.diddiz.util.MaterialName.materialName;
|
||||
import static de.diddiz.util.Utils.isInt;
|
||||
import static de.diddiz.util.Utils.join;
|
||||
import static de.diddiz.util.Utils.listing;
|
||||
|
@@ -1,6 +1,6 @@
|
||||
package de.diddiz.LogBlock;
|
||||
|
||||
import static de.diddiz.util.BukkitUtils.materialName;
|
||||
import static de.diddiz.util.MaterialName.materialName;
|
||||
import static de.diddiz.util.Utils.spaces;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
@@ -1,8 +1,8 @@
|
||||
package de.diddiz.LogBlock;
|
||||
|
||||
import static de.diddiz.util.BukkitUtils.equalTypes;
|
||||
import static de.diddiz.util.BukkitUtils.materialName;
|
||||
import static de.diddiz.util.BukkitUtils.modifyContainer;
|
||||
import static de.diddiz.util.MaterialName.materialName;
|
||||
import static org.bukkit.Bukkit.getLogger;
|
||||
import java.io.File;
|
||||
import java.io.PrintWriter;
|
||||
|
@@ -1,17 +1,15 @@
|
||||
package de.diddiz.util;
|
||||
|
||||
import static de.diddiz.util.MaterialName.materialName;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Chunk;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.BlockState;
|
||||
import org.bukkit.block.ContainerBlock;
|
||||
@@ -22,12 +20,10 @@ import org.bukkit.entity.Player;
|
||||
import org.bukkit.entity.TNTPrimed;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.material.MaterialData;
|
||||
|
||||
public class BukkitUtils
|
||||
{
|
||||
private static final Set<Set<Integer>> blockEquivalents;
|
||||
private static final Map<Integer, String> materialNames;
|
||||
|
||||
static {
|
||||
blockEquivalents = new HashSet<Set<Integer>>(7);
|
||||
@@ -38,9 +34,6 @@ public class BukkitUtils
|
||||
blockEquivalents.add(new HashSet<Integer>(Arrays.asList(73, 74)));
|
||||
blockEquivalents.add(new HashSet<Integer>(Arrays.asList(75, 76)));
|
||||
blockEquivalents.add(new HashSet<Integer>(Arrays.asList(93, 94)));
|
||||
materialNames = new HashMap<Integer, String>(256);
|
||||
for (final Material mat : Material.values())
|
||||
materialNames.put(mat.getId(), mat.toString().replace('_', ' ').toLowerCase());
|
||||
}
|
||||
|
||||
public static ItemStack[] compareInventories(ItemStack[] items1, ItemStack[] items2) {
|
||||
@@ -127,23 +120,6 @@ public class BukkitUtils
|
||||
return entity.getClass().getSimpleName().substring(5);
|
||||
}
|
||||
|
||||
public static String materialName(int type) {
|
||||
return materialNames.containsKey(type) ? materialNames.get(type) : String.valueOf(type);
|
||||
}
|
||||
|
||||
public static String materialName(int type, byte rawData) {
|
||||
if ((type == 6 || type == 17 || type == 18) && rawData > 0 || type == 35 || type == 43 || type == 44 || type == 351) {
|
||||
final MaterialData data = Material.getMaterial(type).getNewData(rawData);
|
||||
if (data != null)
|
||||
return data.toString().toLowerCase().replace('_', ' ').replaceAll("[^a-z ]", "");
|
||||
}
|
||||
return materialName(type);
|
||||
}
|
||||
|
||||
public static void overrideMaterialNames(Map<Integer, String> materialNames) {
|
||||
BukkitUtils.materialNames.putAll(materialNames);
|
||||
}
|
||||
|
||||
public static String senderName(CommandSender sender) {
|
||||
if (sender instanceof Player)
|
||||
return ((Player)sender).getName();
|
||||
|
101
src/de/diddiz/util/MaterialName.java
Normal file
101
src/de/diddiz/util/MaterialName.java
Normal file
@@ -0,0 +1,101 @@
|
||||
package de.diddiz.util;
|
||||
|
||||
import static de.diddiz.util.Utils.isByte;
|
||||
import static de.diddiz.util.Utils.isInt;
|
||||
import static org.bukkit.Bukkit.getLogger;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Level;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.material.MaterialData;
|
||||
|
||||
/**
|
||||
* @author DiddiZ
|
||||
*/
|
||||
public class MaterialName
|
||||
{
|
||||
private static final Map<Integer, String> materialNames = new HashMap<Integer, String>();
|
||||
private static final Map<Integer, Map<Byte, String>> materialDataNames = new HashMap<Integer, Map<Byte, String>>();
|
||||
|
||||
static {
|
||||
// Add all known materials
|
||||
for (final Material mat : Material.values())
|
||||
materialNames.put(mat.getId(), mat.toString().replace('_', ' ').toLowerCase());
|
||||
// Load config
|
||||
final File file = new File("plugins/LogBlock/materials.yml");
|
||||
final YamlConfiguration cfg = YamlConfiguration.loadConfiguration(file);
|
||||
if (cfg.getKeys(false).isEmpty()) {
|
||||
// Generate defaults
|
||||
cfg.options().header("Add block or item names you want to be overridden or also names for custom blocks");
|
||||
cfg.set("6.1", "redwood sapling");
|
||||
cfg.set("6.2", "birch sapling");
|
||||
cfg.set("9", "water");
|
||||
cfg.set("11", "lava");
|
||||
cfg.set("17.1", "redwood log");
|
||||
cfg.set("17.2", "birch log");
|
||||
cfg.set("18.1", "redwood leaves");
|
||||
cfg.set("18.2", "birch leaves");
|
||||
cfg.set("31.0", "dead long grass");
|
||||
cfg.set("31.2", "fern");
|
||||
for (byte i = 0; i < 7; i++) {
|
||||
cfg.set("35." + i, toReadable(Material.STEP.getNewData(i)));
|
||||
cfg.set("351." + i, toReadable(Material.DOUBLE_STEP.getNewData(i)));
|
||||
}
|
||||
for (byte i = 0; i < 16; i++) {
|
||||
cfg.set("35." + i, toReadable(Material.WOOL.getNewData(i)));
|
||||
cfg.set("351." + i, toReadable(Material.INK_SACK.getNewData(i)));
|
||||
}
|
||||
try {
|
||||
cfg.save(file);
|
||||
} catch (final IOException ex) {
|
||||
getLogger().log(Level.WARNING, "Unable to save material.yml: ", ex);
|
||||
}
|
||||
}
|
||||
for (final String entry : cfg.getKeys(false))
|
||||
if (isInt(entry)) {
|
||||
if (cfg.isString(entry))
|
||||
materialNames.put(Integer.valueOf(entry), cfg.getString(entry));
|
||||
else if (cfg.isConfigurationSection(entry)) {
|
||||
final Map<Byte, String> dataNames = new HashMap<Byte, String>();
|
||||
materialDataNames.put(Integer.valueOf(entry), dataNames);
|
||||
final ConfigurationSection sec = cfg.getConfigurationSection(entry);
|
||||
for (final String data : sec.getKeys(false))
|
||||
if (isByte(data)) {
|
||||
if (sec.isString(data))
|
||||
dataNames.put(Byte.valueOf(data), sec.getString(data));
|
||||
else
|
||||
getLogger().warning("Parsing materials.yml: '" + data + "' is not a string.");
|
||||
} else
|
||||
getLogger().warning("Parsing materials.yml: '" + data + "' is no valid material data");
|
||||
} else
|
||||
getLogger().warning("Parsing materials.yml: '" + entry + "' is neither a string nor a section.");
|
||||
} else
|
||||
getLogger().warning("Parsing materials.yml: '" + entry + "' is no valid material id");
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Name of the material, or if it's unknown, the id.
|
||||
*/
|
||||
public static String materialName(int type) {
|
||||
return materialNames.containsKey(type) ? materialNames.get(type) : String.valueOf(type);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Name of the material regarding it's data, or if it's unknown, the basic name.
|
||||
*/
|
||||
public static String materialName(int type, byte data) {
|
||||
final Map<Byte, String> dataNames = materialDataNames.get(type);
|
||||
if (dataNames != null)
|
||||
if (dataNames.containsKey(data))
|
||||
return dataNames.get(data);
|
||||
return materialName(type);
|
||||
}
|
||||
|
||||
private static String toReadable(MaterialData matData) {
|
||||
return matData.toString().toLowerCase().replace('_', ' ').replaceAll("[^a-z ]", "");
|
||||
}
|
||||
}
|
@@ -62,6 +62,14 @@ public class Utils
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean isByte(String str) {
|
||||
try {
|
||||
Byte.parseByte(str);
|
||||
return true;
|
||||
} catch (final NumberFormatException ex) {}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static String listing(String[] entries, String delimiter, String finalDelimiter) {
|
||||
final int len = entries.length;
|
||||
if (len == 0)
|
||||
|
Reference in New Issue
Block a user