Skip to content

Commit

Permalink
Add Villager Trades cache and API to provide Villager trades manually (
Browse files Browse the repository at this point in the history
…#91)

* Merge getModListVersion

* Rename getModListVersion to getModListVersionForMobDrops

* Cache Villager trades + IVillagerInfoProvider

* Fix

* helpers
  • Loading branch information
kuba6000 authored Aug 29, 2024
1 parent 6258b3e commit 6f958fa
Show file tree
Hide file tree
Showing 10 changed files with 574 additions and 275 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public interface IMobExtraInfoProvider {
* You should provide all changes that you make in events. You should modify provided drops list, DO NOT EDIT
* {@link MobRecipe#mOutputs}, IT WON'T WORK!
* You can check for actual entity using {@link MobRecipe#entity} instead of entityString
*
*
* @param entityString Entity registration name
* @param drops Drop list (you should edit that)
* @param drops Drop list (you should edit that) (use {@link MobDrop#create} to create drops)
* @param recipe Recipe
*/
void provideExtraDropsInformation(@Nonnull final String entityString, @Nonnull final ArrayList<MobDrop> drops,
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/kuba6000/mobsinfo/api/IMobInfoProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ public interface IMobInfoProvider {
* {@link EntityLivingBase#dropRareDrop(int)},
* {@link EntityLiving#addRandomArmor()} + {@link EntityLiving#enchantEquipment()}
*
* @param drops
* @param drops Add drops to this list (use {@link MobDrop#create} to create drops)
*/
void provideDropsInformation(@Nonnull ArrayList<MobDrop> drops);

/**
* Add drops from vanilla mob
*
*
* @param drops Drops from provideDropsInformation
* @param entityString Entity String
* @param type Desired drop type or null to get add of them
Expand All @@ -47,7 +47,7 @@ static void provideSuperVanillaDrops(@Nonnull ArrayList<MobDrop> drops, @Nonnull

/**
* Add drops from vanilla mob
*
*
* @param drops Drops from provideDropsInformation
* @param vanillaMob Entity class
* @param type Desired drop type or null to get add of them
Expand All @@ -59,7 +59,7 @@ static void provideSuperVanillaDrops(@Nonnull ArrayList<MobDrop> drops,

/**
* Add drops from vanilla mob
*
*
* @param drops Drops from provideDropsInformation
* @param vanillaMob Entity class
*/
Expand All @@ -70,7 +70,7 @@ static void provideSuperVanillaDrops(@Nonnull ArrayList<MobDrop> drops,

/**
* Add drops from vanilla mob
*
*
* @param drops Drops from provideDropsInformation
* @param entityString Entity String
*/
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/com/kuba6000/mobsinfo/api/IVillagerInfoProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.kuba6000.mobsinfo.api;

import java.util.ArrayList;

import javax.annotation.Nonnull;

import net.minecraft.entity.passive.EntityVillager;

import cpw.mods.fml.common.registry.VillagerRegistry.IVillageTradeHandler;

/**
* You should add this interface on your villager handler (on a class that implements {@link IVillageTradeHandler})
*/
public interface IVillagerInfoProvider {

/**
* You should provide information about all the things that your handler can add to trade list:
*
* @param villager Villager
* @param profession Profession id
* @param trades Trade list, add trades to this list (use {@link VillagerTrade#create} to create drops)
*/
void provideTrades(@Nonnull final EntityVillager villager, final int profession,
@Nonnull ArrayList<VillagerTrade> trades);

}
2 changes: 1 addition & 1 deletion src/main/java/com/kuba6000/mobsinfo/api/MobDrop.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static DropType get(int ordinal) {
private MobDrop() {}

/**
* All-in-one constructor, you should use {@link #create(ItemStack)} instead!
* All-in-one constructor, you should use {@link #create} instead!
*/
public MobDrop(ItemStack stack, DropType type, int chance, Integer enchantable, HashMap<Integer, Integer> damages,
boolean lootable, boolean playerOnly) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/kuba6000/mobsinfo/api/VillagerRecipe.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package com.kuba6000.mobsinfo.api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import net.minecraft.entity.passive.EntityVillager;

public class VillagerRecipe {

public static HashMap<Integer, VillagerRecipe> recipes = new HashMap<>();

public final List<VillagerTrade> trades;
public final ArrayList<VillagerTrade> trades;
public final int profession;
public final EntityVillager mob;

public VillagerRecipe(List<VillagerTrade> trades, int profession, EntityVillager villager) {
public VillagerRecipe(ArrayList<VillagerTrade> trades, int profession, EntityVillager villager) {
this.trades = trades;
this.profession = profession;
this.mob = villager;
Expand Down
157 changes: 151 additions & 6 deletions src/main/java/com/kuba6000/mobsinfo/api/VillagerTrade.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,201 @@
import java.util.HashSet;
import java.util.Set;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;

import com.kuba6000.mobsinfo.api.utils.GSONUtils;

public class VillagerTrade {

public static class TradeItem {

@GSONUtils.SkipGSON
public ItemStack stack;
public ConstructableItemStack reconstructableStack;
public Set<Integer> possibleSizes;
public Integer enchantability;

public TradeItem(ItemStack stack, Set<Integer> possibleSizes, Integer enchantability) {
/**
* All-in-one constructor, you should use {@link VillagerTrade#createItem} instead!
*/
public TradeItem(@Nonnull ItemStack stack, @Nullable Set<Integer> possibleSizes,
@Nullable Integer enchantability) {
this.stack = stack;
this.reconstructableStack = new ConstructableItemStack(this.stack);
this.possibleSizes = possibleSizes;
this.enchantability = enchantability;
}

public TradeItem(ItemStack stack) {
public TradeItem(@Nonnull ItemStack stack) {
this(stack, null, null);
}

public TradeItem setPossibleSizes(int min, int max) {
/**
* Sets this Item possible stack size values
*
* @param min From (minimum stack size)
* @param max To (maximum stack size)
*/
public TradeItem withPossibleSizes(int min, int max) {
possibleSizes = new HashSet<>(max - min);
for (int i = min; i <= max; i++) {
possibleSizes.add(i);
}
return this;
}

/**
* Set this Item to be randomly enchanted
*
* @param enchantmentPower Enchantment power
*/
public TradeItem withRandomEnchant(int enchantmentPower) {
this.enchantability = enchantmentPower;
return this;
}

public void reconstructStack() {
this.stack = this.reconstructableStack.construct();
}
}

TradeItem firstInput;
TradeItem secondInput;
TradeItem output;
double chance;

public VillagerTrade(TradeItem firstInput, TradeItem secondInput, TradeItem output, Double chance) {
/**
* All-in-one constructor, you should use {@link #create} instead!
*/
public VillagerTrade(@Nonnull TradeItem firstInput, @Nullable TradeItem secondInput, @Nonnull TradeItem output,
double chance) {
this.firstInput = firstInput;
this.secondInput = secondInput;
this.output = output;
this.chance = chance;
}

public VillagerTrade(ItemStack firstInput, ItemStack secondInput, ItemStack output, Double chance) {
/**
* All-in-one constructor, you should use {@link #create} instead!
*/
public VillagerTrade(@Nonnull ItemStack firstInput, @Nullable ItemStack secondInput, @Nonnull ItemStack output,
double chance) {
this.firstInput = new TradeItem(firstInput);
this.secondInput = secondInput == null ? null : new TradeItem(secondInput);
this.output = new TradeItem(output);
this.chance = chance;
}

public VillagerTrade(Item firstInput, Item secondInput, Item output, Double chance) {
/**
* All-in-one constructor, you should use {@link #create} instead!
*/
public VillagerTrade(@Nonnull Item firstInput, @Nullable Item secondInput, @Nonnull Item output, double chance) {
this.firstInput = new TradeItem(new ItemStack(firstInput));
this.secondInput = secondInput == null ? null : new TradeItem(new ItemStack(secondInput));
this.output = new TradeItem(new ItemStack(output));
this.chance = chance;
}

/**
* Create Trade, to be used as builder.
* If you need to set possible sizes or enchantability on the item use {@link #create(TradeItem, TradeItem)}
*
* @param input First input item (item to buy), to set second input use {@link #withSecondaryInput}
* @param output Output item (item to sell)
*/
public static VillagerTrade create(@Nonnull Item input, @Nonnull Item output) {
return new VillagerTrade(input, null, output, 1d);
}

/**
* Create Trade, to be used as builder.
* If you need to set possible sizes or enchantability on the item use {@link #create(TradeItem, TradeItem)}
*
* @param input First input item (item to buy), to set second input use {@link #withSecondaryInput}
* @param output Output item (item to sell)
*/
public static VillagerTrade create(@Nonnull ItemStack input, @Nonnull ItemStack output) {
return new VillagerTrade(input, null, output, 1d);
}

/**
* Create Trade, to be used as builder. Create Trade Items using {@link #createItem}
*
* @param input First input item (item to buy), to set second input use {@link #withSecondaryInput}
* @param output Output item (item to sell)
*/
public static VillagerTrade create(@Nonnull TradeItem input, @Nonnull TradeItem output) {
return new VillagerTrade(input, null, output, 1d);
}

/**
* Create Trade Item.
* You should only create Trade Item directly if you want to set possible sizes or enchantability.
* To be used as builder
*
* @param item Item
*/
public static TradeItem createItem(@Nonnull Item item) {
return new TradeItem(new ItemStack(item));
}

/**
* Create Trade Item.
* You should only create Trade Item directly if you want to set possible sizes or enchantability.
* To be used as builder
*
* @param item Item
*/
public static TradeItem createItem(@Nonnull ItemStack item) {
return new TradeItem(item);
}

/**
* Set this Trade secondary input (second item to buy).
*
* @param secondInput Item
*/
public VillagerTrade withSecondaryInput(@Nonnull TradeItem secondInput) {
this.secondInput = secondInput;
return this;
}

/**
* Set this Trade secondary input (second item to buy)
* If you need to set possible sizes or enchantability on the item use {@link #withSecondaryInput(TradeItem)}
*
* @param secondInput Item
*/
public VillagerTrade withSecondaryInput(@Nonnull ItemStack secondInput) {
this.secondInput = new TradeItem(secondInput);
return this;
}

/**
* Set this Trade secondary input (second item to buy)
* If you need to set possible sizes or enchantability on the item use {@link #withSecondaryInput(TradeItem)}
*
* @param secondInput Item
*/
public VillagerTrade withSecondaryInput(@Nonnull Item secondInput) {
this.secondInput = new TradeItem(new ItemStack(secondInput));
return this;
}

/**
* Set this Trade chance to be added to the list
*
* @param chance Drop chance
*/
public VillagerTrade withChance(double chance) {
this.chance = chance;
return this;
}

public boolean hasSecondInput() {
return secondInput != null;
}
Expand All @@ -78,4 +217,10 @@ public TradeItem getOutput() {
public double getChance() {
return chance;
}

public void reconstructStacks() {
firstInput.reconstructStack();
if (secondInput != null) secondInput.reconstructStack();
output.reconstructStack();
}
}
Loading

0 comments on commit 6f958fa

Please sign in to comment.