Error switching phase when teleport player

Hi. I have a trouble in follow code:

@Listener
	public void OnZoneEnter(MoveEntityEvent event, @First Player player) {
		int guildId = -1;
		for(int i = 0; i < Main.guilds.size(); i++) {
			if (Main.guilds.get(i).IsInGuild(player.getName())) {
				guildId = Main.guilds.get(i).GetId();
				break;
			}
		}
		
		for(int i = 0; i < Main.halls.size(); i++) {
			GuildHall hall = Main.halls.get(i);
			boolean IsInX = hall.CheckExistance(hall.getFirstPos().getX(), hall.getSecondPos().getX(), player.getLocation().getX());
			boolean IsInY = hall.CheckExistance(hall.getFirstPos().getY(), hall.getSecondPos().getY() , player.getLocation().getY());
			boolean IsInZ = hall.CheckExistance(hall.getFirstPos().getZ(), hall.getSecondPos().getZ(), player.getLocation().getZ());
			
			if (hall.getMasterGuild() != 0 && guildId != hall.getMasterGuild() && IsInX && IsInY && IsInZ) {
				Location<World> loc = new Location<World>(Sponge.getServer().getWorld(Sponge.getServer().getDefaultWorldName()).get(), 0,0,0);
				player.sendMessage(Text.of(TextColors.RED, "[Guild] You don't have permission to visit this guild hall."));
				player.setLocation(loc);
				event.setCancelled(true);
			}
		}
	}

When I try to enter to the zone must occured teleport but I recieve error:

So, I have two question:

  1. How to fix that error?
  2. Is there any way to get has player permission or not?

So the error is occuring because the MoveEvent prevents the entity from moving until the event is done. If you set the new position on the event instead of the player then the error should disappear.

What do you mean by your second question? Do you mean the following?

player.hasPermission("random permission");

Yes, exactly.
Can you give example how to fix it?

Currently your doing

player.setLocation(loc);
event.setCancelled(true);

Which is causing the error. If you set the players location though the event using the following.

event.setToTransform();

You will need to create a new transform (a location and rotation) so something like the following

event.setToTransform(new Transform(loc));

If you mean an example of the permissions, my previous comment has one

@MoseMister,.thank you very much. This is not the first time when you help me )

1 Like

Glad I could help

1 Like