Jump to content

Granate funktioniert nicht?


Reddi
 Share

Recommended Posts

Hallo.

Hier ein Granatencode:

    @EventHandler
    public void interact(PlayerInteractEvent e) {
    	Player p = e.getPlayer();
    	if(e.getAction() == Action.RIGHT_CLICK_AIR) {
    		if(e.getItem().getType() == Material.TNT) {
    			
    			if(p.getItemInHand().getAmount() == 1) {
    				p.setItemInHand(new ItemStack(Material.AIR));
    			}
    				
    			p.getItemInHand().setAmount(p.getItemInHand().getAmount() - 1);
    			
    			final Item tnt = p.getWorld().dropItem(p.getLocation(), new ItemStack(Material.TNT, 1));
    			
    			tnt.setVelocity(p.getLocation().getDirection().multiply(0.8D));
    			tnt.setPickupDelay(10000);
    			
    			Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable(){

					@Override
					public void run() {
						tnt.getWorld().createExplosion(tnt.getLocation().getX(), tnt.getLocation().getY(), tnt.getLocation().getZ(), 3, false, false);
					}
    				
    			}, 20*3);
    		}
    		
    	}
    	
    }

Aber wenn ich Ingame das Item rechtsklicke, passiert einfach garnichts.....

Wenn ihr den Code der ganzen Klasse braucht, sagt es mir :D

Link to comment
Share on other sites

vor 17 Minuten schrieb BloodSKreaper:

first of All: Hast du den Listener registriert?

Ja habe ich :)

 

Bzw. ich habe das garnicht in einem Listener xD

Edited by Reddi
Korrektur
Link to comment
Share on other sites

vor 20 Minuten schrieb Reddi:

Bzw. ich habe das garnicht in einem Listener xD

Ehm doch solltest du haben. Sonst funktioniert das natürlich nicht. Zum Nachlesen: https://www.spigotmc.org/wiki/bbcode.1415/archive

vor 40 Minuten schrieb Reddi:

public void interact(PlayerInteractEvent e) {...}

Hast du es mal mit Debugging versucht? Damit könntest du rausfinden, ob die Methode überhaupt aufgerufen wird.

 

Freundliche Grüße

BloodSKreaper

Link to comment
Share on other sites

Kurz nachschauen...

The method scheduleSyncDelayedTask(Plugin, Runnable, long) in the type BukkitScheduler is not applicable for the arguments (listener, new Runnable(){}, int)

Link to comment
Share on other sites

package at.proxxy.main;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.entity.Item;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import org.bukkit.util.Vector;


public class listener implements Listener{

	@EventHandler
	public void onJoin(PlayerJoinEvent e) {
		Player p = e.getPlayer();
	
		e.setJoinMessage("");
		p.setLevel(0);
		
	
	
	}
	
	@EventHandler
	public void onQuit(PlayerQuitEvent e) {
		Player p  = e.getPlayer();
		e.setQuitMessage("");
		
	
	
	}
	
	@EventHandler
	public void onDeath(PlayerDeathEvent e) {
		Player p = e.getEntity();
		Player k = e.getEntity().getKiller();
		
		
		p.sendMessage("§cDu wurdest von §e" + k.getDisplayName() + " §cgetötet.");
		p.setLevel(0);
		
		if(k != null) {
			k.sendMessage("§aDu hast §e" + p.getDisplayName() + " §agetötet.");
			k.setLevel(k.getLevel() +1 );
		}
		
	}
	
	
	  @EventHandler
	  public void setDroppedExp(int exp) {
	       int i = 0;
	       setDroppedExp(i);
	       
	  }



    
	@EventHandler
	public void onNewPlayerBreak(BlockBreakEvent event){
		Player p  = event.getPlayer();
		if(p.hasPermission("Block.Deny") == false){
				event.setCancelled(true);
				event.getPlayer().sendMessage("§cDu darfst hier nichts abbauen!");
			
		}
	}
	
	@EventHandler
	public void onNewPlayerPlace(BlockPlaceEvent event){
		Player p  = event.getPlayer();
		if(p.hasPermission("Block.Deny") == false){
			event.setBuild(false);
			event.getPlayer().sendMessage("§cDu darfst hier nichts platzieren!");
		}
	}
	
	
	
	@EventHandler
	public void onDeathattack(PlayerDeathEvent e) {
	    if (e.getEntity().getLastDamageCause().getCause() == DamageCause.ENTITY_ATTACK) {
	    	e.setDeathMessage("");
	    }
	}
	@EventHandler
	public void onDeathcont(PlayerDeathEvent e) {
	    if (e.getEntity().getLastDamageCause().getCause() == DamageCause.CONTACT) {
	    	e.setDeathMessage("");
	    }
	}
	@EventHandler
	public void onDeathslava(PlayerDeathEvent e) {
	    if (e.getEntity().getLastDamageCause().getCause() == DamageCause.LAVA) {
	    	e.setDeathMessage("");
	    }
	}
	@EventHandler
	public void onDeathsdrown(PlayerDeathEvent e) {
	    if (e.getEntity().getLastDamageCause().getCause() == DamageCause.DROWNING) {
	    	e.setDeathMessage("");
	    }
	}
	@EventHandler
	public void onDeathsfire(PlayerDeathEvent e) {
	    if (e.getEntity().getLastDamageCause().getCause() == DamageCause.FIRE) {
	    	e.setDeathMessage("");
	    }
	}
	@EventHandler
	public void onDeathslight(PlayerDeathEvent e) {
	    if (e.getEntity().getLastDamageCause().getCause() == DamageCause.LIGHTNING) {
	    	e.setDeathMessage("");
	    }
	}
	@EventHandler
	public void onDeathspoison(PlayerDeathEvent e) {
	    if (e.getEntity().getLastDamageCause().getCause() == DamageCause.POISON) {
	    	e.setDeathMessage("");
	    }
	}
	@EventHandler
	public void onDeathsexplo(PlayerDeathEvent e) {
	    if (e.getEntity().getLastDamageCause().getCause() == DamageCause.BLOCK_EXPLOSION) {
	    	e.setDeathMessage("");
	    }
	}
	
    @EventHandler
    public void onMove(PlayerMoveEvent e){
        Player p = e.getPlayer();
        if(p.getLocation().getBlock().getType() == Material.WATER_LILY){
            Vector v = p.getLocation().getDirection().multiply(3D).setY(3).setX(0).setZ(0);
            p.setVelocity(v);
         
        }
    }
    
    
    
    @EventHandler
    public void onEntityDamage(EntityDamageEvent e) {

    	if(e.getCause() == DamageCause.FALL) {

    		e.setCancelled(true);
    	}
    }
    
    
    @EventHandler
    public void interact(PlayerInteractEvent e) {
    	Player p = e.getPlayer();
    	if(e.getAction() == Action.RIGHT_CLICK_AIR) {
    		if(e.getItem().getType() == Material.TNT) {
    			
    			if(p.getItemInHand().getAmount() == 1) {
    				p.setItemInHand(new ItemStack(Material.AIR));
    			}
    				
    			p.getItemInHand().setAmount(p.getItemInHand().getAmount() - 1);
    			
    			final Item tnt = p.getWorld().dropItem(p.getLocation(), new ItemStack(Material.TNT, 1));
    			
    			tnt.setVelocity(p.getLocation().getDirection().multiply(0.8D));
    			tnt.setPickupDelay(10000);
    			
    			Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable(){

					@Override
					public void run() {
						tnt.getWorld().createExplosion(tnt.getLocation().getX(), tnt.getLocation().getY(), tnt.getLocation().getZ(), 3, false, false);
					}
    				
    			}, 20*3L);
    		}
    		
    	}
    	
    }
    
    

	
}

Immer noch der gleiche Fehler

Link to comment
Share on other sites

I see

Weil das nicht deine Main-Klasse ist welche von JavaPlugin erbt, kannst du nicht "this", also ein Objekt deiner Klasse listener (Laut Konvention schreib man Klassennamen groß) übergeben.

Du musst also beim Registrieren des Listeners entweder deine Main-Klasse, die von Java-Plugin erbt übergeben (im Konstruktor) oder deine Main-Klasse in irgendeiner Art als static verfügbar machen. (Ich empfehle Ersteres)

Das hätte man eigentlich schon an der Fehlermeldung erkennen können, aber die hab ich wohl nicht so genau angeschaut 🙄

 

1. Lösungsvorschlag als Code

In deiner Main-Klasse deines Plugins

public void onEnable(){
   getServer().getPluginManager().registerEvents(new MyListener(this), this);
}

In deiner ListenerKlasse (ich hab sie in MyListener umbenannt)

public class MyListener implements Listener{
   private Plugin plugin;

   public MyListener(Plugin plugin){
      this.plugin = plugin;
   }

   [...diverse Methoden...]
   @EventHandler
	public void interact(PlayerInteractEvent e) {
		Player p = e.getPlayer();
		if (e.getAction() == Action.RIGHT_CLICK_AIR) {
			if (e.getItem().getType() == Material.TNT) {

				if (p.getItemInHand().getAmount() == 1) {
					p.setItemInHand(new ItemStack(Material.AIR));
				}

				p.getItemInHand().setAmount(p.getItemInHand().getAmount() - 1);

				final Item tnt = p.getWorld().dropItem(p.getLocation(), new ItemStack(Material.TNT, 1));

				tnt.setVelocity(p.getLocation().getDirection().multiply(0.8D));
				tnt.setPickupDelay(10000);

				Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {

					@Override
					public void run() {
						tnt.getWorld().createExplosion(tnt.getLocation().getX(), tnt.getLocation().getY(),
								tnt.getLocation().getZ(), 3, false, false);
					}

				}, 20 * 3L);
			}

		}

	}

Freundliche Grüße

BloodSKreaper

Edited by BloodSKreaper
Link to comment
Share on other sites

Okay Danke.

Es funktioniert halbwegs.

Man droppt es nun, wenn man das Item rechtsklickt, jedoch kommt dann folgendes in der console:

[13:47:13] [Server thread/ERROR]: Could not pass event PlayerInteractEvent to mapchange v1.0
org.bukkit.event.EventException
	at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:305) ~[spigot_server.jar:git-Spigot-c3c767f-33d5de3]
	at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot_server.jar:git-Spigot-c3c767f-33d5de3]
	at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot_server.jar:git-Spigot-c3c767f-33d5de3]
	at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot_server.jar:git-Spigot-c3c767f-33d5de3]
	at org.bukkit.craftbukkit.v1_8_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:226) [spigot_server.jar:git-Spigot-c3c767f-33d5de3]
	at org.bukkit.craftbukkit.v1_8_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:193) [spigot_server.jar:git-Spigot-c3c767f-33d5de3]
	at org.bukkit.craftbukkit.v1_8_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:189) [spigot_server.jar:git-Spigot-c3c767f-33d5de3]
	at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:682) [spigot_server.jar:git-Spigot-c3c767f-33d5de3]
	at net.minecraft.server.v1_8_R1.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:50) [spigot_server.jar:git-Spigot-c3c767f-33d5de3]
	at net.minecraft.server.v1_8_R1.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:80) [spigot_server.jar:git-Spigot-c3c767f-33d5de3]
	at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [spigot_server.jar:git-Spigot-c3c767f-33d5de3]
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_25]
	at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_25]
	at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:696) [spigot_server.jar:git-Spigot-c3c767f-33d5de3]
	at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316) [spigot_server.jar:git-Spigot-c3c767f-33d5de3]
	at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:634) [spigot_server.jar:git-Spigot-c3c767f-33d5de3]
	at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:537) [spigot_server.jar:git-Spigot-c3c767f-33d5de3]
	at java.lang.Thread.run(Thread.java:745) [?:1.8.0_25]
Caused by: java.lang.ClassCastException: at.proxxy.main.listener cannot be cast to org.bukkit.plugin.Plugin
	at at.proxxy.main.listener.interact(listener.java:190) ~[?:?]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_25]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_25]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_25]
	at java.lang.reflect.Method.invoke(Method.java:483) ~[?:1.8.0_25]
	at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:301) ~[spigot_server.jar:git-Spigot-c3c767f-33d5de3]
	... 17 more

 

Link to comment
Share on other sites

Ich hätte eine andere Lösung... Einfach "Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin("DEIN PLUGIN-NAME"); in der Klasse initialisieren.
Beispiel:

public class MyListener implements Listener{    
    Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin("DEIN-PLUGIN-NAME");
    
    // Sonstiger Code...
    
    @EventHandler
    public void interact(PlayerInteractEvent e) {
        Player p = e.getPlayer();
        if (e.getAction() == Action.RIGHT_CLICK_AIR) {
            if (e.getItem().getType() == Material.TNT) {
                if (p.getItemInHand().getAmount() == 1) {
                    p.setItemInHand(new ItemStack(Material.AIR));
                }
                p.getItemInHand().setAmount(p.getItemInHand().getAmount() - 1);
                final Item tnt = p.getWorld().dropItem(p.getLocation(), new ItemStack(Material.TNT, 1));
                tnt.setVelocity(p.getLocation().getDirection().multiply(0.8D));
                tnt.setPickupDelay(10000);
                Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                    @Override
                    public void run() {
                        tnt.getWorld().createExplosion(tnt.getLocation().getX(), tnt.getLocation().getY(),
                                tnt.getLocation().getZ(), 3, false, false);
                    }
                }, 20 * 3L);
            }
        }
    }

    // Sonstiger Code...
    
}

Keine Ahnung ob es mehr Ressourcen frisst oder sonstwas, auf jeden Fall funktioniert es bei mir, und ich denke nicht dass es auch bei größeren Sachen Probleme verursachen könnte...^^
    

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...
B
B