Basically im new to plugin development and i was tryna make a plugin which initializes 2 teams namely teamA and teamB, the point of this was to make teamA immune to hostile mobs, like hostile mobs are passive to players on teamA but hostile to teamB, this is what i got so far package me.morbintime.hostilepassiveentity; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.entity.EntityTargetLivingEntityEvent; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.scoreboard.Scoreboard; import org.bukkit.scoreboard.Team; public final class HostilePassiveEntity extends JavaPlugin implements Listener { public static Scoreboard board; public static Team teamA; public static Team teamB; public static void initializeTeams() { board = Bukkit.getServer().getScoreboardManager().getNewScoreboard(); teamA = board.registerNewTeam("teamA"); teamB = board.registerNewTeam("teamB"); } @SuppressWarnings("deprecation") @EventHandler public void onEntityTargetLivingEntity(EntityTargetLivingEntityEvent e) { if (!(e.getTarget() instanceof Player)) return; Player p = (Player) e.getTarget(); if (!teamA.getPlayers().contains(p)) return; e.setCancelled(true); } @Override public void onEnable() { System.out.println("Test"); initializeTeams(); // Plugin startup logic } } The problem im facing is that when ever i reload the server with the plugin, it doesnt initialize the teams a and b, and if i create the teams manually and assign my user to teamA, hostile mobs are still hostile to me please help
Yep, i can confirm you are not registering your event in your case, use this : Code (Java): Bukkit.getPluginManager().registerEvents(this, this);
funny thing it still doesnt work, it doesnt create the teams when its enabled, i have triple checked, heres the code package me.morbintime.hostilepassiveentity; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.entity.EntityTargetLivingEntityEvent; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.scoreboard.Scoreboard; import org.bukkit.scoreboard.Team; public class HostilePassiveEntity extends JavaPlugin implements Listener { public static Scoreboard board; public static Team teamA; public static Team teamB; public static void initializeTeams() { board = Bukkit.getServer().getScoreboardManager().getNewScoreboard(); teamA = board.registerNewTeam("teamA"); teamB = board.registerNewTeam("teamB"); } @SuppressWarnings("deprecation") @EventHandler public void onEntityTargetLivingEntity(EntityTargetLivingEntityEvent e) { if (!(e.getTarget() instanceof Player)) return; Player p = (Player) e.getTarget(); if (!teamA.getPlayers().contains(p)) return; e.setCancelled(true); } @Override public void onEnable() { Bukkit.getPluginManager().registerEvents(this, this); System.out.println("Test"); initializeTeams(); // Plugin startup logic } }
The spigot forum section provides a special block to show code clearly, use it please, it's the + (insert...) Also, for your plugin, better use list of uuid that stores the players Finally, can you be more precise, you say it doesn't work but do you get an error, did you at least added your players into the team ?
i did /team list and theres no teams, i checked console and the sys out is printing i created teams manually after it didnt work automatically, and it still doesnt work
What do i do? i registered my event and thats all u told me. Code (Java): package me.morbintime.hostilepassiveentity; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.entity.EntityTargetLivingEntityEvent; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.scoreboard.Scoreboard; import org.bukkit.scoreboard.Team; public class HostilePassiveEntity extends JavaPlugin implements Listener { public static Scoreboard board; public static Team teamA; public static Team teamB; public static void initializeTeams() { board = Bukkit.getServer().getScoreboardManager().getNewScoreboard(); teamA = board.registerNewTeam("teamA"); teamB = board.registerNewTeam("teamB"); } @SuppressWarnings("deprecation") @EventHandler public void onEntityTargetLivingEntity(EntityTargetLivingEntityEvent e) { if (!(e.getTarget() instanceof Player)) return; Player p = (Player) e.getTarget(); if (!teamA.getPlayers().contains(p)) return; e.setCancelled(true); } @Override public void onEnable() { Bukkit.getPluginManager().registerEvents(this, this); System.out.println("Test"); initializeTeams(); // Plugin startup logic } }
May I ask, why you're even using Teams instead of an ArrayList? You could simply insert a part of the players into an ArrayList, go through it in the event and then cancel the event for all players in the ArrayList TeamA.
I think you should redo your approach to this problem, as teams aren't meant to be used as storing groups of players, but rather to group or set properties on an actual scoreboard. If you want to store a list of players on a 'team', you should use a List or Set of Player or UUID to add players to a team and check if that collection contains e.getTarget() in your event. Code (Java): private Set<UUID> players = new HashSet<>(); ... if (players.contains(e.getTarget().getUniqueId())) { // do something } Also, you probably only need 1 team for players who are immune to hostile mobs, since players by default are targeted, making teamB useless (depends on your use case though) edit: sniped by cracki
im a new spigot programmer and i quite litterally, do not understand what i should do with this code, could u implement it on my piece of code if its not too much to ask?
Using a List of UUID's makes everything easier and uncomplicated. You can add and remove players from it where and whenever you'd like to. If you keep searching through it in the event, only the players from your list will be protected from damage. There is actually no example needed here. The Code snippet you were given, only needs the event to be cancelled. Just use it in your event method. Also I am not quite sure if EntityTargetLivingEntityEvent will do the trick. I think EntityDamageByEntityEvent is used to cancel damage. After doing a bit of research I figured out that entitytargetlivingentityevent would stop mobs from targeting other entities so I am sure this is exactly what's needed here. [please correct if wrong]
HOW do i use a list of uuids? im like really new and i would appreciate if u could possibly write the code for it
To create the list : Code (Java): List<UUID> teamA = new ArrayList<>(); then to manage the list : Code (Java): teamA.add(); //add player in it teamA.remove(); //remove player from teamA.contains(); //to check if the player is in the list