use of com.denizenscript.denizencore.objects.core.ScriptTag in project Denizen-For-Bukkit by DenizenScript.
the class InventoryScriptHelper method onPlayerClicks.
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onPlayerClicks(InventoryClickEvent event) {
InventoryTag inventory = InventoryTag.mirrorBukkitInventory(event.getInventory());
if (event.getRawSlot() >= event.getInventory().getSize() || event.getRawSlot() < 0) {
if (allowedClicks.contains(event.getClick())) {
return;
}
}
if (inventory.getIdHolder() instanceof ScriptTag) {
if (((InventoryScriptContainer) ((ScriptTag) inventory.getIdHolder()).getContainer()).gui) {
event.setCancelled(true);
Bukkit.getScheduler().scheduleSyncDelayedTask(Denizen.getInstance(), () -> {
((Player) event.getWhoClicked()).updateInventory();
}, 1);
}
}
}
use of com.denizenscript.denizencore.objects.core.ScriptTag in project Denizen-For-Bukkit by DenizenScript.
the class ItemScriptHelper method onItemSmithing.
@EventHandler(priority = EventPriority.LOW)
public void onItemSmithing(PrepareSmithingEvent event) {
if (!isItemscript(event.getInventory().getItem(0))) {
return;
}
Recipe recipe = event.getInventory().getRecipe();
SmithingRecipe smithRecipe = (SmithingRecipe) recipe;
if (smithRecipe == null || !(smithRecipe.getKey().getNamespace().equals("denizen"))) {
if (!isAllowedToCraftWith(event.getInventory().getItem(0))) {
event.setResult(new ItemStack(Material.AIR));
}
return;
}
ItemScriptContainer realResult = recipeIdToItemScript.get(smithRecipe.getKey().toString());
if (realResult == null) {
if (!isAllowedToCraftWith(event.getInventory().getItem(0))) {
event.setResult(new ItemStack(Material.AIR));
}
return;
}
String[] retain = smithingRetain.get(smithRecipe.getKey().getKey());
if (retain == null) {
Debug.echoError("Smithing recipe mis-registered for script item: " + realResult.getName());
if (!isAllowedToCraftWith(event.getInventory().getItem(0))) {
event.setResult(new ItemStack(Material.AIR));
}
return;
}
PlayerTag player = null;
if (!event.getInventory().getViewers().isEmpty()) {
HumanEntity human = event.getInventory().getViewers().get(0);
if (!EntityTag.isNPC(human) && human instanceof Player) {
player = new PlayerTag((Player) human);
}
}
ItemTag got = realResult.getItemFrom(new BukkitTagContext(player, null, new ScriptTag(realResult)));
if (got == null) {
return;
}
if (retain.length > 0) {
ItemMeta originalMeta = event.getInventory().getItem(0).getItemMeta();
ItemMeta newMeta = got.getItemMeta();
for (String retainable : retain) {
switch(retainable) {
case "display":
newMeta.setDisplayName(originalMeta.hasDisplayName() ? originalMeta.getDisplayName() : null);
break;
case "enchantments":
if (newMeta.hasEnchants()) {
for (Enchantment enchant : new ArrayList<>(newMeta.getEnchants().keySet())) {
newMeta.removeEnchant(enchant);
}
}
if (originalMeta.hasEnchants()) {
for (Map.Entry<Enchantment, Integer> enchant : originalMeta.getEnchants().entrySet()) {
newMeta.addEnchant(enchant.getKey(), enchant.getValue(), true);
}
}
break;
}
}
got.setItemMeta(newMeta);
}
event.setResult(got.getItemStack());
}
use of com.denizenscript.denizencore.objects.core.ScriptTag in project Denizen-For-Bukkit by DenizenScript.
the class ItemScriptHelper method registerShapelessRecipe.
public void registerShapelessRecipe(ItemScriptContainer container, ItemStack item, String shapelessString, String internalId, String group) {
TagContext context = new BukkitTagContext(null, null, new ScriptTag(container));
String list = TagManager.tag(shapelessString, context);
List<ItemStack[]> ingredients = new ArrayList<>();
List<Boolean> exacts = new ArrayList<>();
for (String element : ListTag.valueOf(list, context)) {
String itemText = element;
boolean isExact = !itemText.startsWith("material:");
if (!isExact) {
itemText = itemText.substring("material:".length());
}
exacts.add(isExact);
ItemStack[] items = textToItemArray(container, itemText, isExact);
if (items == null) {
return;
}
ingredients.add(items);
}
boolean[] bools = new boolean[exacts.size()];
for (int i = 0; i < exacts.size(); i++) {
bools[i] = exacts.get(i);
}
NMSHandler.getItemHelper().registerShapelessRecipe(internalId, group, item, ingredients, bools);
}
use of com.denizenscript.denizencore.objects.core.ScriptTag in project Denizen-For-Bukkit by DenizenScript.
the class Debug method echoError.
public static void echoError(ScriptEntry source, String addedContext, String message, boolean reformat) {
message = cleanTextForDebugOutput(message);
if (errorDuplicatePrevention) {
if (!com.denizenscript.denizencore.utilities.debugging.Debug.verbose) {
finalOutputDebugText("Error within error (??!!!! SOMETHING WENT SUPER WRONG!): " + message, source, reformat);
}
return;
}
errorDuplicatePrevention = true;
ScriptQueue sourceQueue = CommandExecutor.currentQueue;
if (source != null && source.queue != null) {
sourceQueue = source.queue;
}
ScriptTag sourceScript = null;
if (source != null) {
sourceScript = source.getScript();
}
if (throwErrorEvent) {
throwErrorEvent = false;
boolean cancel = ScriptGeneratesErrorScriptEvent.instance.handle(message, sourceQueue, sourceScript, source == null ? -1 : source.internal.lineNumber);
throwErrorEvent = true;
if (cancel) {
errorDuplicatePrevention = false;
return;
}
}
if (!showDebug) {
errorDuplicatePrevention = false;
return;
}
StringBuilder headerBuilder = new StringBuilder();
headerBuilder.append(ERROR_HEADER_START);
if (sourceScript != null) {
headerBuilder.append(" in script '").append(ChatColor.AQUA).append(sourceScript.getName()).append(ChatColor.RED).append("'");
}
if (sourceQueue != null) {
headerBuilder.append(" in queue '").append(sourceQueue.debugId).append(ChatColor.RED).append("'");
}
if (source != null) {
headerBuilder.append(" while executing command '").append(ChatColor.AQUA).append(source.getCommandName()).append(ChatColor.RED).append("'");
if (sourceScript != null) {
headerBuilder.append(" in file '").append(ChatColor.AQUA).append(sourceScript.getContainer().getRelativeFileName()).append(ChatColor.RED).append("' on line '").append(ChatColor.AQUA).append(source.internal.lineNumber).append(ChatColor.RED).append("'");
}
BukkitScriptEntryData data = Utilities.getEntryData(source);
if (data.hasPlayer()) {
headerBuilder.append(" with player '").append(ChatColor.AQUA).append(data.getPlayer().getName()).append(ChatColor.RED).append("'");
}
if (data.hasNPC()) {
headerBuilder.append(" with NPC '").append(ChatColor.AQUA).append(data.getNPC().debuggable()).append(ChatColor.RED).append("'");
}
}
if (addedContext != null) {
headerBuilder.append("\n<FORCE_ALIGN>").append(addedContext);
}
headerBuilder.append(ERROR_HEADER_END);
String header = headerBuilder.toString();
boolean showDebugSuffix = sourceScript != null && !sourceScript.getContainer().shouldDebug();
String headerRef = header;
if (header.equals(lastErrorHeader)) {
header = ADDITIONAL_ERROR_HEADER;
showDebugSuffix = false;
}
finalOutputDebugText(header + message + (showDebugSuffix ? ENABLE_DEBUG_MESSAGE : ""), sourceQueue, reformat);
errorDuplicatePrevention = false;
if (com.denizenscript.denizencore.utilities.debugging.Debug.verbose && depthCorrectError == 0) {
depthCorrectError++;
try {
throw new RuntimeException("Verbose info for above error");
} catch (Throwable e) {
echoError(source, e);
}
depthCorrectError--;
}
lastErrorHeader = headerRef;
}
use of com.denizenscript.denizencore.objects.core.ScriptTag in project Denizen-For-Bukkit by DenizenScript.
the class InventoryTag method internalGetInventoryFor.
public static InventoryTag internalGetInventoryFor(TagContext context, List<String> properties) {
String typeName = properties.get(0);
String holder = null;
int size = -1;
for (String property : properties) {
if (property.startsWith("holder=")) {
holder = ObjectFetcher.unescapeProperty(property.substring("holder=".length()));
} else if (property.startsWith("script_name=")) {
holder = ObjectFetcher.unescapeProperty(property.substring("script_name=".length()));
typeName = "script";
} else if (property.startsWith("uniquifier=")) {
String idText = property.substring("uniquifier=".length());
if (!ArgumentHelper.matchesInteger(idText)) {
return null;
}
long id = Long.parseLong(idText);
InventoryTag fixedResult = InventoryTrackerSystem.idTrackedInventories.get(id);
if (fixedResult != null) {
trackTemporaryInventory(fixedResult);
return fixedResult;
}
} else if (property.startsWith("size=")) {
String sizeText = property.substring("size=".length());
if (!ArgumentHelper.matchesInteger(sizeText)) {
return null;
}
size = Integer.parseInt(sizeText);
}
}
if (holder != null) {
switch(typeName) {
case "player":
case "enderchest":
case "workbench":
case "crafting":
PlayerTag player = PlayerTag.valueOf(holder, context);
if (player == null) {
if (context == null || context.showErrors()) {
Debug.echoError("Invalid inventory player '" + holder + "'");
}
return null;
}
switch(typeName) {
case "player":
return player.getInventory();
case "enderchest":
return player.getEnderChest();
case "workbench":
return player.getWorkbench();
case "crafting":
Inventory opened = player.getPlayerEntity().getOpenInventory().getTopInventory();
if (opened instanceof CraftingInventory) {
return new InventoryTag(opened, player.getPlayerEntity());
} else {
return player.getInventory();
}
}
break;
case "npc":
NPCTag npc = NPCTag.valueOf(holder, context);
if (npc == null) {
if (context == null || context.showErrors()) {
Debug.echoError("Invalid inventory npc '" + holder + "'");
}
return null;
}
return npc.getDenizenInventory();
case "entity":
EntityTag entity = EntityTag.valueOf(holder, context);
if (entity == null) {
if (context == null || context.showErrors()) {
Debug.echoError("Invalid inventory entity '" + holder + "'");
}
return null;
}
return entity.getInventory();
case "location":
LocationTag location = LocationTag.valueOf(holder, context);
if (location == null) {
if (context == null || context.showErrors()) {
Debug.echoError("Invalid inventory location '" + holder + "'");
}
return null;
}
return location.getInventory();
}
}
InventoryTag result = null;
if (typeName.equals("generic")) {
if (holder != null && !new ElementTag(holder).matchesEnum(InventoryType.class)) {
if (context == null || context.showErrors()) {
Debug.echoError("Unknown inventory type '" + holder + "'");
}
return null;
}
InventoryType type = holder == null ? InventoryType.CHEST : InventoryType.valueOf(holder.toUpperCase());
if (size == -1 || type != InventoryType.CHEST) {
result = new InventoryTag(type);
} else {
result = new InventoryTag(size);
}
} else if (typeName.equals("script") && holder != null) {
ScriptTag script = ScriptTag.valueOf(holder, context);
if (script == null || !(script.getContainer() instanceof InventoryScriptContainer)) {
if (context == null || context.showErrors()) {
Debug.echoError("Unknown inventory script '" + holder + "'");
}
return null;
}
result = ((InventoryScriptContainer) script.getContainer()).getInventoryFrom(context);
if (size != -1) {
result.setSize(size);
}
}
if (result == null && holder != null) {
ScriptTag script = ScriptTag.valueOf(holder, context);
if (script != null && (script.getContainer() instanceof InventoryScriptContainer)) {
result = ((InventoryScriptContainer) script.getContainer()).getInventoryFrom(context);
}
}
if (result == null && new ElementTag(typeName).matchesEnum(InventoryType.class)) {
InventoryType type = InventoryType.valueOf(typeName.toUpperCase());
if (size == -1 || type != InventoryType.CHEST) {
result = new InventoryTag(type);
} else {
result = new InventoryTag(size);
}
}
if (result == null) {
if (context == null || context.showErrors()) {
Debug.echoError("Unknown inventory idType '" + typeName + "'");
}
return null;
}
internalApplyPropertySet(result, context, properties);
return result;
}
Aggregations