-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
18f4a39
commit 9756d06
Showing
2 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
src/main/java/pl/pabilo8/immersiveintelligence/common/util/IEVillagerHandlerCustom.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package pl.pabilo8.immersiveintelligence.common.util; | ||
|
||
import blusunrize.immersiveengineering.common.util.IEVillagerHandler; | ||
import net.minecraft.entity.IMerchant; | ||
import net.minecraft.entity.passive.EntityVillager; | ||
import net.minecraft.init.Items; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.village.MerchantRecipe; | ||
import net.minecraft.village.MerchantRecipeList; | ||
import net.minecraftforge.fml.common.registry.VillagerRegistry; | ||
import net.minecraftforge.oredict.OreDictionary; | ||
|
||
import java.util.Random; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.List; | ||
|
||
public class IEVillagerHandlerCustom extends IEVillagerHandler { | ||
|
||
// Initialize trades with instance method | ||
public static void initIEVillagerTrades() { | ||
// Call parent initialization method if needed | ||
IEVillagerHandler.initIEVillagerTrades(); | ||
|
||
// Retrieve the profession and add career if it doesn't already exist | ||
VillagerRegistry.VillagerProfession engineerProfession = IEVillagerHandler.PROF_ENGINEER; | ||
if (engineerProfession != null) { | ||
boolean careerExists = false; | ||
|
||
try { | ||
// Access private "careers" field using reflection | ||
Field careersField = VillagerRegistry.VillagerProfession.class.getDeclaredField("careers"); | ||
careersField.setAccessible(true); | ||
List<VillagerRegistry.VillagerCareer> careers = (List<VillagerRegistry.VillagerCareer>) careersField.get(engineerProfession); | ||
|
||
// Check if the "gunsmith" career already exists | ||
for (VillagerRegistry.VillagerCareer career : careers) { | ||
if ("immersiveengineering.gunsmith".equals(career.getName())) { | ||
careerExists = true; | ||
break; | ||
} | ||
} | ||
|
||
// Add career if it doesn't exist | ||
if (!careerExists) { | ||
VillagerRegistry.VillagerCareer careerGunsmith = new VillagerRegistry.VillagerCareer(engineerProfession, "immersiveengineering.gunsmith"); | ||
|
||
// Add a custom trade for iron barrel replacement | ||
careerGunsmith.addTrade(1, new EntityVillager.ITradeList[]{ | ||
new ReplaceSteelBarrelWithIron() | ||
}); | ||
} | ||
} catch (NoSuchFieldException | IllegalAccessException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
|
||
// Custom trade class for replacing steel barrel with iron barrel | ||
private static class ReplaceSteelBarrelWithIron implements EntityVillager.ITradeList | ||
{ | ||
@Override | ||
public void addMerchantRecipe(IMerchant merchant, MerchantRecipeList recipeList, Random random) | ||
{ | ||
// Retrieve OreDict items | ||
ItemStack steelBarrelStack = getOreDictStack("gunbarrelSteel"); | ||
ItemStack ironBarrelStack = getOreDictStack("gunbarrelIron"); | ||
|
||
// Check that both items exist before adding the trade | ||
if(!steelBarrelStack.isEmpty()&&!ironBarrelStack.isEmpty()) | ||
{ | ||
// Remove existing trade if it matches the trade you want to replace | ||
recipeList.removeIf(recipe -> | ||
recipe.getItemToBuy().isItemEqual(new ItemStack(Items.EMERALD, 4))&& | ||
recipe.getSecondItemToBuy().isItemEqual(ironBarrelStack) | ||
); | ||
|
||
// Add the new trade with iron barrel and set the price | ||
recipeList.add(new MerchantRecipe( | ||
new ItemStack(Items.EMERALD, 4), // 4 emeralds price | ||
ironBarrelStack | ||
)); | ||
} | ||
} | ||
|
||
// Retrieve an item from the Ore Dictionary | ||
private ItemStack getOreDictStack(String oreDictName) | ||
{ | ||
if(OreDictionary.doesOreNameExist(oreDictName)&&!OreDictionary.getOres(oreDictName).isEmpty()) | ||
{ | ||
return OreDictionary.getOres(oreDictName).get(0).copy(); | ||
} | ||
return ItemStack.EMPTY; | ||
} | ||
} | ||
} |