Trading support for ModLoader - thanks Herblore!

This commit is contained in:
Christian 2012-08-23 01:40:03 -04:00
parent 4e8668c922
commit e37b40dac9
6 changed files with 126 additions and 9 deletions

View File

@ -294,6 +294,10 @@ public class ModLoader
EntityRegistry.addSpawn(entityName, weightedProb, min, max, spawnList, biomes);
}
public static void addTrade(int profession, TradeEntry entry)
{
ModLoaderHelper.registerTrade(profession, entry);
}
/**
* Send a packet from the client
* @param packet

View File

@ -0,0 +1,30 @@
package net.minecraft.src;
/**
* Compatibility class for ModLoader -- do not use
*
* @author cpw
*
*/
public class TradeEntry
{
public final int id;
public float chance;
public boolean buying;
public int min = 0;
public int max = 0;
public TradeEntry(int id, float chance, boolean buying, int min, int max)
{
this.id = id;
this.chance = chance;
this.buying = buying;
this.min = min;
this.max = max;
}
public TradeEntry(int id, float chance, boolean buying)
{
this(id, chance, buying, 0, 0);
}
}

View File

@ -19,12 +19,15 @@ import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Maps;
import net.minecraft.src.BaseMod;
import net.minecraft.src.Container;
import net.minecraft.src.Entity;
import net.minecraft.src.EntityPlayer;
import net.minecraft.src.TradeEntry;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.ICraftingHandler;
import cpw.mods.fml.common.IDispenseHandler;
@ -39,11 +42,14 @@ import cpw.mods.fml.common.network.IPacketHandler;
import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.common.registry.EntityRegistry;
import cpw.mods.fml.common.registry.EntityRegistry.EntityRegistration;
import cpw.mods.fml.common.registry.VillagerRegistry.IVillageTradeHandler;
import cpw.mods.fml.common.registry.VillagerRegistry;
/**
* @author cpw
*
*/
@SuppressWarnings("deprecation")
public class ModLoaderHelper
{
public static IModLoaderSidedHelper sidedHelper;
@ -158,11 +164,24 @@ public class ModLoaderHelper
}
@SuppressWarnings("deprecation")
public static void buildEntityTracker(BaseModProxy mod, Class<? extends Entity> entityClass, int entityTypeId, int updateRange, int updateInterval,
boolean sendVelocityInfo)
{
EntityRegistration er = EntityRegistry.registerModLoaderEntity(mod, entityClass, entityTypeId, updateRange, updateInterval, sendVelocityInfo);
er.setCustomSpawning(new ModLoaderEntitySpawnCallback(mod, er));
}
private static ModLoaderVillageTradeHandler[] tradeHelpers = new ModLoaderVillageTradeHandler[6];
public static void registerTrade(int profession, TradeEntry entry)
{
assert profession < tradeHelpers.length : "The profession is out of bounds";
if (tradeHelpers[profession] == null)
{
tradeHelpers[profession] = new ModLoaderVillageTradeHandler();
VillagerRegistry.instance().registerVillageTradeHandler(profession, tradeHelpers[profession]);
}
tradeHelpers[profession].addTrade(entry);
}
}

View File

@ -0,0 +1,39 @@
package cpw.mods.fml.common.modloader;
import java.util.List;
import java.util.Random;
import com.google.common.collect.Lists;
import net.minecraft.src.EntityVillager;
import net.minecraft.src.Item;
import net.minecraft.src.MerchantRecipeList;
import net.minecraft.src.TradeEntry;
import cpw.mods.fml.common.registry.VillagerRegistry;
import cpw.mods.fml.common.registry.VillagerRegistry.IVillageTradeHandler;
public class ModLoaderVillageTradeHandler implements IVillageTradeHandler
{
private List<TradeEntry> trades = Lists.newArrayList();
@Override
public void manipulateTradesForVillager(EntityVillager villager, MerchantRecipeList recipeList, Random random)
{
for (TradeEntry ent : trades)
{
if (ent.buying)
{
VillagerRegistry.addEmeraldBuyRecipe(villager, recipeList, random, Item.field_77698_e[ent.id], ent.chance, ent.min, ent.max);
}
else
{
VillagerRegistry.addEmeraldSellRecipe(villager, recipeList, random, Item.field_77698_e[ent.id], ent.chance, ent.min, ent.max);
}
}
}
public void addTrade(TradeEntry entry)
{
trades.add(entry);
}
}

View File

@ -5,20 +5,20 @@ import java.util.List;
import java.util.Map;
import java.util.Random;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import cpw.mods.fml.common.FMLLog;
import net.minecraft.src.ComponentVillage;
import net.minecraft.src.ComponentVillageStartPiece;
import net.minecraft.src.EntityVillager;
import net.minecraft.src.Item;
import net.minecraft.src.MapGenVillage;
import net.minecraft.src.MerchantRecipeList;
import net.minecraft.src.StructureVillagePieceWeight;
import net.minecraft.src.StructureVillagePieces;
import net.minecraft.src.Tuple;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import cpw.mods.fml.common.FMLLog;
/**
* Registry for villager trading control
@ -184,4 +184,22 @@ public class VillagerRegistry
return instance().villageCreationHandlers.get(villagePiece.field_75090_a).buildComponent(villagePiece, startPiece, pieces, random, p1, p2, p3, p4, p5);
}
public static void addEmeraldBuyRecipe(EntityVillager villager, MerchantRecipeList list, Random random, Item item, float chance, int min, int max)
{
if (min > 0 && max > 0)
{
EntityVillager.field_70958_bB.put(item.field_77779_bT, new Tuple(min, max));
}
villager.func_70948_a(list, item.func_77612_l(), random, chance);
}
public static void addEmeraldSellRecipe(EntityVillager villager, MerchantRecipeList list, Random random, Item item, float chance, int min, int max)
{
if (min > 0 && max > 0)
{
EntityVillager.field_70960_bC.put(item.field_77779_bT, new Tuple(min, max));
}
villager.func_70949_b(list, item.func_77612_l(), random, chance);
}
}

View File

@ -66,3 +66,10 @@ public va.<init>(ILjava/lang/String;)V
public va.<init>(ILjava/lang/String;I)V
# ComponentVillage
public aby
# EntityVillager - addbuy and addsell and minmax lists
public od.a(Luj;ILjava/util/Random;F)V
public od.b(Luj;ILjava/util/Random;F)V
public od.bB
public od.bC