有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java不知道如何正确地从配置中提取键

我一直在尝试为一个非常基本的EntityDeAtheEvent脚本实现一个配置文件,以便实现一个重载命令,以便能够添加到我的配置中并重新加载文件,而不是重新启动服务器。我正在使用的当前代码返回零错误,因此我现在完全不知所措。我花了5天时间修补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));
  //}
};

配置。yml

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

请帮我找个方向好吗


共 (2) 个答案

  1. # 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
    
  2. # 2 楼答案

    确保已删除旧的配置文件