取消活动 Spigot 1.19

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

Cancelling an event spigot 1.19

问题

I have written an event called JoinEvent that when an player joins the minecraft server it called. The event checks to see if the player is banned. If it is then it checks my mongodb database for the name to get the staff member and the reason then its meant to kick the member with my custom message but it fails to do so.

JoinEvent:

package xyz.foresthosting.testplugin.events;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import xyz.foresthosting.testplugin.TestPlugin;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;

import javax.print.Doc;

public class PlayerJoin implements Listener
{
    private final TestPlugin plugin;
    public PlayerJoin(TestPlugin plugin)
    {
        this.plugin = plugin;
    }

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event)
    {
        Player player = event.getPlayer();
        if(!player.hasPlayedBefore()) {
            event.setJoinMessage(ChatColor.GREEN + "Welcome to the server " + player.getName() + "! We hope you enjoy your stay.");
        } else if(player.hasPlayedBefore()) {
            event.setJoinMessage(ChatColor.GREEN + "Welcome back to the server " + player.getName() + "!");
        } else if(player.isBanned()) {
            MongoClient mongoClient = new MongoClient("mongodb://admin:<nopasswordforyou>@185.209.223.136:2002");
            MongoDatabase database = mongoClient.getDatabase("test");
            MongoCollection<Document> collection = database.getCollection("bans");
            Document query = new Document("name", event.getPlayer());

            MongoCursor<Document> cursor = collection.find(query).projection(new Document("staff", 1).append("reason", 1)).iterator();
            while(cursor.hasNext()) {
                Document document = cursor.next();
                String staff = document.getString("staff");
                String reason = document.getString("reason");
                player.kickPlayer(ChatColor.RED + "You have been banned from this server!\nReason: " + reason + "\nStaff: " + staff);
            }
        }
    }
}
英文:

I have written an event called JoinEvent that when an player joins the minecraft server it called. The event checks to see if the player is banned. If it is then it checks my mongodb database for the name to get the staff member and the reason then its meant to kick the member with my custom message but it fails to do so.
JoinEvent:

package xyz.foresthosting.testplugin.events;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import xyz.foresthosting.testplugin.TestPlugin;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import javax.print.Doc;
public class PlayerJoin implements Listener
{
private final TestPlugin plugin;
public PlayerJoin(TestPlugin plugin)
{
this.plugin = plugin;
}
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event)
{
Player player = event.getPlayer();
if(!player.hasPlayedBefore()) {
event.setJoinMessage(ChatColor.GREEN + &quot;Welcome to the server &quot; + player.getName() + &quot;! We hope you enjoy your stay.&quot;);
} else if(player.hasPlayedBefore()) {
event.setJoinMessage(ChatColor.GREEN + &quot;Welcome back to the server &quot; + player.getName() + &quot;!&quot;);
} else if(player.isBanned()) {
MongoClient mongoClient = new MongoClient(&quot;mongodb://admin:&lt;nopasswordforyou&gt;@185.209.223.136:2002&quot;);
MongoDatabase database = mongoClient.getDatabase(&quot;test&quot;);
MongoCollection&lt;Document&gt; collection = database.getCollection(&quot;bans&quot;);
Document query = new Document(&quot;name&quot;, event.getPlayer());
MongoCursor&lt;Document&gt; cursor = collection.find(query).projection(new Document(&quot;staff&quot;, 1).append(&quot;reason&quot;, 1)).iterator();
while(cursor.hasNext()) {
Document document = cursor.next();
String staff = document.getString(&quot;staff&quot;);
String reason = document.getString(&quot;reason&quot;);
player.kickPlayer(ChatColor.RED + &quot;You have been banned from this server!\nReason: &quot; + reason + &quot;\nStaff: &quot; + staff);
}
}
}
}

答案1

得分: 1

你的代码永远不会执行连接到数据库的语句。

前两个 if 语句正在阻塞它,因为它们是彼此的相反,肯定会执行其中一个。

为了解决这个问题,你需要将位于 "player.isBanned()" 前面的 "else if" 改为普通的 if,这样它就与其他情况分开了。

英文:

Your code is never reaching the statement where it is connecting to the Database.

The first two if-Statements are blocking it, as one of them definitely gets executed, as they are the opposite of each other.

To fix this, you would need to change the "else if" in front of the "player.isBanned()" to a normal if, so it gets separated from the other cases.

huangapple
  • 本文由 发表于 2023年5月6日 22:49:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189519.html
匿名

发表评论

匿名网友

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

确定