Skip to content

Commit

Permalink
tpsingle offline will now consider missing document as the player is …
Browse files Browse the repository at this point in the history
…in same world

Added tp untracked but i'm not willing to release it. too tired
  • Loading branch information
yueyinqiu committed Aug 31, 2020
1 parent ff93615 commit 9302230
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package top.nololiyt.worldpermissions.commands.tp;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import top.nololiyt.worldpermissions.MarksAPILinker;
import top.nololiyt.worldpermissions.RootPlugin;
import top.nololiyt.worldpermissions.commands.Executor;
import top.nololiyt.worldpermissions.entitiesandtools.DotDividedStringBuilder;
import top.nololiyt.worldpermissions.entitiesandtools.MessagesSender;
import top.nololiyt.worldpermissions.entitiesandtools.OfflinePlayersPosition;
import top.nololiyt.worldpermissions.entitiesandtools.StringPair;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class UntrackedExecutor extends Executor
{
protected final static String layerName = "untracked";

@Override
public String permissionName()
{
return null;
}

@Override
public String messageKey()
{
return layerName;
}

@Override
public List<String> getTabComplete(RootPlugin rootPlugin, int ordinal)
{
switch (ordinal)
{
case 0:
MarksAPILinker marksAPI = rootPlugin.getMarksAPILinker();
return marksAPI == null ?
new ArrayList<>(rootPlugin.getLocalMarksManager().allMarksName()) :
marksAPI.getMarksAPI().getAllMarksKey(() -> null);
default:
return new ArrayList<>();
}
}

@Override
protected boolean run(int layer, RootPlugin rootPlugin,
DotDividedStringBuilder messageKey, CommandSender commandSender,
String[] args)
{
if (args.length - 1 != layer)
return false;

String markName = args[layer];

MessagesSender messagesSender = new MessagesSender(rootPlugin.getMessagesManager(),
commandSender, new StringPair[]{
null, null,
StringPair.markName(markName),
StringPair.senderName(commandSender.getName())
});

if ((!rootPlugin.getConfig().getBoolean("offline-players-tracker.enabled")))
{
messagesSender.send(messageKey.append("tracker-not-enabled"));
return true;
}

MarksAPILinker marksAPI = rootPlugin.getMarksAPILinker();
Location location = marksAPI == null ?
rootPlugin.getLocalMarksManager().getMark(markName) :
marksAPI.getMarksAPI().getMark(markName, () -> commandSender);
if (location == null)
{
messagesSender.send(messageKey.append("no-such-mark"));
return true;
}

File dir = new File(rootPlugin.getDataFolder().getAbsolutePath(), "playersData");
dir.mkdirs();
String dirPath = dir.getAbsolutePath();

long suc = 0;
long fai = 0;
DotDividedStringBuilder failureMessage = new DotDividedStringBuilder(messageKey).append("");
for (OfflinePlayer player : Bukkit.getOfflinePlayers())
{
File file = new File(dirPath, player.getUniqueId().toString() + ".yml");
if (file.exists())
continue;

messagesSender.getArgs()[0] = StringPair.playerName(player.getName());
try
{
new OfflinePlayersPosition(location, true).saveTo(file);
suc++;
}
catch (IOException ex)
{
fai++;
messagesSender.send(failureMessage);
ex.printStackTrace();
}
}

messagesSender.getArgs()[0] = StringPair.teleportedCount(String.valueOf(suc));
messagesSender.getArgs()[1] = StringPair.unteleportedCount(String.valueOf(fai));
messagesSender.send(messageKey.append("completed"));
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public String messageKey()
}

@Override
public List<String> getTabComplete(RootPlugin rootPlugin,int ordinal)
public List<String> getTabComplete(RootPlugin rootPlugin, int ordinal)
{
switch (ordinal)
{
Expand Down Expand Up @@ -89,7 +89,7 @@ protected boolean run(int layer, RootPlugin rootPlugin,
return true;
}

if(Bukkit.getPlayer(arguments.playerName) != null)
if (Bukkit.getPlayer(arguments.playerName) != null)
{
messagesSender.send(messageKey.append("player-is-online"));
return true;
Expand All @@ -110,7 +110,7 @@ protected boolean run(int layer, RootPlugin rootPlugin,
}

World world = null;
if(!arguments.worldName.isEmpty())
if (!arguments.worldName.isEmpty())
{
world = Bukkit.getWorld(arguments.worldName);
if (world == null)
Expand Down Expand Up @@ -179,13 +179,13 @@ private boolean setPosition(File dir, OfflinePlayer player, World world, Locatio
{
dir.mkdirs();
File file = new File(dir.getAbsolutePath(), player.getUniqueId().toString() + ".yml");
if (file.exists())
if (!file.exists())
return false;

OfflinePlayersPosition position = OfflinePlayersPosition.fromFile(file);
if (world != null && (!position.getPosition().getWorld().equals(world)))
{
OfflinePlayersPosition position = OfflinePlayersPosition.fromFile(file);
if (world != null && (!position.getPosition().getWorld().equals(world)))
{
return false;
}
return false;
}
new OfflinePlayersPosition(mark, true).saveTo(file);
return true;
Expand Down

0 comments on commit 9302230

Please # to comment.