Skip to content

Commit

Permalink
Skin changer is fully implemented #9. Plugin is broken.
Browse files Browse the repository at this point in the history
  • Loading branch information
PauMAVA committed May 5, 2020
1 parent ef70322 commit 1cf4417
Show file tree
Hide file tree
Showing 39 changed files with 94 additions and 67 deletions.
14 changes: 10 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
<artifactId>UhcPlugin</artifactId>
<version>1.2</version>
<build>
<sourceDirectory>src</sourceDirectory>
<sourceDirectory>src/main/java</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<directory>src/main/resources</directory>
<includes>
<include>**/*.yml</include>
</includes>
</resource>
<resource>
<directory>resources/lang-packages</directory>
<directory>src/main/resources/lang-packages</directory>
</resource>
</resources>
<plugins>
Expand Down Expand Up @@ -88,13 +88,19 @@
<dependency>
<groupId>me.PauMAVA</groupId>
<artifactId>MojangAPI</artifactId>
<version>2020-0.3</version>
<version>2020-0.4</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

package me.PauMAVA.UhcPlugin;

import javafx.scene.control.Skin;
import me.PauMAVA.UhcPlugin.commands.UhcCmdHub;
import me.PauMAVA.UhcPlugin.commands.UhcCompleteTab;
import me.PauMAVA.UhcPlugin.gameplay.CustomRecipes;
Expand All @@ -35,10 +34,6 @@
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Logger;

public class UhcPluginCore extends JavaPlugin {
Expand All @@ -64,7 +59,6 @@ public void onEnable() {
this.getServer().getPluginManager().registerEvents(new EventsRegister(), this);
this.getCommand("uhc").setExecutor(new UhcCmdHub());
this.getCommand("uhc").setTabCompleter(new UhcCompleteTab());
this.getCommand("skin").setExecutor(new SkinChanger());
//this.getCommand("abort").setExecutor(new AbortCmd(this));
CustomRecipes recipes = new CustomRecipes(true);
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -41,34 +41,50 @@
import java.lang.reflect.Field;
import java.util.*;

public class SkinChanger extends BukkitRunnable implements CommandExecutor {
public class SkinChanger {

private MojangAPI mojangAPI;

private HashMap<Player, Player> playerMapping = new HashMap<>();

private HashMap<UUID, String> originalPlayerNames = new HashMap<>();

public SkinChanger() {
this.mojangAPI = new MojangAPI();
}

@Override
public void run() {
rotateSkins();
this.cancel();
public String getOriginalName(UUID uuid) {
for (UUID key: originalPlayerNames.keySet()) {
if (key.equals(uuid)) {
return originalPlayerNames.get(uuid);
}
}
return "";
}

public void cachePlayerInfo() {
for (Player p: Bukkit.getServer().getOnlinePlayers()) {
originalPlayerNames.put(p.getUniqueId(), p.getName());
}
}

private GameProfile buildProfile(CraftPlayer cPlayer, String userName) {
GameProfile profile = new GameProfile(cPlayer.getUniqueId(), userName);
RawPlayerProfileJson rawProfile = this.mojangAPI.getPlayerInfoHandler().getRawPlayerProfile(this.mojangAPI.getPlayerInfoHandler().fetchUUID(userName));
private GameProfile buildProfile(CraftPlayer cPlayer, UUID uuid) {
GameProfile profile = new GameProfile(cPlayer.getUniqueId(), getOriginalName(uuid));
RawPlayerProfileJson rawProfile;
if (this.mojangAPI.getMojangAPICache().hasCachedRawProfile(uuid)) {
rawProfile = this.mojangAPI.getMojangAPICache().getRawProfile(uuid);
} else {
rawProfile = this.mojangAPI.getPlayerInfoHandler().getRawPlayerProfile(uuid);
}
String skinUrl = rawProfile.getProperties().get(0).getValue();
String signature = rawProfile.getProperties().get(0).getSignature();
profile.getProperties().removeAll("textures");
profile.getProperties().put("textures", new Property("textures", skinUrl, signature));
return profile;
}

private void changeSkin(CraftPlayer cPlayer, String requestedSkinName) {
GameProfile gProfile = buildProfile(cPlayer, requestedSkinName);
private void changeSkin(CraftPlayer cPlayer, UUID requestedSkinUUID) {
GameProfile gProfile = buildProfile(cPlayer, requestedSkinUUID);
try {
Field profileField = cPlayer.getHandle().getClass().getSuperclass().getDeclaredField("bT");
profileField.setAccessible(true);
Expand Down Expand Up @@ -104,52 +120,29 @@ private void dispatchPackets(Player player, Packet... packets) {
}
}

public void rotateSkins() {
mapPlayers();
for (Player target: Bukkit.getServer().getOnlinePlayers()) {
UhcPluginCore.getInstance().getLogger().info("Target player: " + target.getName());
Player random = pickRandomPlayer(target);
UhcPluginCore.getInstance().getLogger().info("Random player: " + random.getName());
changeSkin((CraftPlayer) target, random.getName());
sendTitle(target, random);
}
public void rotateSkinsAsync() {
new BukkitRunnable(){
@Override
public void run() {
mapPlayers(Bukkit.getServer().getOnlinePlayers());
for (Player p: playerMapping.keySet()) {
changeSkin((CraftPlayer) p, playerMapping.get(p).getUniqueId());
sendTitle(p, playerMapping.get(p));
}
}
}.runTaskAsynchronously(UhcPluginCore.getInstance());
}

private void mapPlayers() {
Player[] playerArray = new Player[Bukkit.getServer().getOnlinePlayers().size()];
int i = 0;
for (Player player: Bukkit.getServer().getOnlinePlayers()) {
playerArray[i] = player;
i++;
}
List<Player> players = Arrays.asList(playerArray);
List<Player> shuffledPlayers = new ArrayList<>();
Collections.copy(players, shuffledPlayers);
private void mapPlayers(Collection<? extends Player> players) {
List<? extends Player> shuffledPlayers = new ArrayList<>(players);
Collections.shuffle(shuffledPlayers);
Player[] originalPlayers = players.toArray(new Player[0]);
for (int j = 0; j < players.size(); j++) {
this.playerMapping.put(players.get(j), shuffledPlayers.get(j));
}
}

private Player pickRandomPlayer(Player original) {
for (Player p: playerMapping.keySet()) {
if (p.getUniqueId().equals(original.getUniqueId())) {
return playerMapping.get(p);
}
this.playerMapping.put(originalPlayers[j], shuffledPlayers.get(j));
}
return original;
}

private void sendTitle(Player original, Player disguise) {
original.sendTitle(ChatColor.GREEN + "◆"+ ChatColor.RESET + ChatColor.AQUA + "" + ChatColor.BOLD + " SKIN ROLL " + ChatColor.RESET + ChatColor.GREEN + "◆", ChatColor.GRAY + "Your disguise: " + disguise.getName(), 0, 5*20,1*20);
original.sendTitle(ChatColor.GREEN + "◆"+ ChatColor.RESET + ChatColor.AQUA + "" + ChatColor.BOLD + " SKIN ROLL " + ChatColor.RESET + ChatColor.GREEN + "◆", ChatColor.GRAY + "Your disguise: " + originalPlayerNames.get(disguise.getUniqueId()), 0, 5*20,1*20);
}

@Override
public boolean onCommand(CommandSender theSender, Command command, String s, String[] args) {
changeSkin((CraftPlayer) theSender, args[0]);
return false;
}



}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import me.PauMAVA.UhcPlugin.UhcPluginCore;
import me.PauMAVA.UhcPlugin.chat.Prefix;
import me.PauMAVA.UhcPlugin.commands.UhcConfigCmd;
import me.PauMAVA.UhcPlugin.gameplay.SkinChanger;
import me.PauMAVA.UhcPlugin.lang.PluginStrings;
import me.PauMAVA.UhcPlugin.teams.UhcTeam;
import me.PauMAVA.UhcPlugin.teams.UhcTeamsManager;
Expand All @@ -46,15 +47,19 @@ public class UhcMatchHandler {
private List<UhcTeam> teams = new ArrayList<UhcTeam>();
private UhcMatchTimer timer;

private SkinChanger skinChanger;

/**
* UhcMatchHandler constructor
* @param plugin - the instance of the plugin core */
public UhcMatchHandler(UhcPluginCore plugin) {
this.plugin = plugin;
this.skinChanger = new SkinChanger();
}

public void start() {
this.isRunning = true;
skinChanger.cachePlayerInfo();
for(String teamName: UhcTeamsManager.getTeamsManagementFile().getTeams()) {
teams.add(UhcTeamsManager.getTeamObject(teamName));
}
Expand Down Expand Up @@ -146,6 +151,10 @@ public UhcMatchTimer getTimer() {
return this.timer;
}

public SkinChanger getSkinChanger() {
return skinChanger;
}

void giveItemToAllPlayers(ItemStack item) {
for (UhcTeam team: getRemainingTeams()) {
for (Player p: team.alive()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,11 @@

import me.PauMAVA.UhcPlugin.UhcPluginCore;
import me.PauMAVA.UhcPlugin.commands.UhcConfigCmd;
import me.PauMAVA.UhcPlugin.gameplay.SkinChanger;
import me.PauMAVA.UhcPlugin.lang.PluginStrings;
import me.PauMAVA.UhcPlugin.world.UhcWorldBorder;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;

Expand Down Expand Up @@ -72,7 +69,7 @@ public void run() {
checkForShulkerGive(episode);
chaptersToSkinRoll--;
if (chaptersToSkinRoll <= 0) {
//scheduleAsyncSkinRoll();
scheduleAsyncSkinRoll();
chaptersToSkinRoll = 2;
}
}
Expand Down Expand Up @@ -142,7 +139,7 @@ public int getEpisode() {
}

void scheduleAsyncSkinRoll() {
new SkinChanger().runTaskAsynchronously(UhcPluginCore.getInstance());
match.getSkinChanger().rotateSkinsAsync();
}

}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 1 addition & 3 deletions src/plugin.yml → src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@ api-version: 1.15

commands:
uhc:
description: This is the basic command of the plugin.
skin:
description: TESTING CMD that changes the skin of the sender
description: This is the basic command of the plugin.
File renamed without changes.
30 changes: 30 additions & 0 deletions src/test/java/SkinChangerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* UhcPlugin
* Copyright (c) 2019 Pau Machetti Vallverdú
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import me.PauMAVA.UhcPlugin.gameplay.SkinChanger;
import org.junit.jupiter.api.Test;

class SkinChangerTest {

@Test
void testPlayerMapping() {

}


}

0 comments on commit 1cf4417

Please sign in to comment.