Skip to content

Commit

Permalink
Finish implementing sponge support (Fixes #18)
Browse files Browse the repository at this point in the history
  • Loading branch information
games647 committed Jun 13, 2016
1 parent 79e884e commit a822f5d
Show file tree
Hide file tree
Showing 10 changed files with 401 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ private void setSkinUUID(CommandSender sender, Player receiverPayer, String targ
}

plugin.sendMessage(sender, "skin-change-queue");

SkinDownloader skinDownloader = new SkinDownloader(plugin, sender, receiverPayer, uuid);
Bukkit.getScheduler().runTaskAsynchronously(plugin, skinDownloader);
} catch (IllegalArgumentException illegalArgumentException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public void run() {
SkinData targetSkin = plugin.getStorage().getSkin(targetName);
if (targetSkin != null) {
onNameResolveDatabase(targetSkin);

return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import com.github.games647.changeskin.core.ChangeSkinCore;
import com.github.games647.changeskin.core.SkinStorage;
import com.github.games647.changeskin.sponge.commands.SetSkinCommand;
import com.github.games647.changeskin.sponge.commands.SkinInvalidateCommand;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.Lists;
import com.google.common.io.Resources;
import com.google.inject.Inject;
Expand All @@ -10,32 +14,41 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.yaml.YAMLConfigurationLoader;

import org.slf4j.Logger;
import org.spongepowered.api.Game;
import org.spongepowered.api.command.CommandManager;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.args.GenericArguments;
import org.spongepowered.api.command.spec.CommandSpec;
import org.spongepowered.api.config.DefaultConfig;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.game.state.GameInitializationEvent;
import org.spongepowered.api.event.game.state.GamePreInitializationEvent;
import org.spongepowered.api.plugin.Plugin;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.serializer.TextSerializers;

@Plugin(id = "changeskin", name = "ChangeSkin", version = "1.7"
, url = "https://github.com/games647/ChangeSkin"
, description = "Sponge plugin to change your skin server side")
public class ChangeSkinSponge {

@Inject
@DefaultConfig(sharedRoot = true)
@DefaultConfig(sharedRoot = false)
//We will place more than one config there (i.e. H2/SQLite database)
private File defaultConfigFile;

private final Logger logger;
private final Game game;

private Cache<UUID, Object> cooldowns;
private ChangeSkinCore core;
private ConfigurationNode rootNode;

Expand Down Expand Up @@ -92,11 +105,15 @@ public void onPreInit(GamePreInitializationEvent preInitEvent) {
}

List<String> defaultSkins = Lists.newArrayList();
rootNode.getNode("default-skins").getChildrenList().stream()
.forEach((configurationNode) -> defaultSkins.add(configurationNode.getString()));
for (ConfigurationNode node : rootNode.getNode("default-skins").getChildrenMap().values()) {
defaultSkins.add(node.getString());
}

core.loadDefaultSkins(defaultSkins);
loadLocale();

int cooldown = rootNode.getNode("cooldown").getInt();
cooldowns = CacheBuilder.newBuilder().expireAfterWrite(cooldown, TimeUnit.SECONDS).build();
} catch (IOException ioEx) {
logger.error("Failed to load config", ioEx);
}
Expand All @@ -105,21 +122,52 @@ public void onPreInit(GamePreInitializationEvent preInitEvent) {
@Listener //command and event register
public void onInit(GameInitializationEvent initEvent) {
CommandManager commandManager = game.getCommandManager();
// commandManager.register(this, CommandSpec.builder()
// .executor(new SetSkinCommand(this))
// .arguments(GenericArguments.string(Text.of("skin"))
// , GenericArguments.optional(GenericArguments.string(Text.of("target"))))
// .build(), "changeskin");
commandManager.register(this, CommandSpec.builder()
.executor(new SetSkinCommand(this))
.arguments(GenericArguments.string(Text.of("skin")))
.build(), "changeskin", "setskin");

// commandManager.register(this, CommandSpec.builder()
// .executor(new SkinInvalidateCommand(this))
// .build(), "skininvalidate");
commandManager.register(this, CommandSpec.builder()
.executor(new SkinInvalidateCommand(this))
.build(), "skininvalidate");

game.getEventManager().registerListeners(this, new LoginListener(this));
}

private void loadLocale() {
//todo
File messageFile = new File(defaultConfigFile.getParentFile(), "messages.yml");

if (!messageFile.exists()) {
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(messageFile);
Resources.copy(getClass().getResource("/messages.yml"), fileOutputStream);
} catch (IOException ioEx) {
logger.error("Error deploying default message", ioEx);
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException ex) {
//ignore
}
}
}
}

YAMLConfigurationLoader messageLoader = YAMLConfigurationLoader.builder().setFile(messageFile).build();
try {
ConfigurationNode messageNode = messageLoader.load();
for (ConfigurationNode node : messageNode.getChildrenMap().values()) {
core.addMessage((String) node.getKey(), node.getString());
}

for (ConfigurationNode configurationNode : messageNode.getChildrenList()) {

}
} catch (IOException ioEx) {
logger.error("Failed to load locale", ioEx);
}
}

public ChangeSkinCore getCore() {
Expand All @@ -130,6 +178,27 @@ public ConfigurationNode getRootNode() {
return rootNode;
}

public boolean isCooldown(CommandSource sender) {
if (sender instanceof Player) {
return cooldowns.asMap().containsKey(((Player) sender).getUniqueId());
}

return false;
}

public void sendMessage(CommandSource sender, String key) {
String message = core.getMessage(key);
if (message != null && sender != null) {
sender.sendMessage(TextSerializers.LEGACY_FORMATTING_CODE.deserialize(message.replace('&', '§')));
}
}

public void addCooldown(CommandSource sender) {
if (sender instanceof Player) {
cooldowns.put(((Player) sender).getUniqueId(), new Object());
}
}

public Game getGame() {
return game;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public void onPlayerPreLogin(ClientConnectionEvent.Auth preLoginEvent) {
if (preferences.getTargetSkin() == null
&& (!plugin.getRootNode().getNode("restoreSkins").getBoolean() || !refetch(preferences, profile))) {
setDefaultSkin(preferences, profile);
} else {
applySkin(preferences.getTargetSkin(), profile);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.github.games647.changeskin.sponge.commands;

import com.github.games647.changeskin.sponge.ChangeSkinSponge;
import com.github.games647.changeskin.sponge.tasks.NameResolver;
import com.github.games647.changeskin.sponge.tasks.SkinDownloader;

import java.util.UUID;

import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.args.CommandContext;
import org.spongepowered.api.command.spec.CommandExecutor;
import org.spongepowered.api.entity.living.player.Player;

public class SetSkinCommand implements CommandExecutor {

Expand All @@ -18,6 +23,36 @@ public SetSkinCommand(ChangeSkinSponge plugin) {

@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
throw new UnsupportedOperationException("Not supported yet.");
if (!(src instanceof Player)) {
plugin.sendMessage(src, "no-console");
return CommandResult.empty();
}

if (plugin.isCooldown(src)) {
plugin.sendMessage(src, "cooldown");
return CommandResult.empty();
}

Player receiver = (Player) src;
String targetSkin = args.<String>getOne("skin").get();
if (targetSkin.equals("reset")) {
targetSkin = receiver.getUniqueId().toString();
}

if (targetSkin.length() > 16) {
UUID targetUUID = UUID.fromString(targetSkin);

plugin.sendMessage(src, "skin-change-queue");
plugin.getGame().getScheduler().createTaskBuilder()
.execute(new SkinDownloader(plugin, src, receiver, targetUUID))
.submit(plugin);
return CommandResult.success();
}

plugin.sendMessage(src, "queue-name-resolve");
plugin.getGame().getScheduler().createTaskBuilder()
.execute(new NameResolver(plugin, src, targetSkin, receiver))
.submit(plugin);
return CommandResult.success();
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.github.games647.changeskin.sponge.commands;

import com.github.games647.changeskin.sponge.ChangeSkinSponge;
import com.github.games647.changeskin.sponge.tasks.SkinInvalidater;

import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.args.CommandContext;
import org.spongepowered.api.command.spec.CommandExecutor;
import org.spongepowered.api.entity.living.player.Player;

public class SkinInvalidateCommand implements CommandExecutor {

Expand All @@ -18,6 +20,14 @@ public SkinInvalidateCommand(ChangeSkinSponge plugin) {

@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
throw new UnsupportedOperationException("Not supported yet.");
if (!(src instanceof Player)) {
plugin.sendMessage(src, "no-console");
return CommandResult.empty();
}

Player receiver = (Player) src;
plugin.getGame().getScheduler().createTaskBuilder().async()
.execute(new SkinInvalidater(plugin, receiver)).submit(plugin);
return CommandResult.success();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.github.games647.changeskin.sponge.tasks;

import com.github.games647.changeskin.core.NotPremiumException;
import com.github.games647.changeskin.core.RateLimitException;
import com.github.games647.changeskin.core.SkinData;
import com.github.games647.changeskin.sponge.ChangeSkinSponge;

import java.util.UUID;

import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.entity.living.player.Player;

public class NameResolver implements Runnable {

private final ChangeSkinSponge plugin;
private final CommandSource invoker;
private final String targetName;
private final Player receiver;

public NameResolver(ChangeSkinSponge plugin, CommandSource invoker, String targetName, Player receiver) {
this.plugin = plugin;
this.invoker = invoker;
this.targetName = targetName;
this.receiver = receiver;
}

@Override
public void run() {
UUID uuid = plugin.getCore().getUuidCache().get(targetName);
if (uuid == null) {
if (plugin.getCore().getCrackedNames().containsKey(targetName)) {
if (invoker != null) {
plugin.sendMessage(invoker, "not-premium");
}

return;
}

SkinData targetSkin = plugin.getCore().getStorage().getSkin(targetName);
if (targetSkin != null) {
onNameResolveDatabase(targetSkin);
return;
}

try {
uuid = plugin.getCore().getUUID(targetName);
if (uuid == null) {
if (invoker != null) {
plugin.sendMessage(invoker, "no-resolve");
}
} else {
plugin.getCore().getUuidCache().put(targetName, uuid);
onNameResolve(uuid);
}
} catch (NotPremiumException notPremiumEx) {
plugin.getLogger().debug("Requested not premium", notPremiumEx);
plugin.getCore().getCrackedNames().put(targetName, new Object());

if (invoker != null) {
plugin.sendMessage(invoker, "not-premium");
}
} catch (RateLimitException rateLimitEx) {
plugin.getLogger().error("UUID Rate Limit reached", rateLimitEx);
if (invoker != null) {
plugin.sendMessage(invoker, "rate-limit");
}
}
} else {
onNameResolve(uuid);
}
}

private void onNameResolveDatabase(SkinData targetSkin) {
if (invoker != null) {
plugin.sendMessage(invoker, "uuid-resolved");
plugin.sendMessage(invoker, "skin-downloading");
}

SkinUpdater skinUpdater = new SkinUpdater(plugin, invoker, receiver, targetSkin);
plugin.getGame().getScheduler().createTaskBuilder().execute(skinUpdater).submit(plugin);
}

private void onNameResolve(UUID uuid) {
if (invoker != null) {
plugin.sendMessage(invoker, "uuid-resolved");
plugin.sendMessage(invoker, "skin-downloading");
}

//run this is the same thread
new SkinDownloader(plugin, invoker, receiver, uuid).run();
}
}
Loading

0 comments on commit a822f5d

Please sign in to comment.