如何连续接受两个输入,并且使第二个输入不与第一个输入相同输出?

huangapple go评论94阅读模式
英文:

How to take two inputs one after another and have the second one not output the same as the first one?

问题

@EventHandler
public void onRightClick(PlayerInteractEvent event) {
    Player player = event.getPlayer();
    Integer pos = 0;

    if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
        if (event.getHand() == EquipmentSlot.OFF_HAND) {
            return;
        }
        if (player.getItemInHand().equals(MapSelectorTool.MapSelector)) {

            Block block = event.getClickedBlock();
            String world = block.getWorld().getName();
            Integer X = block.getX();
            Integer Y = block.getY();
            Integer Z = block.getZ();

            if (pos.equals(0)) {
                String[] pos1 = {
                    X.toString(),
                    Y.toString(),
                    Z.toString(),
                    world
                };
                player.sendMessage(ChatColor.GOLD + "First position at " + ChatColor.WHITE + ChatColor.BOLD + X + " " + Y + " " + Z + " " + ChatColor.GOLD + "now right click second position");
                pos = 1;
            }
        }
    }
}
英文:

I am making a minigame plugin and I want to have the player define a region for the game by right-clicking two opposite corners. My idea was to have the code tell you to click one block and once you select a block it would tell you to pick a second one. After that, it would continue and open a GUI to finish creating the minigame. My first attempt made it so after you select the first block it switch a variable saying that the first corner was selected and then have another if statement waiting for you to select the next corner. But when it would switch the variable it would instantly run the next if statement and set both blocks at the same time.

@EventHandler
public void onRightClick(PlayerInteractEvent event) {
    Player player = event.getPlayer();
    Integer pos = 0;

    if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
        if (event.getHand() == EquipmentSlot.OFF_HAND) {
            return;
        }
        if (player.getItemInHand().equals(MapSelectorTool.MapSelector)) {

            Block block = event.getClickedBlock();
            String world = block.getWorld().getName();
            Integer X = block.getX();
            Integer Y = block.getY();
            Integer Z = block.getZ();

            if (pos.equals(0)) {
                String[] pos1 = {
                    X.toString(),
                    Y.toString(),
                    Z.toString(),
                    world
                };
                player.sendMessage(ChatColor.GOLD + "First position at " + ChatColor.WHITE + ChatColor.BOLD + X + " " + Y + " " + Z + " " + ChatColor.GOLD + "now right click second position");
                pos = 1;


            }
        }
    }
}

答案1

得分: 1

问题在于你在onRightClick方法内部使用了局部变量,例如Integer pos = 0;将在每次右键单击时重置。你需要将这个变量存储在方法外部,以便对第二次点击保持持久性,否则你永远无法进行两次单独的点击并将它们关联起来。

// 将关键变量放在方法/事件外部(根据你编写代码的方式,它们可能需要是静态的)
Player player;
Integer pos = 0;
String[] pos1;
String[] pos2;

public void onRightClick(PlayerInteractEvent event) {
    player = event.getPlayer();

    if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
        if (event.getHand() == EquipmentSlot.OFF_HAND) {
            return;
        }
        if (player.getItemInHand().equals(MapSelectorTool.MapSelector)) {

            Block block = event.getClickedBlock();
            String world = block.getWorld().getName();
            Integer X = block.getX();
            Integer Y = block.getY();
            Integer Z = block.getZ();

            if (pos.equals(0)) {
                pos1 = new String[]{X.toString(), Y.toString(), Z.toString(), world};
                player.sendMessage(ChatColor.GOLD + "第一个位置位于 " + ChatColor.WHITE + ChatColor.BOLD + X + " " + Y + " " + Z + " " + ChatColor.GOLD + "现在右键单击获取第二个位置");
                pos = 1;
            }
            else if (pos.equals(1)) {
                pos2 = new String[]{X.toString(), Y.toString(), Z.toString(), world};
                player.sendMessage(ChatColor.GOLD + "第二个位置位于 " + ChatColor.WHITE + ChatColor.BOLD + X + " " + Y + " " + Z);
                pos = 0;

                // 现在执行其他代码来处理这些位置并创建区域,还可以将变量传递给你的游戏方法来进行设置
                //-----------------------
                // yourGameMethod(pos1, pos2, player);
                //-----------------------
                player.sendMessage(ChatColor.GOLD + "区域已创建,玩得开心!");
            }
        }
    }
}

注意:这段代码可能只适用于一个玩家,这取决于你如何设置插件或触发事件。如果同时有两个玩家在使用,那么他们的点击可能会混在一起。为了解决这个问题,你需要将点击与玩家关联起来,并将它们存储在列表中,以便在许多玩家使用插件时可以轻松地匹配它们,还可以包括一个超时函数,在几分钟内如果没有进行第二次点击,则将pos重置为0。

英文:

The issue is that you are using a local variable inside the onRightClick method, for example Integer pos = 0; will always be reset with every right click. You need to store the variable outside of the method so that it is persistent for the second click, otherwise you can never do two separate clicks and link them.

//Place the key variables outside of the method/event (They may need to be static depending on how you wrote the code)
Player player;
Integer pos = 0;
String[] pos1;
String[] pos2;
public void onRightClick(PlayerInteractEvent event) {
player = event.getPlayer();
if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
if (event.getHand() == EquipmentSlot.OFF_HAND) {
return;
}
if (player.getItemInHand().equals(MapSelectorTool.MapSelector)) {
Block block = event.getClickedBlock();
String world = block.getWorld().getName();
Integer X = block.getX();
Integer Y = block.getY();
Integer Z = block.getZ();
if (pos.equals(0)) {
pos1 = {X.toString(), Y.toString(), Z.toString(), world};
player.sendMessage(ChatColor.GOLD + "First position at " + ChatColor.WHITE + ChatColor.BOLD + X + " " + Y + " " + Z + " " + ChatColor.GOLD + "now right click second position");
pos = 1;
}
else if (pos.equals(1)) {
pos2 = {X.toString(), Y.toString(), Z.toString(), world};
player.sendMessage(ChatColor.GOLD + "Second position at " + ChatColor.WHITE + ChatColor.BOLD + X + " " + Y + " " + Z);
pos = 0;
//Now do your other code to process the positions and create the region?, and/or pass the variables to your game method to get it all setup?
//-----------------------
//yourGameMethod(pos1, pos2, player)
//-----------------------
player.sendMessage(ChatColor.GOLD + "Region created, have fun");
}
}
}
}

Caution: This code may only work for one player at a time depending on how you setup the plugin or trigger the events. If two players are using it at the same time then their clicks may get mixed up. To solve this you need to associate the clicks with a player, and store them in a list so that you can easily match them up when lots of players are using the plugin, and you could include a timeout function to reset the pos to 0 if the second click is not made within several minutes.

答案2

得分: 0

整数 pos = 0;

...

如果 (pos.equals(0)) {...}

你的静态整数对象 pos 从不改变?!

编辑看看https://javatutorialhq.com/java/lang/integer-class-tutorial/
英文:
Integer pos = 0;
...
if (pos.equals(0)) {...}

Your static Integer object pos never changes?!

EDIT: Take a look at: https://javatutorialhq.com/java/lang/integer-class-tutorial/

huangapple
  • 本文由 发表于 2020年8月27日 11:45:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63608845.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定