Permalink
Browse files

Establish Trades class of functions

  • Loading branch information...
jb-aero committed Oct 2, 2018
1 parent 0df4eb1 commit 11110b590da120db05de836f9dfe33e4db029037
@@ -4,8 +4,6 @@
public interface MCFurnaceRecipe extends MCRecipe {
String getKey();
MCItemStack getInput();
void setInput(MCItemStack input);
@@ -11,4 +11,6 @@
List<MCMerchantRecipe> getRecipes();
void setRecipes(List<MCMerchantRecipe> recipes);
String getTitle();
}
@@ -7,4 +7,6 @@
MCItemStack getResult();
MCRecipeType getRecipeType();
String getKey();
}
@@ -165,4 +165,6 @@
MCBossBar createBossBar(String title, MCBarColor color, MCBarStyle style);
MCBlockData createBlockData(String data);
MCMerchant createMerchant(String title);
}
@@ -6,8 +6,6 @@
public interface MCShapedRecipe extends MCRecipe {
String getKey();
Map<Character, MCItemStack> getIngredientMap();
String[] getShape();
@@ -6,8 +6,6 @@
public interface MCShapelessRecipe extends MCRecipe {
String getKey();
MCShapelessRecipe addIngredient(MCItemStack ingredient);
MCShapelessRecipe addIngredient(MCMaterial ingredient);
@@ -630,7 +630,7 @@ public MCRecipe GetNewRecipe(String key, MCRecipeType type, MCItemStack result)
case FURNACE:
return new BukkitMCFurnaceRecipe(new FurnaceRecipe(nskey, is, Material.AIR, 0.0F, 200));
case MERCHANT:
return new BukkitMCMerchantRecipe(new MerchantRecipe(is, Integer.MAX_VALUE));
return new BukkitMCMerchantRecipe(new MerchantRecipe(is, Integer.MAX_VALUE), key);
case SHAPED:
return new BukkitMCShapedRecipe(new ShapedRecipe(nskey, is));
case SHAPELESS:
@@ -652,7 +652,7 @@ public static MCRecipe BukkitGetRecipe(Recipe r) {
return new BukkitMCShapedRecipe((ShapedRecipe) r);
} else if(r instanceof FurnaceRecipe) {
return new BukkitMCFurnaceRecipe((FurnaceRecipe) r);
} else if (r instanceof MerchantRecipe) {
} else if(r instanceof MerchantRecipe) {
return new BukkitMCMerchantRecipe((MerchantRecipe) r);
} else {
return null;
@@ -15,9 +15,16 @@
public class BukkitMCMerchant implements MCMerchant {
private String title;
private Merchant merchant;
public BukkitMCMerchant(Merchant mer) {
private List<String> keys;
public BukkitMCMerchant(Merchant mer, String title) {
merchant = mer;
this.title = title;
keys = new ArrayList<>();
for(int i = 0; i < merchant.getRecipes().size(); i++) {
keys.add(null);
}
}
@Override
@@ -48,10 +55,10 @@ public boolean isTrading() {
@Override
public MCHumanEntity getTrader() {
HumanEntity he = getHandle().getTrader();
if (he == null) {
if(he == null) {
return null;
}
if (he instanceof Player) {
if(he instanceof Player) {
return new BukkitMCPlayer(he);
}
return new BukkitMCHumanEntity(he);
@@ -60,18 +67,25 @@ public MCHumanEntity getTrader() {
@Override
public List<MCMerchantRecipe> getRecipes() {
List<MCMerchantRecipe> ret = new ArrayList<>();
for (MerchantRecipe mr : getHandle ().getRecipes()) {
ret.add(new BukkitMCMerchantRecipe(mr));
for(int i = 0; i < getHandle().getRecipes().size(); i++) {
ret.add(new BukkitMCMerchantRecipe(getHandle().getRecipe(i), keys.get(i)));
}
return ret;
}
@Override
public void setRecipes(List<MCMerchantRecipe> recipes) {
List<MerchantRecipe> ret = new ArrayList<>();
for (MCMerchantRecipe mr : recipes) {
keys.clear();
for(MCMerchantRecipe mr : recipes) {
ret.add((MerchantRecipe) mr.getHandle());
keys.add(mr.getKey());
}
getHandle().setRecipes(ret);
}
@Override
public String getTitle() {
return title;
}
}
@@ -12,9 +12,20 @@
public class BukkitMCMerchantRecipe extends BukkitMCRecipe implements MCMerchantRecipe {
private MerchantRecipe handle;
BukkitMCMerchantRecipe(MerchantRecipe recipe) {
private String key;
public BukkitMCMerchantRecipe(MerchantRecipe recipe) {
this(recipe, null);
}
public BukkitMCMerchantRecipe(MerchantRecipe recipe, String key) {
super(recipe);
handle = recipe;
this.key = key;
}
@Override
public String getKey() {
return key;
}
@Override
@@ -70,7 +81,7 @@ public void setHasExperienceReward(boolean flag) {
@Override
public List<MCItemStack> getIngredients() {
List<MCItemStack> ret = new ArrayList<>();
for (ItemStack s : getHandle().getIngredients()) {
for(ItemStack s : getHandle().getIngredients()) {
ret.add(new BukkitMCItemStack(s));
}
return ret;
@@ -80,8 +91,8 @@ public void setHasExperienceReward(boolean flag) {
public void setIngredients(List<MCItemStack> ingredients) {
int i = 0;
List<ItemStack> ings = new ArrayList<>();
for (MCItemStack s : ingredients) {
if (++i > 2) {
for(MCItemStack s : ingredients) {
if(++i > 2) {
break;
// This recipe type only supports two ingredients.
// The Bukkit set method does not include built in enforcement
@@ -10,6 +10,7 @@
import com.laytonsmith.abstraction.MCInventoryHolder;
import com.laytonsmith.abstraction.MCItemFactory;
import com.laytonsmith.abstraction.MCItemStack;
import com.laytonsmith.abstraction.MCMerchant;
import com.laytonsmith.abstraction.MCOfflinePlayer;
import com.laytonsmith.abstraction.MCPlayer;
import com.laytonsmith.abstraction.MCPluginManager;
@@ -571,4 +572,9 @@ public MCBossBar createBossBar(String title, MCBarColor color, MCBarStyle style)
public MCBlockData createBlockData(String data) {
return new BukkitMCBlockData(s.createBlockData(data));
}
@Override
public MCMerchant createMerchant(String title) {
return new BukkitMCMerchant(__Server().createMerchant(title), title);
}
}
@@ -43,7 +43,7 @@ public MCInventory getInventory() {
@Override
public MCMerchant asMerchant() {
return new BukkitMCMerchant(getHandle());
return new BukkitMCMerchant(getHandle(), getHandle().getCareer().toString());
}
@Override
@@ -1432,18 +1432,17 @@ public Construct recipe(MCRecipe r, Target t) {
CArray ret = CArray.GetAssociativeArray(t);
ret.set("type", new CString(r.getRecipeType().name(), t), t);
ret.set("result", item(r.getResult(), t), t);
ret.set("key", r.getKey(), t);
if(r instanceof MCFurnaceRecipe) {
MCFurnaceRecipe furnace = (MCFurnaceRecipe) r;
ret.set("input", item(furnace.getInput(), t), t);
ret.set("key", furnace.getKey(), t);
} else if(r instanceof MCShapelessRecipe) {
MCShapelessRecipe shapeless = (MCShapelessRecipe) r;
CArray il = new CArray(t);
for(MCItemStack i : shapeless.getIngredients()) {
il.push(item(i, t), t);
}
ret.set("ingredients", il, t);
ret.set("key", shapeless.getKey(), t);
} else if(r instanceof MCShapedRecipe) {
MCShapedRecipe shaped = (MCShapedRecipe) r;
CArray shape = new CArray(t);
@@ -1456,11 +1455,10 @@ public Construct recipe(MCRecipe r, Target t) {
}
ret.set("shape", shape, t);
ret.set("ingredients", imap, t);
ret.set("key", shaped.getKey(), t);
} else if (r instanceof MCMerchantRecipe) {
} else if(r instanceof MCMerchantRecipe) {
MCMerchantRecipe merchant = (MCMerchantRecipe) r;
CArray il = new CArray(t);
for (MCItemStack i : merchant.getIngredients()) {
for(MCItemStack i : merchant.getIngredients()) {
il.push(item(i, t), t);
}
ret.set("ingredients", il, t);
@@ -1594,28 +1592,29 @@ public MCRecipe recipe(Construct c, Target t) {
return ret;
case MERCHANT:
if (recipe.containsKey("maxuses")) {
((MCMerchantRecipe) ret).setMaxUses(Static.getInt32(recipe.get("maxuses", t), t));
MCMerchantRecipe mer = (MCMerchantRecipe) ret;
if(recipe.containsKey("maxuses")) {
mer.setMaxUses(Static.getInt32(recipe.get("maxuses", t), t));
}
if (recipe.containsKey("uses")) {
((MCMerchantRecipe) ret).setUses(Static.getInt32(recipe.get("uses", t), t));
if(recipe.containsKey("uses")) {
mer.setUses(Static.getInt32(recipe.get("uses", t), t));
}
if (recipe.containsKey("hasxpreward")) {
((MCMerchantRecipe) ret).setHasExperienceReward(Static.getBoolean(recipe.get("hasxpreward", t), t));
if(recipe.containsKey("hasxpreward")) {
mer.setHasExperienceReward(Static.getBoolean(recipe.get("hasxpreward", t), t));
}
ingredients = Static.getArray(recipe.get("ingredients", t), t);
if (ingredients.inAssociativeMode()) {
if(ingredients.inAssociativeMode()) {
throw new CREFormatException("Ingredients array is invalid.", t);
}
if (ingredients.size() < 1 || ingredients.size() > 2) {
if(ingredients.size() < 1 || ingredients.size() > 2) {
throw new CRERangeException("Ingredients for merchants must contain 1 or 2 items, found "
+ ingredients.size(), t);
}
List<MCItemStack> mcIngredients = new ArrayList<>();
for (Construct ingredient : ingredients.asList()) {
for(Construct ingredient : ingredients.asList()) {
mcIngredients.add(item(ingredient, t));
}
((MCMerchantRecipe) ret).setIngredients(mcIngredients);
mer.setIngredients(mcIngredients);
return ret;
default:
@@ -1,12 +1,8 @@
package com.laytonsmith.core.functions;
import com.laytonsmith.PureUtilities.Version;
import com.laytonsmith.abstraction.MCEntity;
import com.laytonsmith.abstraction.MCItemStack;
import com.laytonsmith.abstraction.MCMerchant;
import com.laytonsmith.abstraction.MCMerchantRecipe;
import com.laytonsmith.abstraction.MCRecipe;
import com.laytonsmith.abstraction.entities.MCVillager;
import com.laytonsmith.annotations.api;
import com.laytonsmith.core.CHVersion;
import com.laytonsmith.core.ObjectGenerator;
@@ -19,11 +15,9 @@
import com.laytonsmith.core.environments.Environment;
import com.laytonsmith.core.exceptions.CRE.CRECastException;
import com.laytonsmith.core.exceptions.CRE.CREFormatException;
import com.laytonsmith.core.exceptions.CRE.CREIllegalArgumentException;
import com.laytonsmith.core.exceptions.CRE.CREThrowable;
import com.laytonsmith.core.exceptions.ConfigRuntimeException;
import java.util.HashMap;
import java.util.List;
/**
@@ -222,7 +216,6 @@ public Version since() {
@Override
public Construct exec(Target t, Environment environment, Construct... args) throws ConfigRuntimeException {
Static.getServer().clearRecipes();
return CVoid.VOID;
}
@@ -259,7 +252,6 @@ public Version since() {
@Override
public Construct exec(Target t, Environment environment, Construct... args) throws ConfigRuntimeException {
Static.getServer().resetRecipes();
return CVoid.VOID;
}
@@ -284,72 +276,4 @@ public Version since() {
}
}
@api
public static class get_merchant_recipes extends recipeFunction {
@Override
public Class<? extends CREThrowable>[] thrown() {
return new Class[0];
}
@Override
public Construct exec(Target t, Environment env, Construct... args) throws ConfigRuntimeException {
CArray ret = new CArray(t);
for (MCMerchantRecipe mr : GetMerchant(args[0], t).getRecipes()) {
ret.push(ObjectGenerator.GetGenerator().recipe(mr, t), t);
}
return ret;
}
@Override
public Version since() {
return CHVersion.V3_3_3;
}
@Override
public String getName() {
return "get_merchant_recipes";
}
@Override
public Integer[] numArgs() {
return new Integer[]{1};
}
@Override
public String docs() {
return "array {specifier} Returns a list of recipes used by the specified merchant."
+ " Specifier can be the UUID of an entity or a virtual merchant ID.";
}
}
public static final HashMap<String, MCMerchantRecipe> MERCHANT_RECIPES = new HashMap<>();
public static final HashMap<String, MCMerchant> VIRTUAL_MERCHANTS = new HashMap<>();
/**
* Returns the merchant specified.
* @param specifier The string representing the merchant, whether entity UUID or virtual id.
* @param t
* @return
*/
private static MCMerchant GetMerchant(Construct specifier, Target t) {
MCMerchant merchant;
if(specifier.val().length() == 36 || specifier.val().length() == 32) {
try {
MCEntity entity = Static.getEntity(specifier, t);
if(!(entity instanceof MCVillager)) {
throw new CREIllegalArgumentException("The entity specified is not capable of being an merchant.", t);
}
return ((MCVillager) entity).asMerchant();
} catch (CREFormatException iae) {
// not a UUID
}
}
merchant = VIRTUAL_MERCHANTS.get(specifier.val());
if(merchant == null) {
throw new CREIllegalArgumentException("A merchant named \"" + specifier.val() + "\" does not exist.", t);
}
return merchant;
}
}
Oops, something went wrong.

0 comments on commit 11110b5

Please sign in to comment.