mirror of
https://github.com/ChestShop-authors/ChestShop-3.git
synced 2024-11-25 19:45:36 +01:00
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:
parent
e3ab44ae32
commit
852f20a50e
6
pom.xml
6
pom.xml
@ -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>
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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()));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
46
src/main/java/com/Acrobot/ChestShop/Plugins/ItemBridge.java
Normal file
46
src/main/java/com/Acrobot/ChestShop/Plugins/ItemBridge.java
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
|
@ -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:
|
||||
|
Loading…
Reference in New Issue
Block a user