Jump to content

FlamingVikingGoat

Members
  • Posts

    6
  • Joined

  • Last visited

Posts posted by FlamingVikingGoat

  1. I'm working on an item that, when right-clicked, pushes back entities in a circle around the player. However, I want the entity to be pushed back following the same line; right now, the entities are pushed back into the closest corner. Additionally, I don't want the item to push entities through walls(so setting entity.posX/entity.posY directly or using entity::moveTo won't work). Here's the code that I have now:

     

    @Override
        public ActionResult<ItemStack> onItemRightClick(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn, EnumHand hand) {
            AxisAlignedBB aabb = playerIn.getEntityBoundingBox().expand(playerIn.posX + range, playerIn.posY + range, playerIn.posZ + range);
            List<Entity> entityList = worldIn.getEntitiesWithinAABBExcludingEntity(playerIn, aabb);
            for (Entity entity : entityList){
                if (entity instanceof EntityLiving){
                    rebukeEntityFromPlayer((EntityLiving) entity, playerIn);
                }
            }
    
            return super.onItemRightClick(itemStackIn, worldIn, playerIn, hand);
        }
    
        private void rebukeEntityFromPlayer(EntityLiving entity, EntityPlayer player){
            //Creates xy plane with player at center by getting the directional distance between the coords of the entity and the player
            double adjX = entity.posX - player.posX;
            double adjZ = entity.posZ - player.posZ;
    
            //Creates a triangle to describe the distance between the entity and player
            double startSideA = adjX;
            double startSideB = adjZ;
            double startHyp = Math.sqrt((Math.pow(startSideA, 2) + Math.pow(startSideB, 2)));
    
            //Scales the triangle to get the point the entity should be pushed to
            double scaleFactor = range/startHyp;
            if(scaleFactor <= 1){return;}//stops spell if entity is already out of target area.
            double scaledSideA = startSideA*scaleFactor;
            double scaledSideB = startSideB*scaleFactor;
    
            double desiredPosX = adjX + (scaledSideA - startSideA);
            double desiredPosZ = adjZ + (scaledSideB - startSideB);
    
            if(Math.abs(adjX) < Math.abs(desiredPosX)){
                if(desiredPosX >= 0){
                    entity.motionX = 3;
                } else if(desiredPosX < 0){
                    entity.motionX = -3;
                }
            }
            if(Math.abs(adjZ) < Math.abs(desiredPosZ)){
                if(desiredPosZ >= 0){
                    entity.motionZ = 3;
                } else if(desiredPosZ < 0){
                    entity.motionZ = -3;
                }
            }
    
        }

    Additionally, here are some images to help convey my problem: Ideal ScenarioCurrent scenario

  2. To help in future cases:

     

    When Obj::field stops working, it's usually because the field has been encapsulated and can only be accessed/changed with accessor/mutator(getter/setter) functions. That is, it typically becomes either Obj::getField/Obj::setField, or something similar if the field name changes(if you're using an IDE, you'll typically find it in the lookup). If you're still having trouble, you can usually find it by skimming the source files(some IDEs allow you to jump to a referenced class if you highlight it and right-click/use a keybind).

    • Like 1
  3. In the PlayerSleepInBed event, there is a public field(for 1.8) result, an EnumStatus(which is used to determine whether or not a player can sleep).

    You can cancel sleep with an event like this:

     

    Spoiler
    
    public void cancelSleep(PlayerSleepInBedEvent event){
        event.result = EntityPlayer.EnumStatus.OTHER_PROBLEM;
    }

    Note: In later versions, the result field is private with the setter setResult. Keep this in mind if you plan to upgrade.

  4. I'm trying to add mana values for players, but I can't seem to change them. I have an item that should change the max value for mana when I right click it, but it doesn't seem to work; I have println's for debugging and they keep displaying that my max mana is 0.

     

    ExtendedPlayer code:

    Spoiler
    
    package com.fvg.blackmagic.entities;
    
    import net.minecraft.entity.Entity;
    import net.minecraft.entity.player.EntityPlayer;
    import net.minecraft.nbt.NBTTagCompound;
    import net.minecraft.world.World;
    import net.minecraftforge.common.IExtendedEntityProperties;
    
    public class ExtendedPlayer implements IExtendedEntityProperties {
        public final static String MANA = "mana";
        protected EntityPlayer player;
        private String manaDebug = "Mana: " + this.currentMana + "/" + this.maxMana;
    
        private boolean isMagicUser = true;
        private int currentMana;
        private int maxMana;
    
        public boolean isMagicUser() {
            return isMagicUser;
        }
    
        public void setMagicUser(boolean magicUser) {
            isMagicUser = magicUser;
            if(magicUser){this.currentMana = this.maxMana = 50;}
        }
    
        public int getCurrentMana() {
            return currentMana;
        }
    
        public void setCurrentMana(int currentMana) {
            this.currentMana = currentMana;
        }
    
        public int getMaxMana() {
            return maxMana;
        }
    
        public void setMaxMana(int maxMana) {
            this.maxMana = maxMana;
        }
    
        public ExtendedPlayer(EntityPlayer player){
            this.player = player;
            this.currentMana = this.maxMana = (this.isMagicUser ? 50 : 0);
        }
    
        public static final void register(EntityPlayer player){
            player.registerExtendedProperties(ExtendedPlayer.MANA, new ExtendedPlayer(player));
        }
    
        public static final ExtendedPlayer get(EntityPlayer player){
            return (ExtendedPlayer) player.getExtendedProperties(MANA);
        }
    
        @Override
        public void saveNBTData(NBTTagCompound compound) {
            NBTTagCompound properties = new NBTTagCompound();
    
            //Var tags
            properties.setBoolean("IsMagicUser", this.isMagicUser);
            properties.setInteger("CurrentMana", this.currentMana);
            properties.setInteger("MaxMana", this.maxMana);
    
            compound.setTag(MANA, properties);
        }
    
        @Override
        public void loadNBTData(NBTTagCompound compound) {
            NBTTagCompound properties = (NBTTagCompound) compound.getTag(MANA);
    
            //get var tags
            this.isMagicUser = properties.getBoolean("IsMagicUser");
            this.currentMana = properties.getInteger("CurrentMana");
            this.maxMana = properties.getInteger("MaxMana");
    
            System.out.println(manaDebug);
        }
    
        @Override
        public void init(Entity entity, World world) {
    
        }
    
        public boolean consumeMana(int amount){
            boolean sufficient = amount <= this.currentMana;
            this.currentMana -= (amount < this.currentMana ? amount : this.currentMana);
    
            System.out.println(manaDebug);
    
            return !sufficient;
        }
    
        public void regainMana(int amount) {
            int sum = this.currentMana + amount;
            this.currentMana = (sum < this.maxMana ? sum : this.maxMana);
    
            System.out.println(manaDebug);
        }
    }

     

     

    ItemModSword(contains the code for the item mentioned above):

    Spoiler
    
    package com.fvg.blackmagic.items.gear;
    
    import com.fvg.blackmagic.core.BlackMagic;
    import com.fvg.blackmagic.entities.ExtendedPlayer;
    import net.minecraft.entity.Entity;
    import net.minecraft.entity.effect.EntityLightningBolt;
    import net.minecraft.entity.player.EntityPlayer;
    import net.minecraft.item.ItemStack;
    import net.minecraft.item.ItemSword;
    import net.minecraft.util.BlockPos;
    import net.minecraft.util.EnumFacing;
    import net.minecraft.world.World;
    
    public class ItemModSword extends ItemSword{
        public ItemModSword(ToolMaterial material, String unlocalizedName){
            super(material);
            this.setUnlocalizedName(unlocalizedName);
            this.setCreativeTab(BlackMagic.TabBlackMagicCore);
        }
    
    
    
        @Override
        public boolean onLeftClickEntity(ItemStack stack, EntityPlayer player, Entity entity){
    
            if(!player.getEntityWorld().isRemote) {
                ExtendedPlayer props = ExtendedPlayer.get(player);
                if(props.consumeMana(15)) {
                    entity.getEntityWorld().addWeatherEffect(new EntityLightningBolt(entity.getEntityWorld(), entity.posX, entity.posY, entity.posZ));
                }
    
            }
    
            return false;
    
        }
    
        @Override
        public boolean onItemUse(ItemStack stack, EntityPlayer playerIn, World worldIn, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ) {
    
            if(!worldIn.isRemote) {
                ExtendedPlayer props = ExtendedPlayer.get(playerIn);
                if(props.consumeMana(10)){
                    worldIn.addWeatherEffect(new EntityLightningBolt(worldIn, pos.getX(), pos.getY(), pos.getZ()));
                }
            }
            return false;
    
        }
    
        @Override
        public ItemStack onItemRightClick(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn) {
            ExtendedPlayer props = ExtendedPlayer.get(playerIn);
            if(!props.isMagicUser()){
                props.setMagicUser(true);
                System.out.println(props.isMagicUser());
            }
            else if(props.getMaxMana()==0) {
                props.setMaxMana(50);
                System.out.println(props.isMagicUser());
            }
            else {
                props.regainMana(25);
                System.out.println(props.isMagicUser());
            }
    
            return itemStackIn;
        }
    }

     

     

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.