Block use of bonemeal directly on farms.

This commit is contained in:
Brianna 2020-07-24 09:14:41 -05:00
parent be894cb1e7
commit efdb78c0c2
4 changed files with 53 additions and 38 deletions

View File

@ -124,7 +124,7 @@ public class EpicFarming extends SongodaPlugin {
this.loadLevelManager();
this.farmManager = new FarmManager();
this.farmManager = new FarmManager(levelManager);
this.boostManager = new BoostManager();
/*

View File

@ -1,6 +1,11 @@
package com.songoda.epicfarming.farming;
import com.songoda.core.compatibility.CompatibleMaterial;
import com.songoda.epicfarming.farming.levels.Level;
import com.songoda.epicfarming.farming.levels.LevelManager;
import com.songoda.epicfarming.settings.Settings;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import java.util.Collections;
@ -9,6 +14,12 @@ import java.util.Map;
public class FarmManager {
private final LevelManager levelManager;
public FarmManager(LevelManager levelManager) {
this.levelManager = levelManager;
}
private final Map<Location, Farm> registeredFarms = new HashMap<>();
public void addFarm(Location location, Farm farm) {
@ -27,6 +38,35 @@ public class FarmManager {
return getFarm(block.getLocation());
}
public Farm checkForFarm(Location location) {
Material farmBlock = Settings.FARM_BLOCK_MATERIAL.getMaterial(CompatibleMaterial.END_ROD).getBlockMaterial();
Block block = location.getBlock();
for (Level level : levelManager.getLevels().values()) {
int radius = level.getRadius();
int bx = block.getX();
int by = block.getY();
int bz = block.getZ();
for (int fx = -radius; fx <= radius; fx++) {
for (int fy = -2; fy <= 2; fy++) {
for (int fz = -radius; fz <= radius; fz++) {
Block b2 = block.getWorld().getBlockAt(bx + fx, by + fy, bz + fz);
if (b2.getType() == farmBlock) {
Farm farm = getFarms().get(b2.getLocation());
if (farm == null) continue;
if (level.getRadius() != getFarm(b2.getLocation()).getLevel().getRadius())
continue;
return farm;
}
}
}
}
}
return null;
}
public Map<Location, Farm> getFarms() {
return Collections.unmodifiableMap(registeredFarms);
}

View File

@ -35,14 +35,14 @@ public class BlockListeners implements Listener {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBlockFade(BlockFadeEvent e) {
Farm farm = checkForFarm(e.getBlock().getLocation());
Farm farm = instance.getFarmManager().checkForFarm(e.getBlock().getLocation());
if (farm != null && farm.getFarmType() != FarmType.LIVESTOCK)
e.setCancelled(true);
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onGrow(BlockGrowEvent e) {
Farm farm = checkForFarm(e.getBlock().getLocation());
Farm farm = instance.getFarmManager().checkForFarm(e.getBlock().getLocation());
if (farm != null && farm.getFarmType() != FarmType.LIVESTOCK)
e.setCancelled(true);
}
@ -84,7 +84,7 @@ public class BlockListeners implements Listener {
Location location = e.getBlock().getLocation();
if (e.getBlockPlaced().getType().equals(Material.MELON_SEEDS) || e.getBlockPlaced().getType().equals(Material.PUMPKIN_SEEDS)) {
if (checkForFarm(location) != null) {
if (instance.getFarmManager().checkForFarm(location) != null) {
instance.getLocale().getMessage("event.warning.noauto").sendPrefixedMessage(e.getPlayer());
}
}
@ -99,36 +99,6 @@ public class BlockListeners implements Listener {
}, 1);
}
private Farm checkForFarm(Location location) {
Material farmBlock = Settings.FARM_BLOCK_MATERIAL.getMaterial(CompatibleMaterial.END_ROD).getBlockMaterial();
FarmManager farmManager = instance.getFarmManager();
Block block = location.getBlock();
for (Level level : instance.getLevelManager().getLevels().values()) {
int radius = level.getRadius();
int bx = block.getX();
int by = block.getY();
int bz = block.getZ();
for (int fx = -radius; fx <= radius; fx++) {
for (int fy = -2; fy <= 2; fy++) {
for (int fz = -radius; fz <= radius; fz++) {
Block b2 = block.getWorld().getBlockAt(bx + fx, by + fy, bz + fz);
if (b2.getType() == farmBlock) {
Farm farm = farmManager.getFarms().get(b2.getLocation());
if (farm == null) continue;
if (level.getRadius() != farmManager.getFarm(b2.getLocation()).getLevel().getRadius())
continue;
return farm;
}
}
}
}
}
return null;
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBlockBreak(BlockBreakEvent event) {
if (event.getBlock().getType() != Settings.FARM_BLOCK_MATERIAL.getMaterial(CompatibleMaterial.END_ROD).getMaterial())

View File

@ -1,5 +1,6 @@
package com.songoda.epicfarming.listeners;
import com.songoda.core.compatibility.CompatibleMaterial;
import com.songoda.epicfarming.EpicFarming;
import com.songoda.epicfarming.settings.Settings;
import com.songoda.skyblock.SkyBlock;
@ -24,8 +25,14 @@ public class InteractListeners implements Listener {
@EventHandler(priority = EventPriority.HIGHEST)
public void onBlockInteract(PlayerInteractEvent e) {
if (e.getClickedBlock() == null
|| e.getClickedBlock().getType() != Settings.FARM_BLOCK_MATERIAL.getMaterial().getMaterial())
if (e.getClickedBlock() == null) return;
Location location = e.getClickedBlock().getLocation();
if (e.getItem() != null && CompatibleMaterial.getMaterial(e.getItem()) == CompatibleMaterial.BONE_MEAL
&& instance.getFarmManager().checkForFarm(location) != null)
e.setCancelled(true);
if (e.getClickedBlock().getType() != Settings.FARM_BLOCK_MATERIAL.getMaterial().getMaterial())
return;
if (e.getAction() != Action.RIGHT_CLICK_BLOCK) return;
@ -40,8 +47,6 @@ public class InteractListeners implements Listener {
return;
}
Location location = e.getClickedBlock().getLocation();
if (instance.getFarmManager().getFarms().containsKey(location)) {
e.setCancelled(true);
instance.getFarmManager().getFarm(location).view(e.getPlayer(), false);