Solved I don't understand my problem...

Discussion in 'Plugin Development' started by Koraizon, Jul 31, 2020 at 6:18 AM.

  1. Offline

    Koraizon

    I don't even know how to explain my problem because it's not logic at all...

    First, here's my code, I will explain hit just below:

    Code:
    private MainJ main;
        boolean SpawnHorse = false;
        boolean SpawningHorse = false;
        UUID UUIDFH;
        UUID UUIDF;
    
        public PlayerListener(MainJ main) {
            this.main = main;
        }
       
        @EventHandler
        public void onJoin(PlayerJoinEvent event) {
            Player player = event.getPlayer();
            player.getInventory().clear();
            player.setFoodLevel(20);
            player.setHealth(20);
            ItemStack item = new ItemStack(Material.SADDLE, 1);
            ItemMeta itemm = item.getItemMeta();
            itemm.setDisplayName("§6Véhicule de Wheel of Fortune");
            item.setItemMeta(itemm);
            player.getInventory().addItem(item);
           
            if(!main.isState(State.WAITING))
            {
                player.setGameMode(GameMode.SPECTATOR);
                player.sendMessage("Le jeu a déjà démarré !");
                event.setJoinMessage(null);
                return;
            }
            player.setGameMode(GameMode.ADVENTURE);
            if (player.isOp()) {
                player.setGameMode(GameMode.CREATIVE);
            }
           
            event.setJoinMessage("§7[§eDé à coudre§7]§r" + player.getName() + "§e joined the game ! <" + Bukkit.getOnlinePlayers().size() + "/" + Bukkit.getMaxPlayers() + ">");
        }
       
        @EventHandler
        public void onPvp(EntityDamageByEntityEvent event) {
            if(event.getEntity().getUniqueId() == UUIDFH) Bukkit.broadcastMessage("Horse okay");
            if(event.getDamager().getUniqueId() == UUIDF) Bukkit.broadcastMessage("Human okay");
           
            if(event.getEntity().getUniqueId() == UUIDFH && event.getDamager().getUniqueId() == UUIDF) {
                event.setCancelled(true);
                event.getEntity().remove();
                new BukkitRunnable() {
                   
                    @Override
                    public void run() {
                        for(Player player : Bukkit.getOnlinePlayers()) {
                            if(player.getUniqueId() == UUIDF) {
                                player.sendMessage("§6Vous pouvez de nouveau utiliser votre véhicule");
                                SpawnHorse = false;
                            }
                        }
                       
                    }
                }.runTaskLater(this.main, 20);
            }
        }
       
        @EventHandler
        public void onEntitySpawnEvent(EntitySpawnEvent event) {
            ItemStack saddleH = new ItemStack(Material.SADDLE);
            ItemStack armureH = new ItemStack(Material.GOLD_BARDING);
    
            if(event.getEntityType() == EntityType.HORSE && SpawningHorse) {
                UUIDFH = event.getEntity().getUniqueId();
                Horse horse = (Horse)event.getEntity();
                horse.setAdult();
                horse.setDomestication(horse.getMaxDomestication());
                horse.getInventory().setSaddle(saddleH);
                horse.getInventory().setArmor(armureH);
                horse.setJumpStrength(1);
                SpawningHorse = false;
            }
        }
       
        @EventHandler
        public void onInteract(PlayerInteractEvent e) {
            ItemStack it = e.getItem();
            Player player = e.getPlayer();
            Action action = e.getAction();
            if(it == null) return;
           
               
            if(action == Action.RIGHT_CLICK_BLOCK) {
                if(it.getItemMeta().getDisplayName() == "§6Véhicule de Wheel of Fortune") {
                    player.sendMessage("Wouaw");
                    EntityType fortuneshorse = EntityType.HORSE;
                    UUIDF = player.getUniqueId();
                    if(!SpawnHorse) {
                        SpawningHorse = true;
                        SpawnHorse = true;
                        player.getWorld().spawnEntity(e.getClickedBlock().getLocation().add(0, 1, 0), fortuneshorse);
                    } else {
                        Entity horseF = getEntityByUniqueId(UUIDFH);
                        if(horseF.getUniqueId() == UUIDFH) Bukkit.broadcastMessage("Horse okay");
                        horseF.remove();
                        new BukkitRunnable() {
                           
                            @Override
                            public void run() {
                                player.sendMessage("§6Vous pouvez de nouveau utiliser votre véhicule");
                                SpawnHorse = false;
                               
                            }
                        }.runTaskLater(this.main, 20);
                    }
                }
            }
        }
       
        @EventHandler
        public void onHorseDeath(EntityDeathEvent e) {
            if(e.getEntity().getUniqueId() == UUIDFH) {
                new BukkitRunnable() {
                   
                    @Override
                    public void run() {
                        for(Player player : Bukkit.getOnlinePlayers()) {
                            if(player.getUniqueId() == UUIDF) {
                                player.sendMessage("§6Vous pouvez de nouveau utiliser votre véhicule");
                                SpawnHorse = false;
                            }
                        }
                       
                    }
                }.runTaskLater(this.main, 20);
            }
        }
       
       
        public Entity getEntityByUniqueId(UUID uniqueId) {
            for (World world : Bukkit.getWorlds()) {
                for (Entity entity : world.getEntities()) {
                    if (entity.getUniqueId().equals(uniqueId))
                        return entity;
                 }
            }
    
            return null;
        }
    In my program, I made a saddle that when you right click on a block with it, a Horse appear, awesome!
    Afte that I made 2 ways for the Horse to disappear.
    The first one is, when you right click on a block with the saddle, the horse disappear
    The second one is, when you hit the horse, the horse disappear
    When the Horse disappear, i created a Cooldown for the player to use the saddle again

    Problem is: When I right click on a block with the saddle, the horse appear with no problem but if i disconnect and reconnect, when i hit the horse, he don't disappear but when i use the saddle it does and it maked no sense to me... So I tried to search why does he doesn't disappear when i hit him and put some print.

    First case: When i don't disconnect and reconnect, all the prints works so it broadcast "Human okay" and "Horse Okay" when I hit him and when I use the right click on block with the saddle, it broadcast "Horse okay", it works well

    Second case: When i disconnect and reconnect, the prints don't work anymore so i though there was a problem with the UUID but it dooesn't seems like it because when i use the saddle, the horse disappear so it does reconize the horse UUID but it don't want to broadcast the message so I don't understand anything anymore... Help me plz:'(
     
  2. Offline

    CraftCreeper6

    @Koraizon
    On this line:
    Code:
    if(player.getUniqueId() == UUIDF) {
    Change == to .equals

    == checks to see if they are from the same location in memory, which, when you reconnect, may not be the case anymore, .equals however will check the content. Which will be the same, reconnect or not.

    Also, your current functionality will only work for one player. If that's what you want then fine, but it seems like a server wide thing to me.
     
    Koraizon likes this.
  3. Offline

    Koraizon

    Wow it works thanks a lot, i didn't think that == was checking location in memory and not the content, I had lots of other problems with disconnecting and reconnecting but I guessed that it was from the same type of problem that this one so i just asked for one of my problems and it seems like it was true!
    You really saved me there I appreciate the help and the explication so thank you so much ^^
    Also to answer your question, i made it like that because i use it for only a player because i made a role that just one player can have in a uhc so i don't need more of it ^^
     

Share This Page