Files
MinecraftForge/src/main/java/net/minecraftforge/common/VillagerTradingManager.java
T
be5446b91c Update to 1.17
Update to Official classnames
Patchwork
Make the game compile and run
Merge in the new HUD overlay API
Merge in the new render properties API
PEEL
Use Gradle 7.2 snapshot to fix eclipse integration bug in 7.1, Would use 7.0 but FG needs 7.1 for some reason.
New FML installer
Update to 1.17.1

Co-authored-by: LexManos <lexmanos@gmail.com>
Co-authored-by: SizableShrimp <sizableshrimp@sizableshrimp.me>
Co-authored-by: cpw <cpw+github@weeksfamily.ca>
Co-authored-by: Curle <curle@gemwire.uk>
Co-authored-by: Unnoen <Unnoen@users.noreply.github.com>
Co-authored-by: covers1624 <laughlan.cov@internode.on.net>
Co-authored-by: DaemonUmbra <daemon.umbra@gmail.com>
Co-authored-by: gigaherz <gigaherz@gmail.com>
Signed-off-by: SizableShrimp <sizableshrimp@sizableshrimp.me>
2021-07-22 19:57:02 +01:00

100 lines
4.0 KiB
Java

/*
* Minecraft Forge
* Copyright (c) 2016-2021.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.common;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import net.minecraft.world.entity.npc.VillagerProfession;
import net.minecraft.world.entity.npc.VillagerTrades;
import net.minecraft.world.entity.npc.VillagerTrades.ItemListing;
import net.minecraft.core.NonNullList;
import net.minecraftforge.event.village.VillagerTradesEvent;
import net.minecraftforge.event.village.WandererTradesEvent;
import net.minecraftforge.fmlserverevents.FMLServerAboutToStartEvent;
import net.minecraftforge.registries.ForgeRegistries;
public class VillagerTradingManager
{
private static final Map<VillagerProfession, Int2ObjectMap<ItemListing[]>> VANILLA_TRADES = new HashMap<>();
private static final Int2ObjectMap<ItemListing[]> WANDERER_TRADES = new Int2ObjectOpenHashMap<>();
static
{
VillagerTrades.TRADES.entrySet().forEach(e ->
{
Int2ObjectMap<ItemListing[]> copy = new Int2ObjectOpenHashMap<>();
e.getValue().int2ObjectEntrySet().forEach(ent -> copy.put(ent.getIntKey(), Arrays.copyOf(ent.getValue(), ent.getValue().length)));
VANILLA_TRADES.put(e.getKey(), copy);
});
VillagerTrades.WANDERING_TRADER_TRADES.int2ObjectEntrySet().forEach(e -> WANDERER_TRADES.put(e.getIntKey(), Arrays.copyOf(e.getValue(), e.getValue().length)));
}
static void loadTrades(FMLServerAboutToStartEvent e)
{
postWandererEvent();
postVillagerEvents();
}
/**
* Posts the WandererTradesEvent.
*/
private static void postWandererEvent()
{
List<ItemListing> generic = NonNullList.create();
List<ItemListing> rare = NonNullList.create();
Arrays.stream(WANDERER_TRADES.get(1)).forEach(generic::add);
Arrays.stream(WANDERER_TRADES.get(2)).forEach(rare::add);
MinecraftForge.EVENT_BUS.post(new WandererTradesEvent(generic, rare));
VillagerTrades.WANDERING_TRADER_TRADES.put(1, generic.toArray(new ItemListing[0]));
VillagerTrades.WANDERING_TRADER_TRADES.put(2, rare.toArray(new ItemListing[0]));
}
/**
* 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);
}
}
}