Minecraft plugin player name error

284 Views Asked by At

I'm writing something like SRM, and everything is fine until server reset. When I'm writing a sign and trying to buy it (region assigned to sign) it should send me a message that I can't, and it does, but after a server reset it doesn't, and in both cases variables are the same... This is my code:

package pl.maccraft.regs;

import java.util.logging.Logger;

import net.milkbowl.vault.economy.Economy;

import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEvent;

import com.sk89q.worldguard.bukkit.RegionContainer;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.domains.DefaultDomain;
import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;

import org.bukkit.plugin.Plugin;

public final class egs extends JavaPlugin implements Listener {

    private static final Logger log = Logger.getLogger("Minecraft");
    public static Economy econ = null;

    @Override
    public void onEnable() {
        getServer().getPluginManager().registerEvents(this, this);
        getWorldGuard();
        if (!setupEconomy() ) {
            log.severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
            getServer().getPluginManager().disablePlugin(this);
            return;
        }
    }

    private WorldGuardPlugin getWorldGuard() {
        Plugin plugin = getServer().getPluginManager().getPlugin("WorldGuard");

        // WorldGuard may not be loaded
        if (plugin == null || !(plugin instanceof WorldGuardPlugin)) {
            return null; // Maybe you want throw an exception instead
        }

        return (WorldGuardPlugin) plugin;
    }

    private boolean setupEconomy() {
        if (getServer().getPluginManager().getPlugin("Vault") == null) {
            return false;
        }
        RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
        if (rsp == null) {
            return false;
        }

        econ = rsp.getProvider();
        return econ != null;
    }

    @EventHandler
    public void playerInteract(PlayerInteractEvent event){
        Player player = event.getPlayer();
        if(event.getAction() == org.bukkit.event.block.Action.LEFT_CLICK_BLOCK){
        Block b = event.getClickedBlock();
        if (b.getType() == (Material.SIGN) || b.getType() == (Material.SIGN_POST) || b.getType() == (Material.WALL_SIGN)){
            Sign sign = (Sign) b.getState();
            if (sign.getLine(0).equals("[sell]")){
                if (!sign.getLine(1).isEmpty()){


                RegionContainer container = getWorldGuard().getRegionContainer();
                RegionManager regions = container.get(player.getWorld());
                if (regions != null) {

                    ProtectedRegion region = regions.getRegion(sign.getLine(1));
                    if (region != null) {
                    DefaultDomain owners = region.getOwners();
                    if(owners.contains(player.getUniqueId())){
                        if (!sign.getLine(2).isEmpty()){
                            Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "say stan:" + econ.getBalance(player));
                        event.setCancelled(true);
                        player.sendMessage("To jest twój region");
                        sign.setLine(0, ChatColor.DARK_RED + "[sprzedam]");
                        sign.setLine(3, sign.getLine(1));
                        sign.setLine(1, player.getName());
                        sign.update(true);  
                    }else{
                        player.sendMessage("Podaj cenę w trzeciej linii!");
                    }
                    }else{
                        player.sendMessage("To nie jest twój region!");
                    };
                    } else {
                        player.sendMessage("Taki region nie istnieje!");
                    }
                } else {
                    player.sendMessage("Wystąpił błąd, który nie powinien się zdarzyć, powiadom o tym administrację!");
                }


                }else{
                    player.sendMessage("Podaj nazwę regionu w drugiej linii!");
                }   
            }
        }
        }
        if(event.getAction() == org.bukkit.event.block.Action.RIGHT_CLICK_BLOCK){
            Block b = event.getClickedBlock();
            if (b.getType() == (Material.SIGN) || b.getType() == (Material.SIGN_POST) || b.getType() == (Material.WALL_SIGN)){
                Sign sign = (Sign) b.getState();
                if (sign.getLine(0).equals(ChatColor.DARK_RED + "[sprzedam]")){
                    if (event.getPlayer().getName() == sign.getLine(1)){
                        player.sendMessage("nie możesz kupić własnej działki");
                        player.sendMessage(event.getPlayer().getName() + " " + sign.getLine(1));
                    }else{
                        player.sendMessage(event.getPlayer().getName() + " " + sign.getLine(1));
                        Double si = Double.valueOf(sign.getLine(2));
                        if(si <= econ.getBalance(player)){//TO POWINNO BYĆ WIĘKSZE LUB RÓWNE A NIE TAKIE SAMO!!!
                        RegionContainer container = getWorldGuard().getRegionContainer();
                        RegionManager regions = container.get(player.getWorld());

                        if (regions != null) {
                            ProtectedRegion region = regions.getRegion(sign.getLine(3));
                            if (region != null) {
                                Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "eco take " + player.getName() + " " + si);
                                Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "eco give " + sign.getLine(1) + " " + si);
                                Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "rg removeowner " + sign.getLine(3) + " " + sign.getLine(1));
                                Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "rg addowner " + sign.getLine(3) + " " + player.getName());
                                sign.setLine(0, ChatColor.DARK_RED + "[sprzedane]");
                                player.sendMessage("kupiono działkę " + sign.getLine(3) + " za " + si);
                                sign.setLine(1, event.getPlayer().getName());
                                sign.setLine(2, "");
                                sign.setLine(3, "");
                                sign.update(true);
                            }

                        }else{
                            player.sendMessage("wystąpił błąd");
                        }


                        }else{
                            player.sendMessage("Nie masz wystarczająco dużo kasy!");
                        }
                    }
                }
            }
        }


    }

}

and console returns only this:

[12:27:42] [Server thread/INFO]: [0;35;1m[Server[0;35;1m] stan:1000.0[m
[12:28:17] [Server thread/INFO]: CONSOLE issued server command: /eco take 10anat10 1000.0
[12:28:17] [Server thread/INFO]: [0;32;1m$1000 zostalo pobrane z [0;31;22m10anat10[m[0;32;1m konta. Nowy stan konta: $0[m
[12:28:17] [Server thread/INFO]: CONSOLE issued server command: /eco give 10anat10 1000.0
[12:28:17] [Server thread/INFO]: [0;32;1m$1000 zostalo dodane do konta [0;31;22m10anat10[m§. Nowy stan konta: $1000.[m
[12:28:17] [Server thread/INFO]: [0;31;1mPlease specify the world with -w world_name.[m
[12:28:17] [Server thread/INFO]: [0;31;1mPlease specify the world with -w world_name.[m

I know that I don't specify world for rg addowner and removeovner, but this isn't it and there are no more errors in the logs... Please help me.

2

There are 2 best solutions below

3
On BEST ANSWER

When I change

if(event.getPlayer().getName() == sign.getLine(1)){

to

RegionContainer container = getWorldGuard().getRegionContainer();
RegionManager regions = container.get(player.getWorld());
if(regions != null){
  ProtectedRegion region = regions.getRegion(sign.getLine(3));
  DefaultDomain owners = region.getOwners();
if(owners.contains(player.getUniqueId())){

It acts as intended.

1
On

You need to compare strings rather then checking if the objects are equal. string1.equals(string2)/string1.equalsIgnoreCase(string2) compares strings while string1 == string2 doesn't.

You could try this:

if (event.getPlayer().getName().equals(sign.getLine(1)))

or if you don't want it case sensitive

if (event.getPlayer().getName().equalsIgnoreCase(sign.getLine(1)))