public static void init() { mechanicProfession = new VillagerProfession(Names.MOD_ID + ":mechanic", Textures.VILLAGER_MECHANIC, "minecraft:textures/entity/zombie_villager/zombie_villager.png"); VillagerCareer career = new VillagerCareer(mechanicProfession, Names.MOD_ID + ".mechanic"); career.addTrade(1, new ListItemForEmeralds(Itemss.PCB_BLUEPRINT, new PriceInfo(10, 19)), new ListItemForEmeralds(Itemss.NUKE_VIRUS, new PriceInfo(1, 5)), new ListItemForEmeralds(Itemss.STOP_WORM, new PriceInfo(1, 5)) ); for (int i = 0; i < ItemAssemblyProgram.PROGRAMS_AMOUNT; i++) { career.addTrade(1, new ListItemForEmeralds(new ItemStack(Itemss.ASSEMBLY_PROGRAM, 1, i), new PriceInfo(5, 11))); } }
/** * Creates a new villager trade from the supplied JSON object and adds it to the specified career * @param career The VillagerCareer instance to add the new trade to * @param careerLevel the level at which the new trade should be added * @param jsonRecipeObject the JSON object representing the new trade */ private static void addTradeToCareer(VillagerCareer career, int careerLevel, JsonObject jsonRecipeObject) { JsonElement jsonBuyElement = jsonRecipeObject.get("buy"); JsonElement jsonBuyElementB = (jsonRecipeObject.has("buyB") ? jsonRecipeObject.get("buyB") : null); JsonElement jsonSellElement = jsonRecipeObject.get("sell"); ExtraTradeData extraTradeData = new ExtraTradeData(); extraTradeData.chance = (jsonRecipeObject.has("chance") ? jsonRecipeObject.get("chance").getAsDouble() : 1); if (jsonRecipeObject.has("rewardExp")) extraTradeData.rewardsExp = jsonRecipeObject.get("rewardExp").getAsBoolean(); if (jsonRecipeObject.has("maxUses")) extraTradeData.maxTradeUses = jsonRecipeObject.get("maxUses").getAsInt(); ItemStacksAndPrices buy = getItemStacksAndPrices(jsonBuyElement); ItemStacksAndPrices buyB = (jsonBuyElementB != null ? getItemStacksAndPrices(jsonBuyElementB) : null); ItemStacksAndPrices sell = getItemStacksAndPrices(jsonSellElement); boolean isVillagerBuying = containsCurrencyItems(sell.getItemStacks()); boolean isVillagerSelling = (containsCurrencyItems(buy.getItemStacks()) || (buyB != null ? containsCurrencyItems(buyB.getItemStacks()) : false)); if (isVillagerBuying) { career.addTrade(careerLevel, new VTTVillagerBuyingTrade(buy, buyB, sell, extraTradeData)); } else if (isVillagerSelling) { career.addTrade(careerLevel, new VTTVillagerSellingTrade(buy, buyB, sell, extraTradeData)); } else { career.addTrade(careerLevel, new VTTVillagerTradeBase(buy, buyB, sell, extraTradeData)); } }
private static void clearCareerTrades(VillagerCareer career) { List<List<ITradeList>> trades = new VTTVillagerCareer(career).getTrades(); for (List<ITradeList> careerTrades : trades) { careerTrades.clear(); } trades.clear(); }
@Override public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException { World world = sender.getEntityWorld(); if (!world.isRemote) { if (args.length == 0) { for (Map.Entry<Integer, String> kvp : VillagerRegistryHelper.getProfessionIdsAndNamesSortedById()) { sender.sendMessage(new TextComponentString(kvp.getKey() + ": " + kvp.getValue())); } return; } if (args.length == 2 && args[0].equals("profession")) { VillagerProfession profession = VillagerRegistryHelper.getProfession(args[1]); if (profession != null) { for (VillagerCareer career : new VTTVillagerProfession(profession).getCareers()) { VTTVillagerCareer vttCareer = new VTTVillagerCareer(career); sender.sendMessage(new TextComponentString(vttCareer.getId() + ": " + vttCareer.getName())); sender.sendMessage(new TextComponentString(" " + vttCareer.getCareerLevels() + " levels")); } } } } }
private void addVillager(String name, EntityVillager.ITradeList[][] trades) { VillagerProfession prof = new VillagerProfession(Const.MODRES + name, Const.MODRES + "textures/entity/villager/" + name + ".png", "minecraft:textures/entity/zombie_villager/zombie_villager.png"); VillagerProfRegistry.register(prof); VillagerCareer villager = new VillagerCareer(prof, name); for (int i = 0; i < trades.length; i++) { villager.addTrade(i + 1, trades[i]); } }
public static void registerUselessVillager() { final VillagerProfession prof = new VillagerProfession( "openblocks:radio", "openblocks:textures/models/king-ish.png", "minecraft:textures/entity/zombie_villager/zombie_villager.png"); // TODO: zombie texture? GameRegistry.findRegistry(VillagerProfession.class).register(prof); final VillagerCareer career = new VillagerCareer(prof, "audiophile") .addTrade(1, new EmeraldForItems(Item.getItemFromBlock(Blocks.NOTEBLOCK), new PriceInfo(5, 7))) .addTrade(2, new ListItemForEmeralds(Item.getItemFromBlock(Blocks.JUKEBOX), new PriceInfo(10, 15))); // extra for sound quality! for (ItemStack record : OreDictionary.getOres("record")) career.addTrade(3, new ListItemForEmeralds(record.getItem(), new PriceInfo(3, 6))); }
private static VillagerCareer regCareer(VillagerProfession parent, String name, HarshenTrade trade) { return new VillagerCareer(parent, name).addTrade(1, trade); }
/** * Parses the contents of an individual trade file, and adds or removes the trades to or from the specified profession and career * @param fileContents */ private static void loadTradesFromFile(String fileContents) { // parse the provided string as JSON JsonObject jsonObject = new JsonParser().parse(fileContents).getAsJsonObject(); // identify the profession and career to apply these trades to String jsonProfession = jsonObject.get("Profession").getAsString(); String jsonCareer = jsonObject.get("Career").getAsString(); // get the specified career and profession from the villager registry VillagerProfession profession = VillagerRegistryHelper.getProfession(jsonProfession); if (profession == null) throw new UnknownProfessionException(jsonProfession); VillagerCareer career = new VTTVillagerProfession(profession).getCareer(jsonCareer); if (career == null) throw new UnknownCareerException(jsonCareer); // iterate over the trade recipes included in the offers object JsonArray jsonRecipes = jsonObject.get("Offers").getAsJsonObject().get("Recipes").getAsJsonArray(); for (JsonElement jsonRecipe : jsonRecipes) { JsonObject jsonRecipeObject = jsonRecipe.getAsJsonObject(); // get the level this trade change applies to, and what type of change it is String jsonRecipeAction = jsonRecipeObject.get("action").getAsString(); int jsonCareerLevel = (jsonRecipeObject.has("CareerLevel") ? jsonRecipeObject.get("CareerLevel").getAsInt() : 0); // add a new trade if we're supposed to add one if (jsonRecipeAction.equals("add")) { addTradeToCareer(career, jsonCareerLevel, jsonRecipeObject); } // or remove a trade if we're supposed to remove one else if (jsonRecipeAction.equals("remove")) { removeTradeFromCareer(career, jsonCareerLevel, jsonRecipeObject); } // if we're supposed to replace a trade else if (jsonRecipeAction.equals("replace")) { // remove the old one, and add a new one removeTradeFromCareer(career, jsonCareerLevel, jsonRecipeObject); addTradeToCareer(career, jsonCareerLevel, jsonRecipeObject); } // if we're supposed to clear the trades else if (jsonRecipeAction.equals("clear")) { // if a career level was specified... if (jsonCareerLevel > 0) { // clear the trades for that level clearCareerTrades(career, jsonCareerLevel); } // if not... else { // clear all the trades from the career clearCareerTrades(career); } } } // sort them so that the buying trades appear before the selling trades for each level (like vanilla) if (ModConfiguration.sortTrades) sortCareerTrades(career); }
private static void clearCareerTrades(VillagerCareer career, int careerLevel) { List<ITradeList> trades = new VTTVillagerCareer(career).getTrades(careerLevel); trades.clear(); }
/** * Sorts the trades at each level of a career so that the buying trades appear before the selling trades * @param career The VillagerCareer instance on which to sort the trades */ private static void sortCareerTrades(VillagerCareer career) { // get the trades from the career instance in a modifiable form List<List<ITradeList>> trades = new VTTVillagerCareer(career).getTrades(); // iterate over the trades at each career level for (List<ITradeList> levelTrades : trades) { // sort the trades using a comparator Collections.sort(levelTrades, new Comparator<ITradeList>() { @Override public int compare(ITradeList tradeA, ITradeList tradeB) { // get the appropriate trade handler for the two trades being compared ITradeHandler handlerA = TradeHandlers.tradeHandlers.get(tradeA.getClass()); ITradeHandler handlerB = TradeHandlers.tradeHandlers.get(tradeB.getClass()); if (handlerA != null && handlerB != null) { // if one is a buying trade and the other a selling trade, put the buying trade first if (handlerA instanceof VTTVillagerBuyingHandler && handlerB instanceof VTTVillagerSellingHandler) { return -1; } else if (handlerA instanceof VTTVillagerSellingHandler && handlerB instanceof VTTVillagerBuyingHandler) { return 1; } } // otherwise leave their current positions unmodified return 0; } }); } }
@SideOnly(Side.CLIENT) @Override public void addInformation(ItemStack stack, @Nullable World worldIn, List<String> tooltip, ITooltipFlag flagIn) { NBTTagCompound tagItemStack = stack.getTagCompound(); if(tagItemStack == null) return; StringBuilder tooltipText = new StringBuilder(); String name = tagItemStack.getString("Name"); if((name != null) && (name.length() > 0)) { tooltipText.append("Name:§b"); tooltipText.append(name); tooltipText.append("§r"); tooltip.add(tooltip.toString()); } String professionString = tagItemStack.getString("prf"); if((professionString == null) || (professionString.length() == 0)) professionString = "minecraft:farmer"; ResourceLocation professionName = new ResourceLocation(professionString); VillagerProfession profession = BlockVillagerTrader.PROFESSIONS.getObject(professionName); int careerId = TypeTransformer.fromUnsignedByte(tagItemStack.getByte("car")); VillagerCareer career = profession.getCareer(careerId); tooltipText = new StringBuilder(); tooltipText.append("entity.Villager."); tooltipText.append(career.getName()); String localizedCareer = I18n.format(tooltipText.toString()); tooltipText = new StringBuilder(); tooltipText.append("Profession:§b"); tooltipText.append(localizedCareer); tooltipText.append("§r"); tooltip.add(tooltipText.toString()); tooltipText = new StringBuilder(); tooltipText.append("Level:§b"); tooltipText.append(TypeTransformer.fromUnsignedByte(tagItemStack.getByte("lov"))); tooltipText.append("§r"); tooltip.add(tooltipText.toString()); }