Nametag method

Status
This thread has been locked.

itsfsnow

Supreme
Feedback score
1
Posts
138
Reactions
31
Resources
0
Excuse not being in the correct place, I don't think there is but this seems most appropiate. SpigotMC doesn't really support 1.7 so I couldn't get anything there really.

I need a method like this one, but for 1.7

Code:
import java.lang.reflect.Field;
import java.util.Collections;

import org.bukkit.ChatColor;
import org.bukkit.craftbukkit.v1_7_R4.entity.CraftPlayer;
import org.bukkit.entity.Player;

import net.minecraft.server.v1_7_R4.PacketPlayOutScoreboardTeam;
import net.minecraft.server.v1_7_R4.ScoreboardTeamBase;



public class NameTagChanger {

    private static PacketPlayOutScoreboardTeam packet; // The packet that will be sent to the player

    /**
     * This is the main method to change the players name.
     *
     * @param toSendTo   The player that you want to send the packet
     * @param player     The player wich name will be changed
     * @param prefix     The prefix that the 'player' will have
     * @param suffix     The suffix that the 'player' will have
     * @param teamAction To CREATE, UPDATE, OR DESTROY the fake team
     */
    public static void changeNameTag(Player toSendTo, Player player, String prefix, String suffix, TeamAction teamAction) {
        sendPacket(toSendTo, teamPacket(player, prefix, suffix, teamAction));
    }
   
    public void updateNameTags(Player player) {
       
    }

    private static PacketPlayOutScoreboardTeam teamPacket(Player player, String prefix, String suffix, TeamAction teamAction) {
        packet = new PacketPlayOutScoreboardTeam();
        setField("a", player.getName());
        setField("b", player.getName());
        setField("c", Color(prefix));
        setField("d", Color(suffix));
        setField("e", ScoreboardTeamBase.EnumNameTagVisibility.ALWAYS.e);

        switch (teamAction) {
            case CREATE:
                addPlayer(player);
                break;
            case UPDATE:
                setField("h", 2);
                break;
            case DESTROY:
                setField("h", 1);
        }

        return packet;
    }

    private static void addPlayer(Player player) {
        setField("g", Collections.singleton(player.getName()));
    }

    private static String Color(String input) {
        return ChatColor.translateAlternateColorCodes('&', input);
    }

    private static void sendPacket(Player player, PacketPlayOutScoreboardTeam packet) {
        ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);
    }

    private static void setField(String field, Object value) {
        try {
            Field f = packet.getClass().getDeclaredField(field);
            f.setAccessible(true);
            f.set(packet, value);
            f.setAccessible(false);
        } catch (Exception exception) {
            exception.printStackTrace();
        }

    }
   
}
When I try that it just crashes the player
 
PebbleHost
High performance, consistent uptime and fast support. Minecraft hosting that just works.

itsfsnow

Supreme
Feedback score
1
Posts
138
Reactions
31
Resources
0
^ The reason why I need to do it using packets is because I want it so 2 player can view a player's name tag differentlyt
 

Cole

Developer
Supreme
Feedback score
19
Posts
944
Reactions
477
Resources
0
I read through it and I can't see how you set a tag for someone per person (e.g. one player sees red when they look at a player, and one person sees green)
You use the player's scoreboards
 

Cole

Developer
Supreme
Feedback score
19
Posts
944
Reactions
477
Resources
0
Would this not conflict with a scoreboard plugin? Will I need to have different scoreboards per person & store it in a hashmap and access that and edit it
Make your own scoreboard plugin or somehow hook into it

Most scoreboard plugins have an individual scoreboard object for each user
 

itsfsnow

Supreme
Feedback score
1
Posts
138
Reactions
31
Resources
0
Make your own scoreboard plugin or somehow hook into it

Most scoreboard plugins have an individual scoreboard object for each user
I imported the source code into my file, but am getting errors for example 'Nametag.getInstance()' getInstance() isn't defined. Do I have to manually add these or have I done something wrong, I haven't gotten errors before importing other than packet errors which is expected cross version[DOUBLEPOST=1564944122][/DOUBLEPOST]& I did install lombok btw
 

Cole

Developer
Supreme
Feedback score
19
Posts
944
Reactions
477
Resources
0
I imported the source code into my file, but am getting errors for example 'Nametag.getInstance()' getInstance() isn't defined. Do I have to manually add these or have I done something wrong, I haven't gotten errors before importing other than packet errors which is expected cross version[DOUBLEPOST=1564944122][/DOUBLEPOST]& I did install lombok btw
Have you added lombok to your dependencies
 
Status
This thread has been locked.
Top