Permalink
Browse files

Add abstraction for merchant recipes.

  • Loading branch information...
jb-aero committed Sep 24, 2018
1 parent 2c9f48a commit e67bc0eb624a6fa43f533d5f62d241dbf20027b4
@@ -0,0 +1,14 @@
package com.laytonsmith.abstraction;
import java.util.List;
public interface MCMerchant extends AbstractionObject {
boolean isTrading();
MCHumanEntity getTrader();
List<MCMerchantRecipe> getRecipes();
void setRecipes(List<MCMerchantRecipe> recipes);
}
@@ -0,0 +1,22 @@
package com.laytonsmith.abstraction;
import java.util.List;
public interface MCMerchantRecipe extends MCRecipe {
int getMaxUses();
void setMaxUses(int maxUses);
int getUses();
void setUses(int uses);
boolean hasExperienceReward();
void setHasExperienceReward(boolean flag);
List<MCItemStack> getIngredients();
void setIngredients(List<MCItemStack> ingredients);
}
@@ -120,6 +120,7 @@
import org.bukkit.inventory.FurnaceRecipe;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.MerchantRecipe;
import org.bukkit.inventory.Recipe;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.ShapelessRecipe;
@@ -628,6 +629,8 @@ public MCRecipe GetNewRecipe(String key, MCRecipeType type, MCItemStack result)
switch(type) {
case FURNACE:
return new BukkitMCFurnaceRecipe(new FurnaceRecipe(nskey, is, Material.AIR, 0.0F, 200));
case MERCHANT:
return new BukkitMCMerchantRecipe(new MerchantRecipe(is, -1));
case SHAPED:
return new BukkitMCShapedRecipe(new ShapedRecipe(nskey, is));
case SHAPELESS:
@@ -649,6 +652,8 @@ 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) {
return new BukkitMCMerchantRecipe((MerchantRecipe) r);
} else {
return null;
}
@@ -0,0 +1,77 @@
package com.laytonsmith.abstraction.bukkit;
import com.laytonsmith.abstraction.MCHumanEntity;
import com.laytonsmith.abstraction.MCMerchant;
import com.laytonsmith.abstraction.MCMerchantRecipe;
import com.laytonsmith.abstraction.bukkit.entities.BukkitMCHumanEntity;
import com.laytonsmith.abstraction.bukkit.entities.BukkitMCPlayer;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Merchant;
import org.bukkit.inventory.MerchantRecipe;
import java.util.ArrayList;
import java.util.List;
public class BukkitMCMerchant implements MCMerchant {
private Merchant merchant;
BukkitMCMerchant(Merchant mer) {
merchant = mer;
}
@Override
public Merchant getHandle() {
return merchant;
}
@Override
public int hashCode() {
return getHandle().hashCode();
}
@Override
public boolean equals(Object obj) {
return getHandle().equals(obj);
}
@Override
public String toString() {
return getHandle().toString();
}
@Override
public boolean isTrading() {
return getHandle().isTrading();
}
@Override
public MCHumanEntity getTrader() {
HumanEntity he = getHandle().getTrader();
if (he == null) {
return null;
}
if (he instanceof Player) {
return new BukkitMCPlayer(he);
}
return new BukkitMCHumanEntity(he);
}
@Override
public List<MCMerchantRecipe> getRecipes() {
List<MCMerchantRecipe> ret = new ArrayList<>();
for (MerchantRecipe mr : getHandle ().getRecipes()) {
ret.add(new BukkitMCMerchantRecipe(mr));
}
return ret;
}
@Override
public void setRecipes(List<MCMerchantRecipe> recipes) {
List<MerchantRecipe> ret = new ArrayList<>();
for (MCMerchantRecipe mr : recipes) {
ret.add((MerchantRecipe) mr.getHandle());
}
getHandle().setRecipes(ret);
}
}
@@ -0,0 +1,98 @@
package com.laytonsmith.abstraction.bukkit;
import com.laytonsmith.abstraction.MCItemStack;
import com.laytonsmith.abstraction.MCMerchantRecipe;
import com.laytonsmith.abstraction.enums.MCRecipeType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.MerchantRecipe;
import java.util.ArrayList;
import java.util.List;
public class BukkitMCMerchantRecipe extends BukkitMCRecipe implements MCMerchantRecipe {
private MerchantRecipe handle;
BukkitMCMerchantRecipe(MerchantRecipe recipe) {
super(recipe);
handle = recipe;
}
@Override
public MerchantRecipe getHandle() {
return handle;
}
@Override
public boolean equals(Object obj) {
return getHandle().equals(obj);
}
@Override
public int hashCode() {
return getHandle().hashCode();
}
@Override
public String toString() {
return getHandle().toString();
}
@Override
public int getMaxUses() {
return getHandle().getMaxUses();
}
@Override
public void setMaxUses(int maxUses) {
getHandle().setMaxUses(maxUses);
}
@Override
public int getUses() {
return getHandle().getUses();
}
@Override
public void setUses(int uses) {
getHandle().setUses(uses);
}
@Override
public boolean hasExperienceReward() {
return getHandle().hasExperienceReward();
}
@Override
public void setHasExperienceReward(boolean flag) {
getHandle().setExperienceReward(flag);
}
@Override
public List<MCItemStack> getIngredients() {
List<MCItemStack> ret = new ArrayList<>();
for (ItemStack s : getHandle().getIngredients()) {
ret.add(new BukkitMCItemStack(s));
}
return ret;
}
@Override
public void setIngredients(List<MCItemStack> ingredients) {
int i = 0;
List<ItemStack> ings = new ArrayList<>();
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
}
ings.add((ItemStack) s.getHandle());
}
getHandle().setIngredients(ings);
}
@Override
public MCRecipeType getRecipeType() {
return MCRecipeType.MERCHANT;
}
}
@@ -2,6 +2,7 @@
public enum MCRecipeType {
FURNACE,
MERCHANT,
SHAPED,
SHAPELESS
}
@@ -1,41 +1,7 @@
package com.laytonsmith.core;
import com.laytonsmith.PureUtilities.Vector3D;
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.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.*;
import com.laytonsmith.abstraction.blocks.MCBlockState;
import com.laytonsmith.abstraction.blocks.MCMaterial;
import com.laytonsmith.abstraction.blocks.MCBanner;
@@ -1456,6 +1422,16 @@ 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) {
MCMerchantRecipe merchant = (MCMerchantRecipe) r;
CArray il = new CArray(t);
for (MCItemStack i : merchant.getIngredients()) {
il.push(item(i, t), t);
}
ret.set("ingredients", il, t);
ret.set("maxuses", new CInt(merchant.getMaxUses(), t), t);
ret.set("uses", new CInt(merchant.getUses(), t), t);
ret.set("hasxpreward", CBoolean.get(merchant.hasExperienceReward()), t);
}
return ret;
}
@@ -1582,6 +1558,24 @@ 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));
ingredients = Static.getArray(recipe.get("ingredients", t), t);
if (ingredients.inAssociativeMode()) {
throw new CREFormatException("Ingredients array is invalid.", t);
}
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()) {
mcIngredients.add(item(ingredient, t));
}
((MCMerchantRecipe) ret).setIngredients(mcIngredients);
default:
throw new CREFormatException("Could not find valid recipe type.", t);
}

0 comments on commit e67bc0e

Please sign in to comment.