Skip to content

Commit

Permalink
Added MyWarp importer
Browse files Browse the repository at this point in the history
  • Loading branch information
datdenkikniet committed Jul 18, 2017
1 parent 3a604c4 commit cf316d6
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 13 deletions.
7 changes: 4 additions & 3 deletions resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name: Warpalicious
main: nl.datdenkikniet.warpalicious.WarpaliciousPlugin
authors: [datdenkikniet, TastyBlueberry, oskar3123]
version: 1.7.5
softdepend: [Multiverse-Core, MultiWorld]
version: 1.8.0
softdepend: [Multiverse-Core, MultiWorld, MyWarp]
commands:
delwarp:
description: delete a warp
Expand Down Expand Up @@ -40,4 +40,5 @@ commands:
usage: /<command> <privatewarp> <playername>
warpuninvite:
description: disallow players to warp to a private warp
usage: /<command> <privatewarp> <playername>
usage: /<command> <privatewarp> <playername>

168 changes: 161 additions & 7 deletions src/nl/datdenkikniet/warpalicious/commands/WarpaliciousCommand.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
package nl.datdenkikniet.warpalicious.commands;

import me.taylorkelly.mywarp.MyWarp;
import me.taylorkelly.mywarp.bukkit.MyWarpPlugin;
import me.taylorkelly.mywarp.warp.EventfulPopulatableWarpManager;
import me.taylorkelly.mywarp.warp.MemoryPopulatableWarpManager;
import me.taylorkelly.mywarp.warp.PopulatableWarpManager;
import me.taylorkelly.mywarp.warp.Warp;
import nl.datdenkikniet.warpalicious.WarpaliciousPlugin;
import nl.datdenkikniet.warpalicious.config.messages.Strings;
import nl.datdenkikniet.warpalicious.handling.Flag;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;

/**
* Created by Jona on 21/10/2016.
*/
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.UUID;

public class WarpaliciousCommand implements CommandExecutor
{

Expand All @@ -23,16 +35,158 @@ public WarpaliciousCommand(WarpaliciousPlugin pl, Strings instance)

public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
{
if (args.length == 1 && args[0].equalsIgnoreCase("reloadmessages") && str.checkPermission(sender, str.universalPerm))
if (args.length == 1)
{
if (args[0].equalsIgnoreCase("reloadmessages") && str.checkPermission(sender, str.universalPerm))
{
plugin.getStrings().loadMessages();
sender.sendMessage(str.prefix + " Succesfully reloaded messages!");
return true;
}
else
{
sender.sendMessage(str.getUsage(cmd, label));
return true;
}
}
else if (args.length == 2)
{
plugin.getStrings().loadMessages();
sender.sendMessage(str.prefix + " Succesfully reloaded messages!");
if (args[0].equalsIgnoreCase("import"))
{
if (args[1].equalsIgnoreCase("mywarp"))
{
if (str.checkPermission(sender, str.universalPerm))
{
MyWarpPlugin mwp = (MyWarpPlugin) plugin.getServer().getPluginManager().getPlugin("MyWarp");
if (mwp == null)
{
sender.sendMessage("The MyWarp plugin is not installed on this server!");
return true;
}
try
{
Field myWarpField = mwp.getClass().getDeclaredField("myWarp");
myWarpField.setAccessible(true);
EventfulPopulatableWarpManager ewm = (EventfulPopulatableWarpManager) ((MyWarp) myWarpField.get(mwp)).getWarpManager();

Method delegateEPWM = ewm.getClass().getDeclaredMethod("delegate");
delegateEPWM.setAccessible(true);
PopulatableWarpManager pwm = (PopulatableWarpManager) delegateEPWM.invoke(ewm);

Method delegatePWM = pwm.getClass().getDeclaredMethod("delegate");
delegatePWM.setAccessible(true);
MemoryPopulatableWarpManager mpw = (MemoryPopulatableWarpManager) delegatePWM.invoke(pwm);

Field warpMapField = mpw.getClass().getDeclaredField("warpMap");
warpMapField.setAccessible(true);

@SuppressWarnings("unchecked")
HashMap<String, me.taylorkelly.mywarp.warp.Warp> warpMap = (HashMap<String, me.taylorkelly.mywarp.warp.Warp>) warpMapField.get(mpw);

int addedWarps = 0;
int failedWarps = 0;
int totalWarps = warpMap.size();
for (String wName : warpMap.keySet())
{

Object initialObj = warpMap.get(wName);

Field warpField = initialObj.getClass().getDeclaredField("delegate");
warpField.setAccessible(true);

Warp delegateFirst = (Warp) warpField.get(initialObj);

Field warpFieldSecond = delegateFirst.getClass().getDeclaredField("delegate");
warpFieldSecond.setAccessible(true);

Warp warpMyWarp = (Warp) warpFieldSecond.get(delegateFirst);

Field posField = warpMyWarp.getClass().getDeclaredField("position");
Field rotField = warpMyWarp.getClass().getDeclaredField("rotation");
Field worldUUIDField = warpMyWarp.getClass().getDeclaredField("worldIdentifier");
posField.setAccessible(true);
rotField.setAccessible(true);
worldUUIDField.setAccessible(true);

UUID worldUUID = (UUID) worldUUIDField.get(warpMyWarp);

Field xField = posField.get(warpMyWarp).getClass().getDeclaredField("x");
Field yField = posField.get(warpMyWarp).getClass().getDeclaredField("y");
Field zField = posField.get(warpMyWarp).getClass().getDeclaredField("z");
xField.setAccessible(true);
yField.setAccessible(true);
zField.setAccessible(true);

Field pitchField = rotField.get(warpMyWarp).getClass().getDeclaredField("x");
Field yawField = rotField.get(warpMyWarp).getClass().getDeclaredField("y");
yawField.setAccessible(true);
pitchField.setAccessible(true);

double x = (double) xField.get(posField.get(warpMyWarp));
double y = (double) yField.get(posField.get(warpMyWarp));
double z = (double) zField.get(posField.get(warpMyWarp));

float yaw = (float) yawField.get(rotField.get(warpMyWarp));
float pitch = (float) pitchField.get(rotField.get(warpMyWarp));

Location loc = new Location(Bukkit.getWorld(worldUUID), x, y, z, yaw, pitch);
ArrayList<UUID> invitedPlayers = new ArrayList<>();
invitedPlayers.addAll(warpMyWarp.getInvitedPlayers());

HashMap<Flag, Boolean> defFlags = plugin.getWarpHandler().getDefaultFlags();

if (warpMyWarp.getType() == Warp.Type.PRIVATE)
{
defFlags.put(Flag.PRIVATE, true);
}

if (plugin.getWarpHandler().getWarp(wName) == null)
{
new nl.datdenkikniet.warpalicious.handling.Warp(plugin, warpMyWarp.getCreator(), loc, warpMyWarp.getName(), defFlags, plugin.getWarpHandler(), warpMyWarp.getVisits(), invitedPlayers, wName);
System.out.println("Imported " + (defFlags.get(Flag.PRIVATE) ? "private" : "") + " warp with name " + wName + " at location x:" + x + ", y:" + y + ", z:" + z + ", yaw:" + yaw + ", pitch:" + pitch + " and " + invitedPlayers.size() + " invited players");
addedWarps++;
}
else
{
System.out.println("Failed to add import warp " + wName + ". A warp with that name already exists!");
failedWarps++;
}
}
sender.sendMessage("Attempting to import " + totalWarps + " warps...");
sender.sendMessage("Imported " + addedWarps + " warps from mywarp.");
sender.sendMessage("Failed to add " + failedWarps + " warps (check the console for more information).");
}
catch (Exception ex)
{
ex.printStackTrace();
sender.sendMessage("Something went wrong!");
return true;
}
return true;
}
else
{
sender.sendMessage(str.noperm);
return true;
}
}
else
{
sender.sendMessage(str.getUsage(cmd, label));
return true;
}
}
else
{
sender.sendMessage(str.getUsage(cmd, label));
return true;
}
}
else
{
sender.sendMessage(str.prefix + " This server is running Warpalicious version " + plugin.getDescription().getVersion() + " by datdenkikniet.");
return true;
}
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,17 @@ public boolean checkPermission(Permissible p, String permission)
{
return p.hasPermission(permission) || p.hasPermission(universalPerm);
} else {
boolean hasNegator = false;
boolean hasPerm = false;
for (PermissionAttachmentInfo at : p.getEffectivePermissions()){
if (at.getPermission().equalsIgnoreCase(permission)){
return true;
hasPerm = true;
}
if (at.getPermission().equalsIgnoreCase(permission) && !at.getValue()){
hasNegator = true;
}
}
return false;
return hasPerm && !hasNegator;
}
}
}
2 changes: 1 addition & 1 deletion src/nl/datdenkikniet/warpalicious/handling/Warp.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ public Warp(WarpaliciousPlugin instance, UUID owner, Location loc, String name,
this.loc = loc;
this.name = name;
this.flags = flags;
handler.addWarp(this);
this.timesWarpedTo = time;
this.plugin = instance;
this.invitedPlayers = invited;
this.worldName = worldName;
handler.addWarp(this);
}

public boolean isPrivate()
Expand Down

0 comments on commit cf316d6

Please sign in to comment.