Jump to content

TheRealMcrafter

Forge Modder
  • Posts

    137
  • Joined

  • Last visited

Posts posted by TheRealMcrafter

  1. public void setShouldStop(boolean shouldStop){
        	if (shouldStop){
        		if (isPlaying){
        			isPlaying = false; 
        			this.shouldStop = true;
        		}
        	} else {
        		this.shouldStop = shouldStop;
        	}
        }

     

     

    I know I could have just done

     

    public void setShouldStop(boolean shouldStop){
        this.shouldStop = shouldStop;
    }
    

     

    but in order for my sirens to work properly, I have to do the above. Everything worked fine until I implemented the AI changes above, then they play for a few seconds and stop.

  2. I play the sound by sending a packet with the sound name from the tile entity to all nearby players in the same world, and then passing the sound name to my Looping Audio Handler, and then passing that to my Siren Looper class.

     

    Tile Entity:

    if (!isPlaying && shouldStart){
    			shouldStart = false; 
    			shouldStop = false; 
    			isPlaying = true; 
    			this.updateClientRender();
    			SirenModPacketDispatcher.sendToAllAround(new SirenModPlayLoopedSoundMessage(xCoord, yCoord, zCoord, "sirenT121"), this.worldObj.provider.dimensionId, xCoord, yCoord, zCoord, 50000000);
    		}

     

    Play Sound Message:

    public IMessage handleClientMessage(EntityPlayer player, SirenModPlayLoopedSoundMessage message, MessageContext ctx) {
        		new LoopingAudioHandler(player.worldObj, message.x, message.y, message.z, message.soundName);
    

     

    Looping Audio Handler:

    public LoopingAudioHandler(World world, int x, int y, int z, String soundName){
    	SirenLooper looper = new SirenLooper(world.getTileEntity(x, y, z), soundName);
    	Minecraft.getMinecraft().getSoundHandler().playSound(looper);
    }

     

    Siren Looper:

    public SirenLooper(TileEntity tile, String soundName){ 
            super(new ResourceLocation("SirenMod:" + soundName));  
            this.tile = tile;
            this.repeat = true; 
            if (tile instanceof NuclearSirenTileEntity){
                volume = 5.0F;
        	} else if (tile instanceof GeneralSirenTileEntity){
                volume = 10.0F;
        	} else if (tile instanceof BurglarSirenTileEntity){
                volume = 5.0F;
        	} else if (tile instanceof FireAlarmTileEntity){
                volume = 5.0F;
        	} else if (tile instanceof AmericanSignalT121TileEntity){
                volume = 20.0F;
    
        	} else {
                volume = 5.0F;
        	}
            this.xPosF = tile.xCoord;
            this.yPosF = tile.yCoord;
            this.zPosF = tile.zCoord;
        }
    
    
        public void update(){ 
        	if (tile instanceof GeneralSirenTileEntity){
        		if (((GeneralSirenTileEntity) tile).isShouldStop() || ((GeneralSirenTileEntity) tile).isInvalid()){  
                    this.donePlaying = true;}
        	} else if (tile instanceof NuclearSirenTileEntity){
        		if (((NuclearSirenTileEntity) tile).isShouldStop() || ((NuclearSirenTileEntity) tile).isInvalid()){    
                    this.donePlaying = true;}
        	} else if (tile instanceof BurglarSirenTileEntity){
        		if (((BurglarSirenTileEntity) tile).isShouldStop() || ((BurglarSirenTileEntity) tile).isInvalid()){    
                    this.donePlaying = true;}
        	} else if (tile instanceof FireAlarmTileEntity){
        		if (((FireAlarmTileEntity) tile).isShouldStop() || ((FireAlarmTileEntity) tile).isInvalid()){    
                    this.donePlaying = true;}
        	} else if (tile instanceof AmericanSignalT121TileEntity){
        		if (((AmericanSignalT121TileEntity) tile).isShouldStop() || ((AmericanSignalT121TileEntity) tile).isInvalid()){    
                    this.donePlaying = true;}
        	} else {
        		throw new ClassCastException();
        	}   
        }
    
    @Override
        public boolean isDonePlaying(){
            return this.donePlaying;} 
    }

  3. Okay, I got it to work how I want it. But now, my sirens only play for like a half a second and then they stop. If I remove this code from my tile entity, the sounds play all the way through and continue looping.

     

    
    if (this.isPlaying){
    List l = this.worldObj.getEntitiesWithinAABB(EntityVillager.class, AxisAlignedBB.getBoundingBox(this.xCoord - 20, this.yCoord - 20, this.zCoord - 20, this.xCoord + 20, this.yCoord + 20, this.zCoord + 20));
    
    if (!l.isEmpty()){
    	for (int i = 0; i < l.size(); i++){
    		EntityVillager villager = (EntityVillager) l.get(i);
    		ExtendedVillager props = ExtendedVillager.get(villager);
    		props.isScared = true;
    	}
    }
    }

     

    Anyone have an idea what I'm doing wrong?

  4. Tried a few things:

     

    if (this.isPlaying){
           List l = this.worldObj.getEntitiesWithinAABB(EntityVillager.class, AxisAlignedBB.getBoundingBox(this.xCoord - 20, this.yCoord - 20, this.zCoord - 20, this.xCoord + 20, this.yCoord + 20, this.zCoord + 20));
    
           if (!l.isEmpty()){
    	for (int i = 0; i < l.size(); i++){
    		EntityVillager villager = (EntityVillager) l.get(i);
    		((EntityVillager) villager).tasks.addTask(0, new EntityAIPanic(villager, 0.9D));
    
                            //((EntityVillager) villager).targetTasks.addTask(0, new EntityAIPanic(villager, 0.9D));
    		//((EntityVillager) villager).tasks.onUpdateTasks();
    		//((EntityVillager) villager).targetTasks.onUpdateTasks();
    	}
    }
    }
    

  5. By the way, this is bad:

    entity.motionX != 0

    since motion fields are all floats, they suffer from approximation errors. See here, it is a really good article on that topic: http://www.theregister.co.uk/2006/08/12/floating_point_approximation/

    So I'd suggest you check for "approximately" 0, like

    entity.motionX > 0.0001 || entity.motionX < -0.0001

     

    Yeah, I just quickly did that to show my point. I figured that out when I was testing for motionY, which is about 1.17. Thanks for reminding me though.

  6. I'm checking on the server, and it is not just for the player, it is for any living entity. I am making a motion detector, so if any entity in the list moves, isDetected turns to true.

     

     

     if (!worldObj.isRemote){
    		List list = this.worldObj.getEntitiesWithinAABB(EntityLivingBase.class, AxisAlignedBB.getBoundingBox(this.xCoord - 10, this.yCoord - 10, this.zCoord - 10, this.xCoord + 10, this.yCoord + 10, this.zCoord + 10));
    		if (list.isEmpty()){
    			this.isDetecting = false;
    		} else {
    			for (int i = 0; i < list.size(); i ++){
    				EntityLivingBase entity = (EntityLivingBase) list.get(i);
    
    				if (entity.motionX != 0){
    					this.isDetecting = true;
    				} else {
    					this.isDetecting = false;
    				}					
    			}
    		}
    
    

     

    Right now I'm focusing only on motionX, but it doesn't work for Y and Z also. A System.out.println() always prints out 0.0

  7. Hey guys, I want to check if an entity is moving from inside my Tile Entity updateEntity method. I have already checked if entity.prevPosX != entity.posX, I've tried using entity.motionX, and I've tried storing the old coordinates in a variable and checking against them. Entity.motion* only works if the player is sprinting, and all of the other methods I've tried don't work. Does anyone know how to do this?

  8. Oh dear lord...

     

    1. DO NOT use 'static' fields to store the player, extended properties, or anything else - clearly you don't know what it's for.

     

    2. DO NOT use Minecraft.getMinecraft().thePlayer - that is CLIENT side only player.

     

    3. You are still using a 'new ExtendedPlayer' - NEVER do that - the player already has an ExtendedPlayer instance created by Forge, which is why you use the ExtendedPlayer.get(player) method to retrieve it (which is just a wrapper for player.getExtendedProperties("YourProperties")).

     

    I know, I know! I've just been trying everything possible to get this to work. (Even doing things I know I shouldnt)  :'(

     

    And THATS what I needed, I forgot I had that .get(player) method. Thanks for all your help!

     

    Edit: Seriously, thank you so much coolAlias. Your tutorials and explanations are amazing and you dont get enough credit for what you do for the forge community :)

     

    -TheRealMcrafter

  9. Welp, that didn't seem to work either.

     

    I'm doing this:

     

     
    private final static ExtendedPlayer extendedPlayer = new ExtendedPlayer(Minecraft.getMinecraft().thePlayer);
    
        public static boolean tryToHolster(EntityPlayer player){
       		ItemStack stack = player.inventory.getCurrentItem();
    	ItemStack holsterSlot = extendedPlayer.inventory.getStackInSlot(0);
    
    
    	System.err.println(holsterSlot);
    	System.err.println(extendedPlayer.inventory.getStackInSlot(0));
    
    	if (stack != null){
    		if (stack.getItem() == CitiesMod.M1911){
    			if (holsterSlot != null){
    				System.err.println("Reached!");
    			}
    		} else if (stack.getItem() == CitiesMod.Glock21){
    			if (holsterSlot != null){
    				System.err.println("Reached!");
    
    			}
    		}
    	}
        	
        	return false;
        }
    

  10. The problem looks to be that you are always creating 'new' instances of your extended player and inventory, when what you really want is the one that should already exist:

    ExtendedPlayer props = ExtendedPlayer.get(player);
    props.inventory.setStackInSlot(0, new ItemStack(Items.stick));
    System.out.println("Stack in slot 0: " + props.inventory.getStackInSlot(0));
    

     

    Oh! That makes sense! So where could I put that instance for the extended player then?

  11.  

     

     
    
    public class ExtendedPlayer implements IExtendedEntityProperties{
    
    public final static String EXT_PROP_NAME = "ExtendedPlayer";
    private final EntityPlayer player;	
    public final InventoryCustomPlayer inventory = new InventoryCustomPlayer();
    
    
    public ExtendedPlayer(EntityPlayer player){
    	this.player = player;
    }
    
    public static final void register(EntityPlayer player){
    	player.registerExtendedProperties(ExtendedPlayer.EXT_PROP_NAME, new ExtendedPlayer(player));
    }
    
    public static final ExtendedPlayer get(EntityPlayer player){
    	return (ExtendedPlayer) player.getExtendedProperties(EXT_PROP_NAME);
    }
    
    // Save any custom data that needs saving here
    @Override
    public void saveNBTData(NBTTagCompound compound){
    	NBTTagCompound properties = new NBTTagCompound();
    
    	this.inventory.writeToNBT(properties);
    
    	compound.setTag(EXT_PROP_NAME, properties);
    }
    
    @Override
    public void loadNBTData(NBTTagCompound compound){
    	NBTTagCompound properties = (NBTTagCompound) compound.getTag(EXT_PROP_NAME);
    
    	this.inventory.readFromNBT(properties);
    
    	System.err.println("Loading Player's NBT: " + this.inventory.getStackInSlot(0) != null ? this.inventory.getStackInSlot(0) : "");
    }
    @Override
    public void init(Entity entity, World world){
    }
    }
    

     

     

  12. Hey,

     

    I have created a custom player inventory, with one extra slot. When I try to get the ItemStack in that slot, it always returns null, even if I do something like

    setInventorySlotContents(0, new ItemStack(Blocks.dirt));

    before a System.out.println(getStackInSlot(0))

     

    I have tried the following ways of getting the itemstack:

     

    ExtendedPlayer extendedPlayer = new ExtendedPlayer(getPlayer());
    ItemStack holsterSlot = extendedPlayer.inventory.getStackInSlot(0);
    

     

    InventoryCustomPlayer inventory = new InventoryCustomPlayer();
    ItemStack holsterSlow = inventory.getStackInSlot(0);
    

     

    but nothing seems to work, it always prints null.

     

    I have tried checking the slot on the Client Side, and also sending a packet to the server side, and checking it there. Nothing seems to work.

     

    Is this the correct way to get an ItemStack in a custom player inventory slot?

     

    Thanks for your time,

     

    -TheRealMcrafter

×
×
  • Create New...

Important Information

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