Solved How to get a Material type from a .yml config file?

Discussion in 'Plugin Development' started by wadu436, Jul 30, 2013.

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

    wadu436

    Hello people.
    I am coding a plugin that will react if the player right clicks with an item.
    I want that item to be configurable using the config file.
    But i dont know how to get a Material from the config so i can filter out the wrong click events.

    Can someone help me?
     
  2. Offline

    Assist

    wadu436
    Code:
    Material material = Material.getMaterial(getConfig().getString("path.to.material"));
     
  3. Offline

    wadu436

    Assist
    How can i compare it to the material in the player hand?
     
  4. Offline

    cummo15

    This should work just fine. (On my phone so its untested btw)
    Code:java
    1. if (player.getItemInHand().equals(material)) {
    2. //Do stuff.
    3. }
     
  5. Offline

    Assist

    You must be my non-existing long-lost brother!
     
  6. Offline

    cummo15

  7. Offline

    Garris0n


    Code:java
    1. if (player.getItemInHand().getType() == material) {
    2. //Do stuff.
    3. }
     
    wadu436 likes this.
  8. Offline

    wadu436

    Okay, the mob spawns and it gets everything from the yml file but i get this weird bug or something with the velocity i well create a video what it does and upload it

    Heres the video with the weird thing:

    Assist
    xTrollxDudex
    Cirno
    Garris0n
    cummo15
    EDIT: i want it i right click with the item in hand and than there shoots a mob wit ha set power, but its overpowered (something like the arrow that bends downward)
    I also have the problem that it reacts on every playerinteract but i only want it todo so when i right click
    EDIT2: Forgot code sorry :/
    heres the code:
    Code:java
    1. package be.wadu.mobcannon;
    2.  
    3. import org.bukkit.Material;
    4. import org.bukkit.entity.Entity;
    5. import org.bukkit.entity.EntityType;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.player.PlayerInteractEvent;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11. import org.bukkit.util.Vector;
    12.  
    13. public class MobCannon extends JavaPlugin implements Listener{
    14. JavaPlugin plugin = this;
    15. @Override
    16. public void onEnable() {
    17. saveDefaultConfig();
    18. getCommand("mobcannon").setExecutor(new MobCannonExecutor(plugin));
    19. getConfig().options().copyDefaults(true);
    20. getConfig().addDefault("test", "test");
    21. saveConfig();
    22. getServer().getPluginManager().registerEvents(this, plugin);
    23. getServer().broadcastMessage("This is the test message: " + this.getConfig().getString("test"));
    24. Player[] player = getServer().getOnlinePlayers();
    25. for(int i=1; i<=player.length; i++){
    26. getConfig().addDefault(player[i-1].getName(), "pig");
    27. saveConfig();
    28. }
    29. }
    30. @EventHandler
    31. public void StickClick(PlayerInteractEvent event){
    32. Player p = event.getPlayer();
    33. Material m = Material.getMaterial(this.getConfig().getString("item").toUpperCase());
    34. EntityType entity = EntityType.fromName(getConfig().getString(p.getName()).toUpperCase());
    35. if (p.getItemInHand().getType().equals(m)){
    36. Entity mob = p.getWorld().spawnEntity(p.getTargetBlock(null, 2).getLocation(), entity);
    37. Vector v = p.getTargetBlock(null, getConfig().getInt("power")).getLocation().toVector();
    38. mob.setVelocity(v);
    39. }
    40. }
    41. }

    the command /mobcannon is used for listing and setting the mob.

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

    Garris0n


    Change
    Code:java
    1. Vector v = p.getTargetBlock(null, getConfig().getInt("power")).getLocation().toVector();

    to
    Code:java
    1. Vector v = p.getLocation().getDirection().multiply(getConfig().getInt("power"));
     
  10. Offline

    wadu436

    Okay i will try it when i have time.

    It works :D
    But one extra question:
    How can i detect if the mob i spawned touches the ground and let it then disapear and do a tnt explosion effect (but without blocks being removed)
    Assist
    xTrollxDudex
    Cirno
    Garris0n
    cummo15

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

    xTrollxDudex

    wadu436
    I think you need a timer for that beacuse the isOnGround is deprecated/doesn't even work. Then do world.createExplosion(...)
     
  12. Offline

    wadu436

  13. Offline

    xTrollxDudex

    wadu436
    Whoops, a scheduler or runnable soz.
     
  14. Offline

    wadu436

    would a for loop work (because i am really really really bad at making schedulers and i dont understand anything about the info page on how to make them and i allways get errors with them. xTrollxDudex
     
  15. Offline

    Cirno

    Loops would be extremely inefficient; if not done right, you'll freeze the main thread and ultimately freeze the entire server.
     
  16. Offline

    wadu436

    Cirno
    Can you please teach me how to make a loop with the scheduler or make the mob explode after x amount of ticks.
     
  17. Offline

    Cirno

    Code:java
    1. Bukkit.getSchedular().scheduleSyncDelayedTask(yourPluginInstance, new Runnable(){
    2. public void run(){
    3. //do explosions
    4. }
    5. }, timeInMilliseconds);
     
  18. Offline

    xTrollxDudex

    Cirno
    I thought the delay was in server ticks?
     
  19. Offline

    wadu436

Thread Status:
Not open for further replies.

Share This Page