【发布时间】:2019-12-07 09:09:07
【问题描述】:
我正在编写此代码,并且碰巧将类型作为原始类。现在,如果我将变量与其子类(扩展类)一起插入,则会出错。我正在运行一个 Spigot 插件,但显然这个问题与 Spigot 本身无关,而是我对 Java 缺乏了解。
我无法提出任何单一的解决方案,我已经查看了所有内容。
编辑(我的解释):如果您查看 PlayerClass,您会看到一个名为 Kit 的变量,它使用 KitClass 类型。如果您查看 KitClass,它是一个包含一个名为 Alchemist 的子类的类。我的问题是,KitClass 类型是否包含所有继承类和原始类?如果不是,那么我必须对 PlayerClass 中的 Kit 变量使用什么类型/修改才能允许将 Alchemist 类的实例设置为 Kit 变量?
package me.kckeith.KitPvP;
import org.bukkit.entity.Player;
public class PlayerClass {
public Player Player;
public double Coins;
public KitClass Kit;
public PlayerClass(Player Player, double Coins, KitClass Kit) {
this.Player = Player;
this.Coins = Coins;
this.Kit = Kit;
}
public void setKit(KitClass Kit) {
this.Kit = Kit;
this.Kit.giveStuff();
}
}
package me.kckeith.KitPvP;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import net.md_5.bungee.api.ChatColor;
public class KitClass {
public Player Player;
public Material InventoryIcon;
public KitClass(Player player) {
this.Player = player;
}
public void giveStuff() {
Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.GOLD + "Given items!");
}
}
package me.kckeith.KitPvP.Kits;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import me.kckeith.KitPvP.KitClass;
public class Alchemist extends KitClass {
public static Material InventoryIcon = Material.SPLASH_POTION;
public Alchemist(Player player) {
super(player);
}
public void giveStuff() {
// Clear items
this.Player.getInventory().clear();
// Give the player their armor
ItemStack[] Armor = new ItemStack[4];
Armor[0] = new ItemStack(Material.LEATHER_BOOTS);
Armor[1] = new ItemStack(Material.LEATHER_LEGGINGS);
Armor[2] = new ItemStack(Material.LEATHER_CHESTPLATE);
Armor[3] = new ItemStack(Material.LEATHER_HELMET);
// Add Enchants
Armor[0].addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
Armor[1].addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
Armor[2].addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
Armor[3].addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
// Give Armor
this.Player.getInventory().setArmorContents(Armor);
// Give Golden Sword
ItemStack GoldenSword = new ItemStack(Material.GOLDEN_SWORD);
GoldenSword.addEnchantment(Enchantment.DURABILITY, 1);
this.Player.getInventory().addItem(GoldenSword);
// Give Regen Potions
ItemStack RegenPotion = new ItemStack(Material.POTION, 4);
PotionMeta RegenPotionMeta = (PotionMeta) RegenPotion.getItemMeta();
RegenPotionMeta.addCustomEffect(new PotionEffect(PotionEffectType.REGENERATION, 20, 2), true);
RegenPotion.setItemMeta(RegenPotionMeta);
this.Player.getInventory().addItem(RegenPotion);
// Give Damage Potions
ItemStack DamagePotion = new ItemStack(Material.SPLASH_POTION, 8);
PotionMeta DamagePotionMeta = (PotionMeta) DamagePotion.getItemMeta();
DamagePotionMeta.addCustomEffect(new PotionEffect(PotionEffectType.HARM, 0, 2), true);
DamagePotion.setItemMeta(DamagePotionMeta);
this.Player.getInventory().addItem(DamagePotion);
super.giveStuff();
}
}
我认为具体的错误是 NullStackException 但我不确定。我在代码顶部更详细地讨论了这个问题。
【问题讨论】:
-
抱歉,这个不清楚。 “类型”和“类”是什么意思?此外,发布 actual 堆栈跟踪,而不仅仅是一段错误文本,并指出代码中发生错误的位置。另请阅读如何创建minimal reproducible example
-
类型是指用于变量的类型。像“ClassName NewClass = new ClassName();”。 ClassName 是类型和类。我想知道如果我还尝试将继承的类(Class extends ClassName)包含为变量,我会使用什么类型。
-
请更新您的答案以包含整个错误/堆栈跟踪。
-
请阅读编辑,我会更详细地解释我的问题是什么,我需要什么解决方案。
-
@kieranckeith 您还阅读了 cmets:“发布实际的堆栈跟踪”和关于 minimal reproducible example -“我相信是错误”无关紧要,我们需要知道真实的