Skip to content

Commit

Permalink
Merge pull request #10 from CoolLord22/2.2
Browse files Browse the repository at this point in the history
Release 2.2
  • Loading branch information
CoolLord22 committed Feb 2, 2023
2 parents eea0a0c + 68adb8b commit 17c1759
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 89 deletions.
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ doc/*
.externalToolBuilders/New_Builder.launch
.externalToolBuilders/New_Builder (1).launch
*.jar
/target/
/target/

buildNumber.properties
dependency-reduced-pom.xml
dependency-reduced-pom.xml

.idea
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.coollord22.otheranimalteleport</groupId>
<artifactId>OtherAnimalTeleport</artifactId>
<version>2.1</version>
<version>2.2</version>
<packaging>jar</packaging>

<properties>
Expand All @@ -31,7 +31,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.18-R0.1-SNAPSHOT</version>
<version>1.19.3-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
18 changes: 16 additions & 2 deletions src/main/java/com/coollord22/otheranimalteleport/OATMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,26 @@ public static void teleportLeashedEnt(Entity ent, Location from, Location to, Pl

plugin.log.logInfo("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("Protecting entity with damage resistance.", Verbosity.HIGHEST);
plugin.log.logInfo("Protecting entity with invulnerability and resistance.", Verbosity.HIGHEST);
ent.setInvulnerable(true);
((LivingEntity) ent).addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 40, 5));

plugin.log.logInfo("Teleporting entity " + ent.getType() + " with ID: " + ent.getEntityId(), Verbosity.HIGH);
ent.teleport(to);

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

if(plugin.toUseTickets)
fromChunk.removePluginChunkTicket(plugin);

ent.setInvulnerable(invulnerable);
}
}.runTaskLater(plugin, 2);
}
Expand All @@ -40,16 +48,22 @@ public static void teleportEnt(Entity ent, Location from, Location to, Player p,
if(plugin.toUseTickets)
fromChunk.addPluginChunkTicket(plugin);

boolean invulnerable = ent.isInvulnerable();

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

plugin.log.logInfo("Teleporting entity" + ent.getType() + " with ID: " + ent.getEntityId(), Verbosity.HIGH);
ent.teleport(to);

if(plugin.toUseTickets)
fromChunk.removePluginChunkTicket(plugin);

ent.setInvulnerable(invulnerable);
}
}.runTaskLater(plugin, 2);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public void run() {
plugin.enabled = true;

String[] serverVersion = (Bukkit.getBukkitVersion().split("-")[0]).split("\\.");
if(Integer.valueOf(serverVersion[0]) >= 1)
if(Integer.valueOf(serverVersion[1]) >= 14) {
if(Integer.parseInt(serverVersion[0]) >= 1)
if(Integer.parseInt(serverVersion[1]) >= 14) {
toUseTickets = true;
plugin.log.logInfo(ChatColor.RED + "Found server version " + serverVersion[0] + "." + serverVersion[1] + " >= 1.14, using chunk tickets!", Verbosity.HIGH);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package com.coollord22.otheranimalteleport.assets;

import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import org.bukkit.ChatColor;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.YamlConfiguration;

import com.coollord22.otheranimalteleport.OtherAnimalTeleport;
import org.bukkit.event.player.PlayerTeleportEvent;

import java.text.DecimalFormat;
import java.util.Set;

public class OATCommon {
private final OtherAnimalTeleport plugin;
private final DecimalFormat df = new DecimalFormat("#.#");

public OATCommon(OtherAnimalTeleport plugin) {
this.plugin = plugin;
Expand All @@ -33,12 +41,39 @@ public void sendMessage(boolean usePrefix, CommandSender s, String msg) {
else
s.sendMessage(ChatColor.translateAlternateColorCodes('&', msg));
}

static public <E extends Enum<E>> E enumValue(Class<E> clazz, String name) {
try {
return Enum.valueOf(clazz, name);
} catch (IllegalArgumentException e) {

public void sendMessage(boolean usePrefix, String msg, PlayerTeleportEvent event) {
sendMessage(usePrefix, event.getPlayer(), msg
.replaceAll("%x", df.format(event.getFrom().getBlockX()))
.replaceAll("%y", df.format(event.getFrom().getBlockY()))
.replaceAll("%z", df.format(event.getFrom().getBlockZ())));
}

public boolean checkWorldGroup(PlayerTeleportEvent event) {
World fromWorld = event.getFrom().getWorld();
World toWorld = event.getTo().getWorld();

// Blocked region check
if(plugin.config.blockedRegions.containsKey(toWorld)) {
for(ProtectedRegion region : plugin.config.blockedRegions.get(toWorld)) {
if(region.contains(BlockVector3.at(event.getTo().getX(), event.getTo().getY(), event.getTo().getZ()))) {
plugin.log.logInfo("Player teleporting into a blocked region; ignoring entity checks.", Verbosity.HIGHEST);
return false;
}
}
}

// World Group checks
if(fromWorld.equals(toWorld)) {
return true;
} else {
for(Set<World> worldList : plugin.config.worldGroup) {
if(worldList.contains(fromWorld) && worldList.contains(toWorld)) {
return true;
}
}
}
return null;
plugin.log.logInfo("From and To worlds were not found in same group, ending checks.", Verbosity.HIGH);
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class OATConfig {
public boolean gColorLogMessages;
public boolean globalUpdateChecking;
public boolean usePrefix = true;
public boolean ignoreUnknownCauses = false;

public int radius;

Expand All @@ -41,6 +42,7 @@ public class OATConfig {

public String prefix;
public String failedTeleportMessage;
public String leftEntityMessage;

public OATConfig(OtherAnimalTeleport plugin) {
this.plugin = plugin;
Expand Down Expand Up @@ -135,11 +137,13 @@ public void loadConfig() throws FileNotFoundException, IOException, InvalidConfi
gColorLogMessages = globalConfig.getBoolean("color_log_messages", true);
radius = globalConfig.getInt("radius", 2);

ignoreUnknownCauses = globalConfig.getBoolean("ignore_unknown_causes", false);
usePrefix = globalConfig.getBoolean("use_prefix", true);
prefix = globalConfig.getString("prefix", "&7[&aOtherAnimalTeleport&7] ");

//messages
failedTeleportMessage = globalConfig.getString("fail_teleport", "&7An entity could not be teleported and is located near (&c%x&7, &c%y&7, &c%z&7).");
leftEntityMessage = globalConfig.getString("entity_left", "&7An entity was left behind near (&c%x&7, &c%y&7, &c%z&7).");

if(globalConfig.contains("blocked_regions")) {
for(String input : globalConfig.getStringList("blocked_regions")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class Updater {
private String spigotPluginVersion;

private static final int ID = 63497;
private static final String ERR_MSG = ChatColor.RED + "Update checking faile!";
private static final String ERR_MSG = ChatColor.RED + "Update checking failed!";

public Updater(OtherAnimalTeleport plugin) {
this.plugin = plugin;
Expand Down Expand Up @@ -60,11 +60,11 @@ else if(spigotPluginVersion.contains("b")) {

if(!UPDATE_MSG.isEmpty()) {
for(String line : UPDATE_MSG) {
if(p == null)
if(!line.contains("Hooray"))
plugin.log.logWarning(ChatColor.stripColor(line));
if(p == null)
plugin.log.logWarning(ChatColor.stripColor(line));
if(p != null)
plugin.common.sendMessage(plugin.config.usePrefix, p, line);
if(!line.contains("Hooray"))
plugin.common.sendMessage(plugin.config.usePrefix, p, line);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
public class OATListeners implements Listener {

private final OtherAnimalTeleport plugin;
private final DecimalFormat df = new DecimalFormat("#.#");

public OATListeners(OtherAnimalTeleport plugin) {
this.plugin = plugin;
Expand All @@ -39,85 +38,76 @@ public void onJoinUpdateChecker(PlayerJoinEvent event) throws InterruptedExcepti

@EventHandler(priority = EventPriority.MONITOR)
public void onTeleport(PlayerTeleportEvent event) {
boolean sameGroup = false;
World fromWorld = event.getFrom().getWorld();
World toWorld = event.getTo().getWorld();
if(plugin.config.blockedRegions.containsKey(toWorld)) {
for(ProtectedRegion region : plugin.config.blockedRegions.get(toWorld)) {
if(region.contains(BlockVector3.at(event.getTo().getX(), event.getTo().getY(), event.getTo().getZ()))) {
if(plugin.config.failedTeleportMessage != null) {
if(!plugin.config.failedTeleportMessage.isEmpty()) {
plugin.common.sendMessage(plugin.config.usePrefix, event.getPlayer(), plugin.config.failedTeleportMessage
.replaceAll("%x", df.format(event.getFrom().getBlockX()))
.replaceAll("%y", df.format(event.getFrom().getBlockY()))
.replaceAll("%z", df.format(event.getFrom().getBlockZ())));
}
}
return;
}
if(plugin.enabled && !event.isCancelled()) {
if(plugin.config.ignoreUnknownCauses && event.getCause() == PlayerTeleportEvent.TeleportCause.UNKNOWN) {
plugin.log.logInfo("Ignore unknown cause was enabled, ignoring this event.", Verbosity.HIGHEST);
return;
}
}
if(fromWorld.equals(toWorld)) {
sameGroup = true;
} else {
for(Set<World> worldList : plugin.config.worldGroup) {
if(worldList.contains(fromWorld) && worldList.contains(toWorld)) {
sameGroup = true;
break;

/*TODO: Wait for this to be added into 1.19.3*/
try {
if(event.getCause() == PlayerTeleportEvent.TeleportCause.DISMOUNT) {
plugin.log.logInfo("Player dismounted vehicle, ignoring teleport event.", Verbosity.HIGHEST);
return;
}
} catch(NoSuchFieldError err) {
plugin.log.logInfo("Version < 1.19.3 so no DISMOUNT cause found.", Verbosity.HIGHEST);
}
}
if(!sameGroup)
plugin.log.logInfo("From and To worlds were not found in same group, ending checks.", Verbosity.HIGH);
if(plugin.enabled && !event.isCancelled() && sameGroup) {
plugin.log.logInfo("From and To worlds were in same group, allowing permission check.", Verbosity.HIGHEST);

if(event.getPlayer().hasPermission("otheranimalteleport.player.use")) {
plugin.log.logInfo("Player permission check passed, gathering nearby entities.", Verbosity.HIGHEST);
int radius = plugin.config.radius;
boolean toSendError = false;
for(Entity ent : event.getFrom().getWorld().getNearbyEntities(event.getFrom(), radius, radius, radius)) {
plugin.log.logInfo("Found an entity to teleport: " + ent.getType() + " . Checking if it is allowed.", Verbosity.HIGHEST);
if(plugin.config.allowedEnts.contains(ent.getType())) {
plugin.log.logInfo("Entity check passed, seeing if player has leashed permissions.", Verbosity.HIGHEST);
if(ent instanceof LivingEntity && event.getPlayer().hasPermission("otheranimalteleport.player.teleportleashed")) {
if(((LivingEntity) ent).isLeashed() && ((LivingEntity) ent).getLeashHolder().equals(event.getPlayer())) {
try {
plugin.log.logInfo("Attempting to send leashed entity: " + ent.getType() + ".", Verbosity.HIGHEST);
OATMethods.teleportLeashedEnt(ent, event.getFrom(), event.getTo(), event.getPlayer(), plugin);
continue;
} catch(Exception e) {
toSendError = true;
continue;
}
}
toSendError = true;
}
if(ent instanceof Tameable && event.getPlayer().hasPermission("otheranimalteleport.player.teleportpets")) {
if(((Tameable) ent).isTamed() && ((Tameable) ent).getOwner() != null && ((Tameable) ent).getOwner().equals(event.getPlayer())) {
if(ent instanceof Sittable && !((Sittable) ent).isSitting()) {
plugin.log.logInfo("Player use permission check passed, running world checks.", Verbosity.HIGHEST);

if(plugin.common.checkWorldGroup(event)) {
plugin.log.logInfo("World group check passed, gathering nearby entities.", Verbosity.HIGHEST);
int radius = plugin.config.radius;
boolean toSendError = false;
boolean toSendLeft = false;

for(Entity ent : event.getFrom().getWorld().getNearbyEntities(event.getFrom(), radius, radius, radius)) {
plugin.log.logInfo("Found an entity to teleport: " + ent.getType() + " . Checking if it is allowed.", Verbosity.HIGHEST);
if(plugin.config.allowedEnts.contains(ent.getType())) {
plugin.log.logInfo("Entity check passed, seeing if player has leashed permissions.", Verbosity.HIGHEST);
if(ent instanceof LivingEntity && event.getPlayer().hasPermission("otheranimalteleport.player.teleportleashed")) {
if(((LivingEntity) ent).isLeashed() && ((LivingEntity) ent).getLeashHolder().equals(event.getPlayer())) {
try {
plugin.log.logInfo("Attempting to send pet entity: " + ent.getType() + ".", Verbosity.HIGHEST);
OATMethods.teleportEnt(ent, event.getFrom(), event.getTo(), event.getPlayer(), plugin);
plugin.log.logInfo("Attempting to send leashed entity: " + ent.getType() + ".", Verbosity.HIGHEST);
OATMethods.teleportLeashedEnt(ent, event.getFrom(), event.getTo(), event.getPlayer(), plugin);
continue;
} catch(Exception e) {
toSendError = true;
continue;
}
}
toSendLeft = true;
}
toSendError = true;
}
}
}
if(plugin.config.failedTeleportMessage != null) {
if(!plugin.config.failedTeleportMessage.isEmpty() && toSendError) {
plugin.common.sendMessage(plugin.config.usePrefix, event.getPlayer(), plugin.config.failedTeleportMessage
.replaceAll("%x", df.format(event.getFrom().getBlockX()))
.replaceAll("%y", df.format(event.getFrom().getBlockY()))
.replaceAll("%z", df.format(event.getFrom().getBlockZ())));
if(ent instanceof Tameable && event.getPlayer().hasPermission("otheranimalteleport.player.teleportpets")) {
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("Attempting to send pet entity: " + ent.getType() + ".", Verbosity.HIGHEST);
OATMethods.teleportEnt(ent, event.getFrom(), event.getTo(), event.getPlayer(), plugin);
continue;
} catch(Exception e) {
toSendError = true;
continue;
}
}
}
toSendLeft = true;
}
}
}

if(plugin.config.failedTeleportMessage != null && !plugin.config.failedTeleportMessage.isEmpty() && toSendError) {
plugin.common.sendMessage(plugin.config.usePrefix, plugin.config.failedTeleportMessage, event);
}

if(plugin.config.leftEntityMessage != null && !plugin.config.leftEntityMessage.isEmpty() && toSendLeft) {
plugin.common.sendMessage(plugin.config.usePrefix, plugin.config.leftEntityMessage, event);
}
}
}
}
}
plugin.log.logInfo("Event was cancelled/plugin was disabled, ignoring teleport.", Verbosity.HIGHEST);
}
}
Loading

0 comments on commit 17c1759

Please sign in to comment.