Cleaned up listeners.

This commit is contained in:
Brianna 2020-09-09 10:10:05 -05:00
parent 1a346e8d1c
commit 5aa19e2f3d
5 changed files with 37 additions and 38 deletions

View File

@ -29,22 +29,22 @@ import org.bukkit.permissions.PermissionAttachmentInfo;
@SuppressWarnings("Duplicates")
public class BlockListeners implements Listener {
private final EpicFarming instance;
private final EpicFarming plugin;
public BlockListeners(EpicFarming instance) {
this.instance = instance;
public BlockListeners(EpicFarming plugin) {
this.plugin = plugin;
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBlockFade(BlockFadeEvent e) {
Farm farm = instance.getFarmManager().checkForFarm(e.getBlock().getLocation());
Farm farm = plugin.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 = instance.getFarmManager().checkForFarm(e.getBlock().getLocation());
Farm farm = plugin.getFarmManager().checkForFarm(e.getBlock().getLocation());
if (farm != null && farm.getFarmType() != FarmType.LIVESTOCK)
e.setCancelled(true);
}
@ -65,13 +65,13 @@ public class BlockListeners implements Listener {
Material farmBlock = Settings.FARM_BLOCK_MATERIAL.getMaterial(CompatibleMaterial.END_ROD).getBlockMaterial();
if (e.getPlayer().getItemInHand().getType() != farmBlock
|| instance.getLevelFromItem(e.getItemInHand()) == 0 && !Settings.NON_COMMAND_FARMS.getBoolean())
|| plugin.getLevelFromItem(e.getItemInHand()) == 0 && !Settings.NON_COMMAND_FARMS.getBoolean())
return;
if (e.getBlockAgainst().getType() == farmBlock) e.setCancelled(true);
int amt = 0;
for (Farm farmm : instance.getFarmManager().getFarms().values()) {
for (Farm farmm : plugin.getFarmManager().getFarms().values()) {
if (farmm.getPlacedBy() == null || !farmm.getPlacedBy().equals(e.getPlayer().getUniqueId())) continue;
amt++;
}
@ -79,23 +79,23 @@ public class BlockListeners implements Listener {
if (limit != -1 && amt >= limit) {
e.setCancelled(true);
instance.getLocale().getMessage("event.limit.hit")
plugin.getLocale().getMessage("event.limit.hit")
.processPlaceholder("limit", limit).sendPrefixedMessage(e.getPlayer());
return;
}
Location location = e.getBlock().getLocation();
if (e.getBlockPlaced().getType().equals(Material.MELON_SEEDS) || e.getBlockPlaced().getType().equals(Material.PUMPKIN_SEEDS)) {
if (instance.getFarmManager().checkForFarm(location) != null) {
instance.getLocale().getMessage("event.warning.noauto").sendPrefixedMessage(e.getPlayer());
if (plugin.getFarmManager().checkForFarm(location) != null) {
plugin.getLocale().getMessage("event.warning.noauto").sendPrefixedMessage(e.getPlayer());
}
}
int level = instance.getLevelFromItem(e.getItemInHand());
Bukkit.getScheduler().runTaskLater(instance, () -> {
int level = plugin.getLevelFromItem(e.getItemInHand());
Bukkit.getScheduler().runTaskLater(plugin, () -> {
if (location.getBlock().getType() != farmBlock) return;
Farm farm = new Farm(location, instance.getLevelManager().getLevel(level == 0 ? 1 : level), e.getPlayer().getUniqueId());
instance.getFarmManager().addFarm(location, farm);
Farm farm = new Farm(location, plugin.getLevelManager().getLevel(level == 0 ? 1 : level), e.getPlayer().getUniqueId());
plugin.getFarmManager().addFarm(location, farm);
farm.tillLand();
}, 1);
@ -106,7 +106,7 @@ public class BlockListeners implements Listener {
if (event.getBlock().getType() != Settings.FARM_BLOCK_MATERIAL.getMaterial(CompatibleMaterial.END_ROD).getMaterial())
return;
Farm farm = instance.getFarmManager().removeFarm(event.getBlock().getLocation());
Farm farm = plugin.getFarmManager().removeFarm(event.getBlock().getLocation());
if (farm == null) return;
@ -114,7 +114,7 @@ public class BlockListeners implements Listener {
event.setCancelled(true);
ItemStack item = instance.makeFarmItem(farm.getLevel());
ItemStack item = plugin.makeFarmItem(farm.getLevel());
Block block = event.getBlock();
@ -131,14 +131,14 @@ public class BlockListeners implements Listener {
if (event.getBlock().getType() != Settings.FARM_BLOCK_MATERIAL.getMaterial(CompatibleMaterial.END_ROD).getMaterial())
return;
Farm farm = instance.getFarmManager().removeFarm(event.getBlock().getLocation());
Farm farm = plugin.getFarmManager().removeFarm(event.getBlock().getLocation());
if (farm == null) return;
FarmTask.getCrops(farm, false);
event.setCancelled(true);
ItemStack item = instance.makeFarmItem(farm.getLevel());
ItemStack item = plugin.makeFarmItem(farm.getLevel());
Block block = event.getBlock();
@ -153,7 +153,7 @@ public class BlockListeners implements Listener {
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onBlockFromToEventMonitor(BlockFromToEvent event) {
// prevent water/lava/egg griefs
if (instance.getFarmManager().getFarm(event.getToBlock()) != null) {
if (plugin.getFarmManager().getFarm(event.getToBlock()) != null) {
event.setCancelled(true);
}
}

View File

@ -32,10 +32,10 @@ import java.util.List;
public class EntityListeners implements Listener {
private final EpicFarming instance;
private final EpicFarming plugin;
public EntityListeners(EpicFarming instance) {
this.instance = instance;
public EntityListeners(EpicFarming plugin) {
this.plugin = plugin;
}
@EventHandler(priority = EventPriority.HIGHEST)
@ -43,7 +43,7 @@ public class EntityListeners implements Listener {
LivingEntity entity = event.getEntity();
if (!entity.hasMetadata("EFA-TAGGED")) return;
Location location = (Location) entity.getMetadata("EFA-TAGGED").get(0).value();
Farm farm = instance.getFarmManager().getFarm(location);
Farm farm = plugin.getFarmManager().getFarm(location);
boolean autoCollect = false;
for (Module module : farm.getLevel().getRegisteredModules()) {
@ -99,7 +99,7 @@ public class EntityListeners implements Listener {
if (block.getType() != Settings.FARM_BLOCK_MATERIAL.getMaterial(CompatibleMaterial.END_ROD).getMaterial())
continue;
Farm farm = instance.getFarmManager().getFarm(block.getLocation());
Farm farm = plugin.getFarmManager().getFarm(block.getLocation());
if (farm == null) continue;
toCancel.add(block);
@ -108,11 +108,11 @@ public class EntityListeners implements Listener {
for (Block block : toCancel) {
event.blockList().remove(block);
Farm farm = instance.getFarmManager().removeFarm(block.getLocation());
Farm farm = plugin.getFarmManager().removeFarm(block.getLocation());
FarmTask.getCrops(farm, false);
ItemStack item = instance.makeFarmItem(farm.getLevel());
ItemStack item = plugin.makeFarmItem(farm.getLevel());
block.setType(Material.AIR);
block.getLocation().getWorld().dropItemNaturally(block.getLocation().add(.5, .5, .5), item);

View File

@ -17,10 +17,10 @@ import org.bukkit.event.player.PlayerInteractEvent;
*/
public class InteractListeners implements Listener {
private final EpicFarming instance;
private final EpicFarming plugin;
public InteractListeners(EpicFarming instance) {
this.instance = instance;
public InteractListeners(EpicFarming plugin) {
this.plugin = plugin;
}
@EventHandler(priority = EventPriority.HIGHEST)
@ -29,7 +29,7 @@ public class InteractListeners implements Listener {
Location location = e.getClickedBlock().getLocation();
if (e.getItem() != null && CompatibleMaterial.getMaterial(e.getItem()) == CompatibleMaterial.BONE_MEAL
&& instance.getFarmManager().checkForFarm(location) != null)
&& plugin.getFarmManager().checkForFarm(location) != null)
e.setCancelled(true);
if (e.getClickedBlock().getType() != Settings.FARM_BLOCK_MATERIAL.getMaterial().getMaterial())
@ -47,9 +47,9 @@ public class InteractListeners implements Listener {
return;
}
if (instance.getFarmManager().getFarms().containsKey(location)) {
if (plugin.getFarmManager().getFarms().containsKey(location)) {
e.setCancelled(true);
instance.getFarmManager().getFarm(location).view(e.getPlayer(), false);
plugin.getFarmManager().getFarm(location).view(e.getPlayer(), false);
}
}
}

View File

@ -17,7 +17,6 @@ public class InventoryListeners implements Listener {
@EventHandler
public void onInventoryClick(InventoryClickEvent event) {
Player player = (Player) event.getWhoClicked();
if (event.getCurrentItem() == null) return;
if (event.getRawSlot() > event.getView().getTopInventory().getSize() - 1) return;

View File

@ -13,21 +13,21 @@ import java.util.ArrayList;
public class UnloadListeners implements Listener {
private final EpicFarming instance;
private final EpicFarming plugin;
public UnloadListeners(EpicFarming instance) {
this.instance = instance;
public UnloadListeners(EpicFarming plugin) {
this.plugin = plugin;
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onUnload(ChunkUnloadEvent event) {
Material type = Settings.FARM_BLOCK_MATERIAL.getMaterial().getMaterial();
for (Farm farm : new ArrayList<>(instance.getFarmManager().getFarms().values())) {
for (Farm farm : new ArrayList<>(plugin.getFarmManager().getFarms().values())) {
int x = farm.getLocation().getBlockX() >> 4;
int z = farm.getLocation().getBlockZ() >> 4;
if (event.getChunk().getX() == x && event.getChunk().getZ() == z) {
if (farm.getLocation().getBlock().getType() != type)
instance.getFarmManager().removeFarm(farm.getLocation());
plugin.getFarmManager().removeFarm(farm.getLocation());
}
}
}