Skip to content

Commit

Permalink
Main plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonardoRRC authored Aug 3, 2023
0 parents commit 58bd3dd
Show file tree
Hide file tree
Showing 6 changed files with 251 additions and 0 deletions.
13 changes: 13 additions & 0 deletions TimberCore.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="FacetManager">
<facet type="minecraft" name="Minecraft">
<configuration>
<autoDetectTypes>
<platformType>PAPER</platformType>
<platformType>ADVENTURE</platformType>
</autoDetectTypes>
</configuration>
</facet>
</component>
</module>
74 changes: 74 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>me.theboykiss.ovh</groupId>
<artifactId>TimberCore</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>TimberCore</name>

<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>

<repositories>
<repository>
<id>papermc-repo</id>
<url>https://repo.papermc.io/repository/maven-public/</url>
</repository>
<repository>
<id>sonatype</id>
<url>https://oss.sonatype.org/content/groups/public/</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>io.papermc.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.20.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
19 changes: 19 additions & 0 deletions src/main/java/me/theboykiss/ovh/timbercore/TimberCore.java
Original file line number Diff line number Diff line change
@@ -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
}
}
107 changes: 107 additions & 0 deletions src/main/java/me/theboykiss/ovh/timbercore/TreeFeller.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
32 changes: 32 additions & 0 deletions src/main/java/me/theboykiss/ovh/timbercore/TreeFellerCommand.java
Original file line number Diff line number Diff line change
@@ -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<UUID, Boolean> 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);
}
}
6 changes: 6 additions & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: TimberCore
version: '${project.version}'
main: me.theboykiss.ovh.timbercore.TimberCore
api-version: '1.20'
commands:
autofell:

0 comments on commit 58bd3dd

Please sign in to comment.