org.bukkit.Server.isPrimaryThread()方法的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(3.6k)|赞(0)|评价(0)|浏览(91)

本文整理了Java中org.bukkit.Server.isPrimaryThread()方法的一些代码示例,展示了Server.isPrimaryThread()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Server.isPrimaryThread()方法的具体详情如下:
包路径:org.bukkit.Server
类名称:Server
方法名:isPrimaryThread

Server.isPrimaryThread介绍

[英]Checks the current thread against the expected primary thread for the server.

Note: this method should not be used to indicate the current synchronized state of the runtime. A current thread matching the main thread indicates that it is synchronized, but a mismatch does not preclude the same assumption.
[中]根据服务器的预期主线程检查当前线程。
注意:此方法不应用于指示运行时的当前同步状态。与主线程匹配的当前线程表示它已同步,但不匹配并不排除相同的假设。

代码示例

代码示例来源:origin: Bukkit/Bukkit

/**
 * @see Server#isPrimaryThread()
 */
public static boolean isPrimaryThread() {
  return server.isPrimaryThread();
}

代码示例来源:origin: Bukkit/Bukkit

/**
 * Calls an event with the given details.
 * <p>
 * This method only synchronizes when the event is not asynchronous.
 *
 * @param event Event details
 */
public void callEvent(Event event) {
  if (event.isAsynchronous()) {
    if (Thread.holdsLock(this)) {
      throw new IllegalStateException(event.getEventName() + " cannot be triggered asynchronously from inside synchronized code.");
    }
    if (server.isPrimaryThread()) {
      throw new IllegalStateException(event.getEventName() + " cannot be triggered asynchronously from primary server thread.");
    }
    fireEvent(event);
  } else {
    synchronized (this) {
      fireEvent(event);
    }
  }
}

代码示例来源:origin: webbukkit/dynmap

@Override
public boolean isServerThread() {
  return Bukkit.getServer().isPrimaryThread();
}

代码示例来源:origin: SpigotMC/Spigot-API

/**
 * @see Server#isPrimaryThread()
 */
public static boolean isPrimaryThread() {
  return server.isPrimaryThread();
}

代码示例来源:origin: lucko/helper

@Nonnull
public static synchronized Thread getMainThread() {
  if (mainThread == null) {
    if (Bukkit.getServer().isPrimaryThread()) {
      mainThread = Thread.currentThread();
    }
  }
  return mainThread;
}

代码示例来源:origin: me.lucko/helper

@Nonnull
public static synchronized Thread getMainThread() {
  if (mainThread == null) {
    if (Bukkit.getServer().isPrimaryThread()) {
      mainThread = Thread.currentThread();
    }
  }
  return mainThread;
}

代码示例来源:origin: SpigotMC/Spigot-API

/**
 * Calls an event with the given details.
 * <p>
 * This method only synchronizes when the event is not asynchronous.
 *
 * @param event Event details
 */
public void callEvent(Event event) {
  if (event.isAsynchronous()) {
    if (Thread.holdsLock(this)) {
      throw new IllegalStateException(event.getEventName() + " cannot be triggered asynchronously from inside synchronized code.");
    }
    if (server.isPrimaryThread()) {
      throw new IllegalStateException(event.getEventName() + " cannot be triggered asynchronously from primary server thread.");
    }
    fireEvent(event);
  } else {
    synchronized (this) {
      fireEvent(event);
    }
  }
}

代码示例来源:origin: BentoBoxWorld/BentoBox

private void tidyUp(Entity entity, String failureMessage) {
  // Nothing left to check and still not canceled
  task.cancel();
  // Check portal
  if (portal && bestSpot != null) {
    // No portals found, teleport to the best spot we found
    teleportEntity(bestSpot);
    if (overrideGamemode && entity instanceof Player && ((Player)entity).getGameMode().equals(GameMode.SPECTATOR)) {
      ((Player)entity).setGameMode(plugin.getIWM().getDefaultGameMode(bestSpot.getWorld()));
    }
  } else if (entity instanceof Player && !failureMessage.isEmpty()) {
    // Failed, no safe spot
    entity.sendMessage(failureMessage);
    if (overrideGamemode && ((Player)entity).getGameMode().equals(GameMode.SPECTATOR)) {
      if (plugin.getIWM().inWorld(entity.getLocation())) {
        ((Player)entity).setGameMode(plugin.getIWM().getDefaultGameMode(entity.getWorld()));
      } else {
        // Last resort
        ((Player)entity).setGameMode(GameMode.SURVIVAL);
        if (Bukkit.getServer().isPrimaryThread()) {
          ((Player)entity).performCommand("spawn");
        } else {
          Bukkit.getScheduler().runTask(plugin, () -> ((Player)entity).performCommand("spawn"));
        }
      }
    }
  }
}

相关文章

微信公众号

最新文章

更多

Server类方法