-1

How do i do this? i want to test if the sign line 2 (1) is equal to "Closed" or "Open" and if not i want to say to please specify if its open or closed it says that if i type open or closed where the || is does not work...

package me.mcmatt.shops;

import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.Sound;
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.block.Action;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.event.player.PlayerInteractEvent;

public class Signs implements Listener {


    @EventHandler
    public void onSignChange(SignChangeEvent e) {
        if (e.getLine(0).equalsIgnoreCase("[shop]")) {
            Block attached = e.getBlock().getRelative(0, -1, 0);
            String name = e.getPlayer().getDisplayName();
            if (!(attached.getType() == Material.CHEST))
                e.getPlayer().sendMessage(ChatColor.RED + "Please place the shop on a chest!");
            else {
                if (!e.getPlayer().hasPermission("shops.create"))
                    e.getPlayer().sendMessage(ChatColor.RED + "You don't have permission to create a shop! (shops.create)");
                else {
                    if (!e.getLine(1).equalsIgnoreCase("open") || (!e.getLine(1).equalsIgnoreCase("closed"))) {
                        e.getPlayer().sendMessage(ChatColor.RED + "You must specify if the shop is open or closed on the second line!");
                    } else {
                        Sign o = (Sign) e.getBlock().getState();
                        String p = o.getLine(1);
                        e.setLine(0, "§9[Shop]");
                        e.setLine(1, "§4" + name + "'s");
                        e.setLine(2, "§4Shop");
                        e.setLine(3, p);
                        e.getPlayer().sendMessage(ChatColor.GREEN + "Shop Created!");
                        e.getPlayer().playSound(e.getPlayer().getLocation(), Sound.LEVEL_UP, 10, 10);
                    }
                }
            }
        }
    }

    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent e) {
        if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
            Player p = e.getPlayer();
            Block b = e.getClickedBlock();
            Material m = b.getType();
            if (!(m == Material.SIGN_POST)) {
                return;
            } else {
                Sign sign = (Sign) e.getClickedBlock().getState();
                if ((sign.getLine(0).equalsIgnoreCase("§9[Shop]"))) {
                    p.sendMessage("I right clicked the sign!");
                }
            }
        }
    }
}
TimoStaudinger
  • 41,396
  • 16
  • 88
  • 94
McMatt
  • 15
  • 2
  • 12
  • Possible duplicate of [Why non-equality check of one variable against many values always returns true?](https://stackoverflow.com/questions/26337003/why-non-equality-check-of-one-variable-against-many-values-always-returns-true) – Davis Herring Jan 07 '18 at 06:21

2 Answers2

2

You can create a list of strings you want to compare with, and then use the List.contains method:

if (!Arrays.asList("open", "closed").contains(e.getLine(1).toLowerCase()) {...
Forketyfork
  • 7,416
  • 1
  • 26
  • 33
  • Thanks it worked! and one more question in the code you may see that i tried to copy line 1 to line 3 when it shop is created but it is not working... – McMatt May 31 '15 at 20:13
0

The reason that your code is not working is because of De Morgan's law

Given two statements, A and B. If you do not want A or B to true

(!A) or (!B) is not correct

(!A) and (!B) is correct or !(A or B) is logically equivalent.

yxre
  • 3,576
  • 21
  • 20