Issue with recording event to log file on Minecraft

Discussion in 'Plugin Development' started by caledonian26, Feb 28, 2024.

  1. Offline

    caledonian26

    Hey all,

    I have the following code below.

    The aim of this code is to place a particular coloured top in a given player's inventory and dress the player in a given colour, based on 4 inputs - the player, the trial type, the color specified and any additional colours specified.

    For some reason, when the function is called, no items are appearing in any player's inventory and the players are not being dressed in any colours.

    I can't understand why this might be.

    Would be so grateful for a helping hand! :)

    Code:
                
    @SuppressWarnings("deprecation")
    public class Main extends JavaPlugin implements Listener
    {
    
    private void dressPlayerAndPlaceItems(Player player, String trial, String color, String additionalColor) {
            player.getInventory().setArmorContents(new ItemStack[]{null, null, null, null});
            player.getInventory().clear();
            ItemStack chestplate = new ItemStack(Material.LEATHER_CHESTPLATE);
            LeatherArmorMeta chestplateMeta = (LeatherArmorMeta) chestplate.getItemMeta();
            player.sendMessage(trialtype);    
    
            switch (color.toLowerCase()) {
                case "yellow":
                    chestplateMeta.setColor(Color.fromRGB(255, 255, 0));
                    chestplate.setItemMeta(chestplateMeta);
                    if (trial.equalsIgnoreCase("solofollow") || trial.equalsIgnoreCase("solochase")) {
                        player.sendMessage("Placing item in inventory");
                        player.getInventory().addItem(chestplate);
                    }
                    break;
                case "red":
                    chestplateMeta.setColor(Color.fromRGB(255, 0, 0));
                    chestplate.setItemMeta(chestplateMeta);
                    if (trial.equalsIgnoreCase("solofollow") || trial.equalsIgnoreCase("solochase")) {
                        player.sendMessage("Placing item in inventory");
                        player.getInventory().addItem(chestplate);
                    }
                    break;
                case "blue":
                    chestplateMeta.setColor(Color.fromRGB(0, 0, 255));
                    chestplate.setItemMeta(chestplateMeta);
                    if (trial.equalsIgnoreCase("groupfollow") || trial.equalsIgnoreCase("groupchase")) {
                        player.sendMessage("Placing item in inventory");
                        player.getInventory().addItem(chestplate);
                    }
                    break;
                case "pink":
                    chestplateMeta.setColor(Color.fromRGB(255, 105, 180));
                    chestplate.setItemMeta(chestplateMeta);
                    if (trial.equalsIgnoreCase("groupfollow") || trial.equalsIgnoreCase("groupchase")) {
                        player.sendMessage("Placing item in inventory");
                        player.getInventory().addItem(chestplate);
                    }
                    break;
                case "white":
                    chestplateMeta.setColor(Color.fromRGB(192, 192, 192));
                    chestplate.setItemMeta(chestplateMeta)
                    if (trial.equalsIgnoreCase("groupfollow") || trial.equalsIgnoreCase("groupchase")) {
                        player.sendMessage("Placing item in inventory");
                        player.getInventory().addItem(chestplate);
                    }
                    break;
                default:
                    player.sendMessage("Invalid color specified!");
                    return;
            }
    
            if (additionalColor != null && !additionalColor.isEmpty()) {
                switch (additionalColor.toLowerCase()) {
                    case "orange":
                        chestplateMeta.setColor(Color.fromRGB(255, 165, 0));
                        chestplate.setItemMeta(chestplateMeta);
                        player.getInventory().setArmorContents(new ItemStack[]{null, null, chestplate, null});
                        break;
                    case "light green":
                        chestplateMeta.setColor(Color.fromRGB(144, 238, 144));
                        chestplate.setItemMeta(chestplateMeta);
                        player.getInventory().setArmorContents(new ItemStack[]{null, null, chestplate, null});
                        break;
                    default:
                        player.sendMessage("Invalid additional color specified!");
                        return;
                }
            }
        }
    
    if (event.getAction() == Action.PHYSICAL && event.getClickedBlock().getType() == Material.LIGHT_WEIGHTED_PRESSURE_PLATE) {
                    if (event.getPlayer().getName().equals("Tao_927")) {
                        trialtype = "solochase";
                        Player tao = Bukkit.getPlayer("Tao_927");
                        Player jhumana = Bukkit.getPlayer("Jhumana");
                        Player emre = Bukkit.getPlayer("caledonian26");
                        Player anna = Bukkit.getPlayer("TwentyPoet26227");
                        if (emre != null) {
                            dressPlayerAndPlaceItems(emre, "solochase", "red", "light green");
                            emre.sendMessage("In the next trial, you will be hunting the prey dressed in red. Please hunt the prey dressed in red.");
                        }
                        if (anna != null) {
                            dressPlayerAndPlaceItems(anna, "solochase", "yellow", "orange");
                            anna.sendMessage("In the next trial, you will be hunting the prey dressed in yellow. Please hunt the prey dressed in yellow.");
                        }
                        if (tao != null) {
                            dressPlayerAndPlaceItems(tao, "solochase", "yellow", null);
                            tao.sendMessage("The next trial is a solo hunt.");
                        }
                        if (jhumana != null) {
                            dressPlayerAndPlaceItems(jhumana, "solochase", "red", null);
                            jhumana.sendMessage("The next trial is a solo hunt.");                 
                        }                  
                    }
     
  2. Online

    timtower Administrator Administrator Moderator

    @caledonian26 And where is the code that executes this function?
     
  3. Offline

    caledonian26

    It is within the code block above. Apologies if you did not see it! I have put it below too:

    Code:
    if (event.getAction() == Action.PHYSICAL && event.getClickedBlock().getType() == Material.LIGHT_WEIGHTED_PRESSURE_PLATE) {
                    if (event.getPlayer().getName().equals("Tao_927")) {
                        trialtype = "solochase";
                        Player tao = Bukkit.getPlayer("Tao_927");
                        Player jhumana = Bukkit.getPlayer("Jhumana");
                        Player emre = Bukkit.getPlayer("caledonian26");
                        Player anna = Bukkit.getPlayer("TwentyPoet26227");
                        if (emre != null) {
                            dressPlayerAndPlaceItems(emre, "solochase", "red", "light green");
                            emre.sendMessage("In the next trial, you will be hunting the prey dressed in red. Please hunt the prey dressed in red.");
                        }
                        if (anna != null) {
                            dressPlayerAndPlaceItems(anna, "solochase", "yellow", "orange");
                            anna.sendMessage("In the next trial, you will be hunting the prey dressed in yellow. Please hunt the prey dressed in yellow.");
                        }
                        if (tao != null) {
                            dressPlayerAndPlaceItems(tao, "solochase", "yellow", null);
                            tao.sendMessage("The next trial is a solo hunt.");
                        }
                        if (jhumana != null) {
                            dressPlayerAndPlaceItems(jhumana, "solochase", "red", null);
                            jhumana.sendMessage("The next trial is a solo hunt.");                
                        }                 
                    }
     
  4. Online

    timtower Administrator Administrator Moderator

    @caledonian26 But there is no function call, I have no idea what it looks like.
    Please post the full class, not snippets.
     
  5. Offline

    caledonian26

    So sorry, please the corrected code below:

    Code:
    @SuppressWarnings("deprecation")
    public class Main extends JavaPlugin implements Listener
    {
    
    private void dressPlayerAndPlaceItems(Player player, String trial, String color, String additionalColor) {
            player.getInventory().setArmorContents(new ItemStack[]{null, null, null, null});
            player.getInventory().clear();
            ItemStack chestplate = new ItemStack(Material.LEATHER_CHESTPLATE);
            LeatherArmorMeta chestplateMeta = (LeatherArmorMeta) chestplate.getItemMeta();
            player.sendMessage(trialtype);  
    
            switch (color.toLowerCase()) {
                case "yellow":
                    chestplateMeta.setColor(Color.fromRGB(255, 255, 0));
                    chestplate.setItemMeta(chestplateMeta);
                    if (trial.equalsIgnoreCase("solofollow") || trial.equalsIgnoreCase("solochase")) {
                        player.sendMessage("Placing item in inventory");
                        player.getInventory().addItem(chestplate);
                    }
                    break;
                case "red":
                    chestplateMeta.setColor(Color.fromRGB(255, 0, 0));
                    chestplate.setItemMeta(chestplateMeta);
                    if (trial.equalsIgnoreCase("solofollow") || trial.equalsIgnoreCase("solochase")) {
                        player.sendMessage("Placing item in inventory");
                        player.getInventory().addItem(chestplate);
                    }
                    break;
                case "blue":
                    chestplateMeta.setColor(Color.fromRGB(0, 0, 255));
                    chestplate.setItemMeta(chestplateMeta);
                    if (trial.equalsIgnoreCase("groupfollow") || trial.equalsIgnoreCase("groupchase")) {
                        player.sendMessage("Placing item in inventory");
                        player.getInventory().addItem(chestplate);
                    }
                    break;
                case "pink":
                    chestplateMeta.setColor(Color.fromRGB(255, 105, 180));
                    chestplate.setItemMeta(chestplateMeta);
                    if (trial.equalsIgnoreCase("groupfollow") || trial.equalsIgnoreCase("groupchase")) {
                        player.sendMessage("Placing item in inventory");
                        player.getInventory().addItem(chestplate);
                    }
                    break;
                case "white":
                    chestplateMeta.setColor(Color.fromRGB(192, 192, 192));
                    chestplate.setItemMeta(chestplateMeta)
                    if (trial.equalsIgnoreCase("groupfollow") || trial.equalsIgnoreCase("groupchase")) {
                        player.sendMessage("Placing item in inventory");
                        player.getInventory().addItem(chestplate);
                    }
                    break;
                default:
                    player.sendMessage("Invalid color specified!");
                    return;
            }
    
            if (additionalColor != null && !additionalColor.isEmpty()) {
                switch (additionalColor.toLowerCase()) {
                    case "orange":
                        chestplateMeta.setColor(Color.fromRGB(255, 165, 0));
                        chestplate.setItemMeta(chestplateMeta);
                        player.getInventory().setArmorContents(new ItemStack[]{null, null, chestplate, null});
                        break;
                    case "light green":
                        chestplateMeta.setColor(Color.fromRGB(144, 238, 144));
                        chestplate.setItemMeta(chestplateMeta);
                        player.getInventory().setArmorContents(new ItemStack[]{null, null, chestplate, null});
                        break;
                    default:
                        player.sendMessage("Invalid additional color specified!");
                        return;
                }
            }
        }
    
    @EventHandler
       public void onPlayerInteract(PlayerInteractEvent event) {
    if (event.getAction() == Action.PHYSICAL && event.getClickedBlock().getType() == Material.LIGHT_WEIGHTED_PRESSURE_PLATE) {
                    if (event.getPlayer().getName().equals("Tao_927")) {
                        trialtype = "solochase";
                        Player tao = Bukkit.getPlayer("Tao_927");
                        Player jhumana = Bukkit.getPlayer("Jhumana");
                        Player emre = Bukkit.getPlayer("caledonian26");
                        Player anna = Bukkit.getPlayer("TwentyPoet26227");
                        if (emre != null) {
                            dressPlayerAndPlaceItems(emre, "solochase", "red", "light green");
                            emre.sendMessage("In the next trial, you will be hunting the prey dressed in red. Please hunt the prey dressed in red.");
                        }
                        if (anna != null) {
                            dressPlayerAndPlaceItems(anna, "solochase", "yellow", "orange");
                            anna.sendMessage("In the next trial, you will be hunting the prey dressed in yellow. Please hunt the prey dressed in yellow.");
                        }
                        if (tao != null) {
                            dressPlayerAndPlaceItems(tao, "solochase", "yellow", null);
                            tao.sendMessage("The next trial is a solo hunt.");
                        }
                        if (jhumana != null) {
                            dressPlayerAndPlaceItems(jhumana, "solochase", "red", null);
                            jhumana.sendMessage("The next trial is a solo hunt.");               
                        }                
                    }
     
    Last edited: Feb 28, 2024
  6. Online

    timtower Administrator Administrator Moderator

    @caledonian26 Did you check if there is a clicked block? And what the type is?
    If the event was physical?
     
  7. Offline

    caledonian26

    The material is a heavy_weighted_pressure_plate. Thus, when a player named 'Tao_927' stands on the heavy_weighted_pressure_plate, it then dresses the players who are logged into the server using the
    dressPlayerAndPlaceItems function depending on which players are logged into the server.
     
  8. Online

    timtower Administrator Administrator Moderator

    @caledonian26 So why are you checking LIGHT_WEIGHTED_PRESSURE_PLATE ?
     
  9. Offline

    caledonian26

    Sorry, that's a typo - I mean heavy_weighted_pressure_plate :)
     
  10. Online

    timtower Administrator Administrator Moderator

    Your code does LIGHT_WEIGHTED_PRESSURE_PLATE, both your explanations do heavy, which one is it?
     
  11. Offline

    caledonian26

    Sorry, my explanation should have said light_weighted_pressure_plate instead of heavy_weighted_pressure_plate!
     
  12. Online

    timtower Administrator Administrator Moderator

    Considered adding print statements to your code to see to which if statement it gets?
     
  13. Offline

    caledonian26

    I have tried that and they are not appearing on-screen or in the log… :(
     
  14. Online

    timtower Administrator Administrator Moderator

    You are missing your onEnable where you tell the server that your plugin wants events.
     
  15. Offline

    caledonian26

    I have this here (see code below). Would that not do the job? :)

    Code:
     @EventHandler
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this,this);
            getLogger().info("HELLO! WELCOME TO THE TRACKER PLUGIN");
            dataFolder = new File(getDataFolder(), "newdata");
            dataFolder.mkdirs();
        }
     
  16. Online

    timtower Administrator Administrator Moderator

    @caledonian26 Let me repeat
    This is where thing go wrong.
     
  17. Offline

    caledonian26

    Apologies for the confusion. Please find the full code below:

    Code:
    @SuppressWarnings("deprecation")
    public class Main extends JavaPlugin implements Listener
    {
    
    @EventHandler
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this,this);
            getLogger().info("HELLO! WELCOME TO THE TRACKER PLUGIN");
            dataFolder = new File(getDataFolder(), "newdata");
            dataFolder.mkdirs();
        }
    
    private void dressPlayerAndPlaceItems(Player player, String trial, String color, String additionalColor) {
            player.getInventory().setArmorContents(new ItemStack[]{null, null, null, null});
            player.getInventory().clear();
            ItemStack chestplate = new ItemStack(Material.LEATHER_CHESTPLATE);
            LeatherArmorMeta chestplateMeta = (LeatherArmorMeta) chestplate.getItemMeta();
            player.sendMessage(trialtype); 
    
            switch (color.toLowerCase()) {
                case "yellow":
                    chestplateMeta.setColor(Color.fromRGB(255, 255, 0));
                    chestplate.setItemMeta(chestplateMeta);
                    if (trial.equalsIgnoreCase("solofollow") || trial.equalsIgnoreCase("solochase")) {
                        player.sendMessage("Placing item in inventory");
                        player.getInventory().addItem(chestplate);
                    }
                    break;
                case "red":
                    chestplateMeta.setColor(Color.fromRGB(255, 0, 0));
                    chestplate.setItemMeta(chestplateMeta);
                    if (trial.equalsIgnoreCase("solofollow") || trial.equalsIgnoreCase("solochase")) {
                        player.sendMessage("Placing item in inventory");
                        player.getInventory().addItem(chestplate);
                    }
                    break;
                case "blue":
                    chestplateMeta.setColor(Color.fromRGB(0, 0, 255));
                    chestplate.setItemMeta(chestplateMeta);
                    if (trial.equalsIgnoreCase("groupfollow") || trial.equalsIgnoreCase("groupchase")) {
                        player.sendMessage("Placing item in inventory");
                        player.getInventory().addItem(chestplate);
                    }
                    break;
                case "pink":
                    chestplateMeta.setColor(Color.fromRGB(255, 105, 180));
                    chestplate.setItemMeta(chestplateMeta);
                    if (trial.equalsIgnoreCase("groupfollow") || trial.equalsIgnoreCase("groupchase")) {
                        player.sendMessage("Placing item in inventory");
                        player.getInventory().addItem(chestplate);
                    }
                    break;
                case "white":
                    chestplateMeta.setColor(Color.fromRGB(192, 192, 192));
                    chestplate.setItemMeta(chestplateMeta)
                    if (trial.equalsIgnoreCase("groupfollow") || trial.equalsIgnoreCase("groupchase")) {
                        player.sendMessage("Placing item in inventory");
                        player.getInventory().addItem(chestplate);
                    }
                    break;
                default:
                    player.sendMessage("Invalid color specified!");
                    return;
            }
    
            if (additionalColor != null && !additionalColor.isEmpty()) {
                switch (additionalColor.toLowerCase()) {
                    case "orange":
                        chestplateMeta.setColor(Color.fromRGB(255, 165, 0));
                        chestplate.setItemMeta(chestplateMeta);
                        player.getInventory().setArmorContents(new ItemStack[]{null, null, chestplate, null});
                        break;
                    case "light green":
                        chestplateMeta.setColor(Color.fromRGB(144, 238, 144));
                        chestplate.setItemMeta(chestplateMeta);
                        player.getInventory().setArmorContents(new ItemStack[]{null, null, chestplate, null});
                        break;
                    default:
                        player.sendMessage("Invalid additional color specified!");
                        return;
                }
            }
        }
    
    @EventHandler
       public void onPlayerInteract(PlayerInteractEvent event) {
    if (event.getAction() == Action.PHYSICAL && event.getClickedBlock().getType() == Material.LIGHT_WEIGHTED_PRESSURE_PLATE) {
                    if (event.getPlayer().getName().equals("Tao_927")) {
                        trialtype = "solochase";
                        Player tao = Bukkit.getPlayer("Tao_927");
                        Player jhumana = Bukkit.getPlayer("Jhumana");
                        Player emre = Bukkit.getPlayer("caledonian26");
                        Player anna = Bukkit.getPlayer("TwentyPoet26227");
                        if (emre != null) {
                            dressPlayerAndPlaceItems(emre, "solochase", "red", "light green");
                            emre.sendMessage("In the next trial, you will be hunting the prey dressed in red. Please hunt the prey dressed in red.");
                        }
                        if (anna != null) {
                            dressPlayerAndPlaceItems(anna, "solochase", "yellow", "orange");
                            anna.sendMessage("In the next trial, you will be hunting the prey dressed in yellow. Please hunt the prey dressed in yellow.");
                        }
                        if (tao != null) {
                            dressPlayerAndPlaceItems(tao, "solochase", "yellow", null);
                            tao.sendMessage("The next trial is a solo hunt.");
                        }
                        if (jhumana != null) {
                            dressPlayerAndPlaceItems(jhumana, "solochase", "red", null);
                            jhumana.sendMessage("The next trial is a solo hunt.");              
                        }               
                    }
     
  18. Online

    timtower Administrator Administrator Moderator

    @caledonian26 Still missing brackets from the class.
    I want the full file, stop removing parts.
    ctrl+a, ctrl+v

    Github / gitlab link is also fine, or pastebin.
    But stop removing stuff!
     
  19. Offline

    caledonian26

  20. Online

    timtower Administrator Administrator Moderator

    @caledonian26 Have you considered splitting things?
    Like: hey, these functions are for pressure plates, and within that: hey, function per pressure plate type.
    Your function is 1500 lines, if I have a class that long I am already reconsidering rewriting.
    And please: run a formatter.
     
  21. Offline

    caledonian26

    My code now works - I realised an indent was missing!

    Corrected code:

    https://paste.gg/p/anonymous/049b3351fe224868bb79b38ebc3cd181

    Thank you so much for all your support :)
     
    Last edited: Feb 29, 2024
  22. Online

    timtower Administrator Administrator Moderator

    @caledonian26 You never said you had an error...
    That is information that should have been given by the start.
    The error even says what is wrong.
     

Share This Page