Solved [New solution] Custom mob

Discussion in 'Plugin Development' started by CraftBang, Nov 5, 2013.

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

    CraftBang

    Hey, I've got problems with my custom mob :

    My code onEnable
    Code:
    try{
    @SuppressWarnings("rawtypes")
    Class[] args = new Class[3];
    args[0] = Class.class;
    args[1] = String.class;
    args[2] = int.class;
     
    Method a = net.minecraft.server.v1_6_R3.EntityTypes.class.getDeclaredMethod("a", args);
    a.setAccessible(true);
     
    a.invoke(a, EntityIronGolem.class, "EntityGolem", 99);
    }catch (Exception e){
    e.printStackTrace();
    this.setEnabled(false);
    }
    
    My mob class
    Code:
    package me.CraftBang;
     
    import java.lang.reflect.Field;
    import java.util.ArrayList;
     
    import net.minecraft.server.v1_6_R3.EntityHuman;
    import net.minecraft.server.v1_6_R3.EntityVillager;
    import net.minecraft.server.v1_6_R3.PathfinderGoalBreakDoor;
    import net.minecraft.server.v1_6_R3.PathfinderGoalFloat;
    import net.minecraft.server.v1_6_R3.PathfinderGoalHurtByTarget;
    import net.minecraft.server.v1_6_R3.PathfinderGoalLookAtPlayer;
    import net.minecraft.server.v1_6_R3.PathfinderGoalMeleeAttack;
    import net.minecraft.server.v1_6_R3.PathfinderGoalMoveThroughVillage;
    import net.minecraft.server.v1_6_R3.PathfinderGoalMoveTowardsRestriction;
    import net.minecraft.server.v1_6_R3.PathfinderGoalRandomLookaround;
    import net.minecraft.server.v1_6_R3.PathfinderGoalRandomStroll;
    import net.minecraft.server.v1_6_R3.World;
     
    public class EntityIronGolem extends net.minecraft.server.v1_6_R3.EntityGolem {
        private float bw; // I added this
     
    @SuppressWarnings("rawtypes") // I added this
    public EntityIronGolem(World world) {
            super(world);
            this.bw = 0.46F; // double speed
     
            // I found you need to add this to get it to work properly
                    try {
                //enable PathfinderGoalSelector's "a" field to be editable
                Field gsa = net.minecraft.server.v1_6_R3.PathfinderGoalSelector.class.getDeclaredField("a");
                gsa.setAccessible(true);
     
                // ok now take this instances goals and targets and blank them
                gsa.set(this.goalSelector, new ArrayList());
                gsa.set(this.targetSelector, new ArrayList());
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
     
            // and then reinitialize all AI with the new speed
            this.goalSelector.a(0, new PathfinderGoalFloat(this));
            this.goalSelector.a(1, new PathfinderGoalBreakDoor(this));
            this.goalSelector.a(2, new PathfinderGoalMeleeAttack(this, EntityHuman.class, this.bw, false));
            this.goalSelector.a(3, new PathfinderGoalMeleeAttack(this, EntityVillager.class, this.bw, true));
            this.goalSelector.a(4, new PathfinderGoalMoveTowardsRestriction(this, this.bw));
            this.goalSelector.a(5, new PathfinderGoalMoveThroughVillage(this, this.bw, false));
            this.goalSelector.a(6, new PathfinderGoalRandomStroll(this, this.bw));
            this.goalSelector.a(7, new PathfinderGoalLookAtPlayer(this, EntityHuman.class, 8.0F));
            this.goalSelector.a(7, new PathfinderGoalRandomLookaround(this));
            this.targetSelector.a(1, new PathfinderGoalHurtByTarget(this, false));
            //this.targetSelector.a(2, new PathfinderGoalNearestAttackableTarget(this, EntityHuman.class, 16.0F, 0, true)); < gives error (in eclipse) so removed, it wants me to remove 16.0F
            //this.targetSelector.a(2, new PathfinderGoalNearestAttackableTarget(this, EntityVillager.class, 16.0F, 0, false)); < gives error (in eclipse) so removed, it wants me to remove 16.0F
        }
     
    }
    
    Trying to spawn it on a command :
    Code:
    Player player = (Player) sender;
    World world = player.getWorld();
    net.minecraft.server.v1_6_R3.World mcWorld = ((CraftWorld) world).getHandle();
    EntityIronGolem v = new EntityIronGolem(mcWorld);
    v.spawnIn(mcWorld);
    v.setPosition(player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ());
    mcWorld.addEntity(v);
    
    Gives me this error :
    Code:
    [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'spawncustommob' in plugin CraftBang v1.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:192)
    at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(CraftServer.java:523)
    at net.minecraft.server.v1_6_R3.PlayerConnection.handleCommand(PlayerConnection.java:959)
    at net.minecraft.server.v1_6_R3.PlayerConnection.chat(PlayerConnection.java:877)
    at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java:834)
    at net.minecraft.server.v1_6_R3.Packet3Chat.handle(SourceFile:49)
    at net.minecraft.server.v1_6_R3.NetworkManager.b(NetworkManager.java:296)
    at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java:116)
    at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
    at net.minecraft.server.v1_6_R3.DedicatedServerConnection.b(SourceFile:30)
    at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:592)
    at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:227)
    at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:488)
    at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:421)
    at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    Caused by: java.lang.AssertionError: class me.CraftBang.EntityIronGolem
    at org.bukkit.craftbukkit.v1_6_R3.entity.CraftEntity.getEntity(CraftEntity.java:156)
    at net.minecraft.server.v1_6_R3.Entity.getBukkitEntity(Entity.java:1385)
    at org.bukkit.craftbukkit.v1_6_R3.event.CraftEventFactory.callCreatureSpawnEvent(CraftEventFactory.java:235)
    at net.minecraft.server.v1_6_R3.World.addEntity(World.java:923)
    at net.minecraft.server.v1_6_R3.World.addEntity(World.java:895)
    at me.CraftBang.StartingGame.onCommand(StartingGame.java:185)
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    ... 15 more
    
    Well sorry to say but I don't really understand my code, I tried to look up the tutorial, and copied this code, tried to modify it and learn stuff from it, but I guess I made some mistakes, because I needed to make some fixes.

    If someone can help me I would be very happy.

    Line on StartingGame.java:185 =
    mcWorld.addEntity(v);

    BTW I don't even want this speed multiplier , I just want it to give custom drop, and a message if he got killed and custom hp :p
     
  2. Offline

    Deleted user

    CraftBang
    What do you mean by custom mob?
     
  3. Offline

    CraftBang

    JHG0 (or @JHGO ) sorry, I mean that I want to spawn an iron golem , with some special things.
    I want to have like 4 iron golems ( so I need to make 4 classes I think) and I want them to have a label like "Master Golem" "Pro Golem"
    Something like that, I want them to have a custom ondeathdrop items, and custom hp.

    But isn't there an easier way? couldn't I just set a label(tag) on the golem, and than on events check if the tag from the iron golem is equal to like "Pro Golem" ?
     
  4. Offline

    TheTinySpider

    You can just set the display name if you want, to make sure no-one can re-create your golems with nametags add "§r" infront of it. About the HP, you can use setMaxHealt(...), and finaly custom drops, onDeath check if golem and then use getCustomName() and compare that.
     
  5. Offline

    CraftBang

    JHG0 and TheTinySpider thanks for letting me use my brain, and thanks for your comments :D.
    I'll mark this thread solved now because this is probably going to work.
    Thanks, no need for this custom hardcoded stuff xD!
     
  6. Offline

    Deleted user

    CraftBang
    Okay VERY simple
    Spawn a golem...
    golem.setDispName
    then golem.setDipNameVisible(true);

    Next
    Use an entity death event
    If instanceof Golem
    e.getDrops().clear ();
    e.getDrops().add(BLARG)
     
    CraftBang likes this.
  7. Offline

    CraftBang

    JHG0 works fine, for spede I added a potion effect, and for the range? it's way to far hes range
     
  8. Offline

    Janmm14

    CraftBang
    I think the damage range is not changeable, but you can use potion effects like slow / speed / strength / weakness
     
  9. Offline

    Deleted user

    CraftBang
    Maybe speed potions for speed. The only way to shorten the range is like this:
    - Check if entity damages player
    - Measure distance
    - If too far, cancel the event
     
    CraftBang likes this.
  10. Offline

    Janmm14

    JHG0
    use distancesquared and check against the squared distance for much performance optimization
     
    CraftBang likes this.
  11. Offline

    CraftBang

    Janmm14 and JHG0 thanks a lot, I figured all out,
    I did this
    Code:
    if(e.getDamager().getLocation().distanceSquared(e.getEntity().getLocation()) >= 5){
    
    On an event with some more if statements if it's the right golem blablabla xD.
    Just one question. why squared ?It will check in a square radius of 5 than ? and if it's bigger ( or equal it will block)
     
  12. Offline

    Janmm14

    CraftBang
    If you don't need to get the distance and only have to compare against, you can do the following to check if the distance is lower, equal or higher 5:
    Code:
    if(e.getDamager().getLocation().distanceSquared(e.getEntity().getLocation()) >= 5 * 5){
     
Thread Status:
Not open for further replies.

Share This Page