Search in sources :

Example 1 with VillagerTradesEvent

use of net.minecraftforge.event.village.VillagerTradesEvent in project MinecraftForge by MinecraftForge.

the class VillagerTradingManager method postVillagerEvents.

/**
 * Posts a VillagerTradesEvent for each registered profession.
 */
private static void postVillagerEvents() {
    for (VillagerProfession prof : ForgeRegistries.PROFESSIONS) {
        Int2ObjectMap<ItemListing[]> trades = VANILLA_TRADES.getOrDefault(prof, new Int2ObjectOpenHashMap<>());
        Int2ObjectMap<List<ItemListing>> mutableTrades = new Int2ObjectOpenHashMap<>();
        for (int i = 1; i < 6; i++) {
            mutableTrades.put(i, NonNullList.create());
        }
        trades.int2ObjectEntrySet().forEach(e -> {
            Arrays.stream(e.getValue()).forEach(mutableTrades.get(e.getIntKey())::add);
        });
        MinecraftForge.EVENT_BUS.post(new VillagerTradesEvent(mutableTrades, prof));
        Int2ObjectMap<ItemListing[]> newTrades = new Int2ObjectOpenHashMap<>();
        mutableTrades.int2ObjectEntrySet().forEach(e -> newTrades.put(e.getIntKey(), e.getValue().toArray(new ItemListing[0])));
        VillagerTrades.TRADES.put(prof, newTrades);
    }
}
Also used : Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) VillagerProfession(net.minecraft.world.entity.npc.VillagerProfession) List(java.util.List) NonNullList(net.minecraft.core.NonNullList) VillagerTradesEvent(net.minecraftforge.event.village.VillagerTradesEvent)

Aggregations

Int2ObjectOpenHashMap (it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap)1 List (java.util.List)1 NonNullList (net.minecraft.core.NonNullList)1 VillagerProfession (net.minecraft.world.entity.npc.VillagerProfession)1 VillagerTradesEvent (net.minecraftforge.event.village.VillagerTradesEvent)1