Skip to content

Commit

Permalink
server.generate_loot_table
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Oct 9, 2021
1 parent 7876724 commit bc8bdaf
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
Expand Up @@ -2368,6 +2368,7 @@ else if (foodLevel / maxHunger < 1) {
// @returns ListTag
// @description
// Returns a list of all bossbars from <@link command bossbar> that this player can see.
// Does not list bossbars created by any other source.
// -->
registerOnlineOnlyTag("bossbar_ids", (attribute, object) -> {
ListTag result = new ListTag();
Expand Down
Expand Up @@ -9,7 +9,7 @@
import com.denizenscript.denizen.utilities.ScoreboardHelper;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.VanillaTagHelper;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizen.utilities.depends.Depends;
import com.denizenscript.denizen.utilities.inventory.SlotHelper;
import com.denizenscript.denizencore.events.core.TickScriptEvent;
Expand Down Expand Up @@ -46,12 +46,15 @@
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.inventory.*;
import org.bukkit.loot.LootContext;
import org.bukkit.loot.LootTable;
import org.bukkit.map.MapCursor;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.RegisteredListener;
Expand Down Expand Up @@ -2226,6 +2229,67 @@ else if ((attribute.matches("plugins_handling_event") || attribute.matches("list
}
}
}

// <--[tag]
// @attribute <server.generate_loot_table[<map>]>
// @returns ListTag(ItemTag)
// @description
// Returns a list of items from a loot table, given a map of input data.
// Required input: id: the loot table ID, location: the location where it's being generated.
// Optional inputs: killer: a player (or player-type NPC) that is looting, entity: a dead entity being looted from,
// loot_bonus: the loot bonus level (defaults to -1), luck: the luck potion level (defaults to 0).
//
// Some inputs will be strictly required for some loot tables, and ignored for others.
//
// A list of valid loot tables can be found here: <@link url https://minecraft.fandom.com/wiki/Loot_table#List_of_loot_tables>
// CAUTION: Invalid loot table IDs will generate an empty list rather than an error.
//
// For example: <server.generate_loot_table[id=chests/spawn_bonus_chest;killer=<player>;location=<player.location>]>
// -->
else if (attribute.startsWith("generate_loot_table") && attribute.hasContext(1)) {
MapTag map = attribute.contextAsType(1, MapTag.class);
ObjectTag idObj = map.getObject("id");
ObjectTag locationObj = map.getObject("location");
if (idObj == null || locationObj == null) {
return;
}
NamespacedKey key = NamespacedKey.fromString(CoreUtilities.toLowerCase(idObj.toString()));
if (key == null) {
return;
}
LootTable table = Bukkit.getLootTable(key);
LootContext.Builder context = new LootContext.Builder(locationObj.asType(LocationTag.class, attribute.context));
ObjectTag killer = map.getObject("killer");
ObjectTag luck = map.getObject("luck");
ObjectTag bonus = map.getObject("loot_bonus");
if (killer != null) {
context = context.killer((HumanEntity) killer.asType(EntityTag.class, attribute.context).getLivingEntity());
}
if (luck != null) {
context = context.luck(luck.asType(ElementTag.class, attribute.context).asFloat());
}
if (bonus != null) {
context = context.lootingModifier(bonus.asType(ElementTag.class, attribute.context).asInt());
}
Collection<ItemStack> items;
try {
items = table.populateLoot(CoreUtilities.getRandom(), context.build());
}
catch (Throwable ex) {
attribute.echoError("Loot table failed to generate: " + ex.getMessage());
if (Debug.verbose) {
attribute.echoError(ex);
}
return;
}
ListTag result = new ListTag();
for (ItemStack item : items) {
if (item != null && item.getType() != Material.AIR) {
result.addObject(new ItemTag(item));
}
}
event.setReplacedObject(result.getObjectAttribute(attribute.fulfill(1)));
}
}

public static void listDeprecateWarn(Attribute attribute) {
Expand Down

0 comments on commit bc8bdaf

Please sign in to comment.