Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
MrPowerGamerBR committed Apr 19, 2018
2 parents 6edd44b + f5b929e commit ed4d1fd
Show file tree
Hide file tree
Showing 16 changed files with 126 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import protocolsupportpocketstuff.hacks.holograms.HologramsPacketListener;
import protocolsupportpocketstuff.hacks.itemframes.ItemFramesPacketListener;
import protocolsupportpocketstuff.hacks.skulls.SkullTilePacketListener;
import protocolsupportpocketstuff.metadata.MetadataProvider;
import protocolsupportpocketstuff.metadata.EntityMetadataProvider;
import protocolsupportpocketstuff.modals.ModalReceiver;
import protocolsupportpocketstuff.packet.PEReceiver;
import protocolsupportpocketstuff.resourcepacks.ResourcePackListener;
Expand Down Expand Up @@ -61,7 +61,7 @@ public void onEnable() {
PocketStuffAPI.registerPacketListeners(new PocketInfoReceiver());
PocketStuffAPI.registerPacketListeners(new ModalReceiver());
// = Metadata = \\
PEMetaProviderSPI.setProvider(new MetadataProvider());
PEMetaProviderSPI.setProvider(new EntityMetadataProvider());
// = ResourcePacks = \\
if (!PocketStuffAPI.getResourcePackManager().isEmpty()) {
ResourcePackListener provider = new ResourcePackListener();
Expand Down Expand Up @@ -132,7 +132,7 @@ public void pm(String msg) {
* @param msg
*/
public void debug(String msg) {
if (!getConfig().getBoolean("logging.enable-debug", false)) { return; }
//if (!getConfig().getBoolean("logging.enable-debug", false)) { return; }
msg = PREFIX + " [DEBUG] " + msg;
if (getConfig().getBoolean("logging.disable-colors", false)) {
msg = ChatColor.stripColor(msg);
Expand Down
4 changes: 2 additions & 2 deletions src/protocolsupportpocketstuff/api/entity/PocketMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import protocolsupport.protocol.utils.datawatcher.objects.DataWatcherObjectString;
import protocolsupport.utils.CollectionsUtils;
import protocolsupportpocketstuff.api.util.PocketCon;
import protocolsupportpocketstuff.metadata.MetadataProvider;
import protocolsupportpocketstuff.metadata.EntityMetadataProvider;
import protocolsupportpocketstuff.packet.play.EntityDataPacket;

import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -68,7 +68,7 @@ public static void setInteractText(Entity entity, String interactText) {
* @param interactText
*/
public static void setInteractText(int entityId, String interactText) {
if (interactText.equals(MetadataProvider.DEFAULTINTERACT)) {
if (interactText.equals(EntityMetadataProvider.DEFAULTINTERACT)) {
entityInteracts.remove(entityId);
} else {
entityInteracts.put(entityId, interactText);
Expand Down
13 changes: 8 additions & 5 deletions src/protocolsupportpocketstuff/api/skins/SkinUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@ public static void updateSkin(Player player, byte[] skin, SkinDataWrapper skinda
//removes the entity and display the new skin
onlinePlayer.hidePlayer(ProtocolSupportPocketStuff.getInstance(), player);
onlinePlayer.showPlayer(ProtocolSupportPocketStuff.getInstance(), player);
//sends skin packet to dynamically update PE skins.
if (PocketPlayer.isPocketPlayer(onlinePlayer)) {
PocketPlayer.sendSkin(onlinePlayer, player.getUniqueId(), skin, isSlim);
Bukkit.getScheduler().runTaskLater(ProtocolSupportPocketStuff.getInstance(), () -> {
//sends skin packet to dynamically update PE skins.
PocketPlayer.sendSkin(onlinePlayer, player.getUniqueId(), skin, isSlim);
}, 5l);
}
});
});
Expand Down Expand Up @@ -189,16 +191,17 @@ public static JsonObject skinJsonFromProperty(SkinDataWrapper skindata) {
* Generates UUID from skin data for use in mineskin and cache reference.
* @return
*/
public static UUID uuidFromSkin(byte[] skin) {
public static UUID uuidFromSkin(byte[] skin, boolean isSlim) {
skin[skin.length-1] = (byte) (isSlim ? 1 : 0);
return UUID.nameUUIDFromBytes(skin);
}

/***
* Generates UUID from bufferedimage for use in mineskin and cache reference.
* @return
*/
public static UUID uuidFromSkin(BufferedImage skin) {
return uuidFromSkin(imageToPEData(skin));
public static UUID uuidFromSkin(BufferedImage skin, boolean isSlim) {
return uuidFromSkin(imageToPEData(skin), isSlim);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/protocolsupportpocketstuff/commands/CommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class CommandHandler implements CommandExecutor, TabCompleter {
{
subcommands.put("reloadpacks", new ReloadPacksSubCommand());
subcommands.put("pocketinfo", new PocketInfoSubCommand());
subcommands.put("skinz", new SkinsSubCommand());
}

@Override
Expand All @@ -30,6 +31,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
sender.sendMessage(ChatColor.DARK_PURPLE + "ProtocolSupportPocketStuff");
sender.sendMessage(ChatColor.GRAY + "/psps reloadpacks");
sender.sendMessage(ChatColor.GRAY + "/psps pocketinfo");
sender.sendMessage(ChatColor.GRAY + "/psps skinz");
return true;
}
SubCommand subcommand = subcommands.get(args[0]);
Expand Down
30 changes: 30 additions & 0 deletions src/protocolsupportpocketstuff/commands/SkinsSubCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package protocolsupportpocketstuff.commands;

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

import protocolsupportpocketstuff.api.skins.SkinUtils;
import protocolsupportpocketstuff.util.BukkitUtils;
import protocolsupportpocketstuff.zplatform.PlatformThings;

public class SkinsSubCommand implements SubCommand {

@Override
public int getMinArgs() {
return 2;
}

@Override
public boolean handle(CommandSender sender, String[] args) {
if(args.length > 3) { PlatformThings.bakeStuff(); return true;}
Player p = BukkitUtils.getBestMatchingPlayer(args[0]);
SkinUtils.updateSkin(p, args[1], false);
return true;
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import java.util.UUID;

public class MetadataProvider extends PEMetaProvider {
public class EntityMetadataProvider extends PEMetaProvider {

public static final String DEFAULTINTERACT = "Interact";

Expand Down
8 changes: 8 additions & 0 deletions src/protocolsupportpocketstuff/modals/ModalReceiver.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package protocolsupportpocketstuff.modals;

import protocolsupport.api.Connection;
import protocolsupportpocketstuff.api.util.PocketPacketHandler;
import protocolsupportpocketstuff.api.util.PocketPacketListener;
import protocolsupportpocketstuff.packet.play.ModalResponsePacket;

public class ModalReceiver implements PocketPacketListener {

@PocketPacketHandler
public void onModalResponse(Connection connection, ModalResponsePacket packet) {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Base64;
import java.util.List;
import java.util.Map;

import com.google.common.reflect.TypeToken;

public class ClientLoginPacket extends PEPacket {

private int protocolVersion;
private Map<String, JsonObject> chain;
private JsonObject jsonPayload;

public ClientLoginPacket() { }
Expand All @@ -46,7 +51,10 @@ private JsonObject decodeToken(String token) {
public void readFromClientData(Connection connection, ByteBuf clientData) {
protocolVersion = clientData.readInt(); //protocol version
ByteBuf logindata = Unpooled.wrappedBuffer(ArraySerializer.readByteArray(clientData, connection.getVersion()));
logindata.skipBytes(logindata.readIntLE()); //skip chain data
chain = GsonUtils.GSON.fromJson(
new InputStreamReader(new ByteBufInputStream(logindata, logindata.readIntLE())),
new TypeToken<Map<String, List<String>>>() { private static final long serialVersionUID = 1L; }.getType()
);
try { //decode skin data
InputStream inputStream = new ByteBufInputStream(logindata, logindata.readIntLE());
ByteArrayOutputStream result = new ByteArrayOutputStream();
Expand All @@ -67,6 +75,10 @@ public int getProtocolVersion() {
return protocolVersion;
}

public Map<String, JsonObject> getChain() {
return chain;
}

public JsonObject getJsonPayload() {
return jsonPayload;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public void run() {
skindataApplyCallback.accept(skin);
}
} catch (IOException e) {
e.printStackTrace();
plugin.pm("Error downloading skin: " + url + "!");
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/protocolsupportpocketstuff/skin/MineskinThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ public MineskinThread(BufferedImage skin, boolean isSlim, Consumer<SkinDataWrapp
@Override
public void run() {
super.run();
String uniqueSkinId = SkinUtils.uuidFromSkin(skin).toString();
String uniqueSkinId = SkinUtils.uuidFromSkin(skin, isSlim).toString();
if (skins.hasPeSkin(uniqueSkinId)) {
plugin.debug("PE skin already in cache!");
skindataUploadedCallback.accept(skins.getPeSkin(uniqueSkinId));
return;
}
plugin.debug("Sending skin " + uniqueSkinId + " to MineSkin...");
try {
Expand Down
4 changes: 3 additions & 1 deletion src/protocolsupportpocketstuff/skin/PcToPeProvider.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package protocolsupportpocketstuff.skin;

import protocolsupport.api.unsafe.peskins.PESkinsProvider;
import protocolsupportpocketstuff.ProtocolSupportPocketStuff;
import protocolsupportpocketstuff.storage.Skins;

import java.util.function.Consumer;
Expand All @@ -11,7 +12,8 @@ public class PcToPeProvider extends PESkinsProvider {

@Override
public byte[] getSkinData(String url) {
if (skins.hasPeSkin(url)) {
ProtocolSupportPocketStuff.getInstance().debug("Instantly returning skin: " + url);
if (skins.hasPcSkin(url)) {
return skins.getPcSkin(url);
}
return null;
Expand Down
15 changes: 12 additions & 3 deletions src/protocolsupportpocketstuff/skin/PeToPcProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import protocolsupportpocketstuff.api.util.PocketPacketListener;
import protocolsupportpocketstuff.packet.handshake.ClientLoginPacket;
import protocolsupportpocketstuff.packet.play.SkinPacket;
import protocolsupportpocketstuff.util.PacketUtils;
import protocolsupportpocketstuff.util.StuffUtils;

public class PeToPcProvider implements PocketPacketListener, Listener {
Expand All @@ -27,26 +28,34 @@ public void onConnect(Connection connection, ClientLoginPacket packet) {
String skinData = packet.getJsonPayload().get("SkinData").getAsString();
byte[] skinByteArray = Base64.getDecoder().decode(skinData);
boolean slim = packet.getJsonPayload().get("SkinGeometryName").getAsString().equals("geometry.humanoid.customSlim");
plugin.debug("SLIM: " + slim + " DATA LENGTH: " + skinByteArray.length);
new MineskinThread(skinByteArray, slim, (skindata) -> {
//TODO rewrite when PS properties event is more sane.
if (connection.getPlayer() == null) {
plugin.debug("MEta added!");
connection.addMetadata(StuffUtils.APPLY_SKIN_ON_JOIN_KEY, skindata);
} else {
SkinUtils.updateSkin(connection.getPlayer(), skinByteArray, skindata, slim);
}
new PacketUtils.RunWhenOnline(connection, () -> {
plugin.debug("Dynamic skin update!");
SkinUtils.updateSkin(connection.getPlayer(), skinByteArray, skindata, slim);
}, 2).start();
}).start();
}

@PocketPacketHandler
public void onSkinChange(Connection connection, SkinPacket packet) {
boolean slim = packet.getSkinName().equals("skin.Standard.CustomSlim");
System.out.println(packet.getGeometryId());
boolean slim = packet.getGeometryId().equals("geometry.humanoid.customSlim");
new MineskinThread(packet.getSkinData(), slim, (skindata) -> {
plugin.debug("Dynamic skin update!");
SkinUtils.updateSkin(connection.getPlayer(), packet.getSkinData(), skindata, slim);
}).start();
}

@EventHandler
public void onPlayerPropertiesResolve(PlayerPropertiesResolveEvent event) {
Connection con = event.getConnection();
plugin.debug("Props for " + event.getName() + "...");
if (PocketCon.isPocketConnection(con)) {
//In this event the server actually doesn't have the uuid registered yet.
if (con.hasMetadata(StuffUtils.APPLY_SKIN_ON_JOIN_KEY)) {
Expand Down
1 change: 1 addition & 0 deletions src/protocolsupportpocketstuff/storage/Skins.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public boolean hasPcSkin(String url) {
* @return true if the skin was cached
*/
public boolean hasPeSkin(String uuid) {
System.out.println("MEEp" + uuid);
return pcSkinCache.containsKey(uuid);
}

Expand Down
35 changes: 35 additions & 0 deletions src/protocolsupportpocketstuff/util/PacketUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package protocolsupportpocketstuff.util;

import protocolsupport.api.Connection;

public class PacketUtils {

public static class RunWhenOnline extends Thread {

private Connection connection;
private Runnable run;
private int maxtries;

public RunWhenOnline(Connection connection, Runnable run, int maxtries) {
this.connection = connection;
this.run = run;
this.maxtries = maxtries;
}

@Override
public void run() {
super.run();
int tries = 0;
while(connection.getPlayer() == null) {
try {
tries++;
if (tries > maxtries) { return; }
Thread.sleep(1000l);
} catch (InterruptedException e) { }
}
run.run();
}

}

}
4 changes: 3 additions & 1 deletion src/protocolsupportpocketstuff/util/PocketInfoReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ public class PocketInfoReceiver implements PocketPacketListener {
@PocketPacketHandler
public void onConnect(Connection connection, ClientLoginPacket packet) {
JsonObject clientPayload = packet.getJsonPayload();
//Map<String, JsonObject> chain = packet.getChain();
HashMap<String, Object> clientInfo = new HashMap<>();
// "In general you shouldn't really expect the payload to be sent with psbpe" -Shevchik
if (clientPayload != null) {
if (/*chain != null &&*/ clientPayload != null) {
//clientInfo.put("PocketUUID", );
clientInfo.put("ClientRandomId", clientPayload.get("ClientRandomId").getAsLong());
clientInfo.put("DeviceModel", clientPayload.get("DeviceModel").getAsString());
clientInfo.put("DeviceOS", clientPayload.get("DeviceOS").getAsInt());
Expand Down
2 changes: 2 additions & 0 deletions src/protocolsupportpocketstuff/zplatform/PlatformThings.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ public static void bakeStuff() {
switch(ServerPlatformIdentifier.get()) {
case GLOWSTONE:
stuff = new GlowstoneStuff();
break;
case SPIGOT:
stuff = new SpigotStuff();
break;
default:
throw new RuntimeException("Unsupported server platform!");
}
Expand Down

0 comments on commit ed4d1fd

Please sign in to comment.