From 58bd3ddc6318709f7eed21d5ca6e83fca0d1a83f Mon Sep 17 00:00:00 2001
From: Leonardo Reynoso <128936819+LeonardoRRC@users.noreply.github.com>
Date: Thu, 3 Aug 2023 11:47:27 -0500
Subject: [PATCH] Main plugin
---
TimberCore.iml | 13 +++
pom.xml | 74 ++++++++++++
.../theboykiss/ovh/timbercore/TimberCore.java | 19 ++++
.../theboykiss/ovh/timbercore/TreeFeller.java | 107 ++++++++++++++++++
.../ovh/timbercore/TreeFellerCommand.java | 32 ++++++
src/main/resources/plugin.yml | 6 +
6 files changed, 251 insertions(+)
create mode 100644 TimberCore.iml
create mode 100644 pom.xml
create mode 100644 src/main/java/me/theboykiss/ovh/timbercore/TimberCore.java
create mode 100644 src/main/java/me/theboykiss/ovh/timbercore/TreeFeller.java
create mode 100644 src/main/java/me/theboykiss/ovh/timbercore/TreeFellerCommand.java
create mode 100644 src/main/resources/plugin.yml
diff --git a/TimberCore.iml b/TimberCore.iml
new file mode 100644
index 0000000..23640fa
--- /dev/null
+++ b/TimberCore.iml
@@ -0,0 +1,13 @@
+
+
+
+
+
+
+ PAPER
+ ADVENTURE
+
+
+
+
+
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..2058164
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,74 @@
+
+
+ 4.0.0
+
+ me.theboykiss.ovh
+ TimberCore
+ 1.0-SNAPSHOT
+ jar
+
+ TimberCore
+
+
+ 1.8
+ UTF-8
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+ 3.8.1
+
+
+ ${java.version}
+
+
+
+ org.apache.maven.plugins
+ maven-shade-plugin
+ 3.2.4
+
+
+ package
+
+ shade
+
+
+ false
+
+
+
+
+
+
+
+ src/main/resources
+ true
+
+
+
+
+
+
+ papermc-repo
+ https://repo.papermc.io/repository/maven-public/
+
+
+ sonatype
+ https://oss.sonatype.org/content/groups/public/
+
+
+
+
+
+ io.papermc.paper
+ paper-api
+ 1.20.1-R0.1-SNAPSHOT
+ provided
+
+
+
diff --git a/src/main/java/me/theboykiss/ovh/timbercore/TimberCore.java b/src/main/java/me/theboykiss/ovh/timbercore/TimberCore.java
new file mode 100644
index 0000000..cd664cc
--- /dev/null
+++ b/src/main/java/me/theboykiss/ovh/timbercore/TimberCore.java
@@ -0,0 +1,19 @@
+package me.theboykiss.ovh.timbercore;
+
+import org.bukkit.plugin.java.JavaPlugin;
+
+public final class TimberCore extends JavaPlugin {
+
+ @Override
+ public void onEnable() {
+ TreeFellerCommand treeFellerCommand = new TreeFellerCommand();
+ getCommand("autofell").setExecutor(treeFellerCommand);
+ getServer().getPluginManager().registerEvents(new TreeFeller(this, treeFellerCommand), this);
+
+ }
+
+ @Override
+ public void onDisable() {
+ // Plugin shutdown logic
+ }
+}
diff --git a/src/main/java/me/theboykiss/ovh/timbercore/TreeFeller.java b/src/main/java/me/theboykiss/ovh/timbercore/TreeFeller.java
new file mode 100644
index 0000000..2dfe7e5
--- /dev/null
+++ b/src/main/java/me/theboykiss/ovh/timbercore/TreeFeller.java
@@ -0,0 +1,107 @@
+package me.theboykiss.ovh.timbercore;
+
+import org.bukkit.Bukkit;
+import org.bukkit.GameMode;
+import org.bukkit.Material;
+import org.bukkit.block.Block;
+import org.bukkit.enchantments.Enchantment;
+import org.bukkit.entity.Player;
+import org.bukkit.event.EventHandler;
+import org.bukkit.event.Listener;
+import org.bukkit.event.block.BlockBreakEvent;
+import org.bukkit.inventory.ItemStack;
+import org.bukkit.inventory.meta.Damageable;
+import org.bukkit.inventory.meta.ItemMeta;
+import org.bukkit.plugin.java.JavaPlugin;
+
+import java.util.Random;
+
+public class TreeFeller implements Listener {
+ private final JavaPlugin plugin;
+ private final Random random = new Random();
+ private final TreeFellerCommand treeFellerCommand;
+
+ public TreeFeller(JavaPlugin plugin, TreeFellerCommand treeFellerCommand) {
+ this.plugin = plugin;
+ this.treeFellerCommand = treeFellerCommand;
+ }
+
+ @EventHandler
+ public void onBlockBreak(BlockBreakEvent event) {
+ Block block = event.getBlock();
+ Player player = event.getPlayer();
+ ItemStack item = player.getInventory().getItemInMainHand();
+ if (isLog(block.getType()) || isLeaves(block.getType())) {
+ if (player.hasPermission("treefeller.use") && player.getGameMode() == GameMode.SURVIVAL && isAxe(item.getType()) && treeFellerCommand.isAutoFellingEnabled(player.getUniqueId())) {
+ fellTree(block);
+ // Desgasta el hacha
+ damageItem(player, item);
+ }
+ }
+ }
+
+ private void fellTree(Block block) {
+ // Si el bloque es de madera, rompe y repite
+ if (isLog(block.getType())) {
+ block.breakNaturally();
+ // Comprueba los bloques directamente encima y a los lados del bloque actual
+ for (int x = -1; x <= 1; x++) {
+ for (int y = 0; y <= 1; y++) {
+ for (int z = -1; z <= 1; z++) {
+ // Ignora el bloque actual
+ if (x != 0 || y != 0 || z != 0) {
+ Block relative = block.getRelative(x, y, z);
+ fellTree(relative);
+ }
+ }
+ }
+ }
+ }
+ else if (isLeaves(block.getType())) {
+ if (random.nextInt(100) < 1) {
+ block.getWorld().dropItemNaturally(block.getLocation(), new ItemStack(Material.APPLE));
+ }
+ block.breakNaturally();
+ }
+ }
+
+
+ private void damageItem(Player player, ItemStack item) {
+ int unbreakingLevel = item.getEnchantmentLevel(Enchantment.DURABILITY);
+ double damagePercent = 5.0;
+ switch (unbreakingLevel) {
+ case 1:
+ damagePercent = 3.0;
+ break;
+ case 2:
+ damagePercent = 2.0;
+ break;
+ case 3:
+ damagePercent = 1.0;
+ break;
+ }
+ ItemMeta meta = item.getItemMeta();
+ if (meta instanceof Damageable) {
+ Damageable damageable = (Damageable) meta;
+ int damage = (int) (item.getType().getMaxDurability() * damagePercent / 100);
+ damageable.setDamage(damageable.getDamage() + damage);
+ item.setItemMeta(meta);
+ if (damageable.getDamage() > item.getType().getMaxDurability()) {
+ player.getInventory().remove(item);
+ player.getWorld().playSound(player.getLocation(), "entity.item.break", 1.0f, 1.0f);
+ }
+ }
+ }
+
+ private boolean isLog(Material material) {
+ return material == Material.OAK_LOG || material == Material.BIRCH_LOG || material == Material.SPRUCE_LOG || material == Material.JUNGLE_LOG || material == Material.ACACIA_LOG || material == Material.DARK_OAK_LOG;
+ }
+
+ private boolean isLeaves(Material material) {
+ return material == Material.OAK_LEAVES || material == Material.BIRCH_LEAVES || material == Material.SPRUCE_LEAVES || material == Material.JUNGLE_LEAVES || material == Material.ACACIA_LEAVES || material == Material.DARK_OAK_LEAVES;
+ }
+
+ private boolean isAxe(Material material) {
+ return material == Material.WOODEN_AXE || material == Material.STONE_AXE || material == Material.IRON_AXE || material == Material.DIAMOND_AXE || material == Material.GOLDEN_AXE;
+ }
+}
diff --git a/src/main/java/me/theboykiss/ovh/timbercore/TreeFellerCommand.java b/src/main/java/me/theboykiss/ovh/timbercore/TreeFellerCommand.java
new file mode 100644
index 0000000..39d3fbd
--- /dev/null
+++ b/src/main/java/me/theboykiss/ovh/timbercore/TreeFellerCommand.java
@@ -0,0 +1,32 @@
+package me.theboykiss.ovh.timbercore;
+
+import org.bukkit.command.Command;
+import org.bukkit.command.CommandExecutor;
+import org.bukkit.command.CommandSender;
+import org.bukkit.entity.Player;
+
+import java.util.HashMap;
+import java.util.UUID;
+
+public class TreeFellerCommand implements CommandExecutor {
+ private final HashMap autoFellStates = new HashMap<>();
+
+ @Override
+ public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
+ if (sender instanceof Player) {
+ Player player = (Player) sender;
+ UUID playerId = player.getUniqueId();
+ boolean newState = !autoFellStates.getOrDefault(playerId, true);
+ autoFellStates.put(playerId, newState);
+ player.sendMessage("El talado automatico esta " + (newState ? "Activado" : "Desactivado") + ".");
+ return true;
+ } else {
+ sender.sendMessage("Este comando solo puede ser ejecutado por jugadores.");
+ return false;
+ }
+ }
+
+ public boolean isAutoFellingEnabled(UUID playerId) {
+ return autoFellStates.getOrDefault(playerId, true);
+ }
+}
diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml
new file mode 100644
index 0000000..6df67b3
--- /dev/null
+++ b/src/main/resources/plugin.yml
@@ -0,0 +1,6 @@
+name: TimberCore
+version: '${project.version}'
+main: me.theboykiss.ovh.timbercore.TimberCore
+api-version: '1.20'
+commands:
+ autofell: