Add an easy way to register Villager and Wandering Trader trades (#84)

* Add TradeRegistry to add Trades a bit more easily. Uses the VillagerTradesEvent on forge

* Added TradeRegistry#registerTradeForWanderer which uses the WandererTraderEvent on forge

* Added javadoc

* Use Fabric own Trade implementation and fixed Test Mod

* Update common/src/main/java/me/shedaniel/architectury/registry/TradeRegistry.java

Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com>

* Update common/src/main/java/me/shedaniel/architectury/registry/TradeRegistry.java

Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com>

* Update common/src/main/java/me/shedaniel/architectury/registry/TradeRegistry.java

Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com>

* Update common/src/main/java/me/shedaniel/architectury/registry/TradeRegistry.java

Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com>

* Added javadoc for SimpleTrade

* Use two lists instead of Int2ObjectMap

* Use "registerTradeForWanderingTrader" instead of "registerTradeForWanderer" for better clarification

* Use IllegalArgumentException instead of RuntimeException

* Remove level limit (Mods may be able to remove this restriction in VillagerData#canLevelUp), Clean up forge's implementation

Signed-off-by: shedaniel <daniel@shedaniel.me>

* Clean up TestTrades and add licenses

Signed-off-by: shedaniel <daniel@shedaniel.me>

* [ciskip] Reintroduce lower bound validation for level

Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com>
Co-authored-by: shedaniel <daniel@shedaniel.me>
Co-authored-by: Max <maxh2709@gmail.com>
This commit is contained in:
canitzp
2021-05-14 23:59:58 +02:00
committed by GitHub
parent bc402d6b5f
commit 2ff5dd500d
6 changed files with 289 additions and 0 deletions

View File

@@ -0,0 +1,75 @@
/*
* This file is part of architectury.
* Copyright (C) 2020, 2021 architectury
*
* This program 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; either
* version 3 of the License, or (at your option) any later version.
*
* This program 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 program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package me.shedaniel.architectury.registry.trade.forge;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import me.shedaniel.architectury.forge.ArchitecturyForge;
import net.minecraft.core.NonNullList;
import net.minecraft.world.entity.npc.VillagerProfession;
import net.minecraft.world.entity.npc.VillagerTrades;
import net.minecraftforge.event.village.VillagerTradesEvent;
import net.minecraftforge.event.village.WandererTradesEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import java.util.*;
@Mod.EventBusSubscriber(modid = ArchitecturyForge.MOD_ID)
public class TradeRegistryImpl {
private static final Map<VillagerProfession, Int2ObjectMap<List<VillagerTrades.ItemListing>>> TRADES_TO_ADD = new HashMap<>();
private static final List<VillagerTrades.ItemListing> WANDERER_TRADER_TRADES_GENERIC = new ArrayList<>();
private static final List<VillagerTrades.ItemListing> WANDERER_TRADER_TRADES_RARE = new ArrayList<>();
private static void registerVillagerTrade0(VillagerProfession profession, int level, VillagerTrades.ItemListing... trades) {
Int2ObjectMap<List<VillagerTrades.ItemListing>> tradesForProfession = TRADES_TO_ADD.computeIfAbsent(profession, $ -> new Int2ObjectOpenHashMap<>());
List<VillagerTrades.ItemListing> tradesForLevel = tradesForProfession.computeIfAbsent(level, $ -> new ArrayList<>());
Collections.addAll(tradesForLevel, trades);
}
public static void registerTradeForWanderingTrader(boolean rare, VillagerTrades.ItemListing... trades) {
if (rare) {
Collections.addAll(WANDERER_TRADER_TRADES_RARE, trades);
} else {
Collections.addAll(WANDERER_TRADER_TRADES_GENERIC, trades);
}
}
@SubscribeEvent
public static void onTradeRegistering(VillagerTradesEvent event) {
Int2ObjectMap<List<VillagerTrades.ItemListing>> trades = TRADES_TO_ADD.get(event.getType());
if (trades != null) {
for (Int2ObjectMap.Entry<List<VillagerTrades.ItemListing>> entry : trades.int2ObjectEntrySet()) {
event.getTrades().computeIfAbsent(entry.getIntKey(), $ -> NonNullList.create()).addAll(entry.getValue());
}
}
}
@SubscribeEvent
public static void onWanderingTradeRegistering(WandererTradesEvent event) {
if (!WANDERER_TRADER_TRADES_GENERIC.isEmpty()) {
event.getGenericTrades().addAll(WANDERER_TRADER_TRADES_GENERIC);
}
if (!WANDERER_TRADER_TRADES_RARE.isEmpty()) {
event.getRareTrades().addAll(WANDERER_TRADER_TRADES_RARE);
}
}
}