Solved Trying to put a BlockBreakEvent method in EntityDamageByEntityEvent method; not working.

Discussion in 'Plugin Development' started by Bamco6657, Oct 3, 2016.

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

    mehboss

    @Bamco6657
    Try
    Code:
            Bukkit.getPluginManager().registerEvents(this, this);
    or you can try this
    Code:
            Bukkit.getPluginManager().registerEvents(new onBreak(), this);
     
  2. Offline

    Zombie_Striker

    This is quite a long read. Took me a while to figure out what your problem was.

    The easiest way to solve your problem would be to do the following:
    1. For the enchantment's constructor, add the main class as a requirement for the enchantment's parameter. What this means is that you will now have the instance of the main class inside the enchantment event.
    2. Inside the enchantment's constructor, register the class as a listener. (this is why you need the main class's instance)
    3. Now, at the top of the class, create an ArrayList of Locations. These will be the locations of the ice blocks.
    4. Create an event for BlockBreakEvent inside that class.
    5. Inside the event, check if the block that was broken is inside the Arraylist. If so, cancel the event.
    6. Now, whenever you place down an ice block, add the location to the Arraylist.
    7. Whenever you set ice blocks to air, remove the locations from the list.
    This will be an easy and efficient way to achieve what you want.
     
  3. Offline

    mehboss

    @Zombie_Striker
    The coding master speaks..

    ~ also you may need to provide some examples (not spoon feeding), he might not understand those fancy words. :)
     
  4. Offline

    Bamco6657

    @Zombie_Striker
    Agreed with @mehboss. I understand how to do some of the following, but I'm still a little unclear on what to do. :(

    EDIT: Perhaps you could direct me to some tutorials?
     
  5. Offline

    Zombie_Striker

    @Bamco6657
    Here is an example of the first 3 steps.
    Code:
    public class IceAspect extends.... implements Listener{
    
    private List<Location> iceSpots = new ArrayList<Location>();
    
    public IceAspect(MainClass main){
     Bukkit.getPluginManager.registerEvents(this, main);
    }
    Now, whenever you set a block to ice, use this line
    Code:
    iceSpots.add(ICE BLOCK.getLocation());
    Whenever you set an ice block to something that is not ice (i.e removing the ice), use this line
    Code:
    iceSpots.remove(ICE BLOCK.getLocation());
    After that, create the BlockBreakEvent, check if the iceSpots List contains the block's location, and if so, cancel the event.
     
  6. Offline

    Bamco6657

    Ok, did that, now what do you mean by using the iceSpots.add and iceSpots.remove to set a block of ice? Do you mean that I do this in replacement of doing .setType()?

    The code I have now:

    Code:
    package me.commandcore.betosenchs.enchants;
    
    import com.rit.sucy.CustomEnchantment;
    import me.commandcore.betosenchs.BetosEnchs;
    import me.commandcore.betosenchs.particles.ParticleEffect;
    import org.bukkit.*;
    import org.bukkit.block.Block;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import org.bukkit.util.Vector;
    
    import java.util.*;
    
    /**
    * Created by Beto on 5/21/2016.
    */
    public class IceAspect extends CustomEnchantment  implements Listener{
    
        private List<Location> iceSpots = new ArrayList<Location>();
    
        // Items able to be enchanted with ice aspect
        static final Material[] ICEASPECT_ITEMS = new Material[]{
                Material.WOOD_SWORD, Material.STONE_SWORD, Material.IRON_SWORD, Material.GOLD_SWORD, Material.DIAMOND_SWORD};
    
        // Constructor
        public IceAspect(BetosEnchs main) {
            super("Ice Aspect", ICEASPECT_ITEMS, 2);
    
            Bukkit.getPluginManager().registerEvents(this, main);
    
            //Max level for enchantment.
            this.max = 2;
    
            //Don't touch these. :)
            this.base = 10;
            this.interval = 8;
        }
    
        @Override
        public void applyEffect(LivingEntity user, LivingEntity target, int enchantLevel, EntityDamageByEntityEvent event) {
    
            Random random = new Random();
            int Chance = random.nextInt(100);
    
            if (Chance <= (5 * enchantLevel) + 1) {
    
                final Location freezeloc = target.getLocation();
    
                final Block block1 = target.getLocation().add(0, 1, 0).getBlock();
                final Block block2 = target.getLocation().getBlock();
    
                final Material type1 = block1.getType();
                final Material type2 = block2.getType();
    
                block1.setType(Material.ICE);
                block2.setType(Material.ICE);
    
                    Vector vector = new Vector(0.0F, 0.0F, 0.0F);
    
                    World world = target.getWorld();
                    world.playEffect(target.getLocation(), Effect.POTION_BREAK, 0);
                    ParticleEffect.CLOUD.display(0.5F, 0.5F, 0.5F, 3.0F, 40, target.getLocation(), 50);
                    target.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 2 * 20, enchantLevel - 1, true));
                    target.addPotionEffect(new PotionEffect(PotionEffectType.WITHER, 2 * 20, enchantLevel - 1, true));
    
                    int task = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(Bukkit.getPluginManager().getPlugin("BetosEnchs"), new Runnable() {
                        @Override
                        public void run() {
                            event.isCancelled();
                            target.teleport(freezeloc);
                            target.setVelocity(vector);
                            ParticleEffect.CLOUD.display(0.5F, 1F, 0.5F, 0.05F, 7, target.getLocation(), 50);
    
                            block1.getWorld().dropItem(block1.getLocation(), new ItemStack(Material.GRASS));
                            block2.getWorld().dropItem(block2.getLocation(), new ItemStack(Material.GRASS));
    
                            block1.setType(Material.ICE);
                            block2.setType(Material.ICE);
                            }
                    }, 0L, 1L);
    
                    int task2 = Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Bukkit.getPluginManager().getPlugin("BetosEnchs"), new Runnable() {
                        @Override
                        public void run() {
    
                            Bukkit.getServer().getScheduler().cancelTask(task);
    
                            if (block1.getType() == Material.ICE) {
                                if (block2.getType() == Material.ICE) {
    
                                    block1.setType(Material.AIR);
                                    block2.setType(Material.AIR);
    
                                    world.playEffect(target.getLocation(), Effect.POTION_BREAK, 0);
                                    ParticleEffect.CLOUD.display(0.5F, 1F, 0.5F, 0.5F, 7, target.getLocation(), 50);
    
                                }
                            } else {
    
                                block1.setType(type1);
                                block2.setType(type2);
    
                                world.playEffect(target.getLocation(), Effect.POTION_BREAK, 0);
                                ParticleEffect.CLOUD.display(0.5F, 1F, 0.5F, 0.5F, 7, target.getLocation(), 50);
    
                            }
                        }
                    }, 2 * 20L);
    
                    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Bukkit.getPluginManager().getPlugin("BetosEnchs"), new Runnable() {
                        @Override
                        public void run() {
                            if (target.isDead()) {
    
                                Bukkit.getServer().getScheduler().cancelTask(task);
                                Bukkit.getServer().getScheduler().cancelTask(task2);
    
                                if (block1.getType() == Material.ICE) {
                                    if (block2.getType() == Material.ICE) {
    
                                        block1.setType(Material.AIR);
                                        block2.setType(Material.AIR);
    
                                        world.playEffect(target.getLocation(), Effect.POTION_BREAK, 0);
                                        ParticleEffect.CLOUD.display(0.5F, 1F, 0.5F, 0.5F, 7, target.getLocation(), 50);
                                    }
                                } else {
    
                                    block1.setType(type1);
                                    block2.setType(type2);
    
                                    world.playEffect(target.getLocation(), Effect.POTION_BREAK, 0);
                                    ParticleEffect.CLOUD.display(0.5F, 1F, 0.5F, 0.5F, 7, target.getLocation(), 50);
    
                                }
                            }
    
    
                        }
    
                    }, 1L);
            }
        }
    }
    
    
     
  7. Offline

    Zombie_Striker

    @Bamco6657
    No, do not replace those lines. Instead, add the "add" lines right under the lines where you set the type. Then inside the delayed task (the one where you set the type to air), add the "remove" lines under where you set those types.
     
  8. Offline

    Bamco6657

    Oh, ok, I'll try that.

    EDIT: Hey, so it seems that the code works, its not giving any errors, but I'm going to have to change some things because now the whole enchantment isn't working, but for now I have to go, we can discuss it more tommorow. Thanks for all of the help @Zombie_Striker and @mehboss! :D
     
    Last edited: Oct 3, 2016
  9. Offline

    Zombie_Striker

    @Bamco6657
    If your main problem has been solved, mark this thread as solved.
     
  10. Offline

    Bamco6657

    Yeah...that's the thing, the problem is not quite solved. :oops: So when I did all that you told me to do, I got an error here in my main class:

    upload_2016-10-4_21-32-17.png

    So then I did this and it removed the error:

    upload_2016-10-4_21-32-58.png

    But now the enchantment doesn't work at all! :( Like it doesn't exist at all. No effect, no console error, no anything.

    EDIT: Do you think we should just put this in a private chat and mark this as solved?
     
    Last edited: Oct 4, 2016
  11. Offline

    timtower Administrator Administrator Moderator

    That would defeat the purpose of this thread.
     
  12. Offline

    Bamco6657

    Can you change the problem of the thread?
     
  13. Offline

    mehboss

    @Bamco6657 The problem of the thread is fine, it doesn't matter as long as you had that problem in the first place. That problem is different, but now is a different problem.. but, that is what started this. :)

    EDIT: Let me see your updated custom enchant class, you messed something up.
     
    Last edited: Oct 5, 2016
  14. Offline

    Bamco6657

    Hey I kinda fixed up some things and started changing things so that errors would go away, this is my new code:

    Code:
    package me.commandcore.betosenchs.enchants;
    
    import com.rit.sucy.CustomEnchantment;
    import me.commandcore.betosenchs.particles.ParticleEffect;
    import org.bukkit.*;
    import org.bukkit.block.Block;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import org.bukkit.util.Vector;
    
    import java.util.*;
    
    /** * Created by Beto on 5/21/2016. */public class IceAspect extends CustomEnchantment implements Listener {
    
    private List<Location> iceSpots = new ArrayList<>();
    
    // Items able to be enchanted with ice aspectstatic final Material[] ICEASPECT_ITEMS = new Material[]{
    Material.WOOD_SWORD, Material.STONE_SWORD, Material.IRON_SWORD, Material.GOLD_SWORD, Material.DIAMOND_SWORD};
    
    // Constructorpublic IceAspect() {
    
    super("Ice Aspect", ICEASPECT_ITEMS, 2);
    
    //Max level for enchantment.this.max = 2;
    
    //Don't touch these. :)this.base = 10;
    this.interval = 8;
    }
    
    @EventHandlerpublic void onBlockBreak(BlockBreakEvent e){
    if(iceSpots.contains(e.getBlock().getLocation())){
    e.isCancelled();
    }
    }
    
    @Overridepublic void applyEffect(LivingEntity user, LivingEntity target, int enchantLevel, EntityDamageByEntityEvent event) {
    
    Random random = new Random();
    int Chance = random.nextInt(100);
    
    if (Chance <= (5 * enchantLevel) + 1) {
    
    final Location freezeloc = target.getLocation();
    
    final Block block1 = target.getLocation().add(0, 1, 0).getBlock();
    final Block block2 = target.getLocation().getBlock();
    
    final Material type1 = block1.getType();
    final Material type2 = block2.getType();
    
    block1.setType(Material.ICE);
    block2.setType(Material.ICE);
    
    Vector vector = new Vector(0.0F, 0.0F, 0.0F);
    
    World world = target.getWorld();
    world.playEffect(target.getLocation(), Effect.POTION_BREAK, 0);
    ParticleEffect.CLOUD.display(0.5F, 0.5F, 0.5F, 3.0F, 40, target.getLocation(), 50);
    target.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 2 * 20, enchantLevel - 1, true));
    target.addPotionEffect(new PotionEffect(PotionEffectType.WITHER, 2 * 20, enchantLevel - 1, true));
    
    int task = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(Bukkit.getPluginManager().getPlugin("BetosEnchs"), new Runnable() {
    @Overridepublic void run() {
    event.isCancelled();
    target.teleport(freezeloc);
    target.setVelocity(vector);
    ParticleEffect.CLOUD.display(0.5F, 1F, 0.5F, 0.05F, 7, target.getLocation(), 50);
    
    block1.setType(Material.ICE);
    block2.setType(Material.ICE);
    
    iceSpots.add(block1.getLocation());
    iceSpots.add(block2.getLocation());
    }
    }, 0L, 1L);
    
    int task2 = Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Bukkit.getPluginManager().getPlugin("BetosEnchs"), new Runnable() {
    @Overridepublic void run() {
    
    Bukkit.getServer().getScheduler().cancelTask(task);
    
    if (block1.getType() == Material.ICE) {
    if (block2.getType() == Material.ICE) {
    
    block1.setType(Material.AIR);
    block2.setType(Material.AIR);
    
    world.playEffect(target.getLocation(), Effect.POTION_BREAK, 0);
    ParticleEffect.CLOUD.display(0.5F, 1F, 0.5F, 0.5F, 7, target.getLocation(), 50);
    
    }
    } else {
    
    block1.setType(type1);
    block2.setType(type2);
    
    iceSpots.remove(block1.getLocation());
    iceSpots.remove(block2.getLocation());
    
    world.playEffect(target.getLocation(), Effect.POTION_BREAK, 0);
    ParticleEffect.CLOUD.display(0.5F, 1F, 0.5F, 0.5F, 7, target.getLocation(), 50);
    
    }
    }
    }, 2 * 20L);
    
    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Bukkit.getPluginManager().getPlugin("BetosEnchs"), new Runnable() {
    @Overridepublic void run() {
    if (target.isDead()) {
    
    Bukkit.getServer().getScheduler().cancelTask(task);
    Bukkit.getServer().getScheduler().cancelTask(task2);
    
    if (block1.getType() == Material.ICE) {
    if (block2.getType() == Material.ICE) {
    
    block1.setType(Material.AIR);
    block2.setType(Material.AIR);
    
    iceSpots.remove(block1.getLocation());
    iceSpots.remove(block2.getLocation());
    
    world.playEffect(target.getLocation(), Effect.POTION_BREAK, 0);
    ParticleEffect.CLOUD.display(0.5F, 1F, 0.5F, 0.5F, 7, target.getLocation(), 50);
    }
    } else {
    
    block1.setType(type1);
    block2.setType(type2);
    
    iceSpots.remove(block1.getLocation());
    iceSpots.remove(block2.getLocation());
    
    world.playEffect(target.getLocation(), Effect.POTION_BREAK, 0);
    ParticleEffect.CLOUD.display(0.5F, 1F, 0.5F, 0.5F, 7, target.getLocation(), 50);
    
    }
    }
    
    
    }
    
    }, 1L);
    }
    }
    }
    
    This got rid of my error here and I remove the "this" from it:
    Code:
    EnchantmentAPI.registerCustomEnchantment(new IceAspect());
    Now, my problem is this:

    upload_2016-10-5_19-9-59.png

    It says that the method is never used, so then I was looking it up, and I have to register the method, so I did this in my main class in the onEnable()

    Code:
    Bukkit.getPluginManager().registerEvents(new IceAspect(), this);
    When I did this, it still said that the method is never used. How do I fix this? :'(
     

    Attached Files:

  15. Offline

    mehboss

    @Bamco6657
    You said there were no errors?

    EDIT:
    What exactly are you trying to do here? You checked if it was in the array list and then you checked if it was cancelled? Where is that logging?
     
  16. Offline

    Bamco6657

    That's where I saw that I messed up. I apparently cant put "this" in the registry of the custom enchantment, even though there were no errors, it was obviously messing it up :p:

    It was this before:

    Code:
    EnchantmentAPI.registerCustomEnchantment(new IceAspect(this));
    Now it's this:

    Code:
    EnchantmentAPI.registerCustomEnchantment(new IceAspect());
    I also had to change some things in the IceAspect class to do this.

    I tried to make it where it's checking if the block broken is part of the List(iceSpots), and if it is, then cancel the BlockBreakEvent.
     
  17. Offline

    Lordloss

    e.isCancelled() is going to do nothing, this is returning a boolean which youre doing nothing with. You have to use setCancelled(true). And, which method is said to be never used?
     
  18. Offline

    mehboss

    @Zombie_Striker @Bamco6657 @Lordloss

    UPDATE:


    I may be incorrect but I am pretty sure,


    yeah so change the isCancelled to setCancel because isCancel checks if it cancelled, it doesn't set it to cancel the block broken.

    Code:
    @EventHandlerpublic void onBlockBreak(BlockBreakEvent e){
    if(iceSpots.contains(e.getBlock().getLocation())){
    e.isCancelled();
    }
    }
    
    needs to go after the block and location gets added..

    To fix it just simple move,
    to after this line of code:
    and once again, I am pretty sure you meant to cancel when an entity attacks another entity, so change isCancelled to setCancelled.


    Also

    What are you doing here?
    Code:
    if (block1.getType() == Material.ICE) {
    if (block2.getType() == Material.ICE) {
    
    block1.setType(Material.AIR);
    block2.setType(Material.AIR);
    
    iceSpots.remove(block1.getLocation());
    iceSpots.remove(block2.getLocation());
    
    world.playEffect(target.getLocation(), Effect.POTION_BREAK, 0);
    ParticleEffect.CLOUD.display(0.5F, 1F, 0.5F, 0.5F, 7, target.getLocation(), 50);
    }
    } else {
    
    block1.setType(type1);
    block2.setType(type2);
    
    iceSpots.remove(block1.getLocation());
    iceSpots.remove(block2.getLocation());
    
    world.playEffect(target.getLocation(), Effect.POTION_BREAK, 0);
    ParticleEffect.CLOUD.display(0.5F, 1F, 0.5F, 0.5F, 7, target.getLocation(), 50);
    
    }
    }
    
    
    }
    
    }, 1L);
    }
    }
    }
    
    First you checked to see if it is ice, then you set to air and remove it from the array, but then the second part you do the check to see if it isn't ice, and if it isn't, you remove it from the array list again,
    why?

    Also why did you put under the check to see if it wasn't ice to set to ice? if you set it back to ice at the end of that, it won't set back to air. Please correct me, once again, if I am incorrect. ;)

    Is your problem solved?
     
    Last edited: Oct 6, 2016
  19. Offline

    Bamco6657

    Hey! So there's a lot going on. :eek: My code is kinda wacky right now and there are some problems that I don't even know how to explain them. My plan was to rewrite my code to potentially fix some issues with it. Is there a way to put this thread on pause in some sort of way so that I can come back to it later? I heard something about not being allowed to bump old threads. :confused:
     
  20. Offline

    Zombie_Striker

    @Bamco6657
    Yes, you cannot necro old threads. What I would recommend is marking this thread as solved, and if/when you want more help, create a new thread.
     
  21. Offline

    Bamco6657

    Ok sounds good! Thanks for all of the help everyone, and I hope that rewriting the code fixes it. If not, then I'll make a new thread.
     
Thread Status:
Not open for further replies.

Share This Page