-
-
Save queer/30011fe3dace274763e9b2c93c26001e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package gg.amy.clockwork.virtual; | |
import gg.amy.clockwork.Clockwork; | |
import gg.amy.clockwork.virtual.VirtualItems.VirtualTemplate; | |
import lombok.Getter; | |
import org.apache.commons.lang.WordUtils; | |
import org.bukkit.ChatColor; | |
import org.bukkit.inventory.ItemStack; | |
import org.bukkit.inventory.meta.ItemMeta; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Map.Entry; | |
/** | |
* @author amy | |
* @since 6/21/18. | |
*/ | |
@Getter | |
@SuppressWarnings("unused") | |
public final class VirtualItem { | |
private final VirtualTemplate template; | |
private final Map<String, String> ctx; | |
private final int amount; | |
private VirtualItem(final VirtualTemplate template, final Map<String, String> ctx, final int amount) { | |
this.template = template; | |
this.ctx = ctx; | |
this.amount = amount; | |
} | |
public static VirtualItem fromItemStack(final ItemStack stack, final Map<String, String> inCtx) { | |
if(!stack.hasItemMeta()) { | |
throw new IllegalArgumentException("Stack has no meta"); | |
} | |
final ItemMeta meta = stack.getItemMeta(); | |
if(!meta.hasDisplayName()) { | |
throw new IllegalArgumentException("Stack has no displayName"); | |
} | |
final Clockwork plugin = Clockwork.getPlugin(Clockwork.class); | |
final String displayName = ChatColor.stripColor(meta.getDisplayName()); | |
if(!plugin.getVirtualItems().isVirtual(stack)) { | |
throw new IllegalArgumentException("Stack is not virtual"); | |
} | |
final String itemId = displayName.replaceFirst("virtual:", ""); | |
List<String> lore = meta.getLore(); | |
if(lore == null) { | |
lore = new ArrayList<>(); | |
} | |
// Build the ctx | |
final Map<String, String> ctx = new HashMap<>(); | |
lore.forEach(e -> { | |
final String[] split = e.split(":", 2); | |
if(split.length == 2) { | |
ctx.put(split[0], split[1]); | |
} | |
}); | |
ctx.putAll(inCtx); | |
final VirtualTemplate template = plugin.getVirtualItems().getVirtualTemplate(itemId); | |
return new VirtualItem(template, ctx, stack.getAmount()); | |
} | |
public ItemStack render() { | |
final ItemStack stack = new ItemStack(template.getMaterial()); | |
final ItemMeta itemMeta = stack.getItemMeta(); | |
itemMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', template.getColor() + template.getName())); | |
final List<String> lore = new ArrayList<>(); | |
template.getLore().forEach(e -> { | |
String tmp = e; | |
for(final Entry<String, String> stringStringEntry : ctx.entrySet()) { | |
tmp = tmp.replace('%' + stringStringEntry.getKey() + '%', stringStringEntry.getValue()); | |
} | |
lore.add(ChatColor.translateAlternateColorCodes('&', tmp)); | |
}); | |
lore.add(ChatColor.GRAY + "" + ChatColor.ITALIC + WordUtils.capitalizeFully(template.getRarity())); | |
itemMeta.setLore(lore); | |
stack.setItemMeta(itemMeta); | |
stack.setAmount(amount); | |
return stack; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment