3 changed files with 91 additions and 0 deletions

No files matched your search

@@ -0,0 +1,32 @@
package eu.mhsl.craftattack.spawn.craftattack.appliances.gameplay.wanderingTrader;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.MerchantRecipe;
import java.util.Objects;
import java.util.function.Supplier;
record TraderOffer(Supplier<ItemStack> resultFactory, int emeraldPrice, int maxUses, double chance) {
TraderOffer {
Objects.requireNonNull(resultFactory, "resultFactory");
if(emeraldPrice < 1 || emeraldPrice > 64)
throw new IllegalArgumentException("emeraldPrice must be between 1 and 64");
if(maxUses < 1)
throw new IllegalArgumentException("maxUses must be positive");
if(!Double.isFinite(chance) || chance < 0 || chance > 1)
throw new IllegalArgumentException("chance must be finite and between 0 and 1");
}
static TraderOffer offer(Material material, int emeraldPrice, int maxUses, double chance) {
Objects.requireNonNull(material, "material");
return new TraderOffer(() -> new ItemStack(material), emeraldPrice, maxUses, chance);
}
MerchantRecipe createRecipe() {
ItemStack result = Objects.requireNonNull(this.resultFactory.get(), "resultFactory result").clone();
MerchantRecipe recipe = new MerchantRecipe(result, 0, this.maxUses, false, 0, 0f);
recipe.addIngredient(new ItemStack(Material.EMERALD, this.emeraldPrice));
return recipe;
}
}
@@ -0,0 +1,19 @@
package eu.mhsl.craftattack.spawn.craftattack.appliances.gameplay.wanderingTrader;
import eu.mhsl.craftattack.spawn.core.Main;
import eu.mhsl.craftattack.spawn.core.appliance.ApplianceListener;
import org.bukkit.Bukkit;
import org.bukkit.entity.WanderingTrader;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.entity.CreatureSpawnEvent;
class WanderingTraderSpawnListener extends ApplianceListener<WanderingTraderTrades> {
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onSpawn(CreatureSpawnEvent event) {
if(!(event.getEntity() instanceof WanderingTrader trader)) return;
// Add custom trades on the next tick, after the spawn has finished.
Bukkit.getScheduler().runTask(Main.instance(), () -> this.getAppliance().addTrades(trader));
}
}
@@ -0,0 +1,40 @@
package eu.mhsl.craftattack.spawn.craftattack.appliances.gameplay.wanderingTrader;
import eu.mhsl.craftattack.spawn.core.Main;
import eu.mhsl.craftattack.spawn.core.appliance.Appliance;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.WanderingTrader;
import org.bukkit.event.Listener;
import org.bukkit.inventory.MerchantRecipe;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
public class WanderingTraderTrades extends Appliance {
private final NamespacedKey appliedKey = new NamespacedKey(Main.instance(), "wandering_trader_trades_applied");
private final List<TraderOffer> additionalTrades = List.of(
TraderOffer.offer(Material.HEART_OF_THE_SEA, 32, 1, 0.5)
);
void addTrades(WanderingTrader trader) {
if(!trader.isValid() || trader.isDead()) return;
if(trader.getPersistentDataContainer().has(this.appliedKey, PersistentDataType.BYTE)) return;
List<MerchantRecipe> recipes = new ArrayList<>(trader.getRecipes());
this.additionalTrades.forEach(offer -> {
if(ThreadLocalRandom.current().nextDouble() < offer.chance()) recipes.add(offer.createRecipe());
});
trader.setRecipes(recipes);
// Mark this trader as having had custom trades applied.
trader.getPersistentDataContainer().set(this.appliedKey, PersistentDataType.BYTE, (byte) 1);
}
@Override
protected @NotNull List<Listener> listeners() {
return List.of(new WanderingTraderSpawnListener());
}
}