Added two TR villagers (#2931)
* Added Tinkerer * Added registration to tinkerer and lang * Added electrician
This commit is contained in:
parent
a6ae3a7984
commit
b14a4c72a1
7 changed files with 178 additions and 19 deletions
|
@ -35,4 +35,25 @@ public class ExceptionUtils {
|
|||
}
|
||||
}
|
||||
|
||||
public static void requireNonNull(Object obj, String name) {
|
||||
if (obj == null)
|
||||
throw new NullPointerException(name + " cannot be null!");
|
||||
}
|
||||
|
||||
public static <T> void requireNonNullEntries(T[] array, String name) {
|
||||
if (array == null)
|
||||
return;
|
||||
for (T obj : array)
|
||||
if (obj == null)
|
||||
throw new NullPointerException("No entry of " + name + " can be null!");
|
||||
}
|
||||
|
||||
public static void requireNonNullEntries(Iterable<?> iterable, String name) {
|
||||
if (iterable == null)
|
||||
return;
|
||||
for (Object obj : iterable)
|
||||
if (obj == null)
|
||||
throw new NullPointerException("No entry of " + name + " can be null!");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package reborncore.common.util;
|
||||
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap;
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemConvertible;
|
||||
|
@ -7,19 +9,40 @@ import net.minecraft.item.ItemStack;
|
|||
import net.minecraft.item.Items;
|
||||
import net.minecraft.village.TradeOffer;
|
||||
import net.minecraft.village.TradeOffers;
|
||||
import net.minecraft.village.VillagerProfession;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class TradeUtils {
|
||||
public final class TradeUtils {
|
||||
|
||||
private TradeUtils() {/* No instantiation.*/}
|
||||
public enum Level {
|
||||
NOVICE, APPRENTICE, JOURNEYMAN, EXPERT, MASTER;
|
||||
|
||||
public static TradeOffer create(ItemConvertible item, int price, int count, int maxUses, int experience) {
|
||||
public static final int SIZE = MASTER.ordinal() + 1;
|
||||
|
||||
/**
|
||||
* The number value of the job level as used internally.
|
||||
* @return the level of the job as an int
|
||||
*/
|
||||
public int asInt() {
|
||||
return ordinal() + 1; // internally job levels start with 1, but ordinal() starts with 0
|
||||
}
|
||||
}
|
||||
|
||||
private TradeUtils() {/* No instantiation. */}
|
||||
|
||||
public static TradeOffer createSell(ItemConvertible item, int price, int count, int maxUses, int experience) {
|
||||
return new TradeOffer(new ItemStack(Items.EMERALD, price), new ItemStack(item, count), maxUses, experience, 0.05F);
|
||||
}
|
||||
|
||||
public static TradeOffer createBuy(ItemConvertible item, int price, int count, int maxUses, int experience) {
|
||||
return new TradeOffer(new ItemStack(item, count), new ItemStack(Items.EMERALD, price), maxUses, experience, 0.05F);
|
||||
}
|
||||
|
||||
@Contract("null -> null; !null -> new")
|
||||
public static TradeOffer copy(TradeOffer tradeOffer) {
|
||||
if (tradeOffer == null)
|
||||
|
@ -39,4 +62,28 @@ public class TradeUtils {
|
|||
};
|
||||
}
|
||||
|
||||
public static void registerTradesForLevel(VillagerProfession profession, Level level, boolean replace, TradeOffer... tradeOffers) {
|
||||
ExceptionUtils.requireNonNull(profession, "profession");
|
||||
ExceptionUtils.requireNonNull(level, "level");
|
||||
ExceptionUtils.requireNonNull(tradeOffers, "tradeOffers");
|
||||
ExceptionUtils.requireNonNullEntries(tradeOffers, "tradeOffers");
|
||||
|
||||
Int2ObjectMap<TradeOffers.Factory[]> allTrades = TradeOffers.PROFESSION_TO_LEVELED_TRADE.getOrDefault(profession, new Int2ObjectArrayMap<>(Level.SIZE));
|
||||
TradeOffers.Factory[] oldLevelTrades = allTrades.getOrDefault(level.asInt(), new TradeOffers.Factory[0]);
|
||||
TradeOffers.Factory[] newLevelTrades = new TradeOffers.Factory[tradeOffers.length];
|
||||
newLevelTrades = Arrays.stream(tradeOffers).map(TradeUtils::asFactory).collect(Collectors.toList()).toArray(newLevelTrades);
|
||||
TradeOffers.Factory[] allLevelTrades;
|
||||
|
||||
if (replace)
|
||||
allLevelTrades = newLevelTrades;
|
||||
else {
|
||||
allLevelTrades = new TradeOffers.Factory[oldLevelTrades.length+newLevelTrades.length];
|
||||
System.arraycopy(oldLevelTrades,0,allLevelTrades,0,oldLevelTrades.length);
|
||||
System.arraycopy(newLevelTrades,0,allLevelTrades,oldLevelTrades.length,newLevelTrades.length);
|
||||
}
|
||||
|
||||
allTrades.put(level.asInt(), allLevelTrades);
|
||||
TradeOffers.PROFESSION_TO_LEVELED_TRADE.put(profession, allTrades);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -77,7 +77,8 @@ public class ModRegistry {
|
|||
registerFluids();
|
||||
registerSounds();
|
||||
registerApis();
|
||||
registerWanderingTraderTrades();
|
||||
TRVillager.registerVillagerTrades();
|
||||
TRVillager.registerWanderingTraderTrades();
|
||||
}
|
||||
|
||||
private static void registerBlocks() {
|
||||
|
@ -256,18 +257,4 @@ public class ModRegistry {
|
|||
EnergyStorage.SIDED.registerForBlockEntity(CableBlockEntity::getSideEnergyStorage, TRBlockEntities.CABLE);
|
||||
ItemStorage.SIDED.registerForBlockEntity(StorageUnitBaseBlockEntity::getExposedStorage, TRBlockEntities.STORAGE_UNIT);
|
||||
}
|
||||
|
||||
private static void registerWanderingTraderTrades() {
|
||||
List<TradeOffer> extraCommonTrades = new LinkedList<>();
|
||||
List<TradeOffer> extraRareTrades = new LinkedList<>();
|
||||
// specify extra trades here
|
||||
extraCommonTrades.add(TradeUtils.create(TRContent.RUBBER_SAPLING, 5, 1, 8, 1));
|
||||
// registration of the trades
|
||||
TradeOfferHelper.registerWanderingTraderOffers(1, allTradesList -> allTradesList.addAll(
|
||||
extraCommonTrades.stream().map(TradeUtils::asFactory).collect(Collectors.toList())
|
||||
));
|
||||
TradeOfferHelper.registerWanderingTraderOffers(2, allTradesList -> allTradesList.addAll(
|
||||
extraRareTrades.stream().map(TradeUtils::asFactory).collect(Collectors.toList())
|
||||
));
|
||||
}
|
||||
}
|
||||
|
|
100
src/main/java/techreborn/init/TRVillager.java
Normal file
100
src/main/java/techreborn/init/TRVillager.java
Normal file
|
@ -0,0 +1,100 @@
|
|||
package techreborn.init;
|
||||
|
||||
import net.fabricmc.fabric.api.object.builder.v1.trade.TradeOfferHelper;
|
||||
import net.fabricmc.fabric.api.object.builder.v1.villager.VillagerProfessionBuilder;
|
||||
import net.fabricmc.fabric.api.object.builder.v1.world.poi.PointOfInterestHelper;
|
||||
import net.minecraft.item.Items;
|
||||
import net.minecraft.sound.SoundEvents;
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.util.registry.Registry;
|
||||
import net.minecraft.village.TradeOffer;
|
||||
import net.minecraft.village.VillagerProfession;
|
||||
import net.minecraft.world.poi.PointOfInterestType;
|
||||
import reborncore.common.util.TradeUtils;
|
||||
import techreborn.TechReborn;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class TRVillager {
|
||||
|
||||
public static final Identifier TINKERER_ID = new Identifier(TechReborn.MOD_ID, "tinkerer");
|
||||
public static final Identifier ELECTRICIAN_ID = new Identifier(TechReborn.MOD_ID, "electrician");
|
||||
|
||||
public static final PointOfInterestType TINKERER_POI = PointOfInterestHelper.register(
|
||||
TINKERER_ID, 1, 1, TRContent.Machine.IRON_ALLOY_FURNACE.block);
|
||||
public static final PointOfInterestType ELECTRICIAN_POI = PointOfInterestHelper.register(
|
||||
ELECTRICIAN_ID, 1, 1, TRContent.Machine.SOLID_FUEL_GENERATOR.block);
|
||||
|
||||
public static final VillagerProfession TINKERER_PROFESSION = Registry.register(Registry.VILLAGER_PROFESSION,TINKERER_ID,
|
||||
VillagerProfessionBuilder.create().id(TINKERER_ID).workstation(TINKERER_POI).workSound(SoundEvents.ENTITY_VILLAGER_WORK_TOOLSMITH).build());
|
||||
public static final VillagerProfession ELECTRICIAN_PROFESSION = Registry.register(Registry.VILLAGER_PROFESSION,ELECTRICIAN_ID,
|
||||
VillagerProfessionBuilder.create().id(ELECTRICIAN_ID).workstation(ELECTRICIAN_POI).workSound(ModSounds.CABLE_SHOCK).build());
|
||||
|
||||
private TRVillager() {/* No instantiation. */}
|
||||
|
||||
public static void registerVillagerTrades() {
|
||||
// tinkerer
|
||||
TradeUtils.registerTradesForLevel(TINKERER_PROFESSION, TradeUtils.Level.NOVICE, false,
|
||||
TradeUtils.createBuy(TRContent.RawMetals.TIN, 1, 6, 12, 2),
|
||||
TradeUtils.createBuy(TRContent.RawMetals.LEAD, 1, 4, 12, 2),
|
||||
TradeUtils.createSell(TRContent.Parts.RUBBER, 2, 3, 16, 2)
|
||||
);
|
||||
TradeUtils.registerTradesForLevel(TINKERER_PROFESSION, TradeUtils.Level.APPRENTICE, false,
|
||||
TradeUtils.createSell(TRContent.Ingots.BRONZE, 2, 1, 12, 10),
|
||||
TradeUtils.createSell(TRContent.Ingots.BRASS, 5, 1, 12, 10),
|
||||
TradeUtils.createSell(TRContent.Parts.ELECTRONIC_CIRCUIT, 3, 2, 12, 10)
|
||||
);
|
||||
TradeUtils.registerTradesForLevel(TINKERER_PROFESSION, TradeUtils.Level.JOURNEYMAN, false,
|
||||
TradeUtils.createSell(TRContent.Ingots.ELECTRUM, 7, 3, 12, 20),
|
||||
TradeUtils.createBuy(TRContent.Parts.CARBON_FIBER, 1, 3, 12, 20)
|
||||
);
|
||||
TradeUtils.registerTradesForLevel(TINKERER_PROFESSION, TradeUtils.Level.EXPERT, false,
|
||||
TradeUtils.createSell(TRContent.Ingots.ADVANCED_ALLOY, 7, 4, 12, 20),
|
||||
TradeUtils.createBuy(TRContent.Ingots.NICKEL, 1, 1, 12, 30)
|
||||
);
|
||||
TradeUtils.registerTradesForLevel(TINKERER_PROFESSION, TradeUtils.Level.MASTER, false,
|
||||
TradeUtils.createSell(TRContent.Parts.ADVANCED_CIRCUIT, 7, 3, 12, 30)
|
||||
);
|
||||
// electrician
|
||||
TradeUtils.registerTradesForLevel(ELECTRICIAN_PROFESSION, TradeUtils.Level.NOVICE, false,
|
||||
TradeUtils.createBuy(TRContent.Parts.RUBBER, 1, 6, 12, 2),
|
||||
TradeUtils.createBuy(Items.COPPER_INGOT, 1, 3, 12, 2),
|
||||
TradeUtils.createSell(TRContent.Cables.INSULATED_COPPER, 1, 3, 12, 2)
|
||||
);
|
||||
TradeUtils.registerTradesForLevel(ELECTRICIAN_PROFESSION, TradeUtils.Level.APPRENTICE, false,
|
||||
TradeUtils.createBuy(Items.GOLD_INGOT, 1, 4, 12, 10),
|
||||
TradeUtils.createSell(TRContent.Cables.INSULATED_GOLD, 5, 3, 12, 10),
|
||||
TradeUtils.createSell(TRContent.Parts.ELECTRONIC_CIRCUIT, 3, 2, 12, 10)
|
||||
);
|
||||
TradeUtils.registerTradesForLevel(ELECTRICIAN_PROFESSION, TradeUtils.Level.JOURNEYMAN, false,
|
||||
TradeUtils.createBuy(TRContent.RED_CELL_BATTERY, 1, 1, 12, 20),
|
||||
TradeUtils.createSell(TRContent.Machine.LOW_VOLTAGE_SU, 8, 1, 12, 20),
|
||||
TradeUtils.createSell(TRContent.Machine.SOLID_FUEL_GENERATOR, 8, 1, 12, 20)
|
||||
);
|
||||
TradeUtils.registerTradesForLevel(ELECTRICIAN_PROFESSION, TradeUtils.Level.EXPERT, false,
|
||||
TradeUtils.createSell(TRContent.Parts.ADVANCED_CIRCUIT, 7, 3, 12, 20),
|
||||
TradeUtils.createBuy(TRContent.Gems.RUBY, 1, 6, 12, 20),
|
||||
TradeUtils.createSell(TRContent.Cables.GLASSFIBER, 4, 1, 8, 30)
|
||||
);
|
||||
TradeUtils.registerTradesForLevel(ELECTRICIAN_PROFESSION, TradeUtils.Level.MASTER, false,
|
||||
TradeUtils.createSell(TRContent.Machine.LAMP_LED, 8, 1, 12, 20),
|
||||
TradeUtils.createSell(TRContent.LITHIUM_ION_BATTERY, 30, 1, 8, 30)
|
||||
);
|
||||
}
|
||||
|
||||
public static void registerWanderingTraderTrades() {
|
||||
List<TradeOffer> extraCommonTrades = new LinkedList<>();
|
||||
List<TradeOffer> extraRareTrades = new LinkedList<>();
|
||||
// specify extra trades below here
|
||||
extraCommonTrades.add(TradeUtils.createSell(TRContent.RUBBER_SAPLING, 5, 1, 8, 1));
|
||||
// registration of the trades, no changes necessary for new trades
|
||||
TradeOfferHelper.registerWanderingTraderOffers(1, allTradesList -> allTradesList.addAll(
|
||||
extraCommonTrades.stream().map(TradeUtils::asFactory).collect(Collectors.toList())
|
||||
));
|
||||
TradeOfferHelper.registerWanderingTraderOffers(2, allTradesList -> allTradesList.addAll(
|
||||
extraRareTrades.stream().map(TradeUtils::asFactory).collect(Collectors.toList())
|
||||
));
|
||||
}
|
||||
}
|
|
@ -1106,5 +1106,9 @@
|
|||
"gui.techreborn.block.output_blocked": "Output blocked",
|
||||
"gui.techreborn.block.progress.active": "Progress: (%s)",
|
||||
"gui.techreborn.block.progress.stopped": "Idle (Stopped)",
|
||||
"gui.techreborn.block.progress.paused": "Idle (%s)"
|
||||
"gui.techreborn.block.progress.paused": "Idle (%s)",
|
||||
|
||||
"_comment28": "Villagers",
|
||||
"entity.minecraft.villager.tinkerer": "Tinkerer",
|
||||
"entity.minecraft.villager.electrician": "Electrician"
|
||||
}
|
Binary file not shown.
After Width: | Height: | Size: 1.4 KiB |
Binary file not shown.
After Width: | Height: | Size: 1 KiB |
Loading…
Reference in a new issue