Files
src
Arena.class
ArenaManager.class
build.gradle
BuildingGamePhase.class
CommandManager.class
FloorMenu.class
LobbyGamePhase.class
Main.class
Plot.class
PlotManager.class
SettingsManager.class
SubjectMenu.class
SubjectVotePhase.class
TransitionSystem.class
VotingGamePhase.class
WinningGamePhase.class
Code
22 1,386
src/main/java/com/kodari/tirelessfarmers/TirelessFarmers.java
package com.kodari.tirelessfarmers;
import com.cryptomorin.xseries.XMaterial;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.data.Ageable;
import org.bukkit.entity.Villager;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.entity.memory.MemoryKey;
public final class TirelessFarmers extends JavaPlugin implements Listener {
private static final int JOB_RADIUS = 16;
private static final int FIELD_RADIUS = 16;
private NamespacedKey jobKey;
@Override
public void onEnable() {
jobKey = new NamespacedKey(this, "job_site");
getServer().getPluginManager().registerEvents(this, this);
getServer().getScheduler().runTaskTimer(this, this::workTick, 1L, 20L);
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlace(BlockPlaceEvent event) {
Block block = event.getBlockPlaced();
if (!is(block, "COMPOSTER")) return;
Location location = block.getLocation();
getServer().getScheduler().runTask(this, () -> {
if (is(location.getBlock(), "COMPOSTER")) claimPlacedSite(location);
});
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBreak(BlockBreakEvent event) {
if (!is(event.getBlock(), "COMPOSTER")) return;
Location location = event.getBlock().getLocation();
getServer().getScheduler().runTask(this, () -> {
if (is(location.getBlock(), "COMPOSTER")) return;
for (Villager villager : location.getWorld().getEntitiesByClass(Villager.class)) {
Location assigned = storedSite(villager);
if (sameBlock(assigned, location)) clearSite(villager);
}
});
}
private void workTick() {
for (World world : Bukkit.getWorlds()) {
for (Villager villager : world.getEntitiesByClass(Villager.class)) {
if (villager.getProfession() != Villager.Profession.FARMER || villager.isDead()) continue;
Location site = storedSite(villager);
if (site != null) {
if (!world.isChunkLoaded(site.getBlockX() >> 4, site.getBlockZ() >> 4)) continue;
if (!is(site.getBlock(), "COMPOSTER")) {
clearSite(villager);
site = null;
}
}
if (site == null) {
Location remembered = villager.getMemory(MemoryKey.JOB_SITE);
if (remembered != null && remembered.getWorld().equals(world)
&& world.isChunkLoaded(remembered.getBlockX() >> 4, remembered.getBlockZ() >> 4)
&& is(remembered.getBlock(), "COMPOSTER") && !claimedByOther(remembered, villager)) {
site = remembered.getBlock().getLocation();
} else {
site = findSite(villager);
}
if (site == null) continue;
assign(villager, site);
} else {
villager.setMemory(MemoryKey.JOB_SITE, site);
}
workField(villager, site);
}
}
}
private void claimPlacedSite(Location site) {
if (claimedByOther(site, null)) return;
Villager nearest = null;
double best = JOB_RADIUS * JOB_RADIUS;
for (Villager villager : site.getWorld().getEntitiesByClass(Villager.class)) {
if (villager.getProfession() != Villager.Profession.FARMER || villager.isDead()
|| storedSite(villager) != null || villager.getMemory(MemoryKey.JOB_SITE) != null) continue;
double distance = villager.getLocation().distanceSquared(site);
if (distance <= best) {
best = distance;
nearest = villager;
}
}
if (nearest != null) {
assign(nearest, site);
workField(nearest, site);
}
}
private Location findSite(Villager villager) {
Location origin = villager.getLocation();
World world = villager.getWorld();
Location nearest = null;
double best = JOB_RADIUS * JOB_RADIUS;
for (int x = -JOB_RADIUS; x <= JOB_RADIUS; x++) {
for (int z = -JOB_RADIUS; z <= JOB_RADIUS; z++) {
int bx = origin.getBlockX() + x;
int bz = origin.getBlockZ() + z;
if (!world.isChunkLoaded(bx >> 4, bz >> 4)) continue;
for (int y = -4; y <= 4; y++) {
int by = origin.getBlockY() + y;
if (by < world.getMinHeight() || by >= world.getMaxHeight()) continue;
Block block = world.getBlockAt(bx, by, bz);
if (!is(block, "COMPOSTER")) continue;
Location candidate = block.getLocation();
double distance = origin.distanceSquared(candidate);
if (distance <= best && !claimedByOther(candidate, villager)) {
best = distance;
nearest = candidate;
}
}
}
}
return nearest;
}
private boolean claimedByOther(Location site, Villager excluded) {
for (Villager villager : site.getWorld().getEntitiesByClass(Villager.class)) {
if (villager.equals(excluded)) continue;
if (sameBlock(site, storedSite(villager)) || sameBlock(site, villager.getMemory(MemoryKey.JOB_SITE))) return true;
}
return false;
}
private void assign(Villager villager, Location site) {
String value = site.getBlockX() + "," + site.getBlockY() + "," + site.getBlockZ();
villager.getPersistentDataContainer().set(jobKey, PersistentDataType.STRING, value);
villager.setMemory(MemoryKey.JOB_SITE, site);
}
private void clearSite(Villager villager) {
Location site = storedSite(villager);
if (sameBlock(site, villager.getMemory(MemoryKey.JOB_SITE))) villager.setMemory(MemoryKey.JOB_SITE, null);
villager.getPersistentDataContainer().remove(jobKey);
}
private Location storedSite(Villager villager) {
String value = villager.getPersistentDataContainer().get(jobKey, PersistentDataType.STRING);
if (value == null) return null;
String[] parts = value.split(",");
if (parts.length != 3) return null;
try {
return new Location(villager.getWorld(), Integer.parseInt(parts[0]), Integer.parseInt(parts[1]), Integer.parseInt(parts[2]));
} catch (NumberFormatException ignored) {
return null;
}
}
private void workField(Villager villager, Location site) {
World world = villager.getWorld();
Location origin = villager.getLocation();
Block target = null;
double best = Double.MAX_VALUE;
for (int x = -FIELD_RADIUS; x <= FIELD_RADIUS; x++) {
for (int z = -FIELD_RADIUS; z <= FIELD_RADIUS; z++) {
int bx = site.getBlockX() + x;
int bz = site.getBlockZ() + z;
if (!world.isChunkLoaded(bx >> 4, bz >> 4)) continue;
for (int y = -4; y <= 4; y++) {
int by = site.getBlockY() + y;
if (by <= world.getMinHeight() || by >= world.getMaxHeight()) continue;
Block crop = world.getBlockAt(bx, by, bz);
if (!isCrop(crop) || !is(crop.getRelative(0, -1, 0), "FARMLAND")) continue;
Ageable age = (Ageable) crop.getBlockData();
if (age.getAge() != age.getMaximumAge()) continue;
double distance = origin.distanceSquared(crop.getLocation().add(0.5, 0, 0.5));
if (distance < best) {
best = distance;
target = crop;
}
}
}
}
if (target == null) return;
if (best > 4.0) {
villager.getPathfinder().moveTo(target.getLocation().add(0.5, 0, 0.5), 1.0);
return;
}
for (ItemStack drop : target.getDrops()) world.dropItemNaturally(target.getLocation(), drop);
Ageable replanted = (Ageable) target.getBlockData();
replanted.setAge(0);
target.setBlockData(replanted);
}
