org.bukkit vs org.craftbukkit vs org.bukkit.craftbukkit

Discussion in 'Plugin Development' started by DeMaggo, Mar 8, 2013.

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

    DeMaggo

    Hello willing reader,

    I am currently trying to start some small interface for easily creating normal mobs with custom attributes, especially custom health and maxhealth. These should also wear potioneffects and items in case they can.

    I searched the APIs, Google, source codes and so on and ended up totally confused. There are multiple Classes for Skeletons, like EntitySkeleton and CraftSkeleton, along with the interface for Skeleton. Each use custom Constructors and possess different operations. I need operations from the classic bukkit one for setting Inventory and Potioneffects, but I cannot find a way to modify its hitpoints.
    Another problem is that those constructors and typecasts seem impossible to realize in a proper way, considering the different kinds of worlds, Entities, Craftentities and so on. I tried extending a class from both EntitySkeleton (which worked for hitpoint modification, but totally doesn't for EntityEquipment) and CraftSkeleton (which hardly worked for EntityEquipment, but not for setting custom hitpoints).
    Getting these Objects constructed still is a horror, as the inheritation trees also seem not to work the way they supposed to be, missing a lot of functionality from the superclasses.

    Thanks in advance

    My current state is this one:
    Skeleton Class
    Code:
    package MegaCreep;
     
    import net.minecraft.server.v1_4_R1.EntitySkeleton;
     
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.craftbukkit.v1_4_R1.CraftServer;
    import org.bukkit.craftbukkit.v1_4_R1.entity.CraftSkeleton;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;
    import org.bukkit.inventory.EntityEquipment;
     
    public class MegaSkeleton extends CraftSkeleton implements MegaCreepItems {
     
        private int maxhp=20;
     
     
        public MegaSkeleton(CraftServer server, EntitySkeleton e) {
     
            super(server, e);
        }
     
        public static MegaCreep create(CraftServer server, World world, Location loc) {
            //TODO Fitting?
            net.minecraft.server.v1_4_R1.World w=(net.minecraft.server.v1_4_R1.World) world;
            EntitySkeleton e=new EntitySkeleton(w);
            w.addEntity(e);
     
            MegaSkeleton m=new MegaSkeleton(server,e);
     
            m.setLocation(loc);
     
            return null;
        }
     
        @Override
        public Entity getEntity() {
            return this;
        }
     
        @Override
        public EntityType getType() {
            return EntityType.SKELETON;
        }
     
        @Override
        public void setLocation(Location l) {
     
            this.setLocation(l);
        }
     
        @Override
        public boolean setMaxHitpoints(int hp) {
     
            if (hp<=0) return false;
     
            this.maxhp=hp;
            return true;
        }
     
        @Override
        public void setHealth(int i) {
    
            if (i<=0) return;
    
            //TODO hitpoint attribute not visible
            getHandle().health= i;
            if (i > this.maxhp) {
                i = this.maxhp;
            }
        }
     
     
        @Override
        public boolean setHitpoints(int hp) {
            setHealth(hp);
     
            return true;
        }
     
        @Override
        public int getMaxHitpoints() {
            return 0;
        }
     
     
        @Override
        public int getHitpoints() {
            return this.getHealth();
        }
     
        //*******Item Operations*********
     
        @Override
        public EntityEquipment getItems() {
            // TODO Auto-generated method stub
            return this.getEquipment();
        }
     
     
    }
    
    The class to handle these MegaCreeps:

    Code:
    package MegaCreep;
     
    import java.util.Collection;
     
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.craftbukkit.v1_4_R1.CraftServer;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.EntityType;
    import org.bukkit.inventory.EntityEquipment;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
     
    public class MegaCreepInstance {
     
        private MegaCreep m=null;
     
        //TODO Bukkit world vs Craftbukkit world!
        MegaCreepInstance(CraftServer c,World w,Location l, EntityType e){
     
            switch (e){
     
            //*********Mobs mit Gegenständen***********\\
            case SKELETON:{
                m=MegaSkeleton.create(c, w, l);
                m.setLocation(l);
                break;
            }
     
            case ZOMBIE:{
                break;
            }
     
     
            case WITHER:{
                break;
            }
     
            case PIG_ZOMBIE:{
                break;
            }
     
            //*********Mobs ohne Gegenstände***********\\
            case BLAZE:
                break;
     
            case CAVE_SPIDER:
                break;
     
            case CREEPER:
                break;
     
            case ENDERMAN:
                break;
     
            case ENDER_DRAGON:
                break;
     
            case GHAST:
                break;
            case GIANT:
                break;
            case IRON_GOLEM:
                break;
     
            case MAGMA_CUBE:
                break;
     
            case OCELOT:
                break;
     
            case PLAYER:
                break;
     
            case SILVERFISH:
                break;
            case SLIME:
                break;
     
            case SNOWMAN:
                break;
            case SPIDER:
     
            case SQUID:
                break;
     
            case WITCH:
                break;
            case WOLF:
                break;
            default:
                break;
            }
     
     
            if (m!=null){
                //Entity was created!
     
            }
        }
     
        public EntityType getType() {
            return (m==null)?null:m.getType();
        }
     
        /*
        * Allowed types:
        *    **holding items**
        * PIG_ZOMBIE
        * SKELETON
        * ZOMBIE
        *
        *  **not holding items**
        * BLAZE
        * CAVE_SPIDER 
        * CREEPER 
        * ENDER_DRAGON 
        * ENDERMAN
        * GHAST
        * GIANT
        * IRON_GOLEM
        * MAGMA_CUBE
        * SILVERFISH
        * SLIME
        * SPIDER
        * WITCH
        * WITHER
        * WOLF
        * (wolf: evil kind)
        */
     
     
        //Infinite duration
        public boolean addEffect(PotionEffectType p, boolean show,int amplifier) {
            return false;
        }
        public boolean addEffects(PotionEffectType[] p, boolean show,int amplifier) {
            return false;
        }
     
        //Timed duration
        //amplifier=level?
        boolean addEffect(PotionEffectType p, int duration, int amplifier, boolean show) {
            return false;
        }
     
        /* Allowed potioneffects:
        *
        * BLINDNESS (player only)
        * CONFUSION (player only)
        * DAMAGE_RESISTANCE
        * FAST_DIGGING
        * FIRE_RESISTANCE
        * HARM
        * HEAL
        * HUNGER
        * INCREASE_DAMAGE
        * INVISIBILITY
        * JUMP
        * NIGHT_VISION
        * POISON
        * REGENERATION
        * SLOW
        * SLOW_DIGGING
        * SPEED
        * WATER_BREATHING
        * WEAKNESS
        * WITHER
        */
     
        public Collection<PotionEffect> getEffects(PotionEffectType p) {
            return null;
        }
     
     
        //**********************************************\\
        //***************Item operationen***************\\
        //**********************************************\\
     
        public boolean addItem(ItemStack itemstack, float dropchance) {
            //TODO altes auslesen, Daten anpassen, hinzufügen
            //m.getEntity().setEquipment(i, itemstack);
     
            if (!(m instanceof MegaCreepItems)) return false;
            EntityEquipment e=((MegaCreepItems) m).getItems();
     
            return true;
        }
     
        public boolean addItems(ItemStack[] i, float Dropchance) {
            if (!(m instanceof MegaCreepItems)) return false;
            EntityEquipment e=((MegaCreepItems) m).getItems();
     
            return true;
        }
     
     
        public boolean addItem(ItemStack basicitem, Enchantment[] enchants, float dropchance) {
            if (!(m instanceof MegaCreepItems)) return false;
            EntityEquipment e=((MegaCreepItems) m).getItems();
     
            return true;
        }
     
        public EntityEquipment getItems() {
            //TODO Problem: Craftbukkit vs Bukkit entity
            if (m instanceof MegaCreepItems) return ((MegaCreepItems) m).getItems();
            return null;
        }
     
     
        /*
        * Allowed enchantments:
        * ARROW_DAMAGE
        * ARROW_FIRE
        * ARROW_INFINITE
        * ARROW_KNOCKBACK
        * DAMAGE_ALL
        * DAMAGE_ARTHROPODS
        * DAMAGE_UNDEAD
        * DIG_SPEED
        * DURABILITY
        * FIRE_ASPECT
        * KNOCKBACK
        * LOOT_BONUS_BLOCKS
        * LOOT_BONUS_MOBS
        * OXYGEN
        * PROTECTION_ENVIRONMENTAL
        * PROTECTION_EXPLOSIONS
        * PROTECTION_FALL
        * PROTECTION_FIRE
        * PROTECTION_PROJECTILE
        * SILK_TOUCH
        * THORNS
        * WATER_WORKER
        */
     
    }
    
     
    Ne0nx3r0 likes this.
  2. Offline

    Tirelessly

    Skeleton (org.bukkit.entity) - Bukkit's API. Interface. Implemented by CraftSkeleton.
    CraftSkeleton (org.bukkit.craftbukkit, I believe) - Craftbukkit's representation of an EntitySkeleton
    EntitySkeleton (net.minecraft, I believe) - Minecraft's native skeleton

    I believe this is how it works.
     
  3. If you, like some stupid plugin makers do, compile your plugin against the craftbukkit.jar rather than the bukkit.jar you're importing the entire server (Not needed) rather than the plugin API.
     
  4. Offline

    seemethere

    AlexLeporiday It's not "stupid" to import a craftbukkit.jar, in fact sometimes it's necessary like in the case of changing NBT data.

    Just a case of the right tool for the right job.
     
  5. Offline

    DeMaggo

    That is what I expected. Now the problem is:
    I need to extend EntitySkeleton to modify the hitpoints AND I need a CraftSkeleton object referring to that EntitySkeleton to use a proper Inventory. In addition, the CraftSkeleton class must also be able to implement a custom interface for separation.
    In this case, I would need two custom classes just for the skeleton type, right?

    Another problem are the different world definitions. I can create an EntitySkeleton in a craftbukkit.world, but when it comes to a Bukkit plugin trying to do so, you cannot use the bukkit.world as a craftbukkit.world. Would a typecast work or how should I do that?
     
  6. Offline

    Tirelessly

    I'm going off memory here, but I believe calling .getHandle() on a CraftSkeleton will return the EntitySkeleton. Otherwise it's calling getHandle() on a Skeleton that will return a CraftSkeleton.
     
  7. Offline

    DeMaggo

    That's right, but unfortunately it is how it is: The Bukkit one has the getHandle() Method that returns the CraftBukkit Skeleton, which makes sense when looking at the inheritence. But I would need to extend the Bukkit Skeleton to modify its attributes and somehow find a way to get a CraftBukkit one out of it, which brings up the problem concerning the different world definitions and so on.
     
  8. Offline

    agaricus

    Simply check if the object is an OBC instance then downcast appropriately, something like:

    Code:
    if (bukkitSkeleton instanceof CraftSkeleton) {
        craftSkeleton = (CraftSkeleton) bukkitSkeleton;
        // etc
    
     
  9. Offline

    DeMaggo

    These are two separated classes, so this won't work.

    And please dont forget about the other problem: Adding the Entity to a world. There is a Bukkit world class and a Craftbukkit world class and if I expand the Craftbukkit Skeleton (which I will have to do in order to modify the hitpoints) I'm having trouble using it because I'd need some Craftbukkit world while plugins used to run on classic bukkit derived implementations.

    €: I eventually found another way to simulate certain hitpoints. I'd rather like the proper way, though.
     
Thread Status:
Not open for further replies.

Share This Page