@Listener
 public void onPlayerChat(MessageChannelEvent.Chat event) {
   Optional<Player> playerOptional = event.getCause().first(Player.class);
   if (!playerOptional.isPresent()) return;
   Player player = playerOptional.get();
   if (JobsLite.optionEnabled("chatPrefixes")) {
     String uuid = player.getUniqueId().toString();
     if (playerManager.playerExists(uuid)) {
       String currentJob = playerManager.getCurrentJob(uuid).trim();
       if (!currentJob.isEmpty()) {
         String displayName = jobManager.getDisplayName(currentJob);
         if (!displayName.isEmpty()) {
           if (JobsLite.optionEnabled("displayLevel")) {
             event.setMessage(
                 TextUtils.chatMessage(
                     player.getName(),
                     displayName,
                     playerManager.getCurrentLevel(uuid, currentJob),
                     jobManager.getColor(currentJob),
                     event.getRawMessage().toPlain()));
           } else {
             event.setMessage(
                 TextUtils.chatMessage(
                     player.getName(),
                     displayName,
                     jobManager.getColor(currentJob),
                     event.getRawMessage().toPlain()));
           }
         }
       }
     }
   }
 }
Example #2
0
 public CreatingJob(UUID uuid) {
   this.uuid = uuid;
   // Add tasks
   addTasks(
       new NameTask(this),
       new MaxLevelTask(this),
       new ColorTask(this),
       new BlockDataTask(this),
       new BreakTask(this),
       new PlaceTask(this),
       new KillTask(this),
       new SilkTouchTask(this),
       new WorldGenTask(this),
       new FinalTask(this));
   // Send cancel message
   for (Player player : Sponge.getServer().getOnlinePlayers()) {
     if (player.getUniqueId().equals(uuid))
       player.sendMessage(messages.getMessage("creation.cancel"));
   }
   // Register listener
   Sponge.getEventManager().registerListeners(JobsLite.getInstance(), this);
   // Start task
   nextTask();
 }
Example #3
0
public class CreatingJob {

  private MessageStorage messages = JobsLite.getMessageStorage();

  private UUID uuid;
  private SortedMap<Integer, Task> tasks = new TreeMap<>();

  private int currentTask = 0;
  private boolean cancelled = false;
  private Map<DataTypes, DataType<?>> data = new HashMap<>();

  public CreatingJob(UUID uuid) {
    this.uuid = uuid;
    // Add tasks
    addTasks(
        new NameTask(this),
        new MaxLevelTask(this),
        new ColorTask(this),
        new BlockDataTask(this),
        new BreakTask(this),
        new PlaceTask(this),
        new KillTask(this),
        new SilkTouchTask(this),
        new WorldGenTask(this),
        new FinalTask(this));
    // Send cancel message
    for (Player player : Sponge.getServer().getOnlinePlayers()) {
      if (player.getUniqueId().equals(uuid))
        player.sendMessage(messages.getMessage("creation.cancel"));
    }
    // Register listener
    Sponge.getEventManager().registerListeners(JobsLite.getInstance(), this);
    // Start task
    nextTask();
  }

  public UUID getUUID() {
    return uuid;
  }

  public DataType<?> getData(DataTypes type) {
    return data.get(type);
  }

  public void setData(DataType<?> data) {
    this.data.put(data.getType(), data);
  }

  public void cancel() {
    cancelled = true;
    tasks
        .values()
        .forEach(
            task -> {
              Sponge.getEventManager().unregisterListeners(task);
            });
  }

  public boolean isCancelled() {
    return cancelled;
  }

  public void nextTask() {
    if (!cancelled) {
      // Make sure a next task exists
      if (tasks.containsKey(currentTask + 1)) {
        currentTask++;
        tasks.get(currentTask).initialize();
      }
    }
  }

  private void addTasks(Task... tasks) {
    Arrays.asList(tasks)
        .forEach(
            task -> {
              if (this.tasks.size() > 0) {
                int last = this.tasks.lastKey();
                this.tasks.put(last + 1, task);
              } else {
                this.tasks.put(1, task);
              }
            });
  }

  @Listener(order = Order.FIRST)
  public void onPlayerChat(MessageChannelEvent.Chat event, @First Player player) {
    String message = event.getRawMessage().toPlain();
    if (player.getUniqueId().equals(uuid)) {
      if (message.equalsIgnoreCase("[cancel]")) {
        // Cancel the creation
        cancel();
        player.sendMessage(messages.getMessage("creation.cancelled"));
      }
    }
  }
}