From 00e7494dce5b640c03e88efb45c9c18fc895929e Mon Sep 17 00:00:00 2001 From: mathiascode Date: Sun, 5 Jan 2020 22:11:57 +0200 Subject: [PATCH] Don't remove player entities --- .../kaboom/extras/helpers/SkinDownloader.java | 1 + .../extras/modules/entity/EntitySpawn.java | 5 +- .../modules/player/PlayerConnection.java | 73 +++++++++++++++---- .../extras/modules/player/PlayerDamage.java | 47 ++++++++---- .../extras/modules/server/ServerCommand.java | 4 +- 5 files changed, 99 insertions(+), 31 deletions(-) diff --git a/src/main/java/pw/kaboom/extras/helpers/SkinDownloader.java b/src/main/java/pw/kaboom/extras/helpers/SkinDownloader.java index b2240b8..87fc286 100644 --- a/src/main/java/pw/kaboom/extras/helpers/SkinDownloader.java +++ b/src/main/java/pw/kaboom/extras/helpers/SkinDownloader.java @@ -8,6 +8,7 @@ import java.util.UUID; import javax.net.ssl.HttpsURLConnection; +import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.scheduler.BukkitRunnable; diff --git a/src/main/java/pw/kaboom/extras/modules/entity/EntitySpawn.java b/src/main/java/pw/kaboom/extras/modules/entity/EntitySpawn.java index 57b6d5b..ae460b9 100644 --- a/src/main/java/pw/kaboom/extras/modules/entity/EntitySpawn.java +++ b/src/main/java/pw/kaboom/extras/modules/entity/EntitySpawn.java @@ -55,7 +55,7 @@ public class EntitySpawn implements Listener { private boolean checkEntityLimits(EntityType entityType, Chunk chunk, World world, boolean isAddToWorldEvent) { final int chunkEntityCount = chunk.getEntities().length; - final int chunkEntityCountLimit = 50; + final int chunkEntityCountLimit = 30; final int worldDragonCount = world.getEntitiesByClass(EnderDragon.class).size(); final int worldDragonCountLimit = 24; @@ -214,7 +214,8 @@ public class EntitySpawn implements Listener { final EntityType entityType = entity.getType(); final boolean isAddToWorldEvent = true; - if (checkEntityLimits(entityType, chunk, world, isAddToWorldEvent)) { + if (checkEntityLimits(entityType, chunk, world, isAddToWorldEvent) + && entity.getType() != EntityType.PLAYER) { entity.remove(); return; } diff --git a/src/main/java/pw/kaboom/extras/modules/player/PlayerConnection.java b/src/main/java/pw/kaboom/extras/modules/player/PlayerConnection.java index 3fa048d..f224b67 100644 --- a/src/main/java/pw/kaboom/extras/modules/player/PlayerConnection.java +++ b/src/main/java/pw/kaboom/extras/modules/player/PlayerConnection.java @@ -1,5 +1,9 @@ package pw.kaboom.extras.modules.player; +import com.destroystokyo.paper.profile.PlayerProfile; +import com.destroystokyo.paper.profile.ProfileProperty; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import org.bukkit.Bukkit; import org.bukkit.Chunk; import org.bukkit.World; @@ -8,19 +12,27 @@ import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.inventory.InventoryCloseEvent; -import org.bukkit.event.player.AsyncPlayerPreLoginEvent; -import org.bukkit.event.player.PlayerJoinEvent; -import org.bukkit.event.player.PlayerKickEvent; -import org.bukkit.event.player.PlayerLoginEvent; +import org.bukkit.event.player.*; import org.bukkit.event.player.PlayerLoginEvent.Result; -import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.scheduler.BukkitRunnable; import pw.kaboom.extras.Main; import pw.kaboom.extras.helpers.SkinDownloader; +import javax.net.ssl.HttpsURLConnection; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.URL; +import java.net.URLConnection; + public final class PlayerConnection implements Listener { + private PlayerProfile profile; + private String texture; + private String signature; + /*public static boolean isIllegalItem(ItemStack item) { //try { if (item != null && @@ -45,6 +57,15 @@ public final class PlayerConnection implements Listener { event.disallow(AsyncPlayerPreLoginEvent.Result.KICK_OTHER, "A player with that username is already logged in"); } } + + try { + profile = event.getPlayerProfile(); + profile.clearProperties(); + + fetchSkinData(event.getName()); + profile.setProperty(new ProfileProperty("textures", texture, signature)); + } catch (Exception ignored) { + } } } @@ -57,6 +78,13 @@ public final class PlayerConnection implements Listener { } }*/ + @EventHandler + void onPlayerCommandSend(final PlayerCommandSendEvent event) { + if (event.getPlayer().isOnline()) { + event.getCommands().clear(); + } + } + @EventHandler void onPlayerJoin(final PlayerJoinEvent event) { final Player player = event.getPlayer(); @@ -93,8 +121,6 @@ public final class PlayerConnection implements Listener { return; } - final Player player = event.getPlayer(); - if (!JavaPlugin.getPlugin(Main.class).getConfig().getBoolean("enableJoinRestrictions")) { event.allow(); } @@ -104,16 +130,18 @@ public final class PlayerConnection implements Listener { event.allow(); } - if (JavaPlugin.getPlugin(Main.class).getConfig().getBoolean("opOnJoin")) { - player.setOp(true); + final Player player = event.getPlayer(); + + try { + player.setPlayerProfile(profile); + profile = null; + } catch (Exception ignored) { } - final String name = player.getName(); - final boolean shouldChangeUsername = false; - final boolean shouldSendMessage = false; - - SkinDownloader skinDownloader = new SkinDownloader(); - skinDownloader.applySkin(player, name, shouldChangeUsername, shouldSendMessage); + if (JavaPlugin.getPlugin(Main.class).getConfig().getBoolean("opOnJoin") + && !player.isOp()) { + player.setOp(true); + } } @SuppressWarnings("deprecation") @@ -140,4 +168,19 @@ public final class PlayerConnection implements Listener { } }*/ } + + private void fetchSkinData(final String playerName) throws IOException { + final URL skinUrl = new URL("https://api.ashcon.app/mojang/v2/user/" + playerName); + HttpsURLConnection skinConnection = (HttpsURLConnection) skinUrl.openConnection(); + skinConnection.setConnectTimeout(0); + + InputStreamReader skinStream = new InputStreamReader(skinConnection.getInputStream()); + final JsonObject responseJson = new JsonParser().parse(skinStream).getAsJsonObject(); + final JsonObject rawSkin = responseJson.getAsJsonObject("textures").getAsJsonObject("raw"); + texture = rawSkin.get("value").getAsString(); + signature = rawSkin.get("signature").getAsString(); + + skinStream.close(); + skinConnection.disconnect(); + } } diff --git a/src/main/java/pw/kaboom/extras/modules/player/PlayerDamage.java b/src/main/java/pw/kaboom/extras/modules/player/PlayerDamage.java index 2fcf5ab..c9e3333 100644 --- a/src/main/java/pw/kaboom/extras/modules/player/PlayerDamage.java +++ b/src/main/java/pw/kaboom/extras/modules/player/PlayerDamage.java @@ -12,6 +12,7 @@ import org.bukkit.block.BlockFace; import org.bukkit.entity.EntityType; import org.bukkit.entity.ExperienceOrb; +import org.bukkit.entity.HumanEntity; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -19,6 +20,8 @@ import org.bukkit.event.Listener; import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.EntityDamageEvent.DamageCause; +import org.bukkit.event.entity.EntityRegainHealthEvent; +import org.bukkit.event.entity.FoodLevelChangeEvent; import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.inventory.ItemStack; @@ -34,6 +37,24 @@ public final class PlayerDamage implements Listener { } } + @EventHandler + void onEntityRegainHealth(final EntityRegainHealthEvent event) { + if (event.getAmount() < 0) { + event.setCancelled(true); + } + } + + @EventHandler + void onFoodLevelChange(final FoodLevelChangeEvent event) { + final HumanEntity player = event.getEntity(); + + if (player.getMaxHealth() <= 0) { + player.setMaxHealth(Double.POSITIVE_INFINITY); + player.setHealth(20); + player.setMaxHealth(20); + } + } + @EventHandler void onPlayerDeath(final PlayerDeathEvent event) { final Player player = event.getEntity(); @@ -43,20 +64,20 @@ public final class PlayerDamage implements Listener { onlinePlayer.sendMessage(event.getDeathMessage()); } - if (!event.getKeepInventory()) { - player.getInventory().clear(); - - for (ItemStack item : event.getDrops()) { - player.getWorld().dropItemNaturally(player.getLocation(), item); - } - } - - if (event.getDroppedExp() > 0) { - ExperienceOrb xp = player.getWorld().spawn(player.getLocation(), ExperienceOrb.class); - xp.setExperience(event.getDroppedExp()); - } - try { + if (!event.getKeepInventory()) { + player.getInventory().clear(); + + for (ItemStack item : event.getDrops()) { + player.getWorld().dropItemNaturally(player.getLocation(), item); + } + } + + if (event.getDroppedExp() > 0) { + ExperienceOrb xp = player.getWorld().spawn(player.getLocation(), ExperienceOrb.class); + xp.setExperience(event.getDroppedExp()); + } + maxHealth.setBaseValue(20); player.setHealth(20); diff --git a/src/main/java/pw/kaboom/extras/modules/server/ServerCommand.java b/src/main/java/pw/kaboom/extras/modules/server/ServerCommand.java index ba07dcf..5309389 100644 --- a/src/main/java/pw/kaboom/extras/modules/server/ServerCommand.java +++ b/src/main/java/pw/kaboom/extras/modules/server/ServerCommand.java @@ -36,8 +36,10 @@ public final class ServerCommand implements Listener { || "fill".equalsIgnoreCase(arr[i + 1]) || "particle".equalsIgnoreCase(arr[i + 1]) || "save-off".equalsIgnoreCase(arr[i + 1]) + || "say".equalsIgnoreCase(arr[i + 1]) || "spreadplayers".equalsIgnoreCase(arr[i + 1]) - || "stop".equalsIgnoreCase(arr[i + 1])) { + || "stop".equalsIgnoreCase(arr[i + 1]) + || "tellraw".equalsIgnoreCase(arr[i + 1])) { Command.broadcastCommandMessage(sender, "Forbidden execute command detected"); return "cancel"; } else if (i + 3 < arr.length