Permalink
Browse files

Add get_merchant_recipes and make extra settings of merchant recipes …

…optional
  • Loading branch information...
jb-aero committed Sep 25, 2018
1 parent e67bc0e commit 0df4eb1e2bc7f64c0c5b0c0d2e44d61bb241c094
@@ -32,6 +32,8 @@
MCInventoryView openWorkbench(MCLocation loc, boolean force);
MCInventoryView openMerchant(MCMerchant merchant, boolean force);
void setGameMode(MCGameMode mode);
void setItemInHand(MCItemStack item);
@@ -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, -1));
return new BukkitMCMerchantRecipe(new MerchantRecipe(is, Integer.MAX_VALUE));
case SHAPED:
return new BukkitMCShapedRecipe(new ShapedRecipe(nskey, is));
case SHAPELESS:
@@ -16,7 +16,7 @@
public class BukkitMCMerchant implements MCMerchant {
private Merchant merchant;
BukkitMCMerchant(Merchant mer) {
public BukkitMCMerchant(Merchant mer) {
merchant = mer;
}
@@ -5,6 +5,7 @@
import com.laytonsmith.abstraction.MCInventoryView;
import com.laytonsmith.abstraction.MCItemStack;
import com.laytonsmith.abstraction.MCLocation;
import com.laytonsmith.abstraction.MCMerchant;
import com.laytonsmith.abstraction.blocks.MCMaterial;
import com.laytonsmith.abstraction.bukkit.BukkitMCInventory;
import com.laytonsmith.abstraction.bukkit.BukkitMCInventoryView;
@@ -16,6 +17,7 @@
import org.bukkit.entity.HumanEntity;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Merchant;
public class BukkitMCHumanEntity extends BukkitMCLivingEntity implements MCHumanEntity {
@@ -121,6 +123,11 @@ public MCInventoryView openWorkbench(MCLocation loc, boolean force) {
return new BukkitMCInventoryView(he.openWorkbench((Location) loc.getHandle(), force));
}
@Override
public MCInventoryView openMerchant(MCMerchant merchant, boolean force) {
return new BukkitMCInventoryView(he.openMerchant((Merchant) merchant.getHandle(), force));
}
@Override
public MCInventoryView openEnchanting(MCLocation loc, boolean force) {
return new BukkitMCInventoryView(he.openEnchanting((Location) loc.getHandle(), force));
@@ -2,7 +2,9 @@
import com.laytonsmith.abstraction.AbstractionObject;
import com.laytonsmith.abstraction.MCInventory;
import com.laytonsmith.abstraction.MCMerchant;
import com.laytonsmith.abstraction.bukkit.BukkitMCInventory;
import com.laytonsmith.abstraction.bukkit.BukkitMCMerchant;
import com.laytonsmith.abstraction.entities.MCVillager;
import com.laytonsmith.abstraction.enums.MCProfession;
import com.laytonsmith.abstraction.enums.bukkit.BukkitMCProfession;
@@ -38,4 +40,19 @@ public void setProfession(MCProfession profession) {
public MCInventory getInventory() {
return new BukkitMCInventory(getHandle().getInventory());
}
@Override
public MCMerchant asMerchant() {
return new BukkitMCMerchant(getHandle());
}
@Override
public int getRiches() {
return getHandle().getRiches();
}
@Override
public void setRiches(int riches) {
getHandle().setRiches(riches);
}
}
@@ -1,9 +1,14 @@
package com.laytonsmith.abstraction.entities;
import com.laytonsmith.abstraction.MCInventoryHolder;
import com.laytonsmith.abstraction.MCMerchant;
import com.laytonsmith.abstraction.enums.MCProfession;
public interface MCVillager extends MCAgeable, MCInventoryHolder {
MCMerchant asMerchant();
int getRiches();
void setRiches(int riches);
MCProfession getProfession();
void setProfession(MCProfession profession);
}
@@ -1,7 +1,42 @@
package com.laytonsmith.core;
import com.laytonsmith.PureUtilities.Vector3D;
import com.laytonsmith.abstraction.*;
import com.laytonsmith.abstraction.MCBannerMeta;
import com.laytonsmith.abstraction.MCBlockStateMeta;
import com.laytonsmith.abstraction.MCBookMeta;
import com.laytonsmith.abstraction.MCBrewerInventory;
import com.laytonsmith.abstraction.MCColor;
import com.laytonsmith.abstraction.MCCreatureSpawner;
import com.laytonsmith.abstraction.MCEnchantment;
import com.laytonsmith.abstraction.MCEnchantmentStorageMeta;
import com.laytonsmith.abstraction.MCFireworkBuilder;
import com.laytonsmith.abstraction.MCFireworkEffect;
import com.laytonsmith.abstraction.MCFireworkEffectMeta;
import com.laytonsmith.abstraction.MCFireworkMeta;
import com.laytonsmith.abstraction.MCFurnaceInventory;
import com.laytonsmith.abstraction.MCFurnaceRecipe;
import com.laytonsmith.abstraction.MCInventory;
import com.laytonsmith.abstraction.MCInventoryHolder;
import com.laytonsmith.abstraction.MCItemFactory;
import com.laytonsmith.abstraction.MCItemMeta;
import com.laytonsmith.abstraction.MCItemStack;
import com.laytonsmith.abstraction.MCLeatherArmorMeta;
import com.laytonsmith.abstraction.MCLivingEntity;
import com.laytonsmith.abstraction.MCLocation;
import com.laytonsmith.abstraction.MCMapMeta;
import com.laytonsmith.abstraction.MCMerchantRecipe;
import com.laytonsmith.abstraction.MCMetadataValue;
import com.laytonsmith.abstraction.MCPattern;
import com.laytonsmith.abstraction.MCPlugin;
import com.laytonsmith.abstraction.MCPotionData;
import com.laytonsmith.abstraction.MCPotionMeta;
import com.laytonsmith.abstraction.MCRecipe;
import com.laytonsmith.abstraction.MCShapedRecipe;
import com.laytonsmith.abstraction.MCShapelessRecipe;
import com.laytonsmith.abstraction.MCSkullMeta;
import com.laytonsmith.abstraction.MCTropicalFishBucketMeta;
import com.laytonsmith.abstraction.MCWorld;
import com.laytonsmith.abstraction.StaticLayer;
import com.laytonsmith.abstraction.blocks.MCBlockState;
import com.laytonsmith.abstraction.blocks.MCMaterial;
import com.laytonsmith.abstraction.blocks.MCBanner;
@@ -1559,9 +1594,15 @@ public MCRecipe recipe(Construct c, Target t) {
return ret;
case MERCHANT:
((MCMerchantRecipe) ret).setMaxUses(Static.getInt32(recipe.get("maxuses", t), t));
((MCMerchantRecipe) ret).setUses(Static.getInt32(recipe.get("uses", t), t));
((MCMerchantRecipe) ret).setHasExperienceReward(Static.getBoolean(recipe.get("hasxpreward", t), t));
if (recipe.containsKey("maxuses")) {
((MCMerchantRecipe) ret).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("hasxpreward")) {
((MCMerchantRecipe) ret).setHasExperienceReward(Static.getBoolean(recipe.get("hasxpreward", t), t));
}
ingredients = Static.getArray(recipe.get("ingredients", t), t);
if (ingredients.inAssociativeMode()) {
throw new CREFormatException("Ingredients array is invalid.", t);
@@ -1575,6 +1616,7 @@ public MCRecipe recipe(Construct c, Target t) {
mcIngredients.add(item(ingredient, t));
}
((MCMerchantRecipe) ret).setIngredients(mcIngredients);
return ret;
default:
throw new CREFormatException("Could not find valid recipe type.", t);
@@ -1,8 +1,12 @@
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;
@@ -15,8 +19,11 @@
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;
/**
@@ -277,4 +284,72 @@ 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;
}
}

0 comments on commit 0df4eb1

Please sign in to comment.