Workaround for command block crashes

This commit is contained in:
mathias 2019-08-04 16:33:05 +03:00
parent 1d7a4166b1
commit 3d67c5aa34
6 changed files with 114 additions and 147 deletions

View file

@ -27,62 +27,6 @@ class CommandSkin implements CommandExecutor {
this.main = main; this.main = main;
} }
private void changeSkin(final Player player, final String name) {
new BukkitRunnable() {
public void run() {
try {
final URL nameUrl = new URL("https://api.mojang.com/users/profiles/minecraft/" + name);
final HttpsURLConnection nameConnection = (HttpsURLConnection) nameUrl.openConnection();
if (nameConnection.getResponseCode() == HttpsURLConnection.HTTP_OK) {
final InputStreamReader nameStream = new InputStreamReader(nameConnection.getInputStream());
final String uuid = new JsonParser().parse(nameStream).getAsJsonObject().get("id").getAsString();
nameStream.close();
nameConnection.disconnect();
final URL uuidUrl = new URL("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid + "?unsigned=false");
final HttpsURLConnection uuidConnection = (HttpsURLConnection) uuidUrl.openConnection();
if (uuidConnection.getResponseCode() == HttpsURLConnection.HTTP_OK) {
final InputStreamReader uuidStream = new InputStreamReader(uuidConnection.getInputStream());
final JsonObject response = new JsonParser().parse(uuidStream).getAsJsonObject().get("properties").getAsJsonArray().get(0).getAsJsonObject();
final String texture = response.get("value").getAsString();
final String signature = response.get("signature").getAsString();
uuidStream.close();
uuidConnection.disconnect();
final PlayerProfile textureProfile = player.getPlayerProfile();
textureProfile.clearProperties();
textureProfile.setProperty(new ProfileProperty("textures", texture, signature));
new BukkitRunnable() {
public void run() {
player.setPlayerProfile(textureProfile);
player.sendMessage("Successfully set your skin to " + name + "'s");
}
}.runTask(main);
} else {
uuidConnection.disconnect();
new BukkitRunnable() {
public void run() {
player.sendMessage("Failed to change skin. Try again later");
}
}.runTask(main);
}
} else {
nameConnection.disconnect();
new BukkitRunnable() {
public void run() {
player.sendMessage("A player with that username doesn't exist");
}
}.runTask(main);
}
} catch (Exception exception) {
}
}
}.runTaskAsynchronously(main);
}
public boolean onCommand(CommandSender sender, Command command, String label, final String[] args) { public boolean onCommand(CommandSender sender, Command command, String label, final String[] args) {
final Player player = (Player) sender; final Player player = (Player) sender;
@ -90,7 +34,40 @@ class CommandSkin implements CommandExecutor {
player.sendMessage(ChatColor.RED + "Usage: /" + label + " <username>"); player.sendMessage(ChatColor.RED + "Usage: /" + label + " <username>");
} else { } else {
final String name = args[0]; final String name = args[0];
changeSkin(player, name); new BukkitRunnable() {
public void run() {
try {
final URL skinUrl = new URL("https://api.ashcon.app/mojang/v2/user/" + name);
final HttpsURLConnection skinConnection = (HttpsURLConnection) skinUrl.openConnection();
if (skinConnection.getResponseCode() == HttpsURLConnection.HTTP_OK) {
final InputStreamReader skinStream = new InputStreamReader(skinConnection.getInputStream());
final JsonObject response = new JsonParser().parse(skinStream).getAsJsonObject();
final String uuid = response.get("uuid").getAsString();
final JsonObject rawSkin = response.getAsJsonObject("textures").getAsJsonObject("raw");
final String texture = rawSkin.get("value").getAsString();
final String signature = rawSkin.get("signature").getAsString();
skinStream.close();
final PlayerProfile textureProfile = player.getPlayerProfile();
textureProfile.clearProperties();
textureProfile.setProperty(new ProfileProperty("textures", texture, signature));
player.sendMessage("Successfully set your skin to " + name + "'s");
new BukkitRunnable() {
public void run() {
player.setPlayerProfile(textureProfile);
}
}.runTask(main);
} else {
player.sendMessage("A player with that username doesn't exist");
}
skinConnection.disconnect();
} catch (Exception exception) {
}
}
}.runTaskAsynchronously(main);
} }
return true; return true;
} }

View file

@ -27,60 +27,6 @@ class CommandUsername implements CommandExecutor {
this.main = main; this.main = main;
} }
private void changeUsernameSkin(final Player player, final String[] name) {
new BukkitRunnable() {
public void run() {
try {
String texture = "";
String signature = "";
final String nameColor = ChatColor.translateAlternateColorCodes('&', String.join(" ", name));
final String nameShort = nameColor.substring(0, Math.min(16, nameColor.length()));
final URL nameUrl = new URL("https://api.mojang.com/users/profiles/minecraft/" + nameShort);
final HttpsURLConnection nameConnection = (HttpsURLConnection) nameUrl.openConnection();
if (nameConnection.getResponseCode() == HttpsURLConnection.HTTP_OK) {
final InputStreamReader nameStream = new InputStreamReader(nameConnection.getInputStream());
final String uuid = new JsonParser().parse(nameStream).getAsJsonObject().get("id").getAsString();
nameStream.close();
nameConnection.disconnect();
final URL uuidUrl = new URL("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid + "?unsigned=false");
final HttpsURLConnection uuidConnection = (HttpsURLConnection) uuidUrl.openConnection();
if (uuidConnection.getResponseCode() == HttpsURLConnection.HTTP_OK) {
final InputStreamReader uuidStream = new InputStreamReader(uuidConnection.getInputStream());
final JsonObject response = new JsonParser().parse(uuidStream).getAsJsonObject().get("properties").getAsJsonArray().get(0).getAsJsonObject();
texture = response.get("value").getAsString();
signature = response.get("signature").getAsString();
uuidStream.close();
uuidConnection.disconnect();
}
}
final PlayerProfile profile = player.getPlayerProfile();
profile.clearProperties();
profile.setName(nameShort);
if (!("".equals(texture)) &&
!("".equals(signature))) {
profile.setProperty(new ProfileProperty("textures", texture, signature));
}
new BukkitRunnable() {
public void run() {
player.setPlayerProfile(profile);
player.sendMessage("Successfully set your username to \"" + nameShort + "\"");
}
}.runTask(main);
} catch (Exception exception) {
}
}
}.runTaskAsynchronously(main);
}
public boolean onCommand(CommandSender sender, Command command, String label, final String[] args) { public boolean onCommand(CommandSender sender, Command command, String label, final String[] args) {
final Player player = (Player) sender; final Player player = (Player) sender;
@ -88,7 +34,49 @@ class CommandUsername implements CommandExecutor {
player.sendMessage(ChatColor.RED + "Usage: /" + label + " <username>"); player.sendMessage(ChatColor.RED + "Usage: /" + label + " <username>");
} else { } else {
final String[] name = args; final String[] name = args;
changeUsernameSkin(player, name);
new BukkitRunnable() {
public void run() {
try {
String texture = "";
String signature = "";
final String nameColor = ChatColor.translateAlternateColorCodes('&', String.join(" ", name));
final String nameShort = nameColor.substring(0, Math.min(16, nameColor.length()));
final URL skinUrl = new URL("https://api.ashcon.app/mojang/v2/user/" + nameShort);
final HttpsURLConnection skinConnection = (HttpsURLConnection) skinUrl.openConnection();
if (skinConnection.getResponseCode() == HttpsURLConnection.HTTP_OK) {
final InputStreamReader skinStream = new InputStreamReader(skinConnection.getInputStream());
final JsonObject response = new JsonParser().parse(skinStream).getAsJsonObject();
final String uuid = response.get("uuid").getAsString();
final JsonObject rawSkin = response.getAsJsonObject("textures").getAsJsonObject("raw");
texture = rawSkin.get("value").getAsString();
signature = rawSkin.get("signature").getAsString();
skinStream.close();
}
skinConnection.disconnect();
final PlayerProfile profile = player.getPlayerProfile();
profile.setName(nameShort);
if (!("".equals(texture)) &&
!("".equals(signature))) {
profile.setProperty(new ProfileProperty("textures", texture, signature));
}
player.sendMessage("Successfully set your username to \"" + nameShort + "\"");
new BukkitRunnable() {
public void run() {
player.setPlayerProfile(profile);
}
}.runTask(main);
} catch (Exception exception) {
}
}
}.runTaskAsynchronously(main);
} }
return true; return true;
} }

View file

@ -1,11 +1,16 @@
package pw.kaboom.extras; package pw.kaboom.extras;
import org.bukkit.block.BlockState;
import org.bukkit.block.CommandBlock;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.block.SignChangeEvent; import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.event.world.ChunkLoadEvent;
class BlockCheck implements Listener { class BlockCheck implements Listener {
@EventHandler @EventHandler
void onBlockPlace(BlockPlaceEvent event) { void onBlockPlace(BlockPlaceEvent event) {
@ -20,6 +25,17 @@ class BlockCheck implements Listener {
} }
} }
@EventHandler
void onChunkLoad(ChunkLoadEvent event) {
if (!event.isNewChunk()) {
for (BlockState block : event.getChunk().getTileEntities()) {
if (block instanceof CommandBlock) {
block.update();
}
}
}
}
@EventHandler @EventHandler
void onSignChange(SignChangeEvent event) { void onSignChange(SignChangeEvent event) {
try { try {

View file

@ -44,35 +44,26 @@ class PlayerConnection implements Listener {
main.interactMillisList.put(event.getUniqueId(), System.currentTimeMillis()); main.interactMillisList.put(event.getUniqueId(), System.currentTimeMillis());
try { try {
final URL nameUrl = new URL("https://api.mojang.com/users/profiles/minecraft/" + event.getName()); final URL skinUrl = new URL("https://api.ashcon.app/mojang/v2/user/" + event.getName());
final HttpsURLConnection nameConnection = (HttpsURLConnection) nameUrl.openConnection(); final HttpsURLConnection skinConnection = (HttpsURLConnection) skinUrl.openConnection();
if (nameConnection != null && if (skinConnection.getResponseCode() == HttpsURLConnection.HTTP_OK) {
nameConnection.getResponseCode() == HttpsURLConnection.HTTP_OK) { final InputStreamReader skinStream = new InputStreamReader(skinConnection.getInputStream());
final InputStreamReader nameStream = new InputStreamReader(nameConnection.getInputStream()); final JsonObject response = new JsonParser().parse(skinStream).getAsJsonObject();
final String uuid = new JsonParser().parse(nameStream).getAsJsonObject().get("id").getAsString(); final String uuid = response.get("uuid").getAsString();
nameStream.close(); final JsonObject rawSkin = response.getAsJsonObject("textures").getAsJsonObject("raw");
nameConnection.disconnect(); final String texture = rawSkin.get("value").getAsString();
final String signature = rawSkin.get("signature").getAsString();
skinStream.close();
final URL uuidUrl = new URL("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid + "?unsigned=false"); final PlayerProfile textureProfile = event.getPlayerProfile();
final HttpsURLConnection uuidConnection = (HttpsURLConnection) uuidUrl.openConnection(); textureProfile.clearProperties();
textureProfile.setProperty(new ProfileProperty("textures", texture, signature));
if (uuidConnection != null && main.playerProfile.put(event.getName(), textureProfile);
uuidConnection.getResponseCode() == HttpsURLConnection.HTTP_OK) {
final InputStreamReader uuidStream = new InputStreamReader(uuidConnection.getInputStream());
final JsonObject response = new JsonParser().parse(uuidStream).getAsJsonObject().get("properties").getAsJsonArray().get(0).getAsJsonObject();
final String texture = response.get("value").getAsString();
final String signature = response.get("signature").getAsString();
uuidStream.close();
uuidConnection.disconnect();
final PlayerProfile textureProfile = event.getPlayerProfile();
textureProfile.clearProperties();
textureProfile.setProperty(new ProfileProperty("textures", texture, signature));
main.playerProfile.put(event.getName(), textureProfile);
}
} }
skinConnection.disconnect();
} catch (Exception exception) { } catch (Exception exception) {
} }
} }

View file

@ -24,12 +24,8 @@ class PlayerDamage implements Listener {
@EventHandler @EventHandler
void onEntityDamage(EntityDamageEvent event) { void onEntityDamage(EntityDamageEvent event) {
if (event.getEntityType() == EntityType.PLAYER) { if (event.getEntityType() == EntityType.PLAYER) {
if (((event.getCause() == DamageCause.CUSTOM || if (event.getCause() == DamageCause.VOID &&
event.getCause() == DamageCause.SUICIDE) && event.getDamage() == Float.MAX_VALUE) {
event.getDamage() == Short.MAX_VALUE) ||
(event.getCause() == DamageCause.VOID &&
event.getDamage() == Float.MAX_VALUE)) {
event.setDamage(Float.MAX_VALUE);
event.setCancelled(true); event.setCancelled(true);
} }
} }
@ -60,10 +56,7 @@ class PlayerDamage implements Listener {
onlinePlayer.sendMessage(event.getDeathMessage()); onlinePlayer.sendMessage(event.getDeathMessage());
} }
if ((player.getLastDamageCause() != null && if (maxHealthLow) {
player.getLastDamageCause().getCause() == DamageCause.SUICIDE &&
player.getLastDamageCause().getDamage() == Float.MAX_VALUE) ||
maxHealthLow) {
return; return;
} }

View file

@ -10,6 +10,8 @@ import org.bukkit.event.Listener;
import org.bukkit.event.server.ServerCommandEvent; import org.bukkit.event.server.ServerCommandEvent;
import org.bukkit.event.world.ChunkLoadEvent;
class ServerCommand implements Listener { class ServerCommand implements Listener {
private Main main; private Main main;
public ServerCommand(Main main) { public ServerCommand(Main main) {