mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-13 00:37:52 +00:00
Fix VillagerTypeTestMod not working (#10426)
This commit is contained in:
1 parent
dbd7da6885
commit
43288f79e3
2 files changed
+25
-3
No files matched your search
@@ -5,6 +5,7 @@
|
||||
|
||||
package net.minecraftforge.common.extensions;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
@@ -61,6 +62,13 @@ public interface IForgeGameTestHelper {
|
||||
throw new GameTestAssertException(message.get());
|
||||
}
|
||||
|
||||
/** Backported from 1.20.6 */
|
||||
default <N> void assertValueEqual(N expected, N actual, String name) {
|
||||
if (!Objects.equals(expected, actual)) {
|
||||
throw new GameTestAssertException("Expected " + name + " to be " + expected + ", but was " + actual);
|
||||
}
|
||||
}
|
||||
|
||||
// Backported from 1.20.6
|
||||
default Player makeMockPlayer(final GameType p_333981_) {
|
||||
return new Player(this.self().getLevel(), BlockPos.ZERO, 0.0F, new GameProfile(UUID.randomUUID(), "test-mock-player")) {
|
||||
|
||||
@@ -14,6 +14,7 @@ import net.minecraft.world.entity.EntityType;
|
||||
import net.minecraft.world.entity.npc.Villager;
|
||||
import net.minecraft.world.entity.npc.VillagerTrades;
|
||||
import net.minecraft.world.entity.npc.VillagerType;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.item.Items;
|
||||
import net.minecraft.world.level.biome.Biome;
|
||||
import net.minecraft.world.level.biome.Biomes;
|
||||
@@ -24,6 +25,7 @@ import net.minecraftforge.gametest.GameTestHolder;
|
||||
import net.minecraftforge.registries.DeferredRegister;
|
||||
import net.minecraftforge.registries.RegistryObject;
|
||||
import net.minecraftforge.test.BaseTestMod;
|
||||
import net.minecraftforge.unsafe.UnsafeHacks;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@@ -58,17 +60,17 @@ public class VillagerTypeTestMod extends BaseTestMod {
|
||||
helper.succeed();
|
||||
}
|
||||
|
||||
/** Test verifies NPE not thrown when looking up a villager type in the trade that doesn't contain that type.*/
|
||||
/** Test verifies NPE not thrown when looking up a villager type in the trade that doesn't contain that type. */
|
||||
@GameTest(template = "forge:empty3x3x3")
|
||||
public static void emeralds_for_villager_type(GameTestHelper helper) {
|
||||
var trade = new VillagerTrades.EmeraldsForVillagerTypeItem(1, 12, 30, Map.of(TEST_VILLAGER_TYPE.get(), Items.DIRT));
|
||||
var trade = unsafeCreateTrade(1, 12, 30, Map.of(TEST_VILLAGER_TYPE.get(), Items.DIRT));
|
||||
var rnd = helper.getLevel().getRandom();
|
||||
|
||||
// Should be a successful trade for dirt for our test villager
|
||||
var test = new Villager(EntityType.VILLAGER, helper.getLevel(), TEST_VILLAGER_TYPE.get());
|
||||
var test_offer = trade.getOffer(test, rnd);
|
||||
helper.assertFalse(test_offer == null, "Failed to retreive trade value for test profession");
|
||||
helper.assertValueEqual(test_offer.getItemCostA().itemStack().getItem(), Items.DIRT, "Offer did not return the expected item");
|
||||
helper.assertValueEqual(test_offer.getCostA().getItem(), Items.DIRT, "Offer did not return the expected item");
|
||||
|
||||
var plains = new Villager(EntityType.VILLAGER, helper.getLevel(), VillagerType.PLAINS);
|
||||
// This will NPE on unpatched code, we need to test that it returns null correctly
|
||||
@@ -77,4 +79,16 @@ public class VillagerTypeTestMod extends BaseTestMod {
|
||||
|
||||
helper.succeed();
|
||||
}
|
||||
|
||||
private static VillagerTrades.ItemListing unsafeCreateTrade(int cost, int maxUses, int villagerXp, Map<VillagerType, Item> trades) {
|
||||
try {
|
||||
var ctor = Class
|
||||
.forName("net.minecraft.world.entity.npc.VillagerTrades$EmeraldsForVillagerTypeItem")
|
||||
.getDeclaredConstructor(int.class, int.class, int.class, Map.class);
|
||||
UnsafeHacks.setAccessible(ctor);
|
||||
return (VillagerTrades.ItemListing) ctor.newInstance(cost, maxUses, villagerXp, trades);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user