Files
src
build.gradle
manifest.kod
Code
5 589
src/main/java/org/sayu/treefeller/TreeFeller.java
package org.sayu.treefeller;
import org.bukkit.Bukkit;
import org.bukkit.Particle;
import org.bukkit.Sound;
import org.bukkit.Tag;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
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.event.player.PlayerToggleSneakEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.plugin.java.JavaPlugin;
import com.tcoded.folialib.FoliaLib;
import com.tcoded.folialib.wrapper.task.WrappedTask;
import java.util.*;
public class TreeFeller extends JavaPlugin implements Listener {
private static final int MAX_BLOCKS = 800;
private static final long ANIMATION_DELAY = 40L;
private FoliaLib foliaLib;
final Set<UUID> enabled = new HashSet<>();
final Map<UUID, Integer> shiftCount = new HashMap<>();
final Map<UUID, Long> lastDisable = new HashMap<>();
final Map<UUID, Long> justEnabled = new HashMap<>();
final Map<UUID, List<WrappedTask>> activeTasks = new HashMap<>();
@Override
public void onEnable() {
foliaLib = new FoliaLib(this);
saveDefaultConfig();
Bukkit.getPluginManager().registerEvents(this, this);
startAutoDisableTask();
startActionBarTask();
}
@Override
public void onDisable() {
foliaLib.getScheduler().cancelAllTasks();
}
@EventHandler
public void onShift(PlayerToggleSneakEvent e) {
Player p = e.getPlayer();
UUID id = p.getUniqueId();
if (!e.isSneaking()) return;
ItemStack tool = p.getInventory().getItemInMainHand();
if (!isAxe(tool.getType())) return;
if (isLowDurability(tool)) {
p.sendActionBar(colorize(getConfig().getString("messages.low-durability", "&6⚠ Durability ขวานของคุณต่ำเกินไป")));
playConfigSound(p, "sounds.low-durability");
return;
}
if (enabled.contains(id)) return;
int count = shiftCount.getOrDefault(id, 0) + 1;
shiftCount.put(id, count);
foliaLib.getScheduler().runLater(() -> {
Integer current = shiftCount.get(id);
if (current != null && current == count) {
shiftCount.remove(id);
}
}, 60L);
if (count < 3) return;
int bars = Math.min(count, 10);
StringBuilder bar = new StringBuilder();
for (int i = 0; i < bars; i++) bar.append("§a▍");
for (int i = bars; i < 10; i++) bar.append("§7▍");
String msg = getConfig().getString("messages.progress-bar", "&6Tree Feller &b{bar}")
.replace("{bar}", bar.toString());
p.sendActionBar(colorize(msg));
if (count >= 10) {
enabled.add(id);
shiftCount.remove(id);
justEnabled.put(id, System.currentTimeMillis());
p.sendActionBar(colorize(getConfig().getString("messages.enabled", "&a✔ เปิด Tree Feller!")));
playConfigSound(p, "sounds.enabled");
}
}
private void startAutoDisableTask() {
foliaLib.getScheduler().runTimer(task -> {
for (Player p : Bukkit.getOnlinePlayers()) {
UUID id = p.getUniqueId();
if (!enabled.contains(id)) continue;
foliaLib.getScheduler().runAtEntity(p, entityTask -> {
ItemStack tool = p.getInventory().getItemInMainHand();
if (!isAxe(tool.getType())) {
disable(p, colorize(getConfig().getString("messages.disabled-no-axe", "&c❌ ปิด Tree Feller (ไม่มีขวาน)")));
return;
}
if (isLowDurability(tool)) {
disable(p, colorize(getConfig().getString("messages.low-durability")));
}
});
}
}, 0L, 40L);
}
private void startActionBarTask() {
foliaLib.getScheduler().runTimer(task -> {
Iterator<UUID> it = enabled.iterator();
while (it.hasNext()) {
UUID id = it.next();
Player p = Bukkit.getPlayer(id);
if (p == null || !p.isOnline()) {
it.remove();
continue;
}
foliaLib.getScheduler().runAtEntity(p, entityTask -> {
if (lastDisable.containsKey(id)) {
long time = lastDisable.get(id);
if (System.currentTimeMillis() - time < 2000) {
return;
} else {
lastDisable.remove(id);
}
}
if (justEnabled.containsKey(id)) {
long time = justEnabled.get(id);
if (System.currentTimeMillis() - time < 2000) {
p.sendActionBar(colorize(getConfig().getString("messages.enabled", "&a✔ เปิด Tree Feller!")));
return;
} else {
justEnabled.remove(id);
}
}
p.sendActionBar(colorize(getConfig().getString("messages.active", "&a✦ Tree Feller ถูกเปิดใช้งานอยู่")));
});
}
}, 0L, 40L);
}
void disable(Player p, String msg) {
UUID id = p.getUniqueId();
enabled.remove(id);
shiftCount.remove(id);
lastDisable.put(id, System.currentTimeMillis());
if (activeTasks.containsKey(id)) {
for (WrappedTask task : activeTasks.get(id)) {
task.cancel();
}
activeTasks.remove(id);
}
p.sendActionBar(msg);
playConfigSound(p, "sounds.disabled");
}
@EventHandler
public void onBreak(BlockBreakEvent e) {
Player p = e.getPlayer();
if (!enabled.contains(p.getUniqueId())) return;
Block start = e.getBlock();
if (!isLog(start.getType())) return;
ItemStack tool = p.getInventory().getItemInMainHand();
if (!isAxe(tool.getType())) return;
if (p.isSneaking()) return;
Block base;
Set<Block> logs;
if (isSpruce2x2(start)) {
Set<Block> bases = getSpruceBases(start);
base = bases.iterator().next();
logs = getLogs(bases);
} else {