Skip to content

Commit

Permalink
Extracting repeated text to string var
Browse files Browse the repository at this point in the history
  • Loading branch information
CoolLord22 committed Mar 31, 2023
1 parent 6ce2cda commit 5066b40
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
14 changes: 8 additions & 6 deletions src/main/java/com/coollord22/otheranimalteleport/OATMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,27 @@

public class OATMethods {
public static void teleportLeashedEnt(Entity ent, Location from, Location to, Player p, OtherAnimalTeleport plugin) {
String entID = "[Ent-" + ent.getEntityId() + "] ";
Chunk fromChunk = from.getChunk();
if(plugin.toUseTickets)
fromChunk.addPluginChunkTicket(plugin);

plugin.log.logInfo("[Ent-" + ent.getEntityId() + "] Attempting to null the leash holder.", Verbosity.HIGHEST);
plugin.log.logInfo(entID + "Attempting to null the leash holder.", Verbosity.HIGHEST);
((LivingEntity) ent).setLeashHolder(null);

boolean invulnerable = ent.isInvulnerable();

new BukkitRunnable() {
@Override
public void run() {
plugin.log.logInfo("[Ent-" + ent.getEntityId() + "] Protecting entity with invulnerability and resistance.", Verbosity.HIGHEST);
plugin.log.logInfo(entID + "Protecting entity with invulnerability and resistance.", Verbosity.HIGHEST);
ent.setInvulnerable(true);
((LivingEntity) ent).addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 40, 5));

plugin.log.logInfo("[Ent-" + ent.getEntityId() + "] Teleporting entity " + ent.getType(), Verbosity.HIGH);
plugin.log.logInfo(entID + "Teleporting entity " + ent.getType(), Verbosity.HIGH);
ent.teleport(to);

plugin.log.logInfo("[Ent-" + ent.getEntityId() + "] Re-attaching leash holder as " + p.getName() + ".", Verbosity.HIGHEST);
plugin.log.logInfo(entID + "Re-attaching leash holder as " + p.getName() + ".", Verbosity.HIGHEST);
((LivingEntity) ent).setLeashHolder(p);

if(plugin.toUseTickets)
Expand All @@ -44,6 +45,7 @@ public void run() {
}

public static void teleportEnt(Entity ent, Location from, Location to, Player p, OtherAnimalTeleport plugin) {
String entID = "[Ent-" + ent.getEntityId() + "] ";
Chunk fromChunk = from.getChunk();
if(plugin.toUseTickets)
fromChunk.addPluginChunkTicket(plugin);
Expand All @@ -53,11 +55,11 @@ public static void teleportEnt(Entity ent, Location from, Location to, Player p,
new BukkitRunnable() {
@Override
public void run() {
plugin.log.logInfo("[Ent-" + ent.getEntityId() + "] Protecting entity with invulnerability and resistance.", Verbosity.HIGHEST);
plugin.log.logInfo(entID + "Protecting entity with invulnerability and resistance.", Verbosity.HIGHEST);
ent.setInvulnerable(true);
((LivingEntity) ent).addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 40, 5));

plugin.log.logInfo("[Ent-" + ent.getEntityId() + "] Teleporting entity" + ent.getType(), Verbosity.HIGH);
plugin.log.logInfo(entID + "Teleporting entity" + ent.getType(), Verbosity.HIGH);
ent.teleport(to);

if(plugin.toUseTickets)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,41 +54,42 @@ public void onTeleport(PlayerTeleportEvent event) {
boolean toSendLeft = false;

for(Entity ent : event.getFrom().getWorld().getNearbyEntities(event.getFrom(), radius, radius, radius)) {
plugin.log.logInfo("[Ent-" + ent.getEntityId() + "] Found a(n) " + ent.getType() + ". Checking if type is allowed.", Verbosity.HIGH);
String entID = "[Ent-" + ent.getEntityId() + "] ";
plugin.log.logInfo(entID + "Found a(n) " + ent.getType() + ". Checking if type is allowed.", Verbosity.HIGH);
if(plugin.config.entityMap.get(ent.getType()) != null && plugin.config.entityMap.get(ent.getType())) {
plugin.log.logInfo("[Ent-" + ent.getEntityId() + "] Entity-type check passed, checking player permissions.", Verbosity.HIGHEST);
plugin.log.logInfo(entID + "Entity-type check passed, checking player permissions.", Verbosity.HIGHEST);
if(ent instanceof LivingEntity && event.getPlayer().hasPermission("otheranimalteleport.player.teleportleashed")) {
plugin.log.logInfo("[Ent-" + ent.getEntityId() + "] Player leashed permissions check passed, checking leash owner.", Verbosity.HIGHEST);
plugin.log.logInfo(entID + "Player leashed permissions check passed, checking leash owner.", Verbosity.HIGHEST);
if(((LivingEntity) ent).isLeashed() && ((LivingEntity) ent).getLeashHolder().equals(event.getPlayer())) {
try {
plugin.log.logInfo("[Ent-" + ent.getEntityId() + "] Leash owner passed. Attempting to teleport entity.", Verbosity.HIGH);
plugin.log.logInfo(entID + "Leash owner passed. Attempting to teleport entity.", Verbosity.HIGH);
OATMethods.teleportLeashedEnt(ent, event.getFrom(), event.getTo(), event.getPlayer(), plugin);
continue;
} catch(Exception e) {
plugin.log.logInfo("[Ent-" + ent.getEntityId() + "] Teleport reached exception. Sending player error.", Verbosity.HIGHEST);
plugin.log.logInfo(entID + "Teleport reached exception. Sending player error.", Verbosity.HIGHEST);
toSendError = true;
continue;
}
}
plugin.log.logInfo("[Ent-" + ent.getEntityId() + "] Left behind. Sending player notification.", Verbosity.HIGHEST);
plugin.log.logInfo(entID + "Left behind. Sending player notification.", Verbosity.HIGHEST);
toSendLeft = true;
}
if(ent instanceof Tameable && event.getPlayer().hasPermission("otheranimalteleport.player.teleportpets")) {
plugin.log.logInfo("[Ent-" + ent.getEntityId() + "] Player pet permissions check passed, checking pet owner.", Verbosity.HIGHEST);
plugin.log.logInfo(entID + "Player pet permissions check passed, checking pet owner.", Verbosity.HIGHEST);
if(((Tameable) ent).isTamed() && ((Tameable) ent).getOwner() != null && ((Tameable) ent).getOwner().equals(event.getPlayer())) {
if(ent instanceof Sittable && !((Sittable) ent).isSitting()) {
try {
plugin.log.logInfo("[Ent-" + ent.getEntityId() + "] Pet checks passed. Attempting to teleport entity.", Verbosity.HIGH);
plugin.log.logInfo(entID + "Pet checks passed. Attempting to teleport entity.", Verbosity.HIGH);
OATMethods.teleportEnt(ent, event.getFrom(), event.getTo(), event.getPlayer(), plugin);
continue;
} catch(Exception e) {
plugin.log.logInfo("[Ent-" + ent.getEntityId() + "] Teleport reached exception. Sending player error.", Verbosity.HIGHEST);
plugin.log.logInfo(entID + "Teleport reached exception. Sending player error.", Verbosity.HIGHEST);
toSendError = true;
continue;
}
}
}
plugin.log.logInfo("[Ent-" + ent.getEntityId() + "] Left behind. Sending player notification.", Verbosity.HIGHEST);
plugin.log.logInfo(entID + "Left behind. Sending player notification.", Verbosity.HIGHEST);
toSendLeft = true;
}
}
Expand Down

0 comments on commit 5066b40

Please sign in to comment.