Solved [HELP] Give player sword on login not working

Discussion in 'Plugin Development' started by CreepahMC, Jan 28, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    CreepahMC

    For some reason in my code for a simple plugin, all my events aren't working meaning when I upload the plugin and put it in my server, it has no errors or anything but just nothing works lol.

    Here's a snip of the first part of my code:
    Code:java
    1. package me.creepah.LegionsCore;
    2.  
    3. import java.util.List;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.Sound;
    7. import org.bukkit.World;
    8. import org.bukkit.entity.Item;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.entity.EntityDamageEvent;
    12. import org.bukkit.event.player.AsyncPlayerChatEvent;
    13. import org.bukkit.event.player.PlayerDropItemEvent;
    14. import org.bukkit.event.player.PlayerInteractEvent;
    15. import org.bukkit.event.player.PlayerPickupItemEvent;
    16. import org.bukkit.inventory.ItemStack;
    17. import org.bukkit.inventory.meta.ItemMeta;
    18. import org.bukkit.plugin.java.JavaPlugin;
    19.  
    20. public class LegionsCore extends JavaPlugin
    21. {
    22. public void onEnable()
    23. {
    24. }
    25.  
    26. public void onDisable()
    27. {
    28. }
    29.  
    30. @EventHandler
    31. public static void TradeChat(AsyncPlayerChatEvent event)
    32. {
    33. String Prefix = event.getMessage().split(" ")[0];
    34. if ((Prefix.equalsIgnoreCase("WTS")) || (Prefix.equalsIgnoreCase("WTB")) || (Prefix.equalsIgnoreCase("WTT")) || (Prefix.equalsIgnoreCase("PC")) || (Prefix.equalsIgnoreCase("Sell")) || (Prefix.equalsIgnoreCase("Price Check")) || (Prefix.equalsIgnoreCase("Buy")) || (Prefix.equalsIgnoreCase("Selling")) || (Prefix.equalsIgnoreCase("Buying")))
    35. event.setFormat("&3&l<T> &f" + event.getPlayer().getName() + "&7: " + event.getMessage());
    36. }
    37.  
    38. @EventHandler
    39. public void UnDrop(PlayerDropItemEvent event) {
    40. if ((event.getItemDrop().getItemStack().hasItemMeta()) &&
    41. (event.getItemDrop().getItemStack().getItemMeta().hasLore()) &&
    42. (event.getItemDrop().getItemStack().getItemMeta().getLore().contains(ChatColor.GRAY + "Untradeable"))) {
    43. event.getItemDrop().remove();
    44. event.getPlayer().sendMessage(ChatColor.GRAY + "The item was untradeable so it has vanished.");
    45. event.getPlayer().getWorld().playSound(event.getPlayer().getLocation(), Sound.FIZZ, 0.0F, 0.0F);
    46. }
    47. }
     
  2. Offline

    Oo-Zakk-oO

    Register them... onEnable do this code:
    Code:
            PluginManager pm = Bukkit.getServer().getPluginManager();
            pm.registerEvents(this, this);
     
  3. Offline

    beastman3226

    Also, you have too many brackets.
     
  4. Offline

    CreepahMC

    Not sure if I did this right...

    Code:java
    1. public class LegionsCore extends JavaPlugin
    2. {
    3. public void onEnable()
    4. {
    5. PluginManager pm = Bukkit.getServer().getPluginManager();
    6. pm.registerEvents((Listener) this, this);
    7. }
    8.  
    9. public void onDisable()
    10. {
    11. }
     
  5. Offline

    Oo-Zakk-oO

    CreepahMC dont do the (Listener) thing just add "implements Listener" next to extends JavaPlugin
     
  6. Offline

    Desle

    Oo-Zakk-oO You forgot to tell CreepahMC that he needs to implement listener..
    Code:java
    1. public class LegionsCore extends JavaPlugin implements listener {
     
  7. Offline

    Oo-Zakk-oO

    thats what i did? but same thing anyways!
     
  8. Offline

    WhatAaCow

    Th3Br1x likes this.
  9. Offline

    CreepahMC

    So I have an event to repair your armor and weapon to full dura when you take damage and attack mobs/players.
    When attacking, it repairs the armor but taking damage doesn't. Any idea? What's really weird is that my helmet takes 0 dura on damage so everything else but my helmet is taking durability when hit by mob/player.

    Dura Code:
    Code:java
    1. @EventHandler
    2. public void HitRepair(PlayerInteractEvent event) { event.getPlayer().setFoodLevel(20);
    3. if (event.getPlayer().getInventory().getBoots() != null) {
    4. event.getPlayer().getInventory().getBoots().setDurability((short) 0);
    5. }
    6. if (event.getPlayer().getInventory().getLeggings() != null) {
    7. event.getPlayer().getInventory().getLeggings().setDurability((short) 0);
    8. }
    9. if ((event.getPlayer().getInventory().getItemInHand().getTypeId() == 283) || (event.getPlayer().getInventory().getItemInHand().getTypeId() == 286) || (event.getPlayer().getInventory().getItemInHand().getTypeId() == 267)) {
    10. event.getPlayer().getItemInHand().setDurability((short) 0);
    11. event.getPlayer().updateInventory();
    12. }
    13. if (event.getPlayer().getInventory().getChestplate() != null) {
    14. event.getPlayer().getInventory().getChestplate().setDurability((short) 0);
    15. }
    16. if (event.getPlayer().getInventory().getHelmet() != null)
    17. event.getPlayer().getInventory().getHelmet().setDurability((short) 0); }
    18.  
    19. @EventHandler
    20. public void DamageRepair(EntityDamageEvent event) {
    21. if ((event.getEntity() instanceof Player)) {
    22. Player s = (Player)event.getEntity();
    23. if (s.getInventory().getBoots() != null) {
    24. s.getInventory().getBoots().setDurability((short) 0);
    25. }
    26. if (s.getInventory().getLeggings() != null) {
    27. s.getInventory().getLeggings().setDurability((short) 0);
    28. }
    29. if ((s.getInventory().getItemInHand().getTypeId() == 283) || (s.getInventory().getItemInHand().getTypeId() == 286) || (s.getInventory().getItemInHand().getTypeId() == 267)) {
    30. s.getItemInHand().setDurability((short) 0);
    31. s.updateInventory();
    32. }
    33. if (s.getInventory().getChestplate() != null) {
    34. s.getInventory().getChestplate().setDurability((short) 0);
    35. }
    36. if (s.getInventory().getHelmet() != null) {
    37. s.getInventory().getHelmet().setDurability((short) 0);
     
  10. Offline

    Maximvdw

    Few tips:
    1) Save yourself some typing:
    Code:java
    1.  
    2. ItemStack boots = event.getPlayer().getInventory().getBoots();
    3. if (boots != null) {
    4. boots.setDurability((short) 0);
    5. }

    2) Try using the "EntityDamageByEntity" event

    Best Regards,
    Maximvdw
     
  11. Offline

    CreepahMC

    Thanks! That fixed my dura! Any idea on why this won't give me a sword on login?

    Code:java
    1. @EventHandler(priority=EventPriority.MONITOR)
    2. public void BasicJoin(PlayerJoinEvent event) { if (!event.getPlayer().hasPlayedBefore()) {
    3. ItemStack Sword = new ItemStack(Material.WOOD_SWORD);
    4. ItemMeta SwordMeta = Sword.getItemMeta();
    5. ArrayList SwordLore = new ArrayList();
    6. SwordMeta.setDisplayName(ChatColor.WHITE + "Training Sword");
    7. SwordLore.add(ChatColor.RED + "DMG: +" + plugin.rand(5, 7) + " - " + plugin.rand(10, 12));
    8. SwordLore.add(ChatColor.GRAY + "Untradeable");
    9. SwordMeta.setLore(SwordLore);
    10. Sword.setItemMeta(SwordMeta);
    11. event.getPlayer().getInventory().addItem(new ItemStack[] { Sword });
    12. }
    13. }
    14. }


    bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  12. Offline

    beastman3226

    I would set the item at slot (get some blank slot) instead of adding items.
     
  13. Offline

    Havime

    Try this :D

    Code:
    event.getPlayer().getInventory().addItem(Sword);
     
    HeavyMine13 likes this.
  14. Offline

    CreepahMC

    Still not working. I think it's the event since I get an error stating it can't understand the PlayerJoinEvent.

    Error:
    Code:
     [SEVERE] Could not pass event PlayerJoinEvent to LegionsCore v1.0
    01:21:55 org.bukkit.event.EventException
    01:21:55 at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
    01:21:55 at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    01:21:55 at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
    01:21:55 at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
    01:21:55 at net.minecraft.server.v1_6_R3.PlayerList.c(PlayerList.java:207)
    01:21:55 at net.minecraft.server.v1_6_R3.PlayerList.a(PlayerList.java:103)
    01:21:55 at net.minecraft.server.v1_6_R3.PendingConnection.e(PendingConnection.java:132)
    01:21:55 at net.minecraft.server.v1_6_R3.PendingConnection.d(PendingConnection.java:43)
    01:21:55 at net.minecraft.server.v1_6_R3.DedicatedServerConnectionThread.a(DedicatedServerConnectionThread.java:41)
    01:21:55 at net.minecraft.server.v1_6_R3.DedicatedServerConnection.b(SourceFile:29)
    01:21:55 at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:592)
    01:21:55 at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:227)
    01:21:55 at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:488)
    01:21:55 at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:421)
    01:21:55 at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    01:21:55 Caused by: java.lang.Error: Unresolved compilation problems:
    01:21:55 plugin cannot be resolved
    01:21:55 plugin cannot be resolved
    01:21:55 at me.creepah.LegionsCore.LegionsCore.BasicJoin(LegionsCore.java:113)
    01:21:55 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    01:21:55 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    01:21:55 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    01:21:55 at java.lang.reflect.Method.invoke(Method.java:606)
    01:21:55 at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
    01:21:55 ... 14 more
    01:21:59  [INFO] CreepahPlaysMC issued server command: /clear
    01:22:31  [INFO] Connection reset
    01:23:23  [FINEST] [WGCustomFlags] Saving flags for world InfernalRealms
    01:23:23  [FINEST] [WGCustomFlags] Saving flags for world InfernalRealms_the_end
    01:23:23  [FINEST] [WGCustomFlags] Saving flags for world Nordic
    01:23:23  [FINEST] [WGCustomFlags] Saving flags for world rpg
    
    Java Code:
    Code:java
    1. @EventHandler(priority=EventPriority.MONITOR)
    2. public void BasicJoin(PlayerJoinEvent event) { if (!event.getPlayer().hasPlayedBefore()) {
    3. ItemStack Sword = new ItemStack(Material.WOOD_SWORD);
    4. ItemMeta SwordMeta = Sword.getItemMeta();
    5. ArrayList SwordLore = new ArrayList();
    6. SwordMeta.setDisplayName(ChatColor.WHITE + "Shortsword");
    7. SwordLore.add(ChatColor.RED + "DMG+" + plugin.rand(5, 7) + " - " + plugin.rand(10, 12));
    8. SwordLore.add(ChatColor.GRAY + "Untradeable");
    9. SwordMeta.setLore(SwordLore);
    10. Sword.setItemMeta(SwordMeta);
    11. event.getPlayer().getInventory().addItem(Sword);
    12.  
    13. }
    14. }
    15.  
    16. }

     
  15. Offline

    L33m4n123

    You have a public variable called plugin? if so where is it?
     
  16. Offline

    CreepahMC

    Not sure why I had it. I removed it and set a method for rand to be and integer and it seemed to not cause errors on save and on reload/login. Still no sign of a wooden sword.

    Also

    How could I add it to where the itemLore can have to types of chat color meaning a word can be Bold and a color.
     
  17. Offline

    L33m4n123

    Try delaying the sword giving for like 10 ticks. And make sure to delete your player.dat since you check if he is new

    About Bold and color. The way you do it already.

    ChatColor.RED + ChatColor.BOLD + "Bolden red text"
     
  18. Offline

    CreepahMC

    Ok thanks! I'll try it out!

    Well the chat color red/bold didn't work; it gave off an error but not a big deal. The sword worked but when it gave it to me it said DMG: null-null. I meant it to say a random integer between the 2 (5-7 for the first). Am I doing this wrong?

    Code:java
    1. SwordMeta.setDisplayName(ChatColor.WHITE + "Shortsword");
    2. SwordLore.add(ChatColor.RED + "DMG+" + rand(5, 7) + "-" + rand(10, 12));
    3. SwordLore.add(ChatColor.GRAY + "Untradeable");


    bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  19. Offline

    xTrollxDudex

    CreepahMC
    Stop bumping your thread every 2 hours.

    Show your whole code. Also, please learn java properly, as well as the Bukkit API and get familiar with basic debugging, your code has horrible readability.
     
Thread Status:
Not open for further replies.

Share This Page