非常简单的问题,困难的解决方案,因为我需要编辑我的整个代码。 我正在做一个哈利波特插件,你可以在你右键点击你的魔杖之后选择一个咒语" (在这种情况下一根棍子) 现在,我把它搞砸了,因为......是的,我创造了1个库存。每个人都应该能够解锁不同的法术,现在当1改变时,所有人都会改变。
我在一个单独的类中创建了Inventory,通过一些方法我可以接收/设置我需要的一切。 现在,我找不到在没有库存类的情况下接收播放器的方法 1)删除修饰符=不能再访问它 2)需要删除/重写我整个代码的90% 如果有人能解决它会更好......现在坐3个小时。
SpellInventory类:
package at.skyblock.main;
import java.util.ArrayList;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.event.Listener;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
public class SpellInventory implements Listener {
// Erstellt bei Rechtsklick mit dem Zauberstab das Inventar, in dem die
// Spells gesetzt werden können. (!Nur erlernte werden angezeigt, siehe ->
// SpellUnlocks!)
// Die hier vorhandenen createDisplays() werden noch zu den Spellbooks (also
// SpellUnlocks class) gemoved.
public static Inventory spells = Bukkit.createInventory(null, 9,
ChatColor.BOLD + "" + ChatColor.WHITE + "Choose your Spell");
// Methode um dem Inventar auch von anderen Klassen (in diesem Fall
// SpellUnlocks) Spells hinzuzufügen
// Für die verarbeitung eines klicks auf eines der im Inventar vorhandenen
// Items, ist die Main class zuständig
public static void createDisplay(Material material, Inventory inv, int Slot, String name, String lore) {
ItemStack item = new ItemStack(material);
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(name);
ArrayList<String> Lore = new ArrayList<String>();
Lore.add(lore);
meta.setLore(Lore);
item.setItemMeta(meta);
inv.setItem(Slot, item);
}
public static Inventory getInventory(){
return spells;
}
// Selbsterklärend
public static void removeDisplay(Inventory inv, Material material) {
if (inv.contains(material)) {
inv.remove(material);
}
}
static {
// Proceed to remove this section, and give all unlocks to the books
// (see -> SpellUnlocks)
createDisplay(Material.MOSSY_COBBLESTONE, spells, 6, "Petrificus Totalus", "Ganzkörperklammer Fluch");
createDisplay(Material.TNT, spells, 7, "Bombarda", "Sprengzauber");
}
}
主要课程:
package at.skyblock.main;
import java.util.HashSet;
import org.bukkit.ChatColor;
import org.bukkit.Color;
import org.bukkit.FireworkEffect;
import org.bukkit.FireworkEffect.Type;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Firework;
import org.bukkit.entity.LightningStrike;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.FireworkMeta;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.util.Vector;
public class Main extends JavaPlugin implements Listener {
// Instanz der Main class wird in "SpellUnlocks" kreiiert
private SpellUnlocks event = new SpellUnlocks(this);
public void onEnable() {
// Andere-Klassen Events werden regestriert
getServer().getPluginManager().registerEvents(this, this);
getServer().getPluginManager().registerEvents(event, this);
getServer().getPluginManager().registerEvents(new SpellInventory(), this);
// Fügt den Spellbooks die ItemMeta hinzu (Siehe funktion in SpellBooks)
event.createSpellbooks();
// Checkt im halben sekunden (20tick=1sec) Intervall ob der Spieler die
// Sprüche schon hat, löscht sie falls nicht.
}
@EventHandler
public void onSpellApply(PlayerJoinEvent e) {
Player p = e.getPlayer();
if (getConfig().getBoolean("MSPotter." + p.getUniqueId() + ".hasUnforgivableCurses") == true) {
SpellInventory.createDisplay(Material.BEDROCK, SpellInventory.getInventory(), 0, "Avada Kedavra",
"Todeszauber");
} else if (getConfig().getBoolean("MSPotter." + p.getUniqueId() + ".hasUnforgivableCurses") != true) {
SpellInventory.removeDisplay(SpellInventory.getInventory(), Material.BEDROCK);
}
if (getConfig().getBoolean("MSPotter." + p.getUniqueId() + ".hasBasicSpells") == true) {
SpellInventory.createDisplay(Material.FIREWORK_CHARGE, SpellInventory.getInventory(), 1, "Stupefy",
"Schockzauber");
SpellInventory.createDisplay(Material.REDSTONE, SpellInventory.getInventory(), 2, "Expelliarmus",
"Entwaffnungszauber");
} else if (getConfig().getBoolean("MSPotter." + p.getUniqueId() + ".hasBasicSpells") != true) {
SpellInventory.removeDisplay(SpellInventory.getInventory(), Material.FIREWORK_CHARGE);
SpellInventory.removeDisplay(SpellInventory.getInventory(), Material.REDSTONE);
}
}
// Selbsterklärend
public void onDisable() {
saveConfig();
}
// Checkt welcher Spell in Inventar ausgewählt wurde, und setzt diesen als
// currentSpell für den Spieler
@EventHandler
public void chooseSpell(InventoryClickEvent event) {
Player player = (Player) event.getWhoClicked();
ItemStack clicked = event.getCurrentItem();
Inventory inventory = event.getInventory();
if (inventory.getName().equals(SpellInventory.spells.getName())) {
event.setCancelled(true);
if (clicked.getType() == Material.BEDROCK) {
player.closeInventory();
getConfig().set("MSPotter." + player.getUniqueId() + ".currentSpell", "AvadaKedavra");
saveConfig();
} else if (clicked.getType() == Material.FIREWORK_CHARGE) {
player.closeInventory();
getConfig().set("MSPotter." + player.getUniqueId() + ".currentSpell", "Stupefy");
saveConfig();
} else if (clicked.getType() == Material.REDSTONE) {
player.closeInventory();
getConfig().set("MSPotter." + player.getUniqueId() + ".currentSpell", "Expelliarmus");
saveConfig();
} else if (clicked.getType() == Material.MOSSY_COBBLESTONE) {
player.closeInventory();
getConfig().set("MSPotter." + player.getUniqueId() + ".currentSpell", "PetrificusTotalus");
saveConfig();
} else if (clicked.getType() == Material.TNT) {
player.closeInventory();
getConfig().set("MSPotter." + player.getUniqueId() + ".currentSpell", "Bombarda");
saveConfig();
}
}
}
// Hauptteil: Checkt bei Linksklick mit dem Zauberstab, welcher Spell
// gesetzt ist, führt diesen dann mit Partikeln sound on effekten aus.
@SuppressWarnings({ "deprecation", "unused" })
@EventHandler
public void useSpell(PlayerInteractEvent e) {
final Player p = e.getPlayer();
if (e.getAction().equals(Action.LEFT_CLICK_AIR) || e.getAction().equals(Action.LEFT_CLICK_BLOCK)) {
if (p.getItemInHand().getType().equals(Material.STICK)) {
Location loc = p.getTargetBlock((HashSet<Byte>) null, 20).getLocation();
final Firework f = (Firework) p.getWorld().spawn(loc.add(0, 1.5, 0), Firework.class);
FireworkMeta fm = f.getFireworkMeta();
if (getConfig().getString("MSPotter." + p.getUniqueId() + ".currentSpell").equals("AvadaKedavra")) {
if (getConfig().getBoolean("MSPotter." + p.getUniqueId() + ".cooldowns.AvadaKedavra") == false) {
getConfig().set("MSPotter." + p.getUniqueId() + ".cooldowns.AvadaKedavra", true);
p.playSound(p.getLocation(), Sound.ENDERDRAGON_GROWL, 1.0F, 0);
fm.addEffect(FireworkEffect.builder().flicker(false).trail(true).with(Type.BURST)
.withColor(Color.GREEN).withFade(Color.BLACK).build());
fm.setPower(5);
f.setFireworkMeta(fm);
this.getServer().getScheduler().scheduleAsyncDelayedTask(this, new Runnable() {
public void run() {
f.detonate();
}
}, 2L);
for (Entity en : f.getNearbyEntities(1, 1, 1)) {
if (en instanceof LivingEntity) {
LivingEntity le = (LivingEntity) en;
LightningStrike ls = (LightningStrike) le.getWorld().spawn(le.getLocation(),
LightningStrike.class);
le.setHealth(0);
}
}
this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
public void run() {
getConfig().set("MSPotter." + p.getUniqueId() + ".cooldowns.AvadaKedavra", false);
}
}, 6000L);
}
// #################################################################################################
} else if (getConfig().getString("MSPotter." + p.getUniqueId() + ".currentSpell").equals("Stupefy")) {
fm.addEffect(FireworkEffect.builder().flicker(false).trail(true).with(Type.BALL)
.withColor(Color.AQUA).withFade(Color.BLUE).build());
fm.setPower(3);
f.setFireworkMeta(fm);
this.getServer().getScheduler().scheduleAsyncDelayedTask(this, new Runnable() {
public void run() {
f.detonate();
}
}, 2L);
for (Entity en : f.getNearbyEntities(1.5, 1.5, 1.5)) {
if (en instanceof LivingEntity) {
LivingEntity le = (LivingEntity) en;
le.setVelocity(p.getLocation().getDirection().multiply(3));
le.setVelocity(new Vector(le.getVelocity().getX(), 1.0D, le.getVelocity().getZ()));
if (le.getHealth() > 3) {
le.setHealth(le.getHealth() - 1.5);
}
}
}
} else
if (getConfig().getString("MSPotter." + p.getUniqueId() + ".currentSpell").equals("Expelliarmus")) {
p.playSound(p.getLocation(), Sound.SHOOT_ARROW, 1.0F, 0);
fm.addEffect(FireworkEffect.builder().flicker(false).trail(true).with(Type.STAR)
.withColor(Color.RED).build());
fm.setPower(1);
f.setFireworkMeta(fm);
this.getServer().getScheduler().scheduleAsyncDelayedTask(this, new Runnable() {
public void run() {
f.detonate();
}
}, 2L);
for (Entity en : f.getNearbyEntities(1, 1, 1)) {
if (en instanceof Player) {
Player target = (Player) en;
if (target.getInventory().getItemInHand().getType().equals(Material.STICK)) {
if (target.getInventory().getItemInHand() != null
&& target.getInventory().getItemInHand().getType() != Material.AIR) {
ItemStack wand = target.getInventory().getItemInHand();
p.getWorld().dropItemNaturally(p.getLocation(), wand);
target.getInventory().remove(Material.STICK);
}
} else {
p.sendMessage(ChatColor.RED + "Dein Gegner hat keinen Zauberstab in der Hand!");
}
}
}
} else if (getConfig().getString("MSPotter." + p.getUniqueId() + ".currentSpell")
.equals("PetrificusTotalus")) {
p.playSound(p.getLocation(), Sound.ITEM_BREAK, 1.0F, 0);
fm.addEffect(FireworkEffect.builder().flicker(false).trail(true).with(Type.BALL)
.withColor(Color.WHITE).build());
fm.setPower(1);
f.setFireworkMeta(fm);
this.getServer().getScheduler().scheduleAsyncDelayedTask(this, new Runnable() {
public void run() {
f.detonate();
}
}, 2L);
for (Entity en : f.getNearbyEntities(1, 1, 1)) {
if (en instanceof LivingEntity) {
LivingEntity le = (LivingEntity) en;
if (le.isOnGround()) {
le.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 100, 10000));
le.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 100, -10000));
} else {
p.sendMessage(ChatColor.RED + "Du kannst nur Gegner am Boden treffen!");
}
}
}
} else if (getConfig().getString("MSPotter." + p.getUniqueId() + ".currentSpell").equals("Bombarda")) {
p.playSound(p.getLocation(), Sound.EXPLODE, 1.0F, 0);
TNTPrimed tnt = loc.getWorld().spawn(loc, TNTPrimed.class);
tnt.setFuseTicks(0);
}
}
} else if (e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
if (p.getItemInHand().getType().equals(Material.STICK)) {
p.openInventory(SpellInventory.spells);
}
}
}
}
答案 0 :(得分:0)
由于没有解决方案,您只需在此处更改10%的代码,以及我是如何做到的:
首先,不要创建主类的新实例(是的,我理解德语)。 您总是通过使用所谓的构造函数将其提供给其他类,然后将其存储在变量中。例如:
public class SpellInventory{
private Main plugin; //This is the main class you can access all public methods through it
public SpellInventory(Main plugin){ //This is a constructor Method
this.plugin = plugin; //The plugin class variable gets stored
}
}
现在您必须实现SpellInventory类,并可选择将其保存在Main类中,以便您可以从那里使用它。
private SpellInventory si;
@Override
public void onEnable(){
this.si = new SpellInventory(this); //You hand over the existing main instance with "this" as argument
}
现在,您可以在包含HashMaps的SpellManager类中创建一些内容,其中包含所有玩家的UUID作为键的HashMaps,以及某种布尔列表作为声明该玩家未解锁的值的值。登记/> 我现在不会把汤匙给你了,我已经帮你做了一个你从那里开始的插件的基本设置。