Add ItemBridge support (Resolves #361)

This also slightly changes how the max width is applied for generating sign item IDs
This commit is contained in:
Phoenix616 2022-12-29 15:25:26 +01:00
parent e3ab44ae32
commit 852f20a50e
No known key found for this signature in database
GPG Key ID: 40E2321E71738EB0
8 changed files with 88 additions and 3 deletions

View File

@ -244,6 +244,12 @@
</exclusions>
</dependency>
<dependency>
<groupId>com.github.jojodmo</groupId>
<artifactId>ItemBridge</artifactId>
<version>b0054538c1</version>
</dependency>
<dependency>
<groupId>br.net.fabiozumbi12.RedProtect</groupId>
<artifactId>RedProtect-Spigot</artifactId>

View File

@ -174,6 +174,9 @@ public class Dependencies implements Listener {
listener = heroes;
break;
case ItemBridge:
listener = new ItemBridge();
break;
case ShowItem:
MaterialUtil.Show.initialize(plugin);
break;
@ -202,6 +205,8 @@ public class Dependencies implements Listener {
Heroes,
ItemBridge,
ShowItem
}

View File

@ -9,13 +9,24 @@ public class ItemStringQueryEvent extends Event {
private String itemString = null;
private final ItemStack item;
private final int maxWidth;
/**
* Query the item string representation of a certain item with a certain length
* @param item The item to query the string for
*/
public ItemStringQueryEvent(ItemStack item) {
this(item, 0);
}
/**
* Query the item string representation of a certain item with a certain length
* @param item The item to query the string for
* @param maxWidth The max width of the item string
*/
public ItemStringQueryEvent(ItemStack item, int maxWidth) {
this.item = item;
this.maxWidth = maxWidth;
}
@Override
@ -50,4 +61,12 @@ public class ItemStringQueryEvent extends Event {
public void setItemString(String itemString) {
this.itemString = itemString;
}
/**
* Get the max width that the result item string should have
* @return The max width of the result item string
*/
public int getMaxWidth() {
return maxWidth;
}
}

View File

@ -11,7 +11,7 @@ public class ItemStringListener implements Listener {
@EventHandler(priority = EventPriority.NORMAL)
public static void calculateItemString(ItemStringQueryEvent event) {
if (event.getItemString() == null) {
event.setItemString(MaterialUtil.getName(event.getItem(), 0));
event.setItemString(MaterialUtil.getName(event.getItem(), event.getMaxWidth()));
}
}

View File

@ -17,6 +17,8 @@ import java.io.IOException;
import java.util.Locale;
import java.util.Map;
import static com.Acrobot.Breeze.Utils.StringUtil.getMinecraftStringWidth;
/**
* @author Acrobot
*/
@ -118,6 +120,13 @@ public class ItemAliasModule implements Listener {
}
if (newCode != null) {
if (event.getMaxWidth() > 0) {
int width = getMinecraftStringWidth(newCode);
if (width > event.getMaxWidth()) {
ChestShop.getBukkitLogger().warning("Can't use configured alias " + newCode + " as it's width (" + width + ") was wider than the allowed max width of " + event.getMaxWidth());
return;
}
}
event.setItemString(newCode);
}
}

View File

@ -0,0 +1,46 @@
package com.Acrobot.ChestShop.Plugins;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Events.ItemParseEvent;
import com.Acrobot.ChestShop.Events.ItemStringQueryEvent;
import com.jojodmo.itembridge.ItemBridgeKey;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import static com.Acrobot.Breeze.Utils.StringUtil.getMinecraftStringWidth;
/**
* @author Acrobot
*/
public class ItemBridge implements Listener {
@EventHandler(priority = EventPriority.LOW)
public void onItemParse(ItemParseEvent event) {
if (event.getItem() == null) {
ItemStack item = com.jojodmo.itembridge.ItemBridge.getItemStack(event.getItemString());
if (item != null) {
event.setItem(item);
}
}
}
@EventHandler(priority = EventPriority.LOW)
public void onItemStringQuery(ItemStringQueryEvent event) {
ItemBridgeKey key = com.jojodmo.itembridge.ItemBridge.getItemKey(event.getItem());
// If namespace is "minecraft" then we ignore it and use our own logic
if (key != null && !"minecraft".equalsIgnoreCase(key.getNamespace())) {
String code = key.toString();
// Make sure the ItemBridge string is not too long as we can't parse shortened ones
if (event.getMaxWidth() > 0) {
int width = getMinecraftStringWidth(code);
if (width > event.getMaxWidth()) {
ChestShop.logDebug("Can't use ItemBridge alias " + code + " as it's width (" + width + ") was wider than the allowed max width of " + event.getMaxWidth());
return;
}
}
event.setItemString(key.toString());
}
}
}

View File

@ -53,7 +53,7 @@ public class ItemUtil {
* @return ItemStack's name
*/
public static String getName(ItemStack itemStack, int maxWidth) {
String code = ChestShop.callEvent(new ItemStringQueryEvent(itemStack)).getItemString();
String code = ChestShop.callEvent(new ItemStringQueryEvent(itemStack, maxWidth)).getItemString();
if (code != null) {
if (maxWidth > 0) {
int codeWidth = getMinecraftStringWidth(code);

View File

@ -4,7 +4,7 @@ version: '${bukkit.plugin.version}'
author: Acrobot
authors: ['https://github.com/ChestShop-authors/ChestShop-3/contributors']
description: A chest shop for economy plugins.
softdepend: [Vault, Reserve, LWC, Lockette, LockettePro, Deadbolt, BlockLocker, OddItem, WorldGuard, GriefPrevention, RedProtect, Heroes, SimpleChestLock, Residence, ShowItem]
softdepend: [Vault, Reserve, LWC, Lockette, LockettePro, Deadbolt, BlockLocker, OddItem, WorldGuard, GriefPrevention, RedProtect, Heroes, SimpleChestLock, Residence, ShowItem, ItemBridge]
api-version: '1.13'
commands: