[LIB] GhostFactory - Make players look like ghosts

Discussion in 'Resources' started by lenis0012, May 24, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    octoshrimpy

    Transparent: cannot be seen
    Invisible: cannot be seen
    Translucent: semi-transparent, only a faint outline.
    with that said, which one are you, which one are players from your point of view, which one are players from their point of view?

    /scoreboard teams ghost list, check who is in the team.

    PS: never say "I use the newest version of [insert thing]" without specifying a version number. in 4 months when bukkit updates, someone might read through the thread, and your "newest version" will be "outdated" to them. Small little thing that always bugs me.
     
  2. Offline

    TheGamblingMan

    i am translucent other players from my point of view are invisible other players from their point of view are translucent

    i have the bukkit 1.7.10-R0.1

    i don't know why but i changed the code now a bit that i can test it with only 2 minecraft accounts and it worked
     
  3. Offline

    octoshrimpy

    TheGamblingMan Were you by chance using PlayerJoinEvent to trigger stuff? I had a few problems with that and had to make a runTask to delay a couple ticks...

    But it's great that you got it working!
     
  4. Offline

    HeavyMine13

    My players are not looking semi invisible...
     
  5. Offline

    octoshrimpy

    HeavyMine13 without more info, there is nothing I can do to help. show me a snippet of code, or explain what you're doing?
     
  6. Offline

    HeavyMine13

    Im doing exactly like the tutorial. My players a full invisible, not semi-invis.
     
  7. Offline

    octoshrimpy

    My Lib? or the one from the first post?
     
  8. Offline

    Apple_

    Is this plugin still needed? I've completed if so.
     
  9. Offline

    metmad22

    Is it possible to add this to zombies or any entities other than players? I tried but had no luck.
     
  10. Offline

    MiniDigger

    No because this is using a feature in minecraft which allows you to see friendly invisibles, people who are in the same team as you are. This is not applicable to any other entity, ASAIK
     
  11. Offline

    metmad22

  12. Offline

    StaticJava

  13. Offline

    TeeePeee

    Sorry to gravedig, but I've just made a new way for this to work regardless as to what Scoreboard a player has active.

    Here's the main class:
    Code:
    /**
    *
    */
    package mypackage;
    
    import java.util.HashMap;
    import java.util.Map;
    
    /**
    * @author Jordan
    *
    */
    public class BigBrother extends ScoreboardTeam {
    
        // The cache of scoreboards this is associated with
        private static final Map<Scoreboard, ScoreboardTeam> scoreboards = new HashMap<Scoreboard, ScoreboardTeam>();
    
        // The GhostFactory
        private static final GhostFactory factory = YOUR_GHOSTFACTORY;
    
        // The name for the team
        private static final String TEAM_NAME = "Ghostly";
    
        public BigBrotherScoreboardTeam(Scoreboard scoreboard, String name) {
            super(scoreboard, name);
            this.scoreboard = scoreboard;
        }
    
        @SuppressWarnings("unchecked")
        public static void registerOn(org.bukkit.scoreboard.Scoreboard board) {
            ScoreboardTeam team = new BigBrother(((CraftScoreboard) board).getHandle(), TEAM_NAME);
    
            Scoreboard handle = ((CraftScoreboard) board).getHandle();
    
            // Minecraft tracks name->team, but it's private final, so let's use some reflection and set everything up.
            Map<String, ScoreboardTeam> teamsByName = (HashMap<String, ScoreboardTeam>) NMS.getDeclaredField(handle, "teamsByName");
            teamsByName.put(TEAM_NAME, team);
            handle.handleTeamAdded(team);
            scoreboards.put(handle, team);
    
            // Some properties...
            team.setAllowFriendlyFire(true);
            team.setCanSeeFriendlyInvisibles(true);
    
            // Make sure we hold all the players!
            update(handle);
        }
    
        public static void update() {
            update(scoreboards.keySet().toArray(new Scoreboard[0]));
        }
    
        @SuppressWarnings("unchecked")
        public static void update(Scoreboard... updating) {
            Set<String> onPlayers = new HashSet<String>();
            for(OfflinePlayer player : factory.getGhosts()) {
                onPlayers.add(player.getName());
            }
    
            for (Scoreboard board : updating) {
                // Minecraft also holds a map of player->team, so let's put everyone in there too
                Map<String, ScoreboardTeam> teamsByPlayer = (HashMap<String, ScoreboardTeam>) NMS.getDeclaredField(board, "teamsByPlayer");
                for (String player : onPlayers) {
                    ScoreboardTeam team = scoreboards.get(board);
                    teamsByPlayer.put(player, team);
                    if (!team.getPlayerNameSet().contains(player))
                        team.getPlayerNameSet().add(player);
                }
            }
        }
    
        public boolean isAlly() {
            return true;
        }
    }
    Now how do you use it? Well first, you'll have to make a few changes to the original GhostFactory class. I basically rewrote it, just making it keep track using a HashSet of player names. Then, every time you create a new Scoreboard in your plugin, just add:
    Code:
    BigBrother.registerOn(YOUR_SCOREBOARD);
    Example usage:
    Code:
    Scoreboard board = Bukkit.getScoreboardManager().getNewScoreboard();
    BigBrother.registerOn(board);
    Objective objective = board.registerNewObjective("something", "dummy");
    objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    objective.setDisplayName("Big Brother");
    You'll also have to keep track of your players. This can be done through your Ghost Factory:

    Code:
    /**
     *
     */
    package mypackage;
    
    import java.util.Collection;
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.Set;
    
    import org.bukkit.Bukkit;
    import org.bukkit.OfflinePlayer;
    import org.bukkit.entity.Player;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    /**
     * @author Comphenix
     * @author Jordan
     *
     */
    public class GhostFactory {
    
       private static Set<String> players = new HashSet<String>();
    
       public GhostFactory() {
       }
    
       public void clearMembers() {
         players.clear();
         BigBrotherScoreboardTeam.update();
       }
    
       public void addPlayer(Player player, boolean ghost) {
         if (!players.contains(player.getName())) {
           players.add(player.getName());
           BigBrotherScoreboardTeam.update();
           setGhost(player, ghost);
         }
       }
    
       public boolean isGhost(Player player) {
         return player != null && hasPlayer(player) && player.hasPotionEffect(PotionEffectType.GHOST);
       }
    
       public boolean hasPlayer(Player player) {
         return players.contains(player.getName());
       }
    
       public void setGhost(Player player, boolean isGhost) {
         if (!isEnabled()) { return; }
    
         if (!hasPlayer(player)) {
           addPlayer(player, isGhost);
         }
    
         if (isGhost && !player.hasPotionEffect(PotionEffectType.INVISIBILITY)) {
           player.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, Integer.MAX_VALUE, 15));
         } else if (!isGhost) {
           player.removePotionEffect(PotionEffectType.INVISIBILITY);
         }
       }
    
       public void removePlayer(Player player) {
         if (players.remove(player.getName())) {
           BigBrotherScoreboardTeam.update();
           player.removePotionEffect(PotionEffectType.INVISIBILITY);
         }
       }
    
       public OfflinePlayer[] getGhosts() {
         Set<OfflinePlayer> players = new HashSet<OfflinePlayer>(getMembers());
    
         return toArray(players);
       }
    
       @SuppressWarnings("deprecation")
       public Set<OfflinePlayer> getMembers() {
         Set<OfflinePlayer> ghosts = new HashSet<OfflinePlayer>();
         for(String player : players) {
           ghosts.add(Bukkit.getPlayerExact(player));
         }
         return ghosts;
       }
    
       private OfflinePlayer[] toArray(Set<OfflinePlayer> players) {
         if (players != null) {
           return players.toArray(new OfflinePlayer[0]);
         } else {
           return new OfflinePlayer[0];
         }
       }
    }
    I've kind of been modifying things on the fly in the code window as I go, so apologies if it's not perfect. But this should get you ghosts and scoreboards at the same time!
     
  14. Offline

    jesus997

    Sorry, but NMS.getDeclaredField (board, "teamsByPlayer") gives me error, Could you ask from where NMS? :'(
     
  15. Offline

    Phasesaber

Thread Status:
Not open for further replies.

Share This Page