Extract duplicated method declaration

This commit is contained in:
sauilitired 2018-11-15 00:06:20 +01:00
parent a8b4729845
commit 06f64aeff0
4 changed files with 127 additions and 195 deletions

View File

@ -14,8 +14,8 @@ dependencies {
compileOnly 'org.projectlombok:lombok:1.16.18' compileOnly 'org.projectlombok:lombok:1.16.18'
} }
sourceCompatibility = 1.7 sourceCompatibility = 1.8
targetCompatibility = 1.7 targetCompatibility = 1.8
processResources { processResources {
from('src/main/resources') { from('src/main/resources') {

View File

@ -9,7 +9,6 @@ import com.github.intellectualsites.plotsquared.plot.PlotSquared;
import com.github.intellectualsites.plotsquared.plot.config.C; import com.github.intellectualsites.plotsquared.plot.config.C;
import com.github.intellectualsites.plotsquared.plot.config.Settings; import com.github.intellectualsites.plotsquared.plot.config.Settings;
import com.github.intellectualsites.plotsquared.plot.flag.Flags; import com.github.intellectualsites.plotsquared.plot.flag.Flags;
import com.github.intellectualsites.plotsquared.plot.flag.IntegerFlag;
import com.github.intellectualsites.plotsquared.plot.listener.PlayerBlockEventType; import com.github.intellectualsites.plotsquared.plot.listener.PlayerBlockEventType;
import com.github.intellectualsites.plotsquared.plot.listener.PlotListener; import com.github.intellectualsites.plotsquared.plot.listener.PlotListener;
import com.github.intellectualsites.plotsquared.plot.object.*; import com.github.intellectualsites.plotsquared.plot.object.*;
@ -79,105 +78,25 @@ public class PlayerEvents extends PlotListener implements Listener {
public static void sendBlockChange(final org.bukkit.Location bloc, final Material type, public static void sendBlockChange(final org.bukkit.Location bloc, final Material type,
final byte data) { final byte data) {
TaskManager.runTaskLater(new Runnable() { TaskManager.runTaskLater(() -> {
@Override public void run() { String world = bloc.getWorld().getName();
String world = bloc.getWorld().getName(); int x = bloc.getBlockX();
int x = bloc.getBlockX(); int z = bloc.getBlockZ();
int z = bloc.getBlockZ(); int distance = Bukkit.getViewDistance() * 16;
int distance = Bukkit.getViewDistance() * 16; for (Entry<String, PlotPlayer> entry : UUIDHandler.getPlayers().entrySet()) {
for (Entry<String, PlotPlayer> entry : UUIDHandler.getPlayers().entrySet()) { PlotPlayer player = entry.getValue();
PlotPlayer player = entry.getValue(); Location loc = player.getLocation();
Location loc = player.getLocation(); if (loc.getWorld().equals(world)) {
if (loc.getWorld().equals(world)) { if (16 * Math.abs(loc.getX() - x) / 16 > distance
if (16 * Math.abs(loc.getX() - x) / 16 > distance || 16 * Math.abs(loc.getZ() - z) / 16 > distance) {
|| 16 * Math.abs(loc.getZ() - z) / 16 > distance) { continue;
continue;
}
((BukkitPlayer) player).player.sendBlockChange(bloc, type, data);
} }
((BukkitPlayer) player).player.sendBlockChange(bloc, type, data);
} }
} }
}, 3); }, 3);
} }
public static boolean checkEntity(Plot plot, IntegerFlag... flags) {
if (Settings.Done.RESTRICT_BUILDING && Flags.DONE.isSet(plot)) {
return true;
}
int[] mobs = null;
for (IntegerFlag flag : flags) {
int i;
switch (flag.getName()) {
case "entity-cap":
i = 0;
break;
case "mob-cap":
i = 3;
break;
case "hostile-cap":
i = 2;
break;
case "animal-cap":
i = 1;
break;
case "vehicle-cap":
i = 4;
break;
case "misc-cap":
i = 5;
break;
default:
i = 0;
}
int cap = plot.getFlag(flag, Integer.MAX_VALUE);
if (cap == Integer.MAX_VALUE) {
continue;
}
if (cap == 0) {
return true;
}
if (mobs == null) {
mobs = plot.countEntities();
}
if (mobs[i] >= cap) {
plot.setMeta("EntityCount", mobs);
plot.setMeta("EntityCountTime", System.currentTimeMillis());
return true;
}
}
if (mobs != null) {
for (IntegerFlag flag : flags) {
int i;
switch (flag.getName()) {
case "entity-cap":
i = 0;
break;
case "mob-cap":
i = 3;
break;
case "hostile-cap":
i = 2;
break;
case "animal-cap":
i = 1;
break;
case "vehicle-cap":
i = 4;
break;
case "misc-cap":
i = 5;
break;
default:
i = 0;
}
mobs[i]++;
}
plot.setMeta("EntityCount", mobs);
plot.setMeta("EntityCountTime", System.currentTimeMillis());
}
return false;
}
public static boolean checkEntity(Entity entity, Plot plot) { public static boolean checkEntity(Entity entity, Plot plot) {
if (plot == null || !plot.hasOwner() || plot.getFlags().isEmpty() && plot if (plot == null || !plot.hasOwner() || plot.getFlags().isEmpty() && plot
.getArea().DEFAULT_FLAGS.isEmpty()) { .getArea().DEFAULT_FLAGS.isEmpty()) {
@ -219,11 +138,11 @@ public class PlayerEvents extends PlotListener implements Listener {
case EVOKER_FANGS: case EVOKER_FANGS:
case UNKNOWN: case UNKNOWN:
// non moving / unmovable // non moving / unmovable
return checkEntity(plot, Flags.ENTITY_CAP); return EntityUtil.checkEntity(plot, Flags.ENTITY_CAP);
case ITEM_FRAME: case ITEM_FRAME:
case PAINTING: case PAINTING:
case ARMOR_STAND: case ARMOR_STAND:
return checkEntity(plot, Flags.ENTITY_CAP, Flags.MISC_CAP); return EntityUtil.checkEntity(plot, Flags.ENTITY_CAP, Flags.MISC_CAP);
// misc // misc
case MINECART: case MINECART:
case MINECART_CHEST: case MINECART_CHEST:
@ -233,7 +152,7 @@ public class PlayerEvents extends PlotListener implements Listener {
case MINECART_MOB_SPAWNER: case MINECART_MOB_SPAWNER:
case MINECART_TNT: case MINECART_TNT:
case BOAT: case BOAT:
return checkEntity(plot, Flags.ENTITY_CAP, Flags.VEHICLE_CAP); return EntityUtil.checkEntity(plot, Flags.ENTITY_CAP, Flags.VEHICLE_CAP);
case POLAR_BEAR: case POLAR_BEAR:
case RABBIT: case RABBIT:
case SHEEP: case SHEEP:
@ -255,7 +174,7 @@ public class PlayerEvents extends PlotListener implements Listener {
case ZOMBIE_HORSE: case ZOMBIE_HORSE:
case SKELETON_HORSE: case SKELETON_HORSE:
// animal // animal
return checkEntity(plot, Flags.ENTITY_CAP, Flags.MOB_CAP, Flags.ANIMAL_CAP); return EntityUtil.checkEntity(plot, Flags.ENTITY_CAP, Flags.MOB_CAP, Flags.ANIMAL_CAP);
case BLAZE: case BLAZE:
case CAVE_SPIDER: case CAVE_SPIDER:
case CREEPER: case CREEPER:
@ -284,25 +203,25 @@ public class PlayerEvents extends PlotListener implements Listener {
case VEX: case VEX:
case ZOMBIE_VILLAGER: case ZOMBIE_VILLAGER:
// monster // monster
return checkEntity(plot, Flags.ENTITY_CAP, Flags.MOB_CAP, Flags.HOSTILE_CAP); return EntityUtil.checkEntity(plot, Flags.ENTITY_CAP, Flags.MOB_CAP, Flags.HOSTILE_CAP);
default: default:
if (entity instanceof LivingEntity) { if (entity instanceof LivingEntity) {
if (entity instanceof Animals) { if (entity instanceof Animals) {
return checkEntity(plot, Flags.ENTITY_CAP, Flags.MOB_CAP, Flags.ANIMAL_CAP); return EntityUtil.checkEntity(plot, Flags.ENTITY_CAP, Flags.MOB_CAP, Flags.ANIMAL_CAP);
} else if (entity instanceof Monster) { } else if (entity instanceof Monster) {
return checkEntity(plot, Flags.ENTITY_CAP, Flags.MOB_CAP, return EntityUtil.checkEntity(plot, Flags.ENTITY_CAP, Flags.MOB_CAP,
Flags.HOSTILE_CAP); Flags.HOSTILE_CAP);
} else { } else {
return checkEntity(plot, Flags.ENTITY_CAP, Flags.MOB_CAP); return EntityUtil.checkEntity(plot, Flags.ENTITY_CAP, Flags.MOB_CAP);
} }
} }
if (entity instanceof Vehicle) { if (entity instanceof Vehicle) {
return checkEntity(plot, Flags.ENTITY_CAP, Flags.VEHICLE_CAP); return EntityUtil.checkEntity(plot, Flags.ENTITY_CAP, Flags.VEHICLE_CAP);
} }
if (entity instanceof Hanging) { if (entity instanceof Hanging) {
return checkEntity(plot, Flags.ENTITY_CAP, Flags.MISC_CAP); return EntityUtil.checkEntity(plot, Flags.ENTITY_CAP, Flags.MISC_CAP);
} }
return checkEntity(plot, Flags.ENTITY_CAP); return EntityUtil.checkEntity(plot, Flags.ENTITY_CAP);
} }
} }
@ -745,8 +664,7 @@ public class PlayerEvents extends PlotListener implements Listener {
vehicle.eject(); vehicle.eject();
vehicle.setVelocity(new Vector(0d, 0d, 0d)); vehicle.setVelocity(new Vector(0d, 0d, 0d));
vehicle.teleport(dest); vehicle.teleport(dest);
for (final Entity entity : passengers) passengers.forEach(vehicle::addPassenger);
vehicle.addPassenger(entity);
} else { } else {
vehicle.eject(); vehicle.eject();
vehicle.setVelocity(new Vector(0d, 0d, 0d)); vehicle.setVelocity(new Vector(0d, 0d, 0d));

View File

@ -0,0 +1,93 @@
package com.github.intellectualsites.plotsquared.plot.util;
import com.github.intellectualsites.plotsquared.plot.config.Settings;
import com.github.intellectualsites.plotsquared.plot.flag.Flags;
import com.github.intellectualsites.plotsquared.plot.flag.IntegerFlag;
import com.github.intellectualsites.plotsquared.plot.object.Plot;
import lombok.experimental.UtilityClass;
/**
* Entity related general utility methods
*/
@UtilityClass
public final class EntityUtil {
public static boolean checkEntity(Plot plot, IntegerFlag... flags) {
if (Settings.Done.RESTRICT_BUILDING && Flags.DONE.isSet(plot)) {
return true;
}
int[] mobs = null;
for (IntegerFlag flag : flags) {
int i;
switch (flag.getName()) {
case "entity-cap":
i = 0;
break;
case "mob-cap":
i = 3;
break;
case "hostile-cap":
i = 2;
break;
case "animal-cap":
i = 1;
break;
case "vehicle-cap":
i = 4;
break;
case "misc-cap":
i = 5;
break;
default:
i = 0;
}
int cap = plot.getFlag(flag, Integer.MAX_VALUE);
if (cap == Integer.MAX_VALUE) {
continue;
}
if (cap == 0) {
return true;
}
if (mobs == null) {
mobs = plot.countEntities();
}
if (mobs[i] >= cap) {
plot.setMeta("EntityCount", mobs);
plot.setMeta("EntityCountTime", System.currentTimeMillis());
return true;
}
}
if (mobs != null) {
for (IntegerFlag flag : flags) {
int i;
switch (flag.getName()) {
case "entity-cap":
i = 0;
break;
case "mob-cap":
i = 3;
break;
case "hostile-cap":
i = 2;
break;
case "animal-cap":
i = 1;
break;
case "vehicle-cap":
i = 4;
break;
case "misc-cap":
i = 5;
break;
default:
i = 0;
}
mobs[i]++;
}
plot.setMeta("EntityCount", mobs);
plot.setMeta("EntityCountTime", System.currentTimeMillis());
}
return false;
}
}

View File

@ -27,7 +27,6 @@ import com.github.intellectualsites.plotsquared.plot.PlotSquared;
import com.github.intellectualsites.plotsquared.plot.config.C; import com.github.intellectualsites.plotsquared.plot.config.C;
import com.github.intellectualsites.plotsquared.plot.config.Settings; import com.github.intellectualsites.plotsquared.plot.config.Settings;
import com.github.intellectualsites.plotsquared.plot.flag.Flags; import com.github.intellectualsites.plotsquared.plot.flag.Flags;
import com.github.intellectualsites.plotsquared.plot.flag.IntegerFlag;
import com.github.intellectualsites.plotsquared.plot.listener.PlotListener; import com.github.intellectualsites.plotsquared.plot.listener.PlotListener;
import com.github.intellectualsites.plotsquared.plot.object.*; import com.github.intellectualsites.plotsquared.plot.object.*;
import com.github.intellectualsites.plotsquared.plot.util.*; import com.github.intellectualsites.plotsquared.plot.util.*;
@ -43,84 +42,6 @@ public class PlayerEvents extends PlotListener implements Listener {
// To prevent recursion // To prevent recursion
private boolean tmpTeleport = true; private boolean tmpTeleport = true;
public static boolean checkEntity(Plot plot, IntegerFlag... flags) {
if (Settings.Done.RESTRICT_BUILDING && Flags.DONE.isSet(plot)) {
return true;
}
int[] mobs = null;
for (IntegerFlag flag : flags) {
int i;
switch (flag.getName()) {
case "entity-cap":
i = 0;
break;
case "mob-cap":
i = 3;
break;
case "hostile-cap":
i = 2;
break;
case "animal-cap":
i = 1;
break;
case "vehicle-cap":
i = 4;
break;
case "misc-cap":
i = 5;
break;
default:
i = 0;
}
int cap = plot.getFlag(flag, Integer.MAX_VALUE);
if (cap == Integer.MAX_VALUE) {
continue;
}
if (cap == 0) {
return true;
}
if (mobs == null) {
mobs = plot.countEntities();
}
if (mobs[i] >= cap) {
plot.setMeta("EntityCount", mobs);
plot.setMeta("EntityCountTime", System.currentTimeMillis());
return true;
}
}
if (mobs != null) {
for (IntegerFlag flag : flags) {
int i;
switch (flag.getName()) {
case "entity-cap":
i = 0;
break;
case "mob-cap":
i = 3;
break;
case "hostile-cap":
i = 2;
break;
case "animal-cap":
i = 1;
break;
case "vehicle-cap":
i = 4;
break;
case "misc-cap":
i = 5;
break;
default:
i = 0;
}
mobs[i]++;
}
plot.setMeta("EntityCount", mobs);
plot.setMeta("EntityCountTime", System.currentTimeMillis());
}
return false;
}
public static boolean checkEntity(Entity entity, Plot plot) { public static boolean checkEntity(Entity entity, Plot plot) {
if (plot == null || !plot.hasOwner() || plot.getFlags().isEmpty() && plot if (plot == null || !plot.hasOwner() || plot.getFlags().isEmpty() && plot
.getArea().DEFAULT_FLAGS.isEmpty()) { .getArea().DEFAULT_FLAGS.isEmpty()) {
@ -129,23 +50,23 @@ public class PlayerEvents extends PlotListener implements Listener {
if (entity instanceof EntityLiving) { if (entity instanceof EntityLiving) {
if (entity instanceof EntityCreature) { if (entity instanceof EntityCreature) {
if (entity instanceof EntityAnimal || entity instanceof EntityWaterAnimal) { if (entity instanceof EntityAnimal || entity instanceof EntityWaterAnimal) {
return checkEntity(plot, Flags.ENTITY_CAP, Flags.MOB_CAP, Flags.ANIMAL_CAP); return EntityUtil.checkEntity(plot, Flags.ENTITY_CAP, Flags.MOB_CAP, Flags.ANIMAL_CAP);
} else if (entity instanceof EntityMob) { } else if (entity instanceof EntityMob) {
return checkEntity(plot, Flags.ENTITY_CAP, Flags.MOB_CAP, Flags.HOSTILE_CAP); return EntityUtil.checkEntity(plot, Flags.ENTITY_CAP, Flags.MOB_CAP, Flags.HOSTILE_CAP);
} else if (entity instanceof EntityHuman) { } else if (entity instanceof EntityHuman) {
return false; return false;
} else { } else {
return checkEntity(plot, Flags.ENTITY_CAP, Flags.MOB_CAP, Flags.MOB_CAP); return EntityUtil.checkEntity(plot, Flags.ENTITY_CAP, Flags.MOB_CAP, Flags.MOB_CAP);
} }
} else { } else {
return checkEntity(plot, Flags.ENTITY_CAP, Flags.MOB_CAP); return EntityUtil.checkEntity(plot, Flags.ENTITY_CAP, Flags.MOB_CAP);
} }
} else if (entity instanceof EntityVehicle) { } else if (entity instanceof EntityVehicle) {
return checkEntity(plot, Flags.ENTITY_CAP, Flags.VEHICLE_CAP); return EntityUtil.checkEntity(plot, Flags.ENTITY_CAP, Flags.VEHICLE_CAP);
} else if (entity instanceof EntityHanging) { } else if (entity instanceof EntityHanging) {
return checkEntity(plot, Flags.ENTITY_CAP, Flags.MISC_CAP); return EntityUtil.checkEntity(plot, Flags.ENTITY_CAP, Flags.MISC_CAP);
} else { } else {
return checkEntity(plot, Flags.ENTITY_CAP); return EntityUtil.checkEntity(plot, Flags.ENTITY_CAP);
} }
} }