Spawn Silverfish on BlockBreak (Small project)

Discussion in 'Archived: Plugin Requests' started by Natesky9, Apr 11, 2013.

  1. Offline

    Natesky9

    I've a request for an aspiring plugin developer that shouldn't take more than 30 minutes to complete.

    Simply put, it's a plugin that will spawn a silverfish when a certain block is broken. Simple enough, but there are a few other things that would be great to implement into it.

    For my purposes, I only require it to work on block 140:3. That is the tekkit Silver ore block. But, if you want your plugin to be usable on Vanilla servers, you may have to make a separate version, since Tekkit is currently in version 1.2.5 R5

    Config file would also do well with a percentage chance added in. Default ~75% and also the option whether or not to destroy the block without dropping anything.

    All Credit goes to developer, and you may distribute it as your own. Thanks for reading, and I hope you consider this project
     
  2. Offline

    TGF

    It's shouldn't take more then 5 minutes ^^
    Isn't it better to just make a plugin and config like this?

    block:
    '140:3': silverfish

    And plugin take the id and entity to spawn?

    And, is it any sense to do plugin like this one? :D
     
  3. Offline

    Natesky9

    It's a bit more than that. You have to create a listener for the blockBreak event, hook that into a chance test, and if you're making a config for alterations, tie that into it. It's simple, but it's still about 20 lines. More like 10, if you can make it compact and neat.
     
  4. Offline

    YOMAMMASBROTHA

    Well, doesn't look like you need much help at all ;)

    Just kidding, but anyway, I'll make this plugin for you, and even allow you to use it with tekkit!

    And, TGF had it right, but he wasn't going to code the whole plugin right onto the forum :p
     
    TGF likes this.
  5. Offline

    noraver

  6. Offline

    TGF

    noraver Plugin with only this what Natesky9 wants gonna be MUCH lighter then OtherDrops ;)
     
  7. Offline

    Score_Under

    >lighter
    >java
    top lel

    Anyway, whipped this up:
    Plugin: https://dl.dropboxusercontent.com/u/518733/req/ILoveSilverfish-1.0-SNAPSHOT.jar
    Source code: https://dl.dropboxusercontent.com/u/518733/req/ILoveSilverfish.zip
    The default config includes planks for easier testing, you probably want to remove that.
    (It also demonstrates an intentional config quirk: setting '5' as the block allows all flavours of plank to trigger it, while setting '5:0' as the block would allow only oak planks)

    I count about 180 :p
    In scala that goes down to about 100, which just shows how much of it is boilerplate, and the brunt of the code is shared between parsing the config and deleting the item spawn (which isn't supposed to be possible)
     
    Natesky9 likes this.
  8. Offline

    Zarius

    TGF

    Whilst a custom plugin is smaller (and I appreciate that some people need and/or prefer that) OtherDrops isn't as heavy as you think. Any custom drop plugin needs to listen to the blockbreak event, generally read config at startup (unless hardcoded), spawn the custom item/mob and - if you don't want the default drop - cancel the event & turn the block to air. I've done some profiling and out of all this it's the replacing the block that takes the longest by far.

    Unless you configure a drop for a particular trigger OtherDrops doesn't even listen to the event. In addition, whilst there are many different conditions/actions to check this too minimised by (in the majority of cases) scanning the details at config reading time and only checking conditions that have been actually configured - eg. if you don't use the "sound" action or "lorename" condition then no actual code for that is run (except at startup when the config file is read).
     
  9. Offline

    Score_Under

    Interesting, but doesn't that mess with block loggers?
     
  10. Offline

    Natesky9

    Looked it over, and you did a good job on it. I like how you added the gamemode check on it also. I'll test it out, and get back to you on how it works.

    And also, I don't know if you can do this with the 1.2.5 version, but the code looked a bit redundant, and I was wondering how much was able to be trimmed down. I know 1.5 is more "tuned" for plugin hooks and all that, which is what I'm used to.

    But thanks for your help, and if you're available sometime soon, I'd love to chat with you
     
  11. Offline

    Zarius

    Score_Under

    Yes, it definitely does :( Therefore I've needed to do some extra work and tap into the API of some of the more popular block loggers (coreprotect, prism, logblock) and manually log the block break.

    Would be great to get fixed (as this issue applies to any block drop replacement plugin) and there's an issue on the Bukkit issue tracker here: https://bukkit.atlassian.net/browse/BUKKIT-1217
     
  12. Offline

    Score_Under

    Looking back I think I should have used Lombok, it would turn this:
    Code:
        private static class BlockLocation {
            private final UUID uuid;
            private int x, y, z;
    
            BlockLocation(Block block) {
                this.uuid = block.getWorld().getUID();
                this.x = block.getX();
                this.y = block.getY();
                this.z = block.getZ();
            }
    
            @Override
            public boolean equals(Object o) {
                if (this == o) return true;
                if (o == null || getClass() != o.getClass()) return false;
    
                BlockLocation that = (BlockLocation) o;
    
                if (x != that.x) return false;
                if (y != that.y) return false;
                if (z != that.z) return false;
                if (!uuid.equals(that.uuid)) return false;
    
                return true;
            }
    
            @Override
            public int hashCode() {
                int result = uuid.hashCode();
                result = 31 * result + x;
                result = 31 * result + y;
                result = 31 * result + z;
                return result;
            }
        }
    
        private static class BlockCriteria {
            private final int id, data;
    
            BlockCriteria(int id, int data) {
                this.id = id;
                this.data = data;
            }
    
            @Override
            public boolean equals(Object o) {
                if (this == o) return true;
                if (o == null || getClass() != o.getClass()) return false;
    
                BlockCriteria that = (BlockCriteria) o;
    
                if (data != that.data) return false;
                if (id != that.id) return false;
    
                return true;
            }
    
            @Override
            public int hashCode() {
                int result = id;
                result = 31 * result + data;
                return result;
            }
        }
    Into this:
    Code:
        @EqualsAndHashCode private static class BlockLocation {
            @NonNull private final UUID uuid;
            private int x, y, z;
    
            BlockLocation(Block block) {
                this.uuid = block.getWorld().getUID();
                this.x = block.getX();
                this.y = block.getY();
                this.z = block.getZ();
            }
        }
    
        @EqualsAndHashCode @RequiredArgsConstructor private static class BlockCriteria {
            private final int id, data;
        }
    If I was writing a larger plugin I'd split the classes out into their own files too - there's 5 classes in there which could be separated out.
     
    Natesky9 likes this.
  13. Offline

    Natesky9

    Now that's what I'm talking about! It looks so nice, and Shiny!
    Thanks, I'll see about editing that part in with Eclipse. I don't see why it shouldn't work.
    Also, I've a question about another plugin that I'm having the hardest time with. It's the stupid config file that's giving me the hardest time. If you want to help me out with that, PM me, if you have the time.
    I 'Preciate it
     
  14. Offline

    Score_Under

    Alright. If you're compiling via the maven pom you should add lombok through there: http://projectlombok.org/mavenrepo/

    xenforo y u no have sage
     
  15. Offline

    TGF

    Zarius I don't know OtherDrops too well, but if i know good, server load plugins to RAM or sth.
    The second think is that if plugin have a handle and configuration part like parkour or sth, plugin still listens for configuration classes :)
     
  16. How do you not know about the API and you don't know java? You pretty much described it perfect.
     
  17. Offline

    Natesky9

    The thing is, I know java, and I even have a general idea of what it looks like.
    However, I am both rusty at java, and I have no idea what has changed before 1.3, so I'm clueless as to how it needs to be done.

    Hopefully someone more experienced, like Score_Under can educate me on the proper way to do it
     

Share This Page