Skip to content

Commit

Permalink
Revert "Try and avoid rate limits on skins by requesting again if fai…
Browse files Browse the repository at this point in the history
…led"

This reverts commit 1cddc52.

Fixes #6
I think this will fix/help with TechzoneMC/CombatTagLegacy#20

Conflicts:
	nms-v1_8_R1/src/main/java/net/techcable/npclib/nms/versions/v1_8_R1/NMS.java
	nms-v1_8_R2/src/main/java/net/techcable/npclib/nms/versions/v1_8_R2/NMS.java
	nms-v1_8_R3/src/main/java/net/techcable/npclib/nms/versions/v1_8_R3/NMS.java
  • Loading branch information
Techcable committed Jun 24, 2015
1 parent d449f19 commit c926483
Show file tree
Hide file tree
Showing 17 changed files with 164 additions and 323 deletions.
15 changes: 0 additions & 15 deletions api/src/main/java/net/techcable/npclib/nms/NMS.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

import net.techcable.npclib.NPC;

import net.techcable.npclib.nms.skins.RateLimitedException;
import net.techcable.npclib.util.ProfileUtils;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
Expand All @@ -18,19 +16,6 @@ public interface NMS {
public boolean isSupported(OptionalFeature feature);
public void look(Entity entity, float pitch, float yaw);
public Player spawnPlayer(Location toSpawn, String name, NPC npc);

/**
* Sets the skin of the npc
*
* <p>
* It is up to the implementation to choose whether or not to refresh the player's skin
* </p>
*
* @param npc the npc to set the skin of
* @param skinProfile the npc's new skin
* @return true if the npc is needed to respawn
*/
public boolean setSkin(NPC npc, ProfileUtils.PlayerProfile skinProfile);
public NPC getAsNPC(Entity entity);
public void notifyOfEquipmentChange(Player[] toNotify, Player npc, int... slot);

Expand Down

This file was deleted.

10 changes: 3 additions & 7 deletions api/src/main/java/net/techcable/npclib/util/ProfileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.util.Map;
import java.util.UUID;

import net.techcable.npclib.nms.skins.RateLimitedException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
Expand Down Expand Up @@ -61,7 +60,7 @@ public static PlayerProfile lookup(String name) {
* @param id look for a profile with this uuid
* @return a profile with the given id
*/
public static PlayerProfile lookup(UUID id) throws RateLimitedException {
public static PlayerProfile lookup(UUID id) {
return lookupProperties(id);
}

Expand Down Expand Up @@ -134,15 +133,11 @@ private static List<PlayerProfile> postNames(String[] names) { //This one doesn'
return profiles;
}

private static PlayerProfile lookupProperties(UUID id) throws RateLimitedException {
private static PlayerProfile lookupProperties(UUID id) {
if (idCache.contains(id)) return idCache.get(id);
Object rawResponse = getJson("https://sessionserver.mojang.com/session/minecraft/profile/" + id.toString().replace("-", ""));
if (rawResponse == null || !(rawResponse instanceof JSONObject)) return null;
JSONObject response = (JSONObject) rawResponse;
if (response.containsKey("errror")) {
if (response.get("error").equals("TooManyRequestsException")) throw new RateLimitedException();
return null;
}
PlayerProfile profile = deserializeProfile(response);
if (profile == null) return null;
idCache.put(id, profile);
Expand Down Expand Up @@ -188,6 +183,7 @@ private static Object getJson(String rawUrl) {
URL url = new URL(rawUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");

reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuffer result = new StringBuffer();
String line;
Expand Down
9 changes: 0 additions & 9 deletions api/src/main/java/net/techcable/npclib/util/ReflectUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,6 @@ public static void setField(Field field, Object objToSet, Object value) {
throw new RuntimeException(e);
}
}

public static <T> T getField(Field field, Object objToSet) {
field.setAccessible(true);
try {
return (T) field.get(objToSet);
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}

public static Method makeMethod(Class<?> clazz, String methodName, Class<?>... paramaters) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import java.util.UUID;

import net.minecraft.server.v1_7_R3.*;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_7_R3.entity.CraftPlayer;

import net.minecraft.server.v1_7_R3.DamageSource;
import net.minecraft.server.v1_7_R3.EntityPlayer;
import net.minecraft.server.v1_7_R3.EnumGamemode;
import net.minecraft.server.v1_7_R3.PlayerInteractManager;
import net.minecraft.util.com.mojang.authlib.GameProfile;
import net.minecraft.util.com.mojang.authlib.properties.Property;
import net.techcable.npclib.NPC;
Expand All @@ -17,8 +20,8 @@
@Getter
public class EntityNPCPlayer extends EntityPlayer {
private final NPC npc;
public EntityNPCPlayer(NPC npc, String name, Location location, GameProfile profile) {
super(NMS.getServer(), NMS.getHandle(location.getWorld()), profile, new PlayerInteractManager(NMS.getHandle(location.getWorld())));
public EntityNPCPlayer(NPC npc, String name, Location location) {
super(NMS.getServer(), NMS.getHandle(location.getWorld()), makeProfile(name, npc.getSkin()), new PlayerInteractManager(NMS.getHandle(location.getWorld())));
playerInteractManager.b(EnumGamemode.SURVIVAL); //MCP = initializeGameType
this.npc = npc;
playerConnection = new NPCConnection(this);
Expand All @@ -33,4 +36,17 @@ public boolean damageEntity(DamageSource source, float damage) {
}
return super.damageEntity(source, damage);
}
public static GameProfile makeProfile(String name, UUID skinId) {
GameProfile profile = new GameProfile(UUID.randomUUID(), name);
if (skinId != null) {
GameProfile skin = new GameProfile(skinId, null);
skin = NMS.getServer().av().fillProfileProperties(skin, true); //Srg = func_147130_as
if (skin.getProperties().get("textures") == null || !skin.getProperties().get("textures").isEmpty()) {
Property textures = skin.getProperties().get("textures").iterator().next();
profile.getProperties().put("textures", textures);
}
}
return profile;
}

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
package net.techcable.npclib.nms.versions.v1_7_R3;

import java.lang.reflect.Field;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;

import net.minecraft.server.v1_7_R3.*;
import net.minecraft.server.v1_7_R3.ChunkCoordinates;
import net.minecraft.server.v1_7_R3.Entity;
import net.minecraft.server.v1_7_R3.EntityHuman;
import net.minecraft.server.v1_7_R3.EntityLiving;
import net.minecraft.server.v1_7_R3.EntityPlayer;
import net.minecraft.server.v1_7_R3.IChatBaseComponent;
import net.minecraft.server.v1_7_R3.MinecraftServer;
import net.minecraft.server.v1_7_R3.Packet;
import net.minecraft.server.v1_7_R3.PacketPlayOutEntityEquipment;
import net.minecraft.server.v1_7_R3.World;
import net.minecraft.server.v1_7_R3.WorldServer;
import net.minecraft.util.com.mojang.authlib.GameProfile;
import net.minecraft.util.com.mojang.authlib.properties.Property;
import net.techcable.npclib.NPC;
import net.techcable.npclib.nms.OptionalFeature;
import net.techcable.npclib.util.ProfileUtils;
import net.techcable.npclib.util.ReflectUtil;

import org.bukkit.Bukkit;
Expand All @@ -21,8 +32,6 @@
import org.bukkit.craftbukkit.v1_7_R3.entity.CraftPlayer;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

public class NMS implements net.techcable.npclib.nms.NMS {

Expand Down Expand Up @@ -65,42 +74,16 @@ public static void setHeadYaw(Entity entity, float yaw) {
handle.aM = yaw; //MCP = renderYawOffset
handle.aP = yaw; //MCP = prevRotationYawHead
}

private Map<UUID, GameProfile> profileMap = new HashMap<>();

@Override
public Player spawnPlayer(Location toSpawn, String name, NPC npc) {
GameProfile profile = profileMap.get(npc.getUUID());
if (profile == null) profile = new GameProfile(npc.getUUID(), npc.getName());
EntityNPCPlayer player = new EntityNPCPlayer(npc, name, toSpawn, profile);
profileMap.remove(npc.getUUID());
WorldServer world = getHandle(toSpawn.getWorld());
EntityNPCPlayer player = new EntityNPCPlayer(npc, name, toSpawn);
WorldServer world = getHandle(toSpawn.getWorld());
world.addEntity(player);
look(player.getBukkitEntity(), toSpawn.getPitch(), toSpawn.getYaw());
return player.getBukkitEntity();
}

@Override
public boolean setSkin(NPC npc, ProfileUtils.PlayerProfile skinProfile) {
GameProfile profile = makeProfile(npc, skinProfile);
profileMap.put(npc.getUUID(), profile);
return true;
}

private static GameProfile makeProfile(NPC npc, ProfileUtils.PlayerProfile skinProfile) {
GameProfile profile = new GameProfile(npc.getUUID(), npc.getName());
if (skinProfile.getProperties() != null) {
for (Object obj : skinProfile.getProperties()) {
JSONObject jsonProperty = (JSONObject) obj;
String name = (String) jsonProperty.get("name");
String value = (String) jsonProperty.get("value");
String signature = jsonProperty.containsKey("signature") ? (String) jsonProperty.get("signature") : null;
Property property = signature == null ? new Property(name, value) : new Property(name, value, signature);
profile.getProperties().put(name, property);
}
}
return profile;
}


public static Entity getHandle(org.bukkit.entity.Entity bukkitEntity) {
if (!(bukkitEntity instanceof CraftEntity))
return null;
Expand Down
7 changes: 0 additions & 7 deletions nms-v1_7_R4/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@
<artifactId>nms-v1_7_R4</artifactId>

<dependencies>
<!-- thanks to DarkSeraphim for dependency ordering fix of Bukkit.getOnlinePlayers() -->
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>1.7.10-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.techcable.npclib</groupId>
<artifactId>api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
@Getter
public class EntityNPCPlayer extends EntityPlayer {
private final NPC npc;
public EntityNPCPlayer(NPC npc, GameProfile profile, Location location) {
super(NMS.getServer(), NMS.getHandle(location.getWorld()), profile, new PlayerInteractManager(NMS.getHandle(location.getWorld())));
public EntityNPCPlayer(NPC npc, String name, Location location) {
super(NMS.getServer(), NMS.getHandle(location.getWorld()), makeProfile(name, npc.getSkin()), new PlayerInteractManager(NMS.getHandle(location.getWorld())));
playerInteractManager.b(EnumGamemode.SURVIVAL); //MCP = initializeGameType
this.npc = npc;
playerConnection = new NPCConnection(this);
Expand All @@ -36,4 +36,17 @@ public boolean damageEntity(DamageSource source, float damage) {
}
return super.damageEntity(source, damage);
}

public static GameProfile makeProfile(String name, UUID skinId) {
GameProfile profile = new GameProfile(UUID.randomUUID(), name);
if (skinId != null) {
GameProfile skin = new GameProfile(skinId, null);
skin = NMS.getServer().av().fillProfileProperties(skin, true); //Srg = func_147130_as
if (skin.getProperties().get("textures") == null || !skin.getProperties().get("textures").isEmpty()) {
Property textures = skin.getProperties().get("textures").iterator().next();
profile.getProperties().put("textures", textures);
}
}
return profile;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package net.techcable.npclib.nms.versions.v1_7_R4;

import java.lang.reflect.Field;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;

import net.minecraft.server.v1_7_R4.ChunkCoordinates;
import net.minecraft.server.v1_7_R4.Entity;
Expand All @@ -18,10 +21,8 @@
import net.minecraft.util.com.google.common.base.Predicate;
import net.minecraft.util.com.google.common.collect.Collections2;
import net.minecraft.util.com.mojang.authlib.GameProfile;
import net.minecraft.util.com.mojang.authlib.properties.Property;
import net.techcable.npclib.NPC;
import net.techcable.npclib.nms.OptionalFeature;
import net.techcable.npclib.util.ProfileUtils;
import net.techcable.npclib.util.ReflectUtil;

import org.bukkit.Bukkit;
Expand All @@ -35,7 +36,6 @@
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.json.simple.JSONObject;

public class NMS implements net.techcable.npclib.nms.NMS {

Expand Down Expand Up @@ -78,44 +78,19 @@ public static void setHeadYaw(Entity entity, float yaw) {
handle.aM = yaw; //MCP = renderYawOffset
handle.aP = yaw; //MCP = prevRotationYawHead
}

private Map<UUID, GameProfile> profileMap = new HashMap<>();

@Override
public Player spawnPlayer(Location toSpawn, String name, NPC npc) {
GameProfile profile = profileMap.get(npc.getUUID()) != null ? profileMap.get(npc.getUUID()) : new GameProfile(npc.getUUID(), npc.getName());
EntityNPCPlayer player = new EntityNPCPlayer(npc, profile, toSpawn);
EntityNPCPlayer player = new EntityNPCPlayer(npc, name, toSpawn);
if (ProtocolHack.isProtocolHack()) {
ProtocolHack.notifyOfSpawn(Bukkit.getOnlinePlayers(), player.getBukkitEntity());
}
WorldServer world = getHandle(toSpawn.getWorld());
world.addEntity(player);
world.addEntity(player);
look(player.getBukkitEntity(), toSpawn.getPitch(), toSpawn.getYaw());
return player.getBukkitEntity();
}

@Override
public boolean setSkin(NPC npc, ProfileUtils.PlayerProfile skinProfile) {
GameProfile profile = makeProfile(npc, skinProfile);
profileMap.put(npc.getUUID(), profile);
return true;
}

private static GameProfile makeProfile(NPC npc, ProfileUtils.PlayerProfile skinProfile) {
GameProfile profile = new GameProfile(npc.getUUID(), npc.getName());
if (skinProfile.getProperties() != null) {
for (Object obj : skinProfile.getProperties()) {
JSONObject jsonProperty = (JSONObject) obj;
String name = (String) jsonProperty.get("name");
String value = (String) jsonProperty.get("value");
String signature = jsonProperty.containsKey("signature") ? (String) jsonProperty.get("signature") : null;
Property property = signature == null ? new Property(name, value) : new Property(name, value, signature);
profile.getProperties().put(name, property);
}
}
return profile;
}



public static Entity getHandle(org.bukkit.entity.Entity bukkitEntity) {
if (!(bukkitEntity instanceof CraftEntity))
return null;
Expand Down Expand Up @@ -213,7 +188,7 @@ public Player apply(NPC npc) {
return (Player) npc.getEntity();
}
});
ProtocolHack.notifyOfSpawn(Arrays.asList(joined), npcEntities.toArray(new Player[npcEntities.size()]));
ProtocolHack.notifyOfSpawn(new Player[] {joined}, npcEntities.toArray(new Player[npcEntities.size()]));
}
}

Expand Down
Loading

0 comments on commit c926483

Please sign in to comment.