Refactor code for clarity.

This commit is contained in:
tastybento 2021-10-09 11:20:48 -07:00
parent 536d7ef1c3
commit f37226b115
5 changed files with 45 additions and 46 deletions

View File

@ -126,7 +126,7 @@ public abstract class FlagListener implements Listener {
*/
public boolean checkIsland(@NonNull Event e, @Nullable Player player, @Nullable Location loc, @NonNull Flag flag, boolean silent) {
// Set user
user = User.getInstance(player);
user = player == null ? null : User.getInstance(player);
if (loc == null) {
if (user.getLocation() != null && user.getLocation().getWorld() != null) {
report(user, e, user.getLocation(), flag, Why.NULL_LOCATION);

View File

@ -42,15 +42,13 @@ public class BreakBlocksListener extends FlagListener {
*/
@EventHandler(priority = EventPriority.LOW)
public void onBreakHanging(final HangingBreakByEntityEvent e) {
if (e.getRemover() instanceof Player) {
checkIsland(e, (Player)e.getRemover(), e.getEntity().getLocation(), Flags.BREAK_BLOCKS);
if (e.getRemover() instanceof Player r) {
checkIsland(e, r, e.getEntity().getLocation(), Flags.BREAK_BLOCKS);
}
// Check for projectiles
if (e.getRemover() instanceof Projectile p) {
// Find out who fired it
if (p.getShooter() instanceof Player) {
checkIsland(e, (Player)p.getShooter(), e.getEntity().getLocation(), Flags.BREAK_BLOCKS);
}
// Find out who fired it
if (e.getRemover() instanceof Projectile p && p.getShooter() instanceof Player s) {
checkIsland(e, s, e.getEntity().getLocation(), Flags.BREAK_BLOCKS);
}
}
@ -65,21 +63,14 @@ public class BreakBlocksListener extends FlagListener {
if (!e.getAction().equals(Action.LEFT_CLICK_BLOCK)) {
return;
}
Player p = e.getPlayer();
Location l = e.getClickedBlock().getLocation();
switch (e.getClickedBlock().getType()) {
case CAKE:
checkIsland(e, e.getPlayer(), e.getClickedBlock().getLocation(), Flags.BREAK_BLOCKS);
break;
case SPAWNER:
checkIsland(e, e.getPlayer(), e.getClickedBlock().getLocation(), Flags.BREAK_SPAWNERS);
break;
case DRAGON_EGG:
checkIsland(e, e.getPlayer(), e.getClickedBlock().getLocation(), Flags.DRAGON_EGG);
break;
case HOPPER:
checkIsland(e, e.getPlayer(), e.getClickedBlock().getLocation(), Flags.BREAK_HOPPERS);
break;
default:
break;
case CAKE -> checkIsland(e, p, l, Flags.BREAK_BLOCKS);
case SPAWNER -> checkIsland(e, p, l, Flags.BREAK_SPAWNERS);
case DRAGON_EGG -> checkIsland(e, p, l, Flags.DRAGON_EGG);
case HOPPER -> checkIsland(e, p, l, Flags.BREAK_HOPPERS);
default -> {}
}
}
@ -89,14 +80,15 @@ public class BreakBlocksListener extends FlagListener {
*/
@EventHandler(priority = EventPriority.LOW, ignoreCancelled=true)
public void onVehicleDamageEvent(VehicleDamageEvent e) {
if (getIWM().inWorld(e.getVehicle().getLocation()) && e.getAttacker() instanceof Player) {
Location l = e.getVehicle().getLocation();
if (getIWM().inWorld(l) && e.getAttacker() instanceof Player p) {
String vehicleType = e.getVehicle().getType().toString();
if (e.getVehicle().getType().equals(EntityType.BOAT)) {
checkIsland(e, (Player) e.getAttacker(), e.getVehicle().getLocation(), Flags.BOAT);
checkIsland(e, p, l, Flags.BOAT);
} else if (vehicleType.contains("MINECART")) {
checkIsland(e, (Player) e.getAttacker(), e.getVehicle().getLocation(), Flags.MINECART);
checkIsland(e, p, l, Flags.MINECART);
} else {
checkIsland(e, (Player) e.getAttacker(), e.getVehicle().getLocation(), Flags.BREAK_BLOCKS);
checkIsland(e, p, l, Flags.BREAK_BLOCKS);
}
}
}
@ -114,9 +106,9 @@ public class BreakBlocksListener extends FlagListener {
return;
}
// Get the attacker
if (e.getDamager() instanceof Player) {
if (e.getDamager() instanceof Player p) {
// Check the break blocks flag
notAllowed(e, (Player)e.getDamager(), e.getEntity().getLocation());
notAllowed(e, p, e.getEntity().getLocation());
} else if (e.getDamager() instanceof Projectile p) {
// Find out who fired the arrow
if (p.getShooter() instanceof Player && notAllowed(e, (Player)p.getShooter(), e.getEntity().getLocation())) {
@ -153,8 +145,8 @@ public class BreakBlocksListener extends FlagListener {
}
// Find out who fired the arrow
if (e.getEntity().getShooter() instanceof Player &&
!checkIsland(e, (Player) e.getEntity().getShooter(), e.getHitBlock().getLocation(), Flags.BREAK_BLOCKS)) {
if (e.getEntity().getShooter() instanceof Player s &&
!checkIsland(e, s, e.getHitBlock().getLocation(), Flags.BREAK_BLOCKS)) {
final BlockData data = e.getHitBlock().getBlockData();
// We seemingly can't prevent the block from being destroyed
// So we need to put it back with a slight delay (yup, this is hacky - it makes the block flicker sometimes)

View File

@ -1,8 +1,10 @@
package world.bentobox.bentobox.listeners.flags.protection;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.MushroomCow;
import org.bukkit.entity.Player;
import org.bukkit.entity.TropicalFish;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@ -38,14 +40,14 @@ public class BucketListener extends FlagListener {
*/
@EventHandler(priority = EventPriority.LOW)
public void onBucketFill(final PlayerBucketFillEvent e) {
Player p = e.getPlayer();
Location l = e.getBlockClicked().getLocation();
// Check filling of various liquids
switch (e.getItemStack().getType()) {
case LAVA_BUCKET -> checkIsland(e, e.getPlayer(), e.getBlockClicked().getLocation(), Flags.COLLECT_LAVA);
case WATER_BUCKET -> checkIsland(e, e.getPlayer(), e.getBlockClicked().getLocation(), Flags.COLLECT_WATER);
case MILK_BUCKET -> checkIsland(e, e.getPlayer(), e.getBlockClicked().getLocation(), Flags.MILKING);
default ->
// Check general bucket use
checkIsland(e, e.getPlayer(), e.getBlockClicked().getLocation(), Flags.BUCKET);
case LAVA_BUCKET -> checkIsland(e, p, l, Flags.COLLECT_LAVA);
case WATER_BUCKET -> checkIsland(e, p, l, Flags.COLLECT_WATER);
case MILK_BUCKET -> checkIsland(e, p, l, Flags.MILKING);
default -> checkIsland(e, p, l, Flags.BUCKET);
}
}

View File

@ -1,11 +1,14 @@
package world.bentobox.bentobox.listeners.flags.protection;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Animals;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Boat;
import org.bukkit.entity.Player;
import org.bukkit.entity.Vehicle;
import org.bukkit.entity.Villager;
import org.bukkit.entity.WanderingTrader;
import org.bukkit.entity.minecart.RideableMinecart;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@ -32,31 +35,33 @@ public class EntityInteractListener extends FlagListener {
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onPlayerInteractEntity(PlayerInteractEntityEvent e) {
Player p = e.getPlayer();
Location l = e.getRightClicked().getLocation();
if (e.getRightClicked() instanceof Vehicle) {
// Animal riding
if (e.getRightClicked() instanceof Animals) {
checkIsland(e, e.getPlayer(), e.getRightClicked().getLocation(), Flags.RIDING);
checkIsland(e, p, l, Flags.RIDING);
}
// Minecart riding
else if (e.getRightClicked() instanceof RideableMinecart) {
checkIsland(e, e.getPlayer(), e.getRightClicked().getLocation(), Flags.MINECART);
checkIsland(e, p, l, Flags.MINECART);
}
// Boat riding
else if (e.getRightClicked() instanceof Boat) {
checkIsland(e, e.getPlayer(), e.getRightClicked().getLocation(), Flags.BOAT);
checkIsland(e, p, l, Flags.BOAT);
}
}
// Villager trading
else if (e.getRightClicked() instanceof Villager || e.getRightClicked().getType().name().equals("WANDERING_TRADER")) { // TODO: Simplify when 1.13.2 support is dropped
else if (e.getRightClicked() instanceof Villager || e.getRightClicked() instanceof WanderingTrader) {
// Check naming and check trading
checkIsland(e, e.getPlayer(), e.getRightClicked().getLocation(), Flags.TRADING);
checkIsland(e, p, l, Flags.TRADING);
if (e.getPlayer().getInventory().getItemInMainHand().getType().equals(Material.NAME_TAG)) {
checkIsland(e, e.getPlayer(), e.getRightClicked().getLocation(), Flags.NAME_TAG);
checkIsland(e, p, l, Flags.NAME_TAG);
}
}
// Name tags
else if (e.getPlayer().getInventory().getItemInMainHand().getType().equals(Material.NAME_TAG)) {
checkIsland(e, e.getPlayer(), e.getRightClicked().getLocation(), Flags.NAME_TAG);
checkIsland(e, p, l, Flags.NAME_TAG);
}
}
}

View File

@ -19,9 +19,9 @@ public class ExperiencePickupListener extends FlagListener {
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onExperienceOrbTargetPlayer(EntityTargetLivingEntityEvent e) {
// Make sure the target is a Player and the entity is an experience orb
if (e.getTarget() instanceof Player && e.getEntity() instanceof ExperienceOrb
&& !checkIsland(e, (Player) e.getTarget(), e.getEntity().getLocation(), Flags.EXPERIENCE_PICKUP)) {
// Cancelling the event won't work, we need to explicitly set the target to null
if (e.getTarget() instanceof Player p && e.getEntity() instanceof ExperienceOrb o
&& !checkIsland(e, p, o.getLocation(), Flags.EXPERIENCE_PICKUP)) {
// Canceling the event won't work, we need to explicitly set the target to null
e.setTarget(null);
}
}