From dbfbab7be2f540cc768cc5031ca1b09846afc345 Mon Sep 17 00:00:00 2001 From: "Luis C.M" Date: Mon, 16 May 2022 18:47:16 -0500 Subject: [PATCH] Players can now have multiple skin overlays, just like GUI overlays. --- .../java/noppes/npcs/PacketHandlerServer.java | 8 ++ .../noppes/npcs/ScriptPlayerEventHandler.java | 16 +++ src/main/java/noppes/npcs/client/Client.java | 4 +- .../java/noppes/npcs/client/NoppesUtil.java | 15 ++ .../npcs/client/PacketHandlerClient.java | 8 +- .../client/renderer/RenderCNPCPlayer.java | 129 +++++++++--------- .../npcs/constants/EnumPacketClient.java | 2 +- .../npcs/constants/EnumPacketServer.java | 4 +- .../controllers/data/SkinOverlayData.java | 11 ++ .../npcs/scripted/entity/ScriptPlayer.java | 41 +++++- 10 files changed, 162 insertions(+), 76 deletions(-) create mode 100644 src/main/java/noppes/npcs/controllers/data/SkinOverlayData.java diff --git a/src/main/java/noppes/npcs/PacketHandlerServer.java b/src/main/java/noppes/npcs/PacketHandlerServer.java index 288e61bc5..576d69ffb 100644 --- a/src/main/java/noppes/npcs/PacketHandlerServer.java +++ b/src/main/java/noppes/npcs/PacketHandlerServer.java @@ -128,6 +128,8 @@ public class PacketHandlerServer{ itemScriptPackets(type, buffer, player); else if (type == EnumPacketServer.ScriptGlobalGuiDataGet || type == EnumPacketServer.ScriptGlobalGuiDataSave) getScriptsEnabled(type, buffer, player); + else if (type == EnumPacketServer.SERVER_UPDATE_SKIN_OVERLAYS) + updateSkinOverlays(player); else if (item.getItem() == CustomItems.scripter) scriptPackets(type, buffer, player, npc); else if (item.getItem() == Item.getItemFromBlock(CustomItems.waypoint) || item.getItem() == Item.getItemFromBlock(CustomItems.border) || item.getItem() == Item.getItemFromBlock(CustomItems.redstoneBlock)) @@ -143,6 +145,12 @@ public class PacketHandlerServer{ NoppesUtilServer.isGUIOpen(buffer, player); } + public void updateSkinOverlays(EntityPlayerMP player) { + NBTTagCompound data = new NBTTagCompound(); + data.setTag("SkinOverlayData",player.getEntityData().getTagList("SkinOverlayData",10)); + Server.sendData(player, EnumPacketClient.CLIENT_UPDATE_SKIN_OVERLAYS, data); + } + private void getScriptsEnabled(EnumPacketServer type, ByteBuf buffer, EntityPlayerMP player) throws IOException { if (type == EnumPacketServer.ScriptGlobalGuiDataGet) { NBTTagCompound compound = new NBTTagCompound(); diff --git a/src/main/java/noppes/npcs/ScriptPlayerEventHandler.java b/src/main/java/noppes/npcs/ScriptPlayerEventHandler.java index 97962af24..75d81209a 100644 --- a/src/main/java/noppes/npcs/ScriptPlayerEventHandler.java +++ b/src/main/java/noppes/npcs/ScriptPlayerEventHandler.java @@ -12,7 +12,10 @@ import cpw.mods.fml.common.gameevent.TickEvent; import cpw.mods.fml.relauncher.Side; import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.inventory.ContainerPlayer; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.server.MinecraftServer; import net.minecraft.util.ChatComponentTranslation; import net.minecraft.util.math.BlockPos; import net.minecraft.world.WorldServer; @@ -26,6 +29,7 @@ import net.minecraftforge.event.entity.living.LivingFallEvent; import net.minecraftforge.event.entity.living.LivingHurtEvent; import net.minecraftforge.event.entity.player.*; import net.minecraftforge.event.world.*; +import noppes.npcs.constants.EnumPacketClient; import noppes.npcs.constants.EnumQuestType; import noppes.npcs.controllers.*; import noppes.npcs.controllers.data.PlayerDataScript; @@ -33,6 +37,8 @@ import noppes.npcs.entity.EntityNPCInterface; import noppes.npcs.scripted.NpcAPI; import noppes.npcs.scripted.interfaces.entity.IPlayer; +import java.util.List; + public class ScriptPlayerEventHandler { public ScriptPlayerEventHandler() { } @@ -426,6 +432,16 @@ public class ScriptPlayerEventHandler { PlayerDataScript handler = ScriptController.Instance.playerScripts; IPlayer scriptPlayer = (IPlayer) NpcAPI.Instance().getIEntity(event.player); EventHooks.onPlayerLogin(handler, scriptPlayer); + + //Loads other players' and own player's skin overlays if they have them + List list = MinecraftServer.getServer().getConfigurationManager().playerEntityList; + for (EntityPlayerMP playerMP : list) { + if (playerMP.getEntityData().hasKey("SkinOverlayData")) { + NBTTagCompound data = new NBTTagCompound(); + data.setTag("SkinOverlayData",playerMP.getEntityData().getTagList("SkinOverlayData",10)); + Server.sendData(playerMP, EnumPacketClient.CLIENT_UPDATE_SKIN_OVERLAYS, data); + } + } } } diff --git a/src/main/java/noppes/npcs/client/Client.java b/src/main/java/noppes/npcs/client/Client.java index 53530e142..0e801c6b9 100644 --- a/src/main/java/noppes/npcs/client/Client.java +++ b/src/main/java/noppes/npcs/client/Client.java @@ -5,6 +5,7 @@ import io.netty.buffer.Unpooled; import java.io.IOException; import java.util.HashMap; +import java.util.UUID; import net.minecraft.util.ResourceLocation; import noppes.npcs.CustomNpcs; @@ -12,10 +13,11 @@ import noppes.npcs.Server; import noppes.npcs.client.gui.customoverlay.OverlayCustom; import noppes.npcs.constants.EnumPacketServer; import cpw.mods.fml.common.network.internal.FMLProxyPacket; +import noppes.npcs.controllers.data.SkinOverlayData; public class Client { public static HashMap customOverlays = new HashMap<>(); - public static ResourceLocation playerOverlay = null; + public static HashMap> skinOverlays = new HashMap<>(); public static void sendData(EnumPacketServer enu, Object... obs) { ByteBuf buffer = Unpooled.buffer(); diff --git a/src/main/java/noppes/npcs/client/NoppesUtil.java b/src/main/java/noppes/npcs/client/NoppesUtil.java index 110ecf88c..253293b48 100644 --- a/src/main/java/noppes/npcs/client/NoppesUtil.java +++ b/src/main/java/noppes/npcs/client/NoppesUtil.java @@ -15,6 +15,7 @@ import net.minecraft.client.gui.GuiScreen; import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.ResourceLocation; import net.minecraft.util.Util; @@ -35,6 +36,7 @@ import noppes.npcs.constants.EnumPlayerPacket; import noppes.npcs.controllers.Dialog; import noppes.npcs.controllers.DialogController; import noppes.npcs.controllers.Quest; +import noppes.npcs.controllers.data.SkinOverlayData; import noppes.npcs.entity.EntityNPCInterface; import org.lwjgl.Sys; @@ -70,6 +72,19 @@ public class NoppesUtil { } } + public static void updateSkinOverlayData(EntityPlayer player, NBTTagCompound compound) { + HashMap skinOverlays = new HashMap<>(); + NBTTagList skinOverlayList = compound.getTagList("SkinOverlayData",10); + + for (int i = 0; i < skinOverlayList.tagCount(); i++) { + int tagID = skinOverlayList.getCompoundTagAt(i).getInteger("SkinOverlayID"); + String tagString = skinOverlayList.getCompoundTagAt(i).getString("SkinOverlayTexture"); + + skinOverlays.put(tagID,new SkinOverlayData(new ResourceLocation(tagString))); + } + Client.skinOverlays.put(player.getUniqueID(), skinOverlays); + } + public static void spawnScriptedParticle(ByteBuf buffer){ Minecraft minecraft = Minecraft.getMinecraft(); diff --git a/src/main/java/noppes/npcs/client/PacketHandlerClient.java b/src/main/java/noppes/npcs/client/PacketHandlerClient.java index 0493ab8bf..d9921ea53 100644 --- a/src/main/java/noppes/npcs/client/PacketHandlerClient.java +++ b/src/main/java/noppes/npcs/client/PacketHandlerClient.java @@ -16,7 +16,6 @@ import net.minecraft.nbt.NBTTagList; import net.minecraft.network.PacketBuffer; import net.minecraft.stats.Achievement; import net.minecraft.util.ChatComponentTranslation; -import net.minecraft.util.ResourceLocation; import net.minecraft.util.StatCollector; import net.minecraft.village.MerchantRecipeList; import noppes.npcs.CustomNpcs; @@ -274,11 +273,8 @@ public class PacketHandlerClient extends PacketHandlerServer{ else if(type == EnumPacketClient.SWING_PLAYER_ARM){ Minecraft.getMinecraft().thePlayer.swingItem(); } - else if(type == EnumPacketClient.PLAYER_OVERLAY) { - String overlayString = Server.readString(buffer); - if (overlayString != null) { - Client.playerOverlay = new ResourceLocation(overlayString); - } + else if(type == EnumPacketClient.CLIENT_UPDATE_SKIN_OVERLAYS) { + NoppesUtil.updateSkinOverlayData(player, Server.readNBT(buffer)); } } } diff --git a/src/main/java/noppes/npcs/client/renderer/RenderCNPCPlayer.java b/src/main/java/noppes/npcs/client/renderer/RenderCNPCPlayer.java index 199dd3a85..082f97daf 100644 --- a/src/main/java/noppes/npcs/client/renderer/RenderCNPCPlayer.java +++ b/src/main/java/noppes/npcs/client/renderer/RenderCNPCPlayer.java @@ -3,54 +3,76 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.entity.RenderPlayer; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.util.ResourceLocation; import noppes.npcs.client.Client; +import noppes.npcs.client.NoppesUtil; +import noppes.npcs.constants.EnumPacketServer; +import noppes.npcs.controllers.data.SkinOverlayData; import org.lwjgl.opengl.GL11; +import java.util.HashMap; + public class RenderCNPCPlayer extends RenderPlayer { public RenderCNPCPlayer() { } + private boolean preRenderOverlay(EntityPlayer player, ResourceLocation overlayLocation) { + try { + this.bindTexture(overlayLocation); + } catch (Exception exception) { + return false; + } + // Overlay & Glow + GL11.glDepthFunc(GL11.GL_LEQUAL); + GL11.glEnable(GL11.GL_BLEND); + GL11.glBlendFunc(GL11.GL_ONE, GL11.GL_ONE); + GL11.glDisable(GL11.GL_LIGHTING); + + GL11.glDepthMask(!player.isInvisible()); + + GL11.glPushMatrix(); + GL11.glMatrixMode(GL11.GL_TEXTURE); + GL11.glLoadIdentity(); + GL11.glScalef(1.0F, 1.0F, 1.0F); + + GL11.glMatrixMode(GL11.GL_MODELVIEW); + float scale = 1.001f; + GL11.glScalef(scale, scale, scale); + + return true; + } + public void postRenderOverlay(EntityPlayer player) { + GL11.glPopMatrix(); + + GL11.glMatrixMode(GL11.GL_TEXTURE); + GL11.glLoadIdentity(); + GL11.glMatrixMode(GL11.GL_MODELVIEW); + + GL11.glEnable(GL11.GL_LIGHTING); + GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); + GL11.glDepthFunc(GL11.GL_LEQUAL); + GL11.glDisable(GL11.GL_BLEND); + } + @Override protected void renderModel(EntityLivingBase p_77036_1_, float p_77036_2_, float p_77036_3_, float p_77036_4_, float p_77036_5_, float p_77036_6_, float p_77036_7_) { + EntityPlayer player = (EntityPlayer) p_77036_1_; this.bindEntityTexture(p_77036_1_); if (!p_77036_1_.isInvisible()) { this.mainModel.render(p_77036_1_, p_77036_2_, p_77036_3_, p_77036_4_, p_77036_5_, p_77036_6_, p_77036_7_); - if (Client.playerOverlay != null) { - try { - this.bindTexture(Client.playerOverlay); - float f1 = 1.0F; - // Overlay & Glow - GL11.glDepthFunc(GL11.GL_LEQUAL); - GL11.glEnable(GL11.GL_BLEND); - GL11.glBlendFunc(GL11.GL_ONE, GL11.GL_ONE); - GL11.glDisable(GL11.GL_LIGHTING); - - GL11.glDepthMask(!p_77036_1_.isInvisible()); - - GL11.glPushMatrix(); - GL11.glMatrixMode(GL11.GL_TEXTURE); - GL11.glLoadIdentity(); - GL11.glScalef(1.0F, 1.0F, 1.0F); - - GL11.glMatrixMode(GL11.GL_MODELVIEW); - float scale = 1.001f; - GL11.glScalef(scale, scale, scale); + if (Client.skinOverlays.containsKey(player.getUniqueID())) { + for (SkinOverlayData overlayData : Client.skinOverlays.get(player.getUniqueID()).values()) { + if (!preRenderOverlay(player, overlayData.location)) + return; this.mainModel.render(p_77036_1_, p_77036_2_, p_77036_3_, p_77036_4_, p_77036_5_, p_77036_6_, p_77036_7_); - GL11.glPopMatrix(); - - GL11.glMatrixMode(GL11.GL_TEXTURE); - GL11.glLoadIdentity(); - GL11.glMatrixMode(GL11.GL_MODELVIEW); - - GL11.glEnable(GL11.GL_LIGHTING); - GL11.glColor4f(1.0F, 1.0F, 1.0F, f1); - GL11.glDepthFunc(GL11.GL_LEQUAL); - GL11.glDisable(GL11.GL_BLEND); - } catch (Exception ignored) {} + postRenderOverlay(player); + } + } else if(player.getEntityData().hasKey("SkinOverlayData")) { + Client.sendData(EnumPacketServer.SERVER_UPDATE_SKIN_OVERLAYS, new Object[0]); } } else if (!p_77036_1_.isInvisibleToPlayer(Minecraft.getMinecraft().thePlayer)) @@ -73,49 +95,26 @@ public class RenderCNPCPlayer extends RenderPlayer { } } - public void renderFirstPersonArm(EntityPlayer p_82441_1_) + public void renderFirstPersonArm(EntityPlayer player) { float f = 1.0F; GL11.glColor3f(f, f, f); this.modelBipedMain.onGround = 0.0F; - this.modelBipedMain.setRotationAngles(0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0625F, p_82441_1_); + this.modelBipedMain.setRotationAngles(0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0625F, player); this.modelBipedMain.bipedRightArm.render(0.0625F); - if (Client.playerOverlay != null) { - try { - this.bindTexture(Client.playerOverlay); - float f1 = 1.0F; - // Overlay & Glow - GL11.glDepthFunc(GL11.GL_LEQUAL); - GL11.glEnable(GL11.GL_BLEND); - GL11.glBlendFunc(GL11.GL_ONE, GL11.GL_ONE); - GL11.glDisable(GL11.GL_LIGHTING); - - GL11.glDepthMask(!p_82441_1_.isInvisible()); - - GL11.glPushMatrix(); - GL11.glMatrixMode(GL11.GL_TEXTURE); - GL11.glLoadIdentity(); - GL11.glScalef(1.0F, 1.0F, 1.0F); - - GL11.glMatrixMode(GL11.GL_MODELVIEW); - float scale = 1.001f; - GL11.glScalef(scale, scale, scale); + if (Client.skinOverlays.containsKey(player.getUniqueID())) { + for (SkinOverlayData overlayData : Client.skinOverlays.get(player.getUniqueID()).values()) { + if (!preRenderOverlay(player, overlayData.location)) + return; GL11.glColor3f(1.0F, 1.0F, 1.0F); this.modelBipedMain.onGround = 0.0F; - this.modelBipedMain.setRotationAngles(0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0625F, p_82441_1_); + this.modelBipedMain.setRotationAngles(0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0625F, player); this.modelBipedMain.bipedRightArm.render(0.0625F); - GL11.glPopMatrix(); - - GL11.glMatrixMode(GL11.GL_TEXTURE); - GL11.glLoadIdentity(); - GL11.glMatrixMode(GL11.GL_MODELVIEW); - - GL11.glEnable(GL11.GL_LIGHTING); - GL11.glColor4f(1.0F, 1.0F, 1.0F, f1); - GL11.glDepthFunc(GL11.GL_LEQUAL); - GL11.glDisable(GL11.GL_BLEND); - } catch (Exception ignored) {} + postRenderOverlay(player); + } + } else if(player.getEntityData().hasKey("SkinOverlayData")) { + Client.sendData(EnumPacketServer.SERVER_UPDATE_SKIN_OVERLAYS, new Object[0]); } } } diff --git a/src/main/java/noppes/npcs/constants/EnumPacketClient.java b/src/main/java/noppes/npcs/constants/EnumPacketClient.java index d3c312f7b..044959ccc 100644 --- a/src/main/java/noppes/npcs/constants/EnumPacketClient.java +++ b/src/main/java/noppes/npcs/constants/EnumPacketClient.java @@ -38,5 +38,5 @@ public enum EnumPacketClient { OVERLAY_CLOSE, SWING_PLAYER_ARM, UPDATE_ITEM, - PLAYER_OVERLAY + CLIENT_UPDATE_SKIN_OVERLAYS } diff --git a/src/main/java/noppes/npcs/constants/EnumPacketServer.java b/src/main/java/noppes/npcs/constants/EnumPacketServer.java index d84364ca0..d33490c9d 100644 --- a/src/main/java/noppes/npcs/constants/EnumPacketServer.java +++ b/src/main/java/noppes/npcs/constants/EnumPacketServer.java @@ -87,7 +87,9 @@ public enum EnumPacketServer { NaturalSpawnGetAll, MailOpenSetup, DimensionsGet, DimensionTeleport, GetTileEntity, Gui, IsGuiOpen, - CustomGuiButton,CustomGuiSlotChange,CustomGuiScrollClick,CustomGuiClose,CustomGuiUnfocused; + CustomGuiButton,CustomGuiSlotChange,CustomGuiScrollClick,CustomGuiClose,CustomGuiUnfocused, + + SERVER_UPDATE_SKIN_OVERLAYS; public CustomNpcsPermissions.Permission permission = null; public boolean needsNpc = false; diff --git a/src/main/java/noppes/npcs/controllers/data/SkinOverlayData.java b/src/main/java/noppes/npcs/controllers/data/SkinOverlayData.java new file mode 100644 index 000000000..726e17b5e --- /dev/null +++ b/src/main/java/noppes/npcs/controllers/data/SkinOverlayData.java @@ -0,0 +1,11 @@ +package noppes.npcs.controllers.data; + +import net.minecraft.util.ResourceLocation; + +public class SkinOverlayData { + public ResourceLocation location; + + public SkinOverlayData(ResourceLocation location) { + this.location = location; + } +} diff --git a/src/main/java/noppes/npcs/scripted/entity/ScriptPlayer.java b/src/main/java/noppes/npcs/scripted/entity/ScriptPlayer.java index a79d3c204..15360a73b 100644 --- a/src/main/java/noppes/npcs/scripted/entity/ScriptPlayer.java +++ b/src/main/java/noppes/npcs/scripted/entity/ScriptPlayer.java @@ -5,6 +5,7 @@ import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; import net.minecraft.stats.Achievement; import net.minecraft.stats.StatBase; import net.minecraft.stats.StatList; @@ -526,8 +527,44 @@ public class ScriptPlayer extends ScriptLivingBase Server.sendData((EntityPlayerMP)this.entity, EnumPacketClient.OVERLAY_CLOSE, id, new NBTTagCompound()); } - public void setPlayerOverlay(String texture) { - Server.sendData((EntityPlayerMP)this.entity, EnumPacketClient.PLAYER_OVERLAY, texture); + public void addSkinOverlay(int id, String overlayString) { + if (overlayString != null) { + NBTTagCompound data = new NBTTagCompound(); + NBTTagCompound compound = new NBTTagCompound(); + + compound.setString("SkinOverlayTexture", overlayString); + compound.setInteger("SkinOverlayID", id); + + if (!player.getEntityData().hasKey("SkinOverlayData")) { + NBTTagList tagList = new NBTTagList(); + tagList.appendTag(compound); + player.getEntityData().setTag("SkinOverlayData", tagList); + } else { + for (int i = 0; i < player.getEntityData().getTagList("SkinOverlayData", 10).tagCount(); i++) { + int tagID = player.getEntityData().getTagList("SkinOverlayData", 10).getCompoundTagAt(i).getInteger("SkinOverlayID"); + if (tagID == id) { + player.getEntityData().getTagList("SkinOverlayData", 10).removeTag(i); + } + } + player.getEntityData().getTagList("SkinOverlayData", 10).appendTag(compound); + } + data.setTag("SkinOverlayData",player.getEntityData().getTagList("SkinOverlayData",10)); + Server.sendData((EntityPlayerMP) this.entity, EnumPacketClient.CLIENT_UPDATE_SKIN_OVERLAYS, data); + } + } + + public void removeSkinOverlay(int id) { + if (player.getEntityData().hasKey("SkinOverlayData")) { + NBTTagCompound data = new NBTTagCompound(); + for (int i = 0; i < player.getEntityData().getTagList("SkinOverlayData",10).tagCount(); i++) { + int tagID = player.getEntityData().getTagList("SkinOverlayData",10).getCompoundTagAt(i).getInteger("SkinOverlayID"); + if (tagID == id) { + player.getEntityData().getTagList("SkinOverlayData",10).removeTag(i); + } + } + data.setTag("SkinOverlayData",player.getEntityData().getTagList("SkinOverlayData",10)); + Server.sendData((EntityPlayerMP)this.entity, EnumPacketClient.CLIENT_UPDATE_SKIN_OVERLAYS, data); + } } public IQuest[] getFinishedQuests() {