Skip to content

Commit

Permalink
support new TradeTag properties
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Aug 7, 2022
1 parent 6e137d6 commit b1d7517
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 11 deletions.
@@ -1,5 +1,7 @@
package com.denizenscript.denizen.objects;

import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.NMSVersion;
import com.denizenscript.denizencore.objects.*;
import com.denizenscript.denizencore.objects.properties.PropertyParser;
import com.denizenscript.denizencore.tags.Attribute;
Expand Down Expand Up @@ -73,10 +75,25 @@ public TradeTag(MerchantRecipe recipe) {
this.recipe = recipe;
}

public static MerchantRecipe duplicateRecipe(MerchantRecipe oldRecipe) {
return duplicateRecipe(oldRecipe.getResult(), oldRecipe);
}

public static MerchantRecipe duplicateRecipe(ItemStack result, MerchantRecipe oldRecipe) {
MerchantRecipe newRecipe;
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_18)) {
newRecipe = new MerchantRecipe(result, oldRecipe.getUses(), oldRecipe.getMaxUses(), oldRecipe.hasExperienceReward(), oldRecipe.getVillagerExperience(), oldRecipe.getPriceMultiplier(), oldRecipe.getDemand(), oldRecipe.getSpecialPrice());
}
else {
newRecipe = new MerchantRecipe(result, oldRecipe.getUses(), oldRecipe.getMaxUses(), oldRecipe.hasExperienceReward(), oldRecipe.getVillagerExperience(), oldRecipe.getPriceMultiplier());
}
newRecipe.setIngredients(oldRecipe.getIngredients());
return newRecipe;
}

@Override
public TradeTag duplicate() {
MerchantRecipe result = new MerchantRecipe(recipe.getResult(), recipe.getUses(), recipe.getMaxUses(), recipe.hasExperienceReward(), recipe.getVillagerExperience(), recipe.getPriceMultiplier());
result.setIngredients(recipe.getIngredients());
MerchantRecipe result = duplicateRecipe(recipe);
return new TradeTag(result);
}

Expand Down
Expand Up @@ -205,11 +205,17 @@ public static void registerMainProperties() {
PropertyParser.registerProperty(MaterialWaterlogged.class, MaterialTag.class);

// register core TradeTag properties
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_18)) {
PropertyParser.registerProperty(TradeDemand.class, TradeTag.class);
}
PropertyParser.registerProperty(TradeHasXp.class, TradeTag.class);
PropertyParser.registerProperty(TradeInputs.class, TradeTag.class);
PropertyParser.registerProperty(TradeMaxUses.class, TradeTag.class);
PropertyParser.registerProperty(TradePriceMultiplier.class, TradeTag.class);
PropertyParser.registerProperty(TradeResult.class, TradeTag.class);
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_18)) {
PropertyParser.registerProperty(TradeSpecialPrice.class, TradeTag.class);
}
PropertyParser.registerProperty(TradeUses.class, TradeTag.class);
PropertyParser.registerProperty(TradeVillagerXP.class, TradeTag.class);
}
Expand Down
@@ -0,0 +1,73 @@
package com.denizenscript.denizen.objects.properties.trade;

import com.denizenscript.denizen.objects.TradeTag;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.properties.Property;
import com.denizenscript.denizencore.objects.properties.PropertyParser;

public class TradeDemand implements Property {

public static boolean describes(ObjectTag recipe) {
return recipe instanceof TradeTag;
}

public static TradeDemand getFrom(ObjectTag recipe) {
if (!describes(recipe)) {
return null;
}
return new TradeDemand((TradeTag) recipe);
}

public static final String[] handledMechs = new String[] {
"demand"
};

private TradeTag recipe;

public TradeDemand(TradeTag recipe) {
this.recipe = recipe;
}

public String getPropertyString() {
if (recipe.getRecipe() == null) {
return null;
}
return String.valueOf(recipe.getRecipe().getDemand());
}

public String getPropertyId() {
return "demand";
}

public static void registerTags() {

// <--[tag]
// @attribute <TradeTag.demand>
// @returns ElementTag(Number)
// @mechanism TradeTag.demand
// @description
// Returns the demand level of the trade.
// -->
PropertyParser.<TradeDemand, ElementTag>registerTag(ElementTag.class, "demand", (attribute, recipe) -> {
return new ElementTag(recipe.recipe.getRecipe().getDemand());
});
}

public void adjust(Mechanism mechanism) {

// <--[mechanism]
// @object TradeTag
// @name demand
// @input ElementTag(Number)
// @description
// Sets the demand level of the trade.
// @tags
// <TradeTag.demand>
// -->
if (mechanism.matches("demand") && mechanism.requireInteger()) {
recipe.getRecipe().setDemand(mechanism.getValue().asInt());
}
}
}
Expand Up @@ -6,8 +6,6 @@
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.properties.Property;
import com.denizenscript.denizencore.objects.properties.PropertyParser;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.MerchantRecipe;

public class TradeResult implements Property {

Expand Down Expand Up @@ -69,13 +67,7 @@ public void adjust(Mechanism mechanism) {
// <TradeTag.result>
// -->
if (mechanism.matches("result") && mechanism.requireObject(ItemTag.class)) {
ItemStack item = mechanism.valueAsType(ItemTag.class).getItemStack();
MerchantRecipe oldRecipe = recipe.getRecipe();

MerchantRecipe newRecipe = new MerchantRecipe(item, oldRecipe.getUses(), oldRecipe.getMaxUses(), oldRecipe.hasExperienceReward());
newRecipe.setIngredients(oldRecipe.getIngredients());

recipe.setRecipe(newRecipe);
recipe.setRecipe(TradeTag.duplicateRecipe(mechanism.valueAsType(ItemTag.class).getItemStack(), recipe.getRecipe()));
}
}
}
@@ -0,0 +1,73 @@
package com.denizenscript.denizen.objects.properties.trade;

import com.denizenscript.denizen.objects.TradeTag;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.properties.Property;
import com.denizenscript.denizencore.objects.properties.PropertyParser;

public class TradeSpecialPrice implements Property {

public static boolean describes(ObjectTag recipe) {
return recipe instanceof TradeTag;
}

public static TradeSpecialPrice getFrom(ObjectTag recipe) {
if (!describes(recipe)) {
return null;
}
return new TradeSpecialPrice((TradeTag) recipe);
}

public static final String[] handledMechs = new String[] {
"special_price"
};

private TradeTag recipe;

public TradeSpecialPrice(TradeTag recipe) {
this.recipe = recipe;
}

public String getPropertyString() {
if (recipe.getRecipe() == null) {
return null;
}
return String.valueOf(recipe.getRecipe().getSpecialPrice());
}

public String getPropertyId() {
return "special_price";
}

public static void registerTags() {

// <--[tag]
// @attribute <TradeTag.special_price>
// @returns ElementTag(Number)
// @mechanism TradeTag.special_price
// @description
// Returns the special price for this trade.
// -->
PropertyParser.<TradeSpecialPrice, ElementTag>registerTag(ElementTag.class, "special_price", (attribute, recipe) -> {
return new ElementTag(recipe.recipe.getRecipe().getSpecialPrice());
});
}

public void adjust(Mechanism mechanism) {

// <--[mechanism]
// @object TradeTag
// @name special_price
// @input ElementTag(Number)
// @description
// Sets the special price for this trade.
// @tags
// <TradeTag.special_price>
// -->
if (mechanism.matches("special_price") && mechanism.requireInteger()) {
recipe.getRecipe().setSpecialPrice(mechanism.getValue().asInt());
}
}
}

0 comments on commit b1d7517

Please sign in to comment.