Skip to content

Instantly share code, notes, and snippets.

@Yuhtin
Last active July 26, 2021 18:08
Show Gist options
  • Save Yuhtin/d142fe4bac27c8ccfbbf25825c3a5397 to your computer and use it in GitHub Desktop.
Save Yuhtin/d142fe4bac27c8ccfbbf25825c3a5397 to your computer and use it in GitHub Desktop.
Support materials from 1.8 to 1.17 with data (colors)
public final class MaterialUtils {
public static ItemStack convertFromLegacy(String materialName, int damage) {
try {
return new ItemStack(Material.getMaterial(materialName), 1, (short) damage);
} catch (Exception error) {
Material material = Material.valueOf("LEGACY_" + materialName);
return new ItemStack(Bukkit.getUnsafe().fromLegacy(new MaterialData(material, (byte) damage)));
}
}
}
public final class UserDisconnectListener implements Listener {
@EventHandler
public void onJoin(PlayerJoinEvent event) {
PlayerInventory inventory = event.getPlayer().getInventory();
inventory.addItem(MaterialUtils.convertFromLegacy("WOOL", 14));
// add a red wool to player (1.8 & 1.17)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment