簡體   English   中英

無法弄清楚如何從配置中正確提取密鑰

[英]Cant figure out how to properly pull keys from config

我一直在嘗試為一個非常基本的 EntityDeathEvent 腳本實現一個配置文件,以便允許實現重新加載命令,以便能夠添加到我的配置並重新加載文件,而不是重新啟動服務器。 我正在使用的當前代碼返回零錯誤,所以此時我很迷茫。 我有 5 天時間來修補 Java,所以我的代碼中可能缺少大量的東西,但這就是我到目前為止所擁有的。

主.java:

package com.mk7smp.LukesMobEffects;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;



public class Main extends JavaPlugin implements Listener {

  @Override
  public void onEnable() {
    this.saveDefaultConfig();
    this.getServer().getPluginManager().registerEvents(this, this);
  }

  @Override
  public void onDisable() {

  }

  public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if (label.equalsIgnoreCase("mobeffects")) {
      if (!sender.hasPermission("mobeffects.reload")) {
        sender.sendMessage(ChatColor.RED + "Nope, big admin guys only.");
        return true;
      }
      if (args.length == 0) {
        // /mobeffects
        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&f[&bmk7 Mob Effects&f] &aUsage: /mobeffects reload"));
        return true;
      }
      if (args.length > 0) {
        // /mobeffects reload
        if (args[0].equalsIgnoreCase("reload")) {
          for (String msg: this.getConfig().getStringList("reload.message")) {
            sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
              msg));
          }
          this.reloadConfig();
        }
      }
    }
    return false;
  }

  @EventHandler
  public void mobDeath(EntityDeathEvent event) {

    Entity entity = event.getEntity();
    Player player = event.getEntity().getKiller();

    if (player == null) {
      return;
    }

    ConfigurationSection mobs = this.getConfig().getConfigurationSection("mobs"); // The "mobs" section of the config file

    for (String mobKey: mobs.getKeys(false)) { // For ONE(one bc getKeys is set to true) mob key in the set
      String mobname = (String) mobs.get(mobKey + ".name"); // use specified path to retrieve name value
      String message = (String) mobs.get(mobKey + ".message");
      if (entity.getType().toString() == mobname) {
        for (String effectString: mobs.getStringList(mobKey + ".effects")) {


          //make sure there are no whitespaces in the "effectString"
          String[] values = effectString.split(",");

          //create PotionEffectType
          PotionEffectType type;
          //set default duration to 30 seconds
          int duration = 30;
          //set default strength to 1
          int strength = 1;

          //get type
          type = PotionEffectType.getByName(values[0].toUpperCase());

          //check, if type is null
          if (type == null) {
            System.err.println(effectString + " could not be interpreted into a correct PotionEffectType.");
            continue;
          }

          //set duration
          if (values.length > 1)
            duration = Integer.parseInt(values[1]);

          //set strength
          if (values.length > 2)
            strength = Integer.parseInt(values[2]);

          //check, if player already has potiontype
          if (player.hasPotionEffect(type))
            player.removePotionEffect(type);

          //create the effect
          PotionEffect effect = type.createEffect(duration * 20, strength);
          player.addPotionEffect(effect);
          player.sendMessage(ChatColor.translateAlternateColorCodes('&', message));
        }
      }
    }
  }
  // THESE ARE THE FUNCTIONS THAT I WANT TO USE WITH DATA FROM CONFIG
  //if (entity.getType().toString() == "RABBIT") {
  //    player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&f[&bmk7 Mob Effects&f] &aYou've been given &c&lLeaping: 15s &afor killing a &crabbit&a!"));
  //    player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 300, 2));
  //    }
  //if (entity.getType().toString() == "ENDERMAN") {
  //    player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&f[&bmk7 Mob Effects&f] &aYou've been given &c&lRegeneration: 10s &afor killing a &cenderman&a!"));
  //    player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 200, 2));
  //}
  //if (entity.getType().toString() == "CHICKEN") {
  //    player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&f[&bmk7 Mob Effects&f] &aYou've been given &c&lFeather Falling: 10s &afor killing a &c&lchicken&a!"));
  //    player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_FALLING, 200, 1));
  //}
};

配置文件

reload:
   message:
      - "&f[&bmk7 Mob Effects&f] &a&lReloaded config!"
      
mobs:
  rabbit:
    name: rabbit
    message: "&f[&bmk7 Mob Effects&f] &aYou've been given &c&lLeaping: 15s &afor killing a &crabbit&a!"
    effects: 
      - JUMP,15,2

請問我能不能幫到正確的方向

請把第 7 行的兔子變成一個字符串。 添加引號。

reload:
   message:
      - "&f[&bmk7 Mob Effects&f] &a&lReloaded config!"
      
mobs:
  rabbit:
    name: "rabbit"
    message: "&f[&bmk7 Mob Effects&f] &aYou've been given &c&lLeaping: 15s &afor killing a &crabbit&a!"
    effects: 
      - JUMP,15,2

確保您已刪除舊的配置文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM