Skip to content

Commit

Permalink
More stuff update
Browse files Browse the repository at this point in the history
- Added /getcode
- Storage works now
- Organization of the classes
- Updated plugin.yml
- Added /skinfixer
- Added PlayerJoinEventListener
  • Loading branch information
TheDutchMC committed Aug 12, 2020
1 parent ec50352 commit 60d4819
Show file tree
Hide file tree
Showing 16 changed files with 372 additions and 95 deletions.
8 changes: 6 additions & 2 deletions src/main/java/nl/thedutchmc/SkinFixer/JdaHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.MessageChannel;
import nl.thedutchmc.SkinFixer.discordEvents.MessageReceivedEventListener;
import nl.thedutchmc.SkinFixer.files.ConfigurationHandler;

public class JdaHandler {

Expand All @@ -20,10 +21,9 @@ public void setupJda() {
jda.awaitReady();

} catch (LoginException e) {
// TODO Auto-generated catch block
SkinFixer.logWarn("Unable to connecto the Discord API. Is your token correct?");
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Expand All @@ -40,4 +40,8 @@ public static JDA getJda() {
public static MessageChannel getChannel() {
return channel;
}

public static void shutdownJda() throws Exception {
jda.shutdownNow();
}
}
33 changes: 26 additions & 7 deletions src/main/java/nl/thedutchmc/SkinFixer/SkinFixer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,28 @@
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

import nl.thedutchmc.SkinFixer.commandHandlers.GetCodeCommandExecutor;
import nl.thedutchmc.SkinFixer.commandHandlers.SetSkinCommandExecutor;
import nl.thedutchmc.SkinFixer.commandHandlers.SkinFixerCommandExecutor;
import nl.thedutchmc.SkinFixer.files.ConfigurationHandler;
import nl.thedutchmc.SkinFixer.files.StorageHandler;
import nl.thedutchmc.SkinFixer.minecraftEvents.PlayerJoinEventListener;

public class SkinFixer extends JavaPlugin {

public static SkinFixer INSTANCE;
public static StorageHandler STORAGE;

public static final String NMS_VERSION = Bukkit.getServer().getClass().getPackage().getName().substring(23);
public static String PLUGIN_VERSION;

@Override
public void onEnable() {
INSTANCE = this;

PLUGIN_VERSION = Bukkit.getPluginManager().getPlugin("SkinFixer").getDescription().getVersion();

SkinFixer.logInfo("Welcome to SkinFixer version " + PLUGIN_VERSION + " by TheDutchMC!");
SkinFixer.logInfo("Using NMS Version " + SkinFixer.NMS_VERSION);

//Read the configuration
Expand All @@ -24,24 +33,34 @@ public void onEnable() {

//Storage of skins and pending keys
STORAGE = new StorageHandler();
STORAGE.readConfig();
STORAGE.loadConfig();

if(Bukkit.getOnlineMode() == true) logInfo("This plugin is not needed on servers running in online mode!");

//Register command executors
this.getCommand("setskin").setExecutor(new SetSkinCommandExecutor());
this.getCommand("getcode").setExecutor(new GetCodeCommandExecutor());
this.getCommand("skinfixer").setExecutor(new SkinFixerCommandExecutor());

//Setup JDA
JdaHandler jdaHandler = new JdaHandler();
jdaHandler.setupJda();

//TODO EventListeners for joining

if(ConfigurationHandler.useDiscord) {
JdaHandler jdaHandler = new JdaHandler();
jdaHandler.setupJda();
}

//Register event listeners
Bukkit.getPluginManager().registerEvents(new PlayerJoinEventListener(), this);
}

@Override
public void onDisable() {
//TODO Log out JDA to quicken shutdown
logInfo("Shutting down JDA");

try {
JdaHandler.shutdownJda();
} catch(Exception e) {}

logInfo("Thank you for using SkinFixer by TheDutchMC");
}

public static void logInfo(String log) {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/nl/thedutchmc/SkinFixer/SkinObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ public class SkinObject {

private UUID owner;
private String value, signature;
private boolean slim;

public SkinObject(UUID owner, String value, String signature) {
this.owner = owner;
this.value = value;
this.signature = signature;
slim = false;
}

public UUID getOwner() {
Expand All @@ -29,4 +31,12 @@ public void updateSkin(String value, String signature) {
this.value = value;
this.signature = signature;
}

public void setSlim(boolean slim) {
this.slim = slim;
}

public boolean getSlim() {
return slim;
}
}
9 changes: 5 additions & 4 deletions src/main/java/nl/thedutchmc/SkinFixer/changeSkin/GetSkin.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
public class GetSkin {


public static String getSkin(String skinUrl) {
public static String getSkin(String skinUrl, boolean slim) {

HttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost("https://api.mineskin.org/generate/url");
Expand All @@ -28,6 +28,10 @@ public static String getSkin(String skinUrl) {
params.add(new BasicNameValuePair("User-Agent", "Java Spigot Plugin"));
params.add(new BasicNameValuePair("url", skinUrl));

if(slim) {
params.add(new BasicNameValuePair("model", "slim"));
}

try {
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

Expand All @@ -45,13 +49,10 @@ public static String getSkin(String skinUrl) {
return responseBuilder.toString();

} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
import net.md_5.bungee.api.ChatColor;
import nl.thedutchmc.SkinFixer.SkinFixer;
import nl.thedutchmc.SkinFixer.SkinObject;
import nl.thedutchmc.SkinFixer.StorageHandler;
import nl.thedutchmc.SkinFixer.changeSkin.changeGameProfile.ChangeGameProfile;
import nl.thedutchmc.SkinFixer.changeSkin.changeGameProfile.ChangeGameProfile_1_16_r1;
import nl.thedutchmc.SkinFixer.files.StorageHandler;

public class SkinChangeOrchestrator {

static ChangeGameProfile changeGameProfile;

public static void changeSkin(String skinUrl, UUID caller) {
public static void changeSkinJson(String skinUrl, UUID caller, boolean slim) {

//Everything needs to be async, because the watchdog will kill the server because it takes too long
new BukkitRunnable() {
Expand All @@ -32,7 +32,7 @@ public void run() {
p.sendMessage(ChatColor.GOLD + "Fetching skin value and signature...");

//Fetch the skin from Mineskin.org's API
String skinJson = GetSkin.getSkin(skinUrl);
String skinJson = GetSkin.getSkin(skinUrl, slim);

//Get the skin texture value, and the skin texture signature
JSONTokener tokener = new JSONTokener(skinJson);
Expand All @@ -46,48 +46,64 @@ public void run() {
value = (String) texture.get("value");
signature = (String) texture.get("signature");

//Store the skin to the storage file, so it can be reapplied when they join.
if(StorageHandler.skins.containsKey(caller)) {
SkinObject skin = StorageHandler.skins.get(caller);
skin.updateSkin(value, signature);
} else {
SkinObject skin = new SkinObject(caller, value, signature);
StorageHandler.skins.put(caller, skin);
}

p.sendMessage(ChatColor.GOLD + "Applying skin...");

//NMS is version dependant. So we need to set the correct class to use.
switch(SkinFixer.NMS_VERSION) {
case "v1_16_R1": changeGameProfile = new ChangeGameProfile_1_16_r1(); break;

default:
//We dont support the version that the user is running, so we inform them of this.
//Calls to the Bukkit API may only be sync, so it's inside a BukkitRunnable
new BukkitRunnable() {
@Override
public void run() {
Player p = Bukkit.getPlayer(caller);
p.sendMessage(ChatColor.RED + "This server is using a Minecraft version that is not supported by SkinFixer!");
p.sendMessage(ChatColor.RED + "You are running NMS version " + SkinFixer.NMS_VERSION);
}
}.runTask(SkinFixer.INSTANCE);
}

//Change the skins on the GameProfile. Needs to be sync
new BukkitRunnable() {

@Override
public void run() {
Player p = Bukkit.getPlayer(caller);
changeGameProfile.changeProfile(p.getUniqueId(), value, signature);
}
}.runTask(SkinFixer.INSTANCE);

//Inform the player that we're done
p.sendMessage(ChatColor.GOLD + "Done.");
changeSkin(value, signature, caller, slim);
}
}.runTaskAsynchronously(SkinFixer.INSTANCE);
}

public static void changeSkinFromObject(SkinObject skin) {
new BukkitRunnable() {
@Override
public void run() {
changeSkin(skin.getValue(), skin.getSignature(), skin.getOwner(), skin.getSlim());
}
}.runTaskAsynchronously(SkinFixer.INSTANCE);
}

private static void changeSkin(String skinValue, String skinSignature, UUID caller, boolean slim ) {
Player p = Bukkit.getPlayer(caller);

//Store the skin to the storage file, so it can be reapplied when they join.
if(StorageHandler.skins.containsKey(caller)) {
SkinObject skin = StorageHandler.skins.get(caller);
if(slim) skin.setSlim(true);
skin.updateSkin(skinValue, skinSignature);
} else {
SkinObject skin = new SkinObject(caller, skinValue, skinSignature);
if(slim) skin.setSlim(true);
StorageHandler.skins.put(caller, skin);
}

p.sendMessage(ChatColor.GOLD + "Applying skin...");

//NMS is version dependant. So we need to set the correct class to use.
switch(SkinFixer.NMS_VERSION) {
case "v1_16_R1": changeGameProfile = new ChangeGameProfile_1_16_r1(); break;

default:
//We dont support the version that the user is running, so we inform them of this.
//Calls to the Bukkit API may only be sync, so it's inside a BukkitRunnable
new BukkitRunnable() {
@Override
public void run() {
Player p = Bukkit.getPlayer(caller);
p.sendMessage(ChatColor.RED + "This server is using a Minecraft version that is not supported by SkinFixer!");
p.sendMessage(ChatColor.RED + "You are running NMS version " + SkinFixer.NMS_VERSION);
}
}.runTask(SkinFixer.INSTANCE);
}

//Change the skins on the GameProfile. Needs to be sync
new BukkitRunnable() {

@Override
public void run() {
Player p = Bukkit.getPlayer(caller);
changeGameProfile.changeProfile(p.getUniqueId(), skinValue, skinSignature);
}
}.runTask(SkinFixer.INSTANCE);

//Inform the player that we're done
p.sendMessage(ChatColor.GOLD + "Done.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,13 @@ public void reloadSkinForSelf(Player player) {

ep.playerConnection.sendPacket(removeInfo);
ep.playerConnection.sendPacket(addInfo);

System.out.println(loc.getWorld());


World teleportToWorld = null;
for(World w : Bukkit.getWorlds()) {
if(!w.equals(loc.getWorld())) teleportToWorld = w;
}

player.teleport(new Location(teleportToWorld, 0, 1, 0));
player.teleport(new Location(teleportToWorld, 0, 255, 0));
new BukkitRunnable() {
@Override
public void run() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package nl.thedutchmc.SkinFixer.commandHandlers;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import net.md_5.bungee.api.ChatColor;
import nl.thedutchmc.SkinFixer.commonEventMethods.AddNewSkin;

public class GetCodeCommandExecutor implements CommandExecutor {

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

if(!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "This command is for players only!");
return true;
}

if(!sender.hasPermission("skinfixer.getcode") ) {
sender.sendMessage(ChatColor.RED + "You don't have permission to use this command!");
return true;
}

if(args.length == 0) {
sender.sendMessage(ChatColor.RED + "You need to provide a URL to your skin!");
return true;
}

int code = AddNewSkin.add(args[0]);
sender.sendMessage(ChatColor.GOLD + "Your skin has been added! You can apply it with /setskin " + code);

return true;
}
}
Loading

0 comments on commit 60d4819

Please sign in to comment.