PlayerPortalEvent Help

Discussion in 'Plugin Development' started by Zettos, Aug 30, 2020.

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

    Zettos

    Portal Class :

    Code:
    package me.defs.events;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerPortalEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.potion.PotionEffectType;
    
    import me.defs.main.Main;
    
    public class PortalEvent implements Listener{
    
        @EventHandler
        public void onPortal(PlayerPortalEvent e) {
           
            try {
                Player p = e.getPlayer();
                if(p.getWorld().getName().equals("Lobby")) {
                   
                    Double x = Main.cfg.getDouble("SurvivalWelt.x");
                    Double y = Main.cfg.getDouble("SurvivalWelt.y");
                    Double z = Main.cfg.getDouble("SurvivalWelt.z");
                    float yaw = (float) Main.cfg.getDouble("SurvivalWelt.yaw");
                    float pitch = (float) Main.cfg.getDouble("SurvivalWelt.pitch");
                    World w = Bukkit.getWorld(Main.cfg.getString("SurvivalWelt.welt"));
                   
                    p.getInventory().clear();
                    p.setAllowFlight(false);
                    p.removePotionEffect(PotionEffectType.SPEED);
                    p.teleport(new Location(w, x, y, z, yaw, pitch));
                    p.sendMessage("§eDu wurdest zur Survival Teleportiert!");
                    Main.silentlist.remove(p.getName());
                    Main.hidelist.remove(p.getName());
                    for(Player players : Bukkit.getOnlinePlayers()) {
                        players.showPlayer(p);
                        p.showPlayer(players);
                    }
                   
                   
                   
                        if(Main.cfg.contains("Items."+p.getName()+".inv")) {
                       
                        ItemStack[] contents = (ItemStack[]) Main.cfg.get("Items."+p.getName()+".inv", Main.cfg.getItemStack("Items."+p.getName()+".inv"));
                        ItemStack[] armor = (ItemStack[]) Main.cfg.get("Items."+p.getName()+".armor", Main.cfg.getItemStack("Items."+p.getName()+".armor"));
                        int lvl = Main.cfg.getInt("Items."+p.getName()+".lvl");
                       
                        p.getInventory().setArmorContents(armor);
                        p.getInventory().setContents(contents);
                        p.setLevel(lvl);
                        p.sendMessage("§c§lItems wurden Geladen!");
                       
                        Main.cfg.set("Items."+p.getName()+".inv", null);
                        Main.cfg.set("Items."+p.getName()+".armor", null);
                        Main.cfg.set("Items."+p.getName()+".lvl", null);
                       
                        try {
                            Main.cfg.save(Main.config);
                        } catch (Exception e2) {
                            // TODO: handle exception
                        }
                        }
                   
                   
                   
                   
                }
            } catch (Exception e2) {
                // TODO: handle exception
            }
           
           
           
        }
    }
    Main Class :

    Code:
    package me.defs.main;
    
    import java.io.File;
    import java.util.ArrayList;
    
    import org.bukkit.Bukkit;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import me.defs.commands.Hub;
    import me.defs.commands.Warp;
    import me.defs.events.CompassEvent;
    import me.defs.events.DamageEvent;
    import me.defs.events.DeathEvent;
    import me.defs.events.DropEvent;
    import me.defs.events.FoodLevelEvent;
    import me.defs.events.InteractEvent;
    import me.defs.events.InventoryClickEvent;
    import me.defs.events.JoinEvent;
    import me.defs.events.PlayerCommandEvent;
    import me.defs.events.PortalEvent;
    import me.defs.events.QuitEvent;
    import me.defs.events.WeatherEvent;
    
    public class Main extends JavaPlugin{
       
        public static File config;
        public static FileConfiguration cfg;
       
        public static Inventory compass = Bukkit.createInventory(null, 9, "§e§lNavigator");
        public static ArrayList<String> hidelist = new ArrayList<String>();
        public static ArrayList<String> silentlist = new ArrayList<String>();
       
    @Override
    public void onEnable() {
        saveDefaultConfig();
        Main.config = new File("plugins/LobbySoupServer", "config.yml");
        Main.cfg = YamlConfiguration.loadConfiguration(Main.config);
       
        Commands();
        Events();
       
    }
    @Override
        public void onDisable() {
            saveConfig();
       
       
        }
        public void Commands() {
            this.getCommand("setwarp").setExecutor(new Warp());
            this.getCommand("delwarp").setExecutor(new Warp());
            this.getCommand("warp").setExecutor(new Warp());
            this.getCommand("hub").setExecutor(new Hub());
           
           
           
        }
        public void Events() {
            this.getServer().getPluginManager().registerEvents(new DamageEvent(), this);
            this.getServer().getPluginManager().registerEvents(new DeathEvent(this), this);
            this.getServer().getPluginManager().registerEvents(new JoinEvent(), this);
            this.getServer().getPluginManager().registerEvents(new FoodLevelEvent(), this);
            this.getServer().getPluginManager().registerEvents(new InteractEvent(), this);
            this.getServer().getPluginManager().registerEvents(new QuitEvent(), this);
            this.getServer().getPluginManager().registerEvents(new CompassEvent(), this);
            this.getServer().getPluginManager().registerEvents(new InventoryClickEvent(), this);
            this.getServer().getPluginManager().registerEvents(new DropEvent(), this);
            this.getServer().getPluginManager().registerEvents(new PortalEvent(), this);
            this.getServer().getPluginManager().registerEvents(new PlayerCommandEvent(), this);
            this.getServer().getPluginManager().registerEvents(new WeatherEvent(), this);
        }
    }
    


    My Problem :


    When the Plugin reload , than the Items doesn´t get Loaded. But if i go to the hub and go Back to the Portal,
    than the Items get loaded. I´ve tried a lot but i didn´t get fixed it. I hope you can help me.

    Best regards

    Zettos
     
  2. Offline

    Kars

    Please explain what you are trying to do step by step. What goes wrong and what goes right. Relate it to your code if you can.
     
  3. Offline

    Strahan

    You're handling config in an awkward and potentially crashing manner, not sure why you aren't just leveraging the built in methods for that. I'd also stop puking static all over everything and use dependency injection. Static is not meant to be used simply as an access modifier, that's not its role. Also you are doing a lot of unnecessary work to retrieve and build a Location. Location implements ConfigurationSerializable, just write/read it directly. No need to manually serialize it as you are doing.

    Now to your problem, of course doing a reload won't load the items. You are only populating them on a PlayerPortalEvent. Actually, I'm surprised that worked at all. I'd have thought you'd get a ClassCastException loading the ItemStack array as you are.

    PS I'd also be storing the config data using the player UUID rather than name; names can change, UUIDs do not. The color character should not be directly embedded either - the ChatColor enum exists for that reason.
     
Thread Status:
Not open for further replies.

Share This Page