Jump to content

[SOLVED] When casting ICommandSender to EntityPlayer, it does not effect player.


Jaycen1000

Recommended Posts

I made a flying command which is still in beta, I was able to make it work so you could fly whilst holding an item, CODE:

	@Override
	public void onUpdate(ItemStack itemstack, World world, Entity entity, int i, boolean flag)
	{
	     if (entity instanceof EntityPlayer)
	    	 
	     {          EntityPlayer Player = (EntityPlayer) entity;
	          if(Player.getHeldItemMainhand() != null && Player.getHeldItemMainhand().getItem() instanceof IHasModel)
	        	  
	          {
	        	  PlayerFlight.setFlight(Player, true, world);
	        	  //System.out.println("holding");
	          }
	          else
	          { 
	        	  PlayerFlight.setFlight(Player, false, world);
	          }
	     }	 
	
	     }

This works, but this doesn't, I'm suspecting that you can't cast ICommandSender to EntityPlayer, because this code will not work, the player will do nothing.

boolean val = false;
	  @Override
	  public void execute(MinecraftServer server, ICommandSender sender, String[] params) throws CommandException {
		
		World w = sender.getEntityWorld();
		EntityPlayer p = (EntityPlayer) sender;

		val = !val;
		if(w.isRemote)
		{
			PlayerFlight.setFlight(p, true, sender.getEntityWorld());
		}
		
	  }

	  @Override
	  public String getName() {
	    return "fly";
	  }

	  @Override
	  public String getUsage(ICommandSender sender) {
	    return "command.fly.usage";
	  }

And my Flying class. #Note that I know how to use hashmaps and when I get this working ill add multiplayer support.

static boolean val = false;
	public static void toggleFlight(EntityPlayer playerIn, boolean flight, World worldIn)
	{
		
		if (worldIn.isRemote)
        {
			flight = !val;
			val = !val;
			System.out.println(flight);
			//Check Activation by player
        	String var1 = playerIn.getName();
        	//playerIn.sendChatToPlayer("Activated by: " + var1 + "");

        //Make the player fly
        	playerIn.capabilities.allowFlying = flight;
        	playerIn.capabilities.isFlying = flight;
        	playerIn.capabilities.isFlying = flight;

        	playerIn.fallDistance = 0; 
        	if(flight)
        	{
        		playerIn.motionY += 1.0;
        	} else
        	{
        		playerIn.motionY += -1.0;
        	}
        	
        	//if(!flight)
        	//{
        	//	playerIn.capabilities.isCreativeMode = false;
        	//}
        	
        	playerIn.sendPlayerAbilities();
        }
        else
        {

        	
            
            return;
        }
	}
	public static void setFlight(EntityPlayer playerIn, boolean flight, World worldIn)
	{
		
		if (worldIn.isRemote)
        {
			
			//Check Activation by player
        	String var1 = playerIn.getName();
        	//playerIn.sendChatToPlayer("Activated by: " + var1 + "");

        //Make the player fly
        	playerIn.capabilities.allowFlying = flight;
        	playerIn.capabilities.isFlying = flight;
        	playerIn.capabilities.isFlying = flight;

        	playerIn.fallDistance = 0; 
        	
        	//if(!flight)
        	//{
        	//	playerIn.capabilities.isCreativeMode = false;
        	//}
        	
        	playerIn.sendPlayerAbilities();
        }
        else
        {

        	
            
            return;
        }
	}

 

Edited by Jaycen1000
Solved
Link to comment
Share on other sites

If you couldn't cast something to something else then you would crash with a ClassCastException.

 

I think you need to send a packet to the client notifying them that they can fly now.

Actually, scratch that, your code will never work

if(w.isRemote)
		{
			PlayerFlight.setFlight(p, true, sender.getEntityWorld());
		}

Commands are executed on the server so this condition will always be false thus your flying code will never execute.

Also

11 minutes ago, Jaycen1000 said:

instanceof IHasModel

IHasModel is stupid. All items need models, no exceptions, and IHasModel makes you write redundand code a lot(as in you need 3 lines PER ITEM as a bare minimum instead of ONE line and that's it). Register your models directly in the ModelRegistryEvent.

Edited by V0idWa1k3r
  • Like 1
Link to comment
Share on other sites

Ok, using your feedback, I changed my flight class to this

public static void setCommandFlight(EntityPlayer playerIn, boolean flight)
	{
		
		String var1 = playerIn.getName();
        //Make the player fly
        playerIn.capabilities.allowFlying = flight;
        playerIn.capabilities.isFlying = flight;
        playerIn.capabilities.isFlying = flight;
        playerIn.fallDistance = 0; 
        playerIn.sendPlayerAbilities();
        	
        	
        	
        

	}

And my execute method to this

		World w = sender.getEntityWorld();
		EntityPlayer p = (EntityPlayer) sender;
		
		PlayerFlight.setCommandFlight(p, true);

What this does is allow you to fly for a tick (I can see my FOV getting bigger), but then after the tick, it goes off.

Link to comment
Share on other sites

After a bit of testing, I got this to work.

PlayerFlight.java

static boolean val = false;
	public static void toggleFlight(EntityPlayer playerIn, boolean flight, World worldIn)
	{
		
		if (worldIn.isRemote)
        {
			flight = !val;
			val = !val;
			System.out.println(flight);
			//Check Activation by player
        	//playerIn.sendChatToPlayer("Activated by: " + var1 + "");

        	if (worldIn.isRemote)
            {
            	//Check Activation by player

            	//playerIn.sendChatToPlayer("Activated by: " + var1 + "");

            //Make the player fly
                playerIn.capabilities.allowFlying = flight;
                playerIn.capabilities.isFlying = flight;
            	playerIn.motionY += 0;
            	playerIn.capabilities.isFlying = flight;
                playerIn.fallDistance = 0; 
                playerIn.sendPlayerAbilities();
    			return;
            }
            else
            {

                
                return;
            }
        	
        	//if(!flight)
        	//{
        	//	playerIn.capabilities.isCreativeMode = false;
        	//}
        	
        	
        }
        else
        {

        	
            
            return;
        }
	}
	public static void setFlight(EntityPlayer playerIn, boolean flight, World worldIn)
	{
		
		if (worldIn.isRemote)
        {
        	//Check Activation by player
        	String var1 = playerIn.getName();
        	//playerIn.sendChatToPlayer("Activated by: " + var1 + "");

        //Make the player fly
            playerIn.capabilities.allowFlying = flight;
            playerIn.capabilities.isFlying = flight;
        	playerIn.motionY += 0;
        	playerIn.capabilities.isFlying = flight;
            playerIn.fallDistance = 0; 
            playerIn.sendPlayerAbilities();
			return;
        }
        else
        {

            
            return;
        }
        	
        	
        
	}
	public static void setCommandFlight(EntityPlayer playerIn, boolean flight)
	{
		
		String var1 = playerIn.getName();
        //Make the player fly
        playerIn.capabilities.allowFlying = flight;
        playerIn.capabilities.isFlying = flight;
    	playerIn.motionY += 1.0;
    	playerIn.capabilities.isFlying = flight;
        playerIn.fallDistance = 0; 
        playerIn.sendPlayerAbilities();
        	
        	
        	
        

	}

And FlyCommand.java

HashMap<EntityPlayer, Boolean> flying = new HashMap<EntityPlayer, Boolean>();
	boolean val = false;
	  @Override
	  public void execute(MinecraftServer server, ICommandSender sender, String[] params) throws CommandException {
		
		World w = sender.getEntityWorld();
		EntityPlayer p = (EntityPlayer) sender;
		try
		{
			flying.replace(p, !flying.get(p));
		} catch(Exception e)
		{
			
		}
		
		if(!flying.containsKey(p))
		{
			flying.put(p, true);
		}
		val = !val;
		PlayerFlight.setCommandFlight(p, flying.get(p));
		
		
	  }

	  @Override
	  public String getName() {
	    return "fly";
	  }

	  @Override
	  public String getUsage(ICommandSender sender) {
	    return "command.fly.usage";
	  }

That should work with multiplayer, also thank you V0idWa1k3r for telling me about World#isRemote.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

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