use of net.minecraft.village.MerchantRecipe in project Railcraft by Railcraft.
the class TileTradeStation method attemptTrade.
private boolean attemptTrade(List<EntityVillager> villagers, int tradeSet) {
ItemStack buy1 = recipeSlots.getStackInSlot(tradeSet * 3 + 0);
ItemStack buy2 = recipeSlots.getStackInSlot(tradeSet * 3 + 1);
ItemStack sell = recipeSlots.getStackInSlot(tradeSet * 3 + 2);
for (EntityVillager villager : villagers) {
MerchantRecipeList recipes = villager.getRecipes(null);
for (MerchantRecipe recipe : recipes) {
if (recipe.isRecipeDisabled())
continue;
//noinspection ConstantConditions
if (recipe.getItemToBuy() != null && !InvTools.isItemLessThanOrEqualTo(recipe.getItemToBuy(), buy1))
continue;
//noinspection ConstantConditions
if (recipe.getSecondItemToBuy() != null && !InvTools.isItemLessThanOrEqualTo(recipe.getSecondItemToBuy(), buy2))
continue;
if (!InvTools.isItemGreaterOrEqualThan(recipe.getItemToSell(), sell))
continue;
// System.out.printf("Buying: %d %s Found: %d%n", recipe.getItemToBuy().stackSize, recipe.getItemToBuy().getDisplayName(), InvTools.countItems(invInput, recipe.getItemToBuy()));
if (canDoTrade(recipe)) {
// System.out.println("Can do trade");
doTrade(villager, recipe);
return true;
}
}
}
return false;
}
use of net.minecraft.village.MerchantRecipe in project NetherEx by LogicTechCorp.
the class Trade method getRandomTrade.
public MerchantRecipe getRandomTrade(Random rand) {
ItemStack outputStack = getItemToSell().copy();
ItemStack inputA = getItemToBuy().copy();
ItemStack inputB = getSecondItemToBuy().copy();
outputStack.setCount(rand.nextInt((outputAmount.getMax() - outputAmount.getMin()) + 1) + outputAmount.getMin());
inputA.setCount(rand.nextInt((inputAAmount.getMax() - inputAAmount.getMin()) + 1) + inputAAmount.getMin());
inputB.setCount(rand.nextInt((inputBAmount.getMax() - inputBAmount.getMin()) + 1) + inputBAmount.getMin());
if (outputEnchantments.size() > 0) {
for (TradeOffer.Enchantment outputEnchantment : outputEnchantments) {
Enchantment enchantment = Enchantment.getEnchantmentByLocation(outputEnchantment.getName());
TradeOffer.Amount enchantmentAmount = outputEnchantment.getAmount();
if (enchantment != null) {
if (outputStack.getItem() instanceof ItemEnchantedBook) {
((ItemEnchantedBook) outputStack.getItem()).addEnchantment(outputStack, new EnchantmentData(enchantment, rand.nextInt((enchantmentAmount.getMax() - enchantmentAmount.getMin()) + 1) + enchantmentAmount.getMin()));
} else {
outputStack.addEnchantment(enchantment, rand.nextInt((enchantmentAmount.getMax() - enchantmentAmount.getMin()) + 1) + enchantmentAmount.getMin());
}
}
}
}
return new MerchantRecipe(inputA, inputB, outputStack, 0, rand.nextInt((offerAmount.getMax() - offerAmount.getMin()) + 1) + offerAmount.getMin());
}
use of net.minecraft.village.MerchantRecipe in project MorePlanets by SteveKunG.
the class EntityNibiruVillager method updateAITasks.
@Override
protected void updateAITasks() {
if (--this.randomTickDivider <= 0) {
BlockPos blockpos = new BlockPos(this);
this.world.getVillageCollection().addToVillagerPositionList(blockpos);
this.randomTickDivider = 70 + this.rand.nextInt(50);
this.villageObj = this.world.getVillageCollection().getNearestVillage(blockpos, 32);
if (this.villageObj == null) {
this.detachHome();
} else {
BlockPos blockpos1 = this.villageObj.getCenter();
this.setHomePosAndDistance(blockpos1, (int) (this.villageObj.getVillageRadius() * 1.0F));
if (this.isLookingForHome) {
this.isLookingForHome = false;
this.villageObj.setDefaultPlayerReputation(5);
}
}
}
if (!this.isTrading() && this.timeUntilReset > 0) {
--this.timeUntilReset;
if (this.timeUntilReset <= 0) {
if (this.needsInitilization) {
for (MerchantRecipe merchantrecipe : this.buyingList) {
if (merchantrecipe.isRecipeDisabled()) {
merchantrecipe.increaseMaxTradeUses(this.rand.nextInt(6) + this.rand.nextInt(6) + 2);
}
}
this.populateBuyingList();
this.needsInitilization = false;
if (this.villageObj != null && this.lastBuyingPlayer != null) {
this.world.setEntityState(this, (byte) 14);
this.villageObj.modifyPlayerReputation(this.lastBuyingPlayer, 1);
}
}
this.addPotionEffect(new PotionEffect(MobEffects.REGENERATION, 200, 0));
}
}
super.updateAITasks();
}
use of net.minecraft.village.MerchantRecipe in project SpongeCommon by SpongePowered.
the class SpongeTradeOfferBuilder method build.
@Override
public TradeOffer build() throws IllegalStateException {
checkState(this.firstItem != null, "Trading item has not been set");
checkState(this.sellingItem != null, "Selling item has not been set");
checkState(this.useCount <= this.maxUses, "Usage count cannot be greater than the max usage count (%s)", this.maxUses);
final ItemStack first = this.firstItem.createStack();
final ItemStack second = this.secondItem == null ? null : this.secondItem.createStack();
final ItemStack selling = this.sellingItem.createStack();
MerchantRecipe recipe = new MerchantRecipe(ItemStackUtil.toNative(first), ItemStackUtil.toNative(second), ItemStackUtil.toNative(selling), this.useCount, this.maxUses);
recipe.rewardsExp = this.allowsExperience;
return (TradeOffer) recipe;
}
use of net.minecraft.village.MerchantRecipe in project SpongeCommon by SpongePowered.
the class IMixinEntityVillager$ITradeList method accept.
@Override
default void accept(Merchant owner, List<TradeOffer> tradeOffers, Random random) {
MerchantRecipeList tempList = new MerchantRecipeList();
for (TradeOffer offer : tradeOffers) {
tempList.add(TradeOfferUtil.toNative(offer));
}
addMerchantRecipe((IMerchant) owner, tempList, random);
tradeOffers.clear();
for (MerchantRecipe recipe : tempList) {
tradeOffers.add(TradeOfferUtil.fromNative(recipe));
}
}
Aggregations