Skip to content

Commit

Permalink
Add custom villager trades support (Close #349)
Browse files Browse the repository at this point in the history
  • Loading branch information
Goldorion committed Jun 8, 2023
1 parent 5e53f7c commit 53810e1
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 165 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Updated to Minecraft 1.20
* Updated Fabric API to 0.83.0
* Updated Fabric Loader to 0.14.21
* [#349] Added support for custom villager trades
* [Bugfix #358] GUIs couldn't be loaded on servers
* [Bugfix #408] Servers crashed when key bindings were used

Expand Down
2 changes: 1 addition & 1 deletion src/fabric-1.20/block.definition.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -440,5 +440,5 @@ global_templates:
variables: level=7
name: "@RESROOT/data/fabric/tags/blocks/mineable/needs_tool_level_7.json"

field_exclusions: [ displayFluidOverlay, plantsGrowOn, beaconColorModifier, isLadder, enchantPowerBonus, aiPathNodeType,
field_exclusions: [ material, displayFluidOverlay, plantsGrowOn, beaconColorModifier, isLadder, enchantPowerBonus, aiPathNodeType,
hasEnergyStorage, isFluidTank, energyInitial, energyCapacity, energyMaxReceive, energyMaxExtract, fluidCapacity, fluidRestrictions, canRedstoneConnect ]
51 changes: 0 additions & 51 deletions src/fabric-1.20/mappings/materials.yaml

This file was deleted.

26 changes: 0 additions & 26 deletions src/fabric-1.20/mappings/pathnodetypes.yaml

This file was deleted.

8 changes: 0 additions & 8 deletions src/fabric-1.20/mappings/planttypes.yaml

This file was deleted.

78 changes: 0 additions & 78 deletions src/fabric-1.20/mappings/screens.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion src/fabric-1.20/mappings/villagerprofessions.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
_default: VillagerProfession.ARMORER
_mcreator_prefix: "CUSTOM:"
_mcreator_map_template: "@JavaModNameVillagerProfessions.@REGISTRYNAME.get()"
_mcreator_map_template: "@JavaModNameVillagerProfessions.@REGISTRYNAME"
ARMORER: VillagerProfession.ARMORER
BUTCHER: VillagerProfession.BUTCHER
CARTOGRAPHER: VillagerProfession.CARTOGRAPHER
Expand Down
92 changes: 92 additions & 0 deletions src/fabric-1.20/templates/elementinits/villagertrades.java.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<#--
# This file is part of Fabric-Generator-MCreator.
# Copyright (C) 2012-2020, Pylo
# Copyright (C) 2020-2023, Pylo, opensource contributors
# Copyright (C) 2020-2023, Goldorion, opensource contributors
#
# Fabric-Generator-MCreator 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.
# Fabric-Generator-MCreator 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 Fabric-Generator-MCreator. If not, see <https://www.gnu.org/licenses/>.
-->

<#-- @formatter:off -->
<#include "../mcitems.ftl">

/*
* MCreator note: This file will be REGENERATED on each build.
*/

package ${package}.init;

<#compress>
import net.minecraft.world.entity.npc.VillagerTrades;

public class ${JavaModName}Trades {

public static void registerTrades() {
<#list villagertrades as trade>
<#list trade.tradeEntries as tradeEntry>
<#assign lv1 = []>
<#assign lv2 = []>
<#assign lv3 = []>
<#assign lv4 = []>
<#assign lv5 = []>

<#list tradeEntry.entries as entry>
<#if entry.level == 1>
<#assign lv1 += [entry]>
<#elseif entry.level == 2>
<#assign lv2 += [entry]>
<#elseif entry.level == 3>
<#assign lv3 += [entry]>
<#elseif entry.level == 4>
<#assign lv4 += [entry]>
<#elseif entry.level == 5>
<#assign lv5 += [entry]>
</#if>
</#list>

<@trades lv1, 1, tradeEntry.villagerProfession/>
<@trades lv2, 2, tradeEntry.villagerProfession/>
<@trades lv3, 3, tradeEntry.villagerProfession/>
<@trades lv4, 4, tradeEntry.villagerProfession/>
<@trades lv5, 5, tradeEntry.villagerProfession/>
</#list>
</#list>
}

private record BasicTrade(ItemStack price, ItemStack price2, ItemStack offer, int maxTrades, int xp,
float priceMult) implements VillagerTrades.ItemListing {

@Override
public @NotNull MerchantOffer getOffer(Entity entity, RandomSource random) {
return new MerchantOffer(price, price2, offer, maxTrades, xp, priceMult);
}
}
}
</#compress>

<#macro trades entries level villagerProfession>
<#if entries?has_content>
TradeOfferHelper.
<#if villagerProfession == "WanderingTrader">registerWanderingTraderOffers(${level}
<#else>registerVillagerOffers(${villagerProfession}, ${level}</#if>,
factories -> {
<#list entries as entry>
factories.add(new BasicTrade(${mappedMCItemToItemStackCode(entry.price1, entry.countPrice1)},
<#if !entry.price2.isEmpty()>${mappedMCItemToItemStackCode(entry.price2, entry.countPrice2)}
<#else> ItemStack.EMPTY</#if>, ${mappedMCItemToItemStackCode(entry.offer, entry.countOffer)},
${entry.maxTrades}, ${entry.xp}, ${entry.priceMultiplier}f)
);
</#list>
});
</#if>
</#macro>
1 change: 1 addition & 0 deletions src/fabric-1.20/templates/modbase/mod.java.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public class ${JavaModName} implements ModInitializer {
<#if w.hasElementsOfType("gui")>${JavaModName}Menus.load();</#if>
<#if w.hasElementsOfType("keybind")>${JavaModName}KeyMappingsServer.serverLoad();</#if>
<#if w.getRecipesOfType("Brewing")?has_content>${JavaModName}BrewingRecipes.load();</#if>
<#if w.hasElementsOfType("villagertrade")>${JavaModName}Trades.registerTrades();</#if>
<#if w.hasSounds()>${JavaModName}Sounds.load();</#if>
<#if w.hasVariablesOfScope("GLOBAL_WORLD") || w.hasVariablesOfScope("GLOBAL_MAP")>${JavaModName}Variables.SyncJoin();</#if>
<#if w.hasVariablesOfScope("GLOBAL_WORLD") || w.hasVariablesOfScope("GLOBAL_MAP")>${JavaModName}Variables.SyncChangeWorld();</#if>
Expand Down
4 changes: 4 additions & 0 deletions src/fabric-1.20/villagertrade.definition.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
global_templates:
- template: elementinits/villagertrades.java.ftl
writer: java
name: "@SRCROOT/@BASEPACKAGEPATH/init/@JavaModNameTrades.java"

0 comments on commit 53810e1

Please sign in to comment.