From 17f83bd9f27a24e61eb3413b595770a02f9ed593 Mon Sep 17 00:00:00 2001 From: Paldiu Date: Fri, 20 May 2022 15:41:34 -0500 Subject: [PATCH] FeelingLucky v1.0 RELEASE --- build.gradle | 2 +- .../io/github/simplex/luck/FeelingLucky.java | 5 +- .../simplex/luck/listener/GiveDamage.java | 28 ++++++++++ .../github/simplex/luck/listener/OreVein.java | 51 +++++++++++++++++++ .../simplex/luck/player/PlayerConfig.java | 2 +- .../simplex/luck/util/SneakyWorker.java | 6 +-- 6 files changed, 88 insertions(+), 6 deletions(-) create mode 100644 src/main/java/io/github/simplex/luck/listener/GiveDamage.java create mode 100644 src/main/java/io/github/simplex/luck/listener/OreVein.java diff --git a/build.gradle b/build.gradle index 0ce9eb5..168ddb2 100644 --- a/build.gradle +++ b/build.gradle @@ -3,7 +3,7 @@ plugins { } group = 'io.github.simplex' -version = 'Beta-1.0.1' +version = 'Beta-1.0.2' repositories { mavenCentral() diff --git a/src/main/java/io/github/simplex/luck/FeelingLucky.java b/src/main/java/io/github/simplex/luck/FeelingLucky.java index afea16e..12bb354 100644 --- a/src/main/java/io/github/simplex/luck/FeelingLucky.java +++ b/src/main/java/io/github/simplex/luck/FeelingLucky.java @@ -59,14 +59,17 @@ public final class FeelingLucky extends JavaPlugin { private void loadPlayerConfigurations() { if (!playerDirectory.exists()) { + getLogger().info("No directory exists. Creating..."); playerDirectory.mkdirs(); + getLogger().info("Created new directory \"FeelingLucky/players\"."); + return; } File[] files = playerDirectory.listFiles(); if (files != null) { Arrays.stream(files).forEach(file -> { UUID uuid = UUID.fromString(file.getName().split("\\.")[0]); - configMap.put(uuid, PlayerConfig.loadFrom(this, file)); + configMap.put(uuid, PlayerConfig.initFrom(this, file)); }); configMap.forEach((u, pc) -> pc.load()); getLogger().info("Successfully loaded all configurations!"); diff --git a/src/main/java/io/github/simplex/luck/listener/GiveDamage.java b/src/main/java/io/github/simplex/luck/listener/GiveDamage.java new file mode 100644 index 0000000..d85b42c --- /dev/null +++ b/src/main/java/io/github/simplex/luck/listener/GiveDamage.java @@ -0,0 +1,28 @@ +package io.github.simplex.luck.listener; + +import io.github.simplex.lib.MiniComponent; +import io.github.simplex.luck.FeelingLucky; +import io.github.simplex.luck.player.Luck; +import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.entity.EntityDamageByEntityEvent; + +public class GiveDamage extends AbstractListener { + public GiveDamage(FeelingLucky plugin) { + super(plugin); + } + + @EventHandler + public void playerAttack(EntityDamageByEntityEvent e) { + if ((e.getDamager() instanceof Player player) + && (e.getEntity() instanceof LivingEntity)) { + double nextDmg = e.getDamage() + Luck.RNG().nextDouble(1.0, 5.0); + Luck luck = plugin.getHandler().getLuckContainer(player); + if (luck.quickRNG(luck.getPercentage())) { + e.setDamage(nextDmg); + player.sendMessage(MiniComponent.info("Your luck has increased your damage output!")); + } + } + } +} diff --git a/src/main/java/io/github/simplex/luck/listener/OreVein.java b/src/main/java/io/github/simplex/luck/listener/OreVein.java new file mode 100644 index 0000000..63fdee8 --- /dev/null +++ b/src/main/java/io/github/simplex/luck/listener/OreVein.java @@ -0,0 +1,51 @@ +package io.github.simplex.luck.listener; + +import io.github.simplex.lib.MiniComponent; +import io.github.simplex.luck.FeelingLucky; +import io.github.simplex.luck.player.Luck; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.Tag; +import org.bukkit.World; +import org.bukkit.block.Block; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.block.BlockBreakEvent; + +import java.util.stream.Stream; + +public class OreVein extends AbstractListener { + + public OreVein(FeelingLucky plugin) { + super(plugin); + } + + @EventHandler + public void playerMine(BlockBreakEvent event) { + Player player = event.getPlayer(); + Luck luck = plugin.getHandler().getLuckContainer(player); + if (luck.quickRNG(luck.getPercentage()) && event.getBlock().isValidTool(player.getInventory().getItemInMainHand())) { + getOresInArea(event.getBlock()).forEach(Block::breakNaturally); + player.sendMessage(MiniComponent.info("Your luck has let you mine all the blocks with one swing.")); + } + } + + public Stream getOresInArea(Block block) { + Stream.Builder streamBuilder = Stream.builder(); + Location start = block.getLocation(); + World world = block.getWorld(); + Stream> materialStream = Stream.of(Tag.COAL_ORES, Tag.COPPER_ORES, Tag.DIAMOND_ORES, Tag.GOLD_ORES, Tag.IRON_ORES, Tag.EMERALD_ORES, Tag.LAPIS_ORES, Tag.REDSTONE_ORES); + for (int x = start.getBlockX() - 15; x <= start.getBlockX() + 15; x++) { + for (int y = start.getBlockY() - 15; y <= start.getBlockY() + 15; y++) { + for (int z = start.getBlockZ() - 15; z <= start.getBlockZ() + 15; z++) { + Location location = new Location(world, x, y, z); + Material blockType = location.getBlock().getType(); + if (materialStream.anyMatch(o -> o.isTagged(blockType))) { + streamBuilder.add(location.getBlock()); + } + } + } + } + return streamBuilder.build(); + } +} diff --git a/src/main/java/io/github/simplex/luck/player/PlayerConfig.java b/src/main/java/io/github/simplex/luck/player/PlayerConfig.java index 59bc2dd..c044104 100644 --- a/src/main/java/io/github/simplex/luck/player/PlayerConfig.java +++ b/src/main/java/io/github/simplex/luck/player/PlayerConfig.java @@ -64,7 +64,7 @@ public class PlayerConfig { } @Contract("_, _ -> new") - public static PlayerConfig loadFrom(FeelingLucky plugin, File file) { + public static PlayerConfig initFrom(FeelingLucky plugin, File file) { return new PlayerConfig(plugin, file); } diff --git a/src/main/java/io/github/simplex/luck/util/SneakyWorker.java b/src/main/java/io/github/simplex/luck/util/SneakyWorker.java index c04110f..a74e601 100644 --- a/src/main/java/io/github/simplex/luck/util/SneakyWorker.java +++ b/src/main/java/io/github/simplex/luck/util/SneakyWorker.java @@ -33,7 +33,7 @@ public class SneakyWorker { } } - public static Class[] getClasses(String packageName) throws ClassNotFoundException, IOException { + public static Class[] getClasses(String packageName) throws ClassNotFoundException, IOException { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); assert classLoader != null; String path = packageName.replace(".", "/"); @@ -43,11 +43,11 @@ public class SneakyWorker { URL resource = resources.nextElement(); dirs.add(new File(resource.getFile())); } - ArrayList classes = new ArrayList<>(); + ArrayList> classes = new ArrayList<>(); for (File directory : dirs) { classes.addAll(findClasses(directory, packageName)); } - return classes.toArray(new Class[classes.size()]); + return classes.toArray(new Class[0]); } private static List> findClasses(File directory, String packageName) throws ClassNotFoundException {