Skip to content

Commit

Permalink
Adding backwards compatibility for chunk tickets
Browse files Browse the repository at this point in the history
Since chunk tickets were only added in 1.14 and above, if plugin is running on older version we will not use them.
  • Loading branch information
CoolLord22 committed Aug 11, 2021
1 parent 399c36d commit e7434a0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 36 deletions.
19 changes: 11 additions & 8 deletions src/main/java/com/coollord22/otheranimalteleport/OATMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
public class OATMethods {
public static void teleportLeashedEnt(Entity ent, Location from, Location to, Player p, OtherAnimalTeleport plugin) {
Chunk fromChunk = from.getChunk();
fromChunk.addPluginChunkTicket(plugin);

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

plugin.log.logInfo("Attempting to null the leash holder.", Verbosity.HIGHEST);
((LivingEntity) ent).setLeashHolder(null);

Expand All @@ -28,25 +29,27 @@ public void run() {
ent.teleport(to);
plugin.log.logInfo("Re-attaching leash holder as " + p.getName() + ".", Verbosity.HIGHEST);
((LivingEntity) ent).setLeashHolder(p);

fromChunk.removePluginChunkTicket(plugin);
if(plugin.toUseTickets)
fromChunk.removePluginChunkTicket(plugin);
}
}.runTaskLater(plugin, 2);
}

public static void teleportEnt(Entity ent, Location from, Location to, Player p, OtherAnimalTeleport plugin) {
Chunk fromChunk = from.getChunk();
fromChunk.addPluginChunkTicket(plugin);

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

new BukkitRunnable() {
@Override
public void run() {
plugin.log.logInfo("Protecting entity with damage resistance.", Verbosity.HIGHEST);
((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);

fromChunk.removePluginChunkTicket(plugin);

if(plugin.toUseTickets)
fromChunk.removePluginChunkTicket(plugin);
}
}.runTaskLater(plugin, 2);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class OtherAnimalTeleport extends JavaPlugin {
public int pluginID = 8020;

public boolean enabled;
public boolean toUseTickets = false;

public OtherAnimalTeleport() {
plugin = this;
Expand All @@ -54,6 +55,13 @@ public void run() {
new Metrics(plugin, pluginID);
plugin.log.logInfo(ChatColor.GREEN + "AnimalTeleport has been enabled!", Verbosity.LOW);
plugin.enabled = true;

String[] serverVersion = (Bukkit.getBukkitVersion().split("-")[0]).split("\\.");
if(Integer.valueOf(serverVersion[0]) >= 1)
if(Integer.valueOf(serverVersion[1]) >= 14) {
toUseTickets = true;
plugin.log.logInfo(ChatColor.RED + "Found server version " + serverVersion[0] + "." + serverVersion[1] + " >= 1.14, using chunk tickets!", Verbosity.HIGH);
}
}
}, 1L);
writeNames(EntityType.class);
Expand All @@ -66,7 +74,7 @@ private void initCommon() {
this.log = new Log(this);
this.common = new OATCommon(this);
}

private void initConfig() {
getDataFolder().mkdirs();
config = new OATConfig(this);
Expand All @@ -81,32 +89,32 @@ private void registerListeners() {
private void registerCommands() {
new OtherAnimalCommand(this);
}
public static void writeNames(Class<? extends Enum<?>> e) {
writeNames(e.getSimpleName(), e);
}

public static void writeNames(String filename, Class<? extends Enum<?>> e) {
List<String> list = new ArrayList<String>();

for (Enum<?> stuff : e.getEnumConstants()) {
list.add(stuff.toString());
}

try {
BufferedWriter out = null;
File folder = plugin.getDataFolder();
File configFile = new File(folder.getAbsolutePath() + File.separator + "known_" + filename + ".txt");
configFile.getParentFile().mkdirs();
configFile.createNewFile();
out = new BufferedWriter(new FileWriter(configFile));
Collections.sort(list);
for(String mat : list)
out.write(mat + "\n");
out.close();
} catch (IOException exception) {
exception.printStackTrace();
}
}

public static void writeNames(Class<? extends Enum<?>> e) {
writeNames(e.getSimpleName(), e);
}

public static void writeNames(String filename, Class<? extends Enum<?>> e) {
List<String> list = new ArrayList<String>();

for (Enum<?> stuff : e.getEnumConstants()) {
list.add(stuff.toString());
}

try {
BufferedWriter out = null;
File folder = plugin.getDataFolder();
File configFile = new File(folder.getAbsolutePath() + File.separator + "known_" + filename + ".txt");
configFile.getParentFile().mkdirs();
configFile.createNewFile();
out = new BufferedWriter(new FileWriter(configFile));
Collections.sort(list);
for(String mat : list)
out.write(mat + "\n");
out.close();
} catch (IOException exception) {
exception.printStackTrace();
}
}

}

0 comments on commit e7434a0

Please sign in to comment.