Skip to content

Commit

Permalink
Merge pull request #3 from minoneer/update-item-grouping
Browse files Browse the repository at this point in the history
Update item groupings to post 1.13 item format
  • Loading branch information
NewbieOrange authored Jan 15, 2021
2 parents 69378dd + c20719f commit cb4b61b
Showing 1 changed file with 14 additions and 30 deletions.
44 changes: 14 additions & 30 deletions src/me/desht/clicksort/ItemGrouping.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@
import org.bukkit.configuration.Configuration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.MaterialData;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class ItemGrouping
{
private static final String mapFile = "groups.yml";
private static final String MAP_FILE = "groups.yml";

private final ClickSortPlugin plugin;
private final Map<String, String> mapping;
Expand All @@ -26,12 +25,12 @@ public ItemGrouping(ClickSortPlugin plugin)
this.mapping = new HashMap<String, String>();
this.plugin = plugin;

new JARUtil(plugin).extractResource(mapFile, plugin.getDataFolder());
new JARUtil(plugin).extractResource(MAP_FILE, plugin.getDataFolder());
}

public void load()
{
File map = new File(plugin.getDataFolder(), mapFile);
File map = new File(plugin.getDataFolder(), MAP_FILE);
Configuration cfg = YamlConfiguration.loadConfiguration(map);

mapping.clear();
Expand All @@ -45,8 +44,7 @@ public void load()
}
catch (IllegalArgumentException e)
{
LogUtils.warning("Unknown material name '" + matName + "' in group '"
+ grpName + "'");
LogUtils.warning("Unknown material name '" + matName + "' in group '" + grpName + "'");
}
}
}
Expand All @@ -57,20 +55,20 @@ private void addMapping(String matName, String grpName)
addMapping(parseMaterial(matName), grpName);
}

private void addMapping(MaterialData mat, String grpName)
private void addMapping(Material material, String grpName)
{
if (mat == null)
if (material == null)
{
throw new IllegalArgumentException();
}
String key = getKey(mat);
mapping.put(getKey(mat), grpName);
String key = getKey(material);
mapping.put(key, grpName);
Debugger.getInstance().debug(2, "addMapping: " + key + " = " + grpName);
}

public String getGroup(ItemStack stack)
{
String group = mapping.get(getKey(stack.getData()));
String group = mapping.get(getKey(stack.getType()));
if (group == null)
{
group = plugin.getConfig().getString("default_group_name", "000-default");
Expand All @@ -84,27 +82,13 @@ public boolean isAvailable()
return !mapping.isEmpty();
}

private String getKey(MaterialData mat)
private String getKey(Material material)
{
// Items with durability should not use the current damage level as part
// of
// grouping criteria. Items which don't have durability *should* use the
// data
// value, e.g. 351:4 is lapis, which could be considered either a dye or
// a gem
return hasDurability(mat) ? mat.getItemType().toString() : mat.toString();
return material.toString();
}

private boolean hasDurability(MaterialData mat)
{
return mat.getItemType().getMaxDurability() > 0;
}

private MaterialData parseMaterial(String s)

private Material parseMaterial(String name)
{
String[] f = s.split(":");
Material mat = Material.matchMaterial(f[0]);
short dur = f.length == 2 ? Short.parseShort(f[1]) : 0;
return new ItemStack(mat, 1, dur).getData();
return Material.matchMaterial(name);
}
}

0 comments on commit cb4b61b

Please sign in to comment.