From ad541a262c68e965d8aca9d9dc118d85be62e447 Mon Sep 17 00:00:00 2001 From: Edith Boles Date: Thu, 30 Sep 2021 21:23:26 -0700 Subject: [PATCH] Add rituals --- .../java/top/penowl/quidproquo/Altar.java | 3 - .../top/penowl/quidproquo/Application.java | 22 --- .../java/top/penowl/quidproquo/Events.java | 136 +++++++++++++++++- .../top/penowl/quidproquo/QuidProQuo.java | 41 ++++++ .../java/top/penowl/quidproquo/Ritual.java | 12 +- .../quidproquo/rituals/BlankRitual.java | 3 +- .../quidproquo/rituals/FeedingRitual.java | 25 ++++ .../penowl/quidproquo/rituals/HealRitual.java | 9 +- .../quidproquo/rituals/WoolingRitual.java | 27 ++++ src/main/resources/plugin.yml | 2 +- 10 files changed, 246 insertions(+), 34 deletions(-) delete mode 100644 src/main/java/top/penowl/quidproquo/Application.java create mode 100644 src/main/java/top/penowl/quidproquo/QuidProQuo.java create mode 100644 src/main/java/top/penowl/quidproquo/rituals/FeedingRitual.java create mode 100644 src/main/java/top/penowl/quidproquo/rituals/WoolingRitual.java diff --git a/src/main/java/top/penowl/quidproquo/Altar.java b/src/main/java/top/penowl/quidproquo/Altar.java index 4f0f2c4..1820685 100644 --- a/src/main/java/top/penowl/quidproquo/Altar.java +++ b/src/main/java/top/penowl/quidproquo/Altar.java @@ -1,8 +1,5 @@ package top.penowl.quidproquo; -import com.avaje.ebean.LogLevel; -import com.sun.tools.sjavac.Log.Level; - import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; diff --git a/src/main/java/top/penowl/quidproquo/Application.java b/src/main/java/top/penowl/quidproquo/Application.java deleted file mode 100644 index 1782bf0..0000000 --- a/src/main/java/top/penowl/quidproquo/Application.java +++ /dev/null @@ -1,22 +0,0 @@ -package top.penowl.quidproquo; -import java.util.ArrayList; - -import org.bukkit.event.HandlerList; -import org.bukkit.plugin.java.JavaPlugin; - -public class Application extends JavaPlugin { - - public ArrayList rituals = new ArrayList(); - - @Override - public void onEnable() { - getLogger().info("Hello, SpigotMC!"); - getServer().getPluginManager().registerEvents(new Events(), this); - } - @Override - public void onDisable() { - getLogger().info("See you again, SpigotMC!"); - HandlerList.unregisterAll(this); - } - -} \ No newline at end of file diff --git a/src/main/java/top/penowl/quidproquo/Events.java b/src/main/java/top/penowl/quidproquo/Events.java index 8ad57e3..e0a1f3a 100644 --- a/src/main/java/top/penowl/quidproquo/Events.java +++ b/src/main/java/top/penowl/quidproquo/Events.java @@ -1,22 +1,152 @@ package top.penowl.quidproquo; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.stream.Collectors; + +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.Effect; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.Sound; +import org.bukkit.World; import org.bukkit.block.Block; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.Item; +import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.inventory.ItemStack; public final class Events implements Listener { @EventHandler public static void onPlayerInteract(PlayerInteractEvent event) { + + if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return; + Block block = event.getClickedBlock(); Player player = event.getPlayer(); + Location location = block.getLocation(); + + if (!Altar.CheckAltar(block.getLocation())) return; + + if (player.isSneaking()) { + + // change target - if (Altar.CheckAltar(block.getLocation())) { - player.sendMessage("you clicked an altar"); } else { - player.sendMessage("not an altar"); + + Boolean success = false; + + Collection items = block.getWorld().getEntitiesByClass(Item.class); + ArrayList near_items = new ArrayList(); + + for (Item item : items) { + if (item.getLocation().distance(location) < 2) { + near_items.add(item); + } + } + + Bukkit.getLogger().info(near_items.toString()); + + Collection sacrifices = block.getWorld().getEntitiesByClass(LivingEntity.class); + ArrayList near_sacrifices = new ArrayList(); + + for (LivingEntity sacrifice : sacrifices) { + if (sacrifice.getLocation().distance(location) < 2) { + near_sacrifices.add(sacrifice); + } + } + + for (Ritual ritual : QuidProQuo.instance.rituals) { + ArrayList> possibleItems = new ArrayList>(); + ArrayList> possibleSacrifices = new ArrayList>(); + Boolean failed = false; + ArrayList byproducts = new ArrayList(); + for (Map.Entry entry : ritual.ingredients.entrySet()) { + Material material = entry.getKey(); + int count = entry.getValue(); + + List matches = near_items.stream().filter(item -> item.getItemStack().getType() == material).collect(Collectors.toList()); + int matchCount = 0; + for (Item item : matches) { + matchCount += item.getItemStack().getAmount(); + } + if (matchCount >= count) { + possibleItems.add(matches); + byproducts.add(new ItemStack(material, matchCount - count)); + } else { + failed = true; + break; + } + } + for (Map.Entry entry : ritual.sacrifices.entrySet()) { + EntityType entityType = entry.getKey(); + int count = entry.getValue(); + + List matches = near_sacrifices.stream().filter(entity -> entity.getType() == entityType).limit(count).collect(Collectors.toList()); + if (matches.size() >= count) { + possibleSacrifices.add(matches); + } else { + failed = true; + break; + } + } + + if (failed) continue; + + for (List itemArray : possibleItems) { + for (Item item : itemArray) { + item.remove(); + } + } + + for (List sacrificeArray : possibleSacrifices) { + for (LivingEntity sacrifice : sacrificeArray) { + sacrifice.setHealth(0); + } + } + + player.setHealth(player.getHealth() - ritual.health); + + player.sendMessage(ChatColor.GREEN + "You enacted a " + ritual.name + " ritual."); + + for (ItemStack additionalByproduct : ritual.byproducts) { + byproducts.add(additionalByproduct); + } + + for (ItemStack byproduct : byproducts) { + block.getLocation().getWorld().dropItem(location.clone().add(0, 2, 0), byproduct); + } + + if(new Random().nextDouble() >= ritual.backfire) { + ritual.execute(player, null, block.getLocation()); + } else { + ritual.execute(null, player, block.getLocation()); + } + + player.playSound(player.getLocation(), Sound.EXPLODE, 50, 0); + player.getWorld().playEffect(location.clone().add(0, 1, 0), Effect.EXPLOSION_LARGE, 0); + player.getWorld().strikeLightningEffect(location.clone().add(0, 1, 0)); + + success = true; + + break; + + } + + if (!success) { + player.sendMessage(ChatColor.RED + "You do not have the nessecary sacrifices to enact a ritual!"); + } + } } diff --git a/src/main/java/top/penowl/quidproquo/QuidProQuo.java b/src/main/java/top/penowl/quidproquo/QuidProQuo.java new file mode 100644 index 0000000..27fc251 --- /dev/null +++ b/src/main/java/top/penowl/quidproquo/QuidProQuo.java @@ -0,0 +1,41 @@ +package top.penowl.quidproquo; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.UUID; + +import org.bukkit.event.HandlerList; +import org.bukkit.plugin.java.JavaPlugin; + +import top.penowl.quidproquo.rituals.FeedingRitual; +import top.penowl.quidproquo.rituals.HealRitual; +import top.penowl.quidproquo.rituals.WoolingRitual; + +public class QuidProQuo extends JavaPlugin { + + public static QuidProQuo instance; + public ArrayList rituals = new ArrayList(); + public HashMap targets = new HashMap(); + + @Override + public void onEnable() { + instance = this; + getLogger().info("Hello, SpigotMC!"); + getServer().getPluginManager().registerEvents(new Events(), this); + + rituals.add(new HealRitual()); + rituals.add(new WoolingRitual()); + rituals.add(new FeedingRitual()); + + for (Ritual ritual : rituals) { + getLogger().info("Loading " + ritual.getClass().toString() + "..."); + ritual.setup(); + getLogger().info("Loaded a " + ritual.name + " ritual."); + } + } + @Override + public void onDisable() { + getLogger().info("See you again, SpigotMC!"); + HandlerList.unregisterAll(this); + } + +} \ No newline at end of file diff --git a/src/main/java/top/penowl/quidproquo/Ritual.java b/src/main/java/top/penowl/quidproquo/Ritual.java index 432565c..c19a15a 100644 --- a/src/main/java/top/penowl/quidproquo/Ritual.java +++ b/src/main/java/top/penowl/quidproquo/Ritual.java @@ -1,21 +1,31 @@ package top.penowl.quidproquo; +import java.util.ArrayList; import java.util.HashMap; +import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; public abstract class Ritual { public HashMap ingredients = new HashMap(); public HashMap sacrifices = new HashMap(); + public ArrayList byproducts = new ArrayList(); public int health = 0; - public abstract void execute(Player caster, Player target); + public double backfire = 0.0; + public String name = "unnamed"; + + public abstract void execute(Player caster, Player target, Location location); + public void addIngredient(Material material, int count) { ingredients.put(material, count); } + public void addSacrifice(EntityType type, int count) { sacrifices.put(type, count); } + public abstract void setup(); } \ No newline at end of file diff --git a/src/main/java/top/penowl/quidproquo/rituals/BlankRitual.java b/src/main/java/top/penowl/quidproquo/rituals/BlankRitual.java index f315630..f0caef7 100644 --- a/src/main/java/top/penowl/quidproquo/rituals/BlankRitual.java +++ b/src/main/java/top/penowl/quidproquo/rituals/BlankRitual.java @@ -1,5 +1,6 @@ package top.penowl.quidproquo.rituals; +import org.bukkit.Location; import org.bukkit.entity.Player; import top.penowl.quidproquo.Ritual; @@ -12,7 +13,7 @@ public class BlankRitual extends Ritual { } @Override - public void execute(Player caster, Player target) { + public void execute(Player caster, Player target, Location location) { } diff --git a/src/main/java/top/penowl/quidproquo/rituals/FeedingRitual.java b/src/main/java/top/penowl/quidproquo/rituals/FeedingRitual.java new file mode 100644 index 0000000..a403e23 --- /dev/null +++ b/src/main/java/top/penowl/quidproquo/rituals/FeedingRitual.java @@ -0,0 +1,25 @@ +package top.penowl.quidproquo.rituals; + +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.Player; + +import top.penowl.quidproquo.Ritual; + +public class FeedingRitual extends Ritual { + + @Override + public void setup() { + addSacrifice(EntityType.PIG, 1); + addIngredient(Material.WOOD_SWORD, 1); + health = 4; + name = "feeding"; + } + + @Override + public void execute(Player caster, Player target, Location location) { + caster.setFoodLevel(20); + } + +} diff --git a/src/main/java/top/penowl/quidproquo/rituals/HealRitual.java b/src/main/java/top/penowl/quidproquo/rituals/HealRitual.java index 769a852..c280370 100644 --- a/src/main/java/top/penowl/quidproquo/rituals/HealRitual.java +++ b/src/main/java/top/penowl/quidproquo/rituals/HealRitual.java @@ -1,5 +1,7 @@ package top.penowl.quidproquo.rituals; +import org.bukkit.Location; +import org.bukkit.Material; import org.bukkit.entity.Player; import top.penowl.quidproquo.Ritual; @@ -8,12 +10,13 @@ public class HealRitual extends Ritual { @Override public void setup() { - + addIngredient(Material.REDSTONE, 9); + name = "healing"; } @Override - public void execute(Player caster, Player target) { - + public void execute(Player caster, Player target, Location location) { + caster.setHealth(Math.min(caster.getMaxHealth(), caster.getHealth()+2.0)); } } diff --git a/src/main/java/top/penowl/quidproquo/rituals/WoolingRitual.java b/src/main/java/top/penowl/quidproquo/rituals/WoolingRitual.java new file mode 100644 index 0000000..6c31022 --- /dev/null +++ b/src/main/java/top/penowl/quidproquo/rituals/WoolingRitual.java @@ -0,0 +1,27 @@ +package top.penowl.quidproquo.rituals; + +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; + +import top.penowl.quidproquo.Ritual; + +public class WoolingRitual extends Ritual { + + @Override + public void setup() { + addIngredient(Material.IRON_SWORD, 1); + addSacrifice(EntityType.SHEEP, 1); + name = "wooling"; + health = 2; + byproducts.add(new ItemStack(Material.WOOL, 100)); + } + + @Override + public void execute(Player caster, Player target, Location location) { + caster.setHealth(Math.min(caster.getMaxHealth(), caster.getHealth()+2.0)); + } + +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 8518d72..8affbef 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,3 +1,3 @@ -main: top.penowl.quidproquo.Application +main: top.penowl.quidproquo.QuidProQuo name: QuidProQuo version: 0.1 \ No newline at end of file