use of net.minecraft.loot.LootTableManager in project minecolonies by Minecolonies.
the class CustomRecipeManager method buildLootData.
/**
* Analyses and builds an approximate list of possible loot drops from registered recipes.
* @param lootTableManager the loot table manager
*/
public void buildLootData(@NotNull final LootTableManager lootTableManager) {
final List<ResourceLocation> lootIds = new ArrayList<>();
for (final Map<ResourceLocation, CustomRecipe> recipes : recipeMap.values()) {
for (final CustomRecipe recipe : recipes.values()) {
final ResourceLocation lootTable = recipe.getLootTable();
if (lootTable != null) {
lootIds.add(lootTable);
}
}
}
for (final BuildingEntry building : IMinecoloniesAPI.getInstance().getBuildingRegistry()) {
building.getModuleProducers().stream().map(Supplier::get).filter(m -> m instanceof AnimalHerdingModule).map(m -> (AnimalHerdingModule) m).forEach(herding -> lootIds.add(herding.getDefaultLootTable()));
}
lootIds.add(ModLootTables.FISHING);
lootIds.addAll(ModLootTables.FISHERMAN_BONUS.values());
lootTables.clear();
lootTables.putAll(lootIds.stream().filter(// just in case
Objects::nonNull).distinct().collect(Collectors.toConcurrentMap(Function.identity(), id -> LootTableAnalyzer.toDrops(lootTableManager, id))));
}
use of net.minecraft.loot.LootTableManager in project Mekanism by mekanism.
the class LootTableModifierReloadListener method onResourceManagerReload.
@Override
public void onResourceManagerReload(@Nonnull IResourceManager resourceManager) {
// Grab the current shears tag instance as we will be doing identity compares and even if we weren't
// the named wrapper for the tag won't be populated yet
ITag<Item> shearsTag = dataPackRegistries.getTags().getItems().getTag(new ResourceLocation("forge", "shears"));
LootTableManager lootTableManager = dataPackRegistries.getLootTables();
for (ResourceLocation id : lootTableManager.getIds()) {
LootTable lootTable = lootTableManager.get(id);
boolean modified = false;
// Scan all the loot tables in the upcoming/"current" loot table manager and evaluate if their pools need modifying
for (LootPool pool : LOOT_TABLE_POOLS.getValue(lootTable)) {
// 2. As the conditions to the loot pool itself
for (LootEntry entry : LOOT_POOL_ENTRIES.getValue(pool)) {
modified |= modifyEntry(entry, shearsTag);
}
for (ILootCondition condition : LOOT_POOL_CONDITIONS.getValue(pool)) {
modified |= modifyCondition(condition, shearsTag);
}
}
if (modified) {
// If we changed any of the conditions in this loot table, add a debug message stating that we did so
Mekanism.logger.debug("Modified and wrapped condition(s) in loot table '{}' to support items with the tool type of shears.", id);
}
}
// We also need to scan all loot predicate in the upcoming/"current" manager, given even though vanilla doesn't add
// any predicates by default that have to do with shears, we want to make sure we catch any that may be defined by
// a mod or data pack
LootPredicateManager predicateManager = dataPackRegistries.getPredicateManager();
for (Map.Entry<ResourceLocation, ILootCondition> entry : LOOT_PREDICATE_CONDITIONS.getValue(predicateManager).entrySet()) {
if (modifyCondition(entry.getValue(), shearsTag)) {
// If we changed any of the conditions in this loot predicate, add a debug message stating that we did so
Mekanism.logger.debug("Modified and wrapped condition for loot predicate '{}' to support items with the tool type of shears.", entry.getKey());
}
}
}
use of net.minecraft.loot.LootTableManager in project minecolonies by ldtteam.
the class CustomRecipeManager method buildLootData.
/**
* Analyses and builds an approximate list of possible loot drops from registered recipes.
* @param lootTableManager the loot table manager
*/
public void buildLootData(@NotNull final LootTableManager lootTableManager) {
final List<ResourceLocation> lootIds = new ArrayList<>();
for (final Map<ResourceLocation, CustomRecipe> recipes : recipeMap.values()) {
for (final CustomRecipe recipe : recipes.values()) {
final ResourceLocation lootTable = recipe.getLootTable();
if (lootTable != null) {
lootIds.add(lootTable);
}
}
}
for (final BuildingEntry building : IMinecoloniesAPI.getInstance().getBuildingRegistry()) {
building.getModuleProducers().stream().map(Supplier::get).filter(m -> m instanceof AnimalHerdingModule).map(m -> (AnimalHerdingModule) m).forEach(herding -> lootIds.add(herding.getDefaultLootTable()));
}
lootIds.add(ModLootTables.FISHING);
lootIds.addAll(ModLootTables.FISHERMAN_BONUS.values());
lootTables.clear();
lootTables.putAll(lootIds.stream().filter(// just in case
Objects::nonNull).distinct().collect(Collectors.toConcurrentMap(Function.identity(), id -> LootTableAnalyzer.toDrops(lootTableManager, id))));
}
use of net.minecraft.loot.LootTableManager in project minecolonies by Minecolonies.
the class LootTableAnalyzer method toDrops.
public static List<LootDrop> toDrops(@Nullable final LootTableManager lootTableManager, @NotNull final JsonObject lootTableJson) {
final List<LootDrop> drops = new ArrayList<>();
if (!lootTableJson.has("pools")) {
return drops;
}
final JsonArray pools = JSONUtils.getAsJsonArray(lootTableJson, "pools");
for (final JsonElement pool : pools) {
final JsonArray entries = JSONUtils.getAsJsonArray(pool.getAsJsonObject(), "entries", new JsonArray());
final float totalWeight = StreamSupport.stream(entries.spliterator(), false).filter(entry -> {
final String type = JSONUtils.getAsString(entry.getAsJsonObject(), "type");
return type.equals("minecraft:empty") || type.equals("minecraft:item") || type.equals("minecraft:tag") || type.equals("minecraft:loot_table");
}).mapToInt(entry -> JSONUtils.getAsInt(entry.getAsJsonObject(), "weight", 1)).sum();
for (final JsonElement ej : entries) {
final JsonObject entryJson = ej.getAsJsonObject();
final String type = JSONUtils.getAsString(entryJson, "type");
if (type.equals("minecraft:item")) {
final Item item = ForgeRegistries.ITEMS.getValue(new ResourceLocation(JSONUtils.getAsString(entryJson, "name")));
final float weight = JSONUtils.getAsFloat(entryJson, "weight", 1);
final float quality = JSONUtils.getAsFloat(entryJson, "quality", 0);
float modifier = 1.0F;
final boolean conditional = JSONUtils.getAsJsonArray(entryJson, "conditions", new JsonArray()).size() > 0;
ItemStack stack = new ItemStack(item);
if (entryJson.has("functions")) {
final Tuple<ItemStack, Float> result = processFunctions(stack, JSONUtils.getAsJsonArray(entryJson, "functions"));
stack = result.getA();
modifier = result.getB();
}
drops.add(new LootDrop(Collections.singletonList(stack), weight / totalWeight * modifier, quality, conditional));
} else if (type.equals("minecraft:loot_table") && lootTableManager != null) {
final ResourceLocation table = new ResourceLocation(JSONUtils.getAsString(entryJson, "name"));
final List<LootTableAnalyzer.LootDrop> tableDrops = toDrops(lootTableManager, table);
final float weight = JSONUtils.getAsFloat(entryJson, "weight", 1);
final float quality = JSONUtils.getAsFloat(entryJson, "quality", 0);
final boolean conditional = JSONUtils.getAsJsonArray(entryJson, "conditions", new JsonArray()).size() > 0;
for (final LootTableAnalyzer.LootDrop drop : tableDrops) {
drops.add(new LootDrop(drop.getItemStacks(), drop.getProbability() * (weight / totalWeight), drop.getQuality() + quality, drop.getConditional() || conditional));
}
}
}
}
drops.sort(Comparator.comparing(LootDrop::getProbability).reversed());
return drops;
}
use of net.minecraft.loot.LootTableManager in project ChocolateQuestRepoured by TeamChocoQuest.
the class LootTableLoader method registerCustomLootTables.
public static void registerCustomLootTables(ServerWorld worldServer) {
Collection<File> files = FileUtils.listFiles(new File(CQRMain.CQ_CHEST_FOLDER, "chests"), new String[] { "json", "properties" }, false);
Set<ResourceLocation> cqrChestLootTables = CQRLoottables.getChestLootTables();
LootTableManager lootTableManager = worldServer.getServer().getLootTables();
LoadingCache<ResourceLocation, LootTable> registeredLootTables = FIELD_REGISTERED_LOOT_TABLES.get(lootTableManager);
for (File file : files) {
String s = file.getName();
ResourceLocation name = new ResourceLocation(CQRMain.MODID, "chests/" + s.substring(0, s.lastIndexOf('.')));
if (cqrChestLootTables.contains(name)) {
continue;
}
LootTable table = LootTable.EMPTY;
table = ForgeEventFactory.loadLootTable(name, table, lootTableManager);
registeredLootTables.put(name, table);
}
}
Aggregations