Skip to content

Instantly share code, notes, and snippets.

@Phoenix616
Last active December 4, 2023 18:00
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Phoenix616/950d0cb5866b25532b2fdc0864613af6 to your computer and use it in GitHub Desktop.
Save Phoenix616/950d0cb5866b25532b2fdc0864613af6 to your computer and use it in GitHub Desktop.
IconRpMapping - Display Minecraft item stacks in chat with WolfieMario's Text Icon Resource pack. For more info see README.md.
package de.themoep.utils;
/*
* IconRpMapping utils for mapping of Bukkit materials to WolfieMario's
* Custom Text Icons Resourcepack (https://s.moep.tv/iconrp)
* Copyright (C) 2018 Max Lee aka Phoenix616 (mail@moep.tv)
*
* 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/.
*/
import org.bukkit.ChatColor;
import org.bukkit.Color;
import org.bukkit.DyeColor;
import org.bukkit.FireworkEffect;
import org.bukkit.Material;
import org.bukkit.block.Banner;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BannerMeta;
import org.bukkit.inventory.meta.BlockStateMeta;
import org.bukkit.inventory.meta.FireworkEffectMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.material.SpawnEgg;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
public class IconRpMapping {
private static final String UNUSED = "unused";
private Map<String, Integer> encodingNames = new HashMap<>();
private final String[] potionids = {
"unused",
"regen",
"speed",
"fire_resistance",
"poison",
"instant_heal",
"night_vision",
"unused",
"weakness",
"strength",
"slowness",
"jump",
"instant_damage",
"water_breathing",
"invisibility"
};
private int offset;
private boolean legacy = true;
public IconRpMapping(JavaPlugin plugin) {
plugin.getLogger().info("Loading Text Icon Resourcepack mapping...");
String fileName = "iconrpmapping.yml";
try {
String apiVersion = plugin.getDescription().getAPIVersion();
plugin.getLogger().info("Found API-Version " + apiVersion + ", using new Material IDs...");
fileName = "iconrpmapping-flattening.yml";
legacy = false;
} catch (NoSuchMethodError e) {
plugin.getLogger().info("Using pre 1.13 API and legacy Material IDs...");
}
ConfigAccessor iconConfig = new ConfigAccessor(plugin, fileName);
iconConfig.saveDefaultConfig();
iconConfig.reloadConfig();
offset = iconConfig.getConfig().getInt("offset");
// Get default list for updating
List<String> defaultList = iconConfig.getDefaults().getStringList("map");
// Get currently stored list
List<String> matList = iconConfig.getConfig().getStringList("map");
// Was the list changed and should be stored?
boolean changed = false;
for(int i = 0; i < matList.size(); i++) {
String name = matList.get(i).toLowerCase(Locale.ROOT);
String defaultName = defaultList.get(i).toLowerCase(Locale.ROOT);
if (!defaultName.equals(UNUSED) && (name.isEmpty() || UNUSED.equals(name))) {
matList.set(i, defaultName);
name = defaultName;
changed = true;
}
encodingNames.put(name, i);
}
// Check if changed and we should save
if (changed) {
plugin.getLogger().info("Default config has had new additions. Adding it to the " + fileName + " file!");
iconConfig.getConfig().set("map", matList);
iconConfig.saveConfig();
}
plugin.getLogger().info("Text Icon Resourcepack mapping loaded.");
}
/**
* Get the unicode character that corresponds with the inputted item
* @param item The item to get the character of
* @return A string of the unicode character which represents the item in the icon rp. Empty string if none was found.
*/
public String getIcon(ItemStack item) {
return getIcon(item, false);
}
/**
* Get the unicode character that corresponds with the inputted item
* @param item The item to get the character of
* @param escape Make the icon use the white chat color?
* @return A string of the unicode character which represents the item in the icon rp. Empty string if none was found.
*/
public String getIcon(ItemStack item, boolean escape) {
ItemMeta meta = item.getItemMeta();
String iconstr = null;
try {
if (meta != null && meta.hasCustomModelData()) {
iconstr = getIcon(item.getType().toString() + ":custommodel:" + meta.getCustomModelData(), escape);
}
} catch (NoSuchMethodError ignored) {}
if (iconstr == null) {
if (meta instanceof LeatherArmorMeta) {
iconstr = ColorUtils.getNearestChatColor(((LeatherArmorMeta) meta).getColor()) + getIcon("white_" + item.getType().toString(), false) + ChatColor.RESET;
} else if (meta instanceof PotionMeta) {
PotionMeta pm = (PotionMeta) meta;
try {
iconstr = getIcon(item.getType().toString() + "_" + pm.getBasePotionData().getType().toString(), escape);
} catch (NoSuchMethodError e) {
if (pm.hasCustomEffects()) {
iconstr = getIcon(item.getType().toString() + "_" + pm.getCustomEffects().get(0).getType().toString(), escape);
}
}
if (iconstr == null) {
iconstr = getIcon(item.getType().toString() + "_water", escape);
}
} else if (item.getType() == Material.POTION) {
String key = "water_potion";
if (item.getDurability() == 0) {
iconstr = getIcon(key, escape);
} else {
String splash = "";
boolean[] bits = BitUtils.getBits(item.getDurability());
int id = BitUtils.getRange(item.getDurability(), 0, 3);
if (id < potionids.length)
key = potionids[id].toLowerCase();
if (bits[1])
splash = "splash_";
iconstr = getIcon(key + "_" + splash + "potion", true);
if (iconstr == null)
iconstr = getIcon(splash + key + "_potion", true);
}
} else if (meta instanceof FireworkEffectMeta) {
FireworkEffect fe = ((FireworkEffectMeta) meta).getEffect();
if (fe != null && !fe.getColors().isEmpty()) {
Color median = fe.getColors().get(0).mixColors((Color[]) fe.getColors().stream().skip(1).toArray());
iconstr = getIcon(item.getType().toString() + "_" + ColorUtils.getNearestChatColor(median).name(), escape);
}
} else if ((legacy || item.getType() == Material.SHIELD) && meta instanceof BannerMeta) {
BannerMeta bm = (BannerMeta) meta;
iconstr = getIcon(item.getType().toString() + "_" + bm.getBaseColor(), escape);
if (iconstr == null) {
iconstr = getIcon(item.getType() + "_" + DyeColor.getByDyeData(item.getData().getData()), escape);
}
} else if (legacy && meta instanceof BlockStateMeta && ((BlockStateMeta) meta).getBlockState() instanceof Banner) {
Banner banner = (Banner) ((BlockStateMeta) meta).getBlockState();
iconstr = getIcon(item.getType().toString() + "_" + banner.getBaseColor(), escape);
} else if (legacy && item.getData() instanceof SpawnEgg) {
SpawnEgg spawnEgg = (SpawnEgg) item.getData();
if (spawnEgg.getSpawnedType() != null) {
iconstr = getIcon(item.getType().toString() + "_" + spawnEgg.getSpawnedType(), escape);
if (iconstr == null) {
iconstr = getIcon(item.getType().toString() + ":" + spawnEgg.getSpawnedType().getTypeId(), escape);
}
}
if (iconstr == null) {
iconstr = getIcon(item.getType().toString() + ":" + spawnEgg.getData(), escape);
}
}
}
boolean isUnbreakable = false;
if (iconstr == null && meta != null) {
try {
isUnbreakable = meta.isUnbreakable();
} catch (NoSuchMethodError e) {
try {
isUnbreakable = meta.spigot().isUnbreakable();
} catch (NoSuchMethodError ignored) {}
}
if (isUnbreakable) {
iconstr = getIcon(item.getType().toString() + ":unbreakable:" + item.getDurability(), escape);
}
}
if (iconstr == null) {
iconstr = getIcon(item.getType().toString() + ":" + item.getDurability(), escape);
}
if (iconstr == null && isUnbreakable) {
iconstr = getIcon(item.getType().toString() + ":unbreakable", escape);
}
if (iconstr == null) {
iconstr = getIcon(item.getType().toString(), escape);
}
return iconstr != null ? iconstr : "";
}
/**
* Get the unicode character of the inputed icon
* @param iconName The name of the icon
* @param escape Make the icon use the white chat color?
* @return A string of the unicode character which represents the icon in the icon rp. <code>null</code> if none was found.
*/
public String getIcon(String iconName, boolean escape) {
Integer iconOffset = encodingNames.get(iconName.toLowerCase(Locale.ROOT));
if (iconOffset != null) {
String symbol = Character.toString((char) (offset + iconOffset));
if (escape)
symbol = ChatColor.WHITE + symbol + ChatColor.RESET;
return symbol;
} else
return null;
}
public boolean isLegacy() {
return legacy;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment