From 6ae069f651a2c31782b65f084faf4390e2393a38 Mon Sep 17 00:00:00 2001 From: Jules Date: Sat, 11 Apr 2020 12:39:55 +0200 Subject: [PATCH] !API stuff for loot chests --- .../Indyuce/mmocore/api/loot/ChestTier.java | 48 ++++++++++++++++ .../Indyuce/mmocore/api/loot/LootChest.java | 45 +++++++++++++++ .../mmocore/api/loot/LootChestRegion.java | 55 +++++++++++++++++++ .../mmocore/api/loot/RegionBounds.java | 53 ++++++++++++++++++ 4 files changed, 201 insertions(+) create mode 100644 src/main/java/net/Indyuce/mmocore/api/loot/ChestTier.java create mode 100644 src/main/java/net/Indyuce/mmocore/api/loot/LootChest.java create mode 100644 src/main/java/net/Indyuce/mmocore/api/loot/LootChestRegion.java create mode 100644 src/main/java/net/Indyuce/mmocore/api/loot/RegionBounds.java diff --git a/src/main/java/net/Indyuce/mmocore/api/loot/ChestTier.java b/src/main/java/net/Indyuce/mmocore/api/loot/ChestTier.java new file mode 100644 index 00000000..364be184 --- /dev/null +++ b/src/main/java/net/Indyuce/mmocore/api/loot/ChestTier.java @@ -0,0 +1,48 @@ +package net.Indyuce.mmocore.api.loot; + +import org.apache.commons.lang.Validate; +import org.bukkit.Location; +import org.bukkit.Particle; +import org.bukkit.configuration.ConfigurationSection; + +import net.Indyuce.mmocore.api.util.math.particle.ChestParticleEffect; + +public class ChestTier { + private final TierEffect effect; + private final int weight; + + public ChestTier(ConfigurationSection config) { + effect = config.isConfigurationSection("effect") ? new TierEffect(config.getConfigurationSection("effect")) + : null; + weight = config.getInt("weight", 1); + } + + public int getWeight() { + return weight; + } + + public boolean hasEffect() { + return effect != null; + } + + public TierEffect getEffect() { + return effect; + } + + public class TierEffect { + private final ChestParticleEffect type; + private final Particle particle; + + public TierEffect(ConfigurationSection config) { + Validate.notNull(config, "Could not load tier config"); + type = ChestParticleEffect + .valueOf(config.getString("type", "OFFSET").toUpperCase().replace("-", "_").replace(" ", "_")); + particle = Particle + .valueOf(config.getString("particle", "FLAME").toUpperCase().replace("-", "_").replace(" ", "_")); + } + + public void play(Location loc) { + type.play(loc, particle); + } + } +} diff --git a/src/main/java/net/Indyuce/mmocore/api/loot/LootChest.java b/src/main/java/net/Indyuce/mmocore/api/loot/LootChest.java new file mode 100644 index 00000000..41ba5119 --- /dev/null +++ b/src/main/java/net/Indyuce/mmocore/api/loot/LootChest.java @@ -0,0 +1,45 @@ +package net.Indyuce.mmocore.api.loot; + +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.block.data.BlockData; + +public class LootChest { + private final LootChestRegion region; + + /* + * saves data of block replaced + */ + private final ReplacedBlock block; + + public LootChest(LootChestRegion region, Block block) { + this.region = region; + this.block = new ReplacedBlock(block); + } + + public ReplacedBlock getBlock() { + return block; + } + + public LootChestRegion getRegion() { + return region; + } + + public class ReplacedBlock { + private final Material material; + private final BlockData data; + private final Location loc; + + public ReplacedBlock(Block block) { + this.material = block.getType(); + this.data = block.getBlockData(); + loc = block.getLocation(); + } + + public void restore() { + loc.getBlock().setType(material); + loc.getBlock().setBlockData(data); + } + } +} diff --git a/src/main/java/net/Indyuce/mmocore/api/loot/LootChestRegion.java b/src/main/java/net/Indyuce/mmocore/api/loot/LootChestRegion.java new file mode 100644 index 00000000..5d078160 --- /dev/null +++ b/src/main/java/net/Indyuce/mmocore/api/loot/LootChestRegion.java @@ -0,0 +1,55 @@ +package net.Indyuce.mmocore.api.loot; + +import java.util.HashSet; +import java.util.Set; +import java.util.logging.Level; + +import org.apache.commons.lang.Validate; +import org.bukkit.configuration.ConfigurationSection; + +import net.Indyuce.mmocore.MMOCore; + +public class LootChestRegion { + private final String id; + + private final long chestSpawnPeriod; + private final RegionBounds bounds; + private final Set tiers = new HashSet<>(); + + /* + * last time + */ + + public LootChestRegion(ConfigurationSection config) { + Validate.notNull(config, "Could not load config"); + id = config.getName(); + + bounds = new RegionBounds(config.getConfigurationSection("bounds")); + chestSpawnPeriod = config.getInt("spawn-period"); + + Validate.isTrue(config.isConfigurationSection("tiers"), "Could not find chest tiers"); + for (String key : config.getConfigurationSection("tiers").getKeys(false)) + try { + tiers.add(new ChestTier(config.getConfigurationSection("tiers." + key))); + } catch (IllegalArgumentException exception) { + MMOCore.plugin.getLogger().log(Level.WARNING, + "Could not load tier '" + key + "' from chest region '" + id + "': " + exception.getMessage()); + } + } + + public String getId() { + return id; + } + + public Set getTiers() { + return tiers; + } + + public RegionBounds getBounds() { + return bounds; + } + + public long getChestSpawnPeriod() { + return chestSpawnPeriod; + } +} diff --git a/src/main/java/net/Indyuce/mmocore/api/loot/RegionBounds.java b/src/main/java/net/Indyuce/mmocore/api/loot/RegionBounds.java new file mode 100644 index 00000000..a59871de --- /dev/null +++ b/src/main/java/net/Indyuce/mmocore/api/loot/RegionBounds.java @@ -0,0 +1,53 @@ +package net.Indyuce.mmocore.api.loot; + +import java.util.Optional; +import java.util.Random; + +import org.apache.commons.lang.Validate; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.entity.Player; + +public class RegionBounds { + private final World world; + private final int x1, z1, x2, z2; + + private static final Random random = new Random(); + + public RegionBounds(ConfigurationSection config) { + Validate.notNull(config, "Could not load config"); + Validate.notNull(world = Bukkit.getWorld(config.getString("world")), + "Could not find world " + config.getString("world")); + x1 = config.getInt("x1"); + z1 = config.getInt("z1"); + + x2 = config.getInt("x2"); + z2 = config.getInt("z2"); + } + + public RegionBounds(Location loc1, Location loc2) { + Validate.isTrue(loc1.getWorld().equals(loc2.getWorld()), "Locations must be in the same world"); + world = loc1.getWorld(); + x1 = loc1.getBlockX(); + z1 = loc1.getBlockZ(); + + x2 = loc2.getBlockX(); + z2 = loc2.getBlockZ(); + } + + public boolean isInRegion(Player player) { + int x = player.getLocation().getBlockX(); + int z = player.getLocation().getBlockZ(); + return player.getWorld().equals(world) && x1 <= x && x2 >= x && z1 <= z && z2 >= z; + } + + public Location findChestLocation() { + + Optional player = world.getPlayers().stream().filter(check -> isInRegion(check)).findAny(); + + // TODO + return null; + } +}