org.bukkit.event.player.PlayerGameModeChangeEvent Java Examples

The following examples show how to use org.bukkit.event.player.PlayerGameModeChangeEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PlayerGameModeChangeListenerTest.java    From PerWorldInventory with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void shouldDoEverything() {
    // given
    Player player = mock(Player.class);
    World world = mock(World.class);
    given(world.getName()).willReturn("world");
    given(player.getWorld()).willReturn(world);
    Group group = mockGroup("world");
    given(groupManager.getGroupFromWorld("world")).willReturn(group);

    PlayerGameModeChangeEvent event = new PlayerGameModeChangeEvent(player, GameMode.CREATIVE);

    // when
    listener.onPlayerGameModeChange(event);

    // then
    verify(groupManager).getGroupFromWorld("world");
    verify(playerManager).addPlayer(player, group);
    verify(bukkitService).runTaskLater(any(Runnable.class), any(Long.class));
}
 
Example #2
Source File: CraftPlayer.java    From Thermos with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void setGameMode(GameMode mode) {
    if (getHandle().playerNetServerHandler == null) return;

    if (mode == null) {
        throw new IllegalArgumentException("Mode cannot be null");
    }

    if (mode != getGameMode()) {
        PlayerGameModeChangeEvent event = new PlayerGameModeChangeEvent(this, mode);
        server.getPluginManager().callEvent(event);
        if (event.isCancelled()) {
            return;
        }

        getHandle().theItemInWorldManager.setGameType(net.minecraft.world.WorldSettings.GameType.getByID(mode.getValue()));
        getHandle().fallDistance = 0;
        getHandle().playerNetServerHandler.sendPacket(new net.minecraft.network.play.server.S2BPacketChangeGameState(3, mode.getValue()));
    }
}
 
Example #3
Source File: PlayerGameModeChangeListenerTest.java    From PerWorldInventory with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void shouldDoEverything() {
    // given
    Player player = mock(Player.class);
    World world = mock(World.class);
    given(world.getName()).willReturn("world");
    given(player.getWorld()).willReturn(world);
    Group group = mockGroup("world");
    given(groupManager.getGroupFromWorld("world")).willReturn(group);

    PlayerGameModeChangeEvent event = new PlayerGameModeChangeEvent(player, GameMode.CREATIVE);

    // when
    listener.onPlayerGameModeChange(event);

    // then
    verify(groupManager).getGroupFromWorld("world");
    verify(playerManager).addPlayer(player, group);
    verify(bukkitService).runTaskLater(any(Runnable.class), any(Long.class));
}
 
Example #4
Source File: Esp.java    From AACAdditionPro with GNU General Public License v3.0 6 votes vote down vote up
@EventHandler
public void onGameModeChange(PlayerGameModeChangeEvent event)
{
    if (event.getNewGameMode() == GameMode.SPECTATOR) {
        final User spectator = UserManager.getUser(event.getPlayer().getUniqueId());

        // Not bypassed
        if (User.isUserInvalid(spectator, this.getModuleType())) {
            return;
        }

        // Spectators can see everyone and can be seen by everyone (let vanilla handle this)
        for (User user : UserManager.getUsersUnwrapped()) {
            updatePairHideMode(spectator, user, HideMode.NONE);
        }
    }
}
 
Example #5
Source File: ListenerGameMode.java    From CombatLogX with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler(priority=EventPriority.HIGH, ignoreCancelled=true)
public void onChangeGameMode(PlayerGameModeChangeEvent e) {
    FileConfiguration config = this.expansion.getConfig("cheat-prevention.yml");
    if(!config.getBoolean("game-mode.prevent-game-mode-change")) return;

    Player player = e.getPlayer();
    ICombatManager manager = this.plugin.getCombatManager();
    if(!manager.isInCombat(player)) return;

    e.setCancelled(true);
    String message = this.plugin.getLanguageMessageColoredWithPrefix("cheat-prevention.game-mode.no-changing");
    this.plugin.sendMessage(player, message);
}
 
Example #6
Source File: PlayerGameModeChangeListenerTest.java    From PerWorldInventory with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void shouldDoNothingEventCancelled() {
    // given
    PlayerGameModeChangeEvent event = new PlayerGameModeChangeEvent(mock(Player.class), GameMode.CREATIVE);
    event.setCancelled(true);

    // when
    listener.onPlayerGameModeChange(event);

    // then
    verifyZeroInteractions(groupManager);
    verifyZeroInteractions(playerManager);
    verifyZeroInteractions(bukkitService);
}
 
Example #7
Source File: PlayerGameModeChangeListener.java    From PerWorldInventory with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerGameModeChange(PlayerGameModeChangeEvent event) {
    if (event.isCancelled()) {
        return;
    }

    Player player = event.getPlayer();
    Group group = groupManager.getGroupFromWorld(player.getWorld().getName());

    ConsoleLogger.debug("[GM PROCESS] Player '" + player.getName() + "' changed to GameMode '" +
            event.getNewGameMode().name() + "' in group '" + group.getName() + "'");

    playerManager.addPlayer(player, group);
    bukkitService.runTaskLater(() -> process.processGameModeChange(player, event.getNewGameMode(), group), 1L);
}
 
Example #8
Source File: PlayerGameModeChangeListenerTest.java    From PerWorldInventory with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void shouldDoNothingEventCancelled() {
    // given
    PlayerGameModeChangeEvent event = new PlayerGameModeChangeEvent(mock(Player.class), GameMode.CREATIVE);
    event.setCancelled(true);

    // when
    listener.onPlayerGameModeChange(event);

    // then
    verifyZeroInteractions(groupManager);
    verifyZeroInteractions(playerManager);
    verifyZeroInteractions(bukkitService);
}
 
Example #9
Source File: PlayerGameModeChangeListener.java    From PerWorldInventory with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerGameModeChange(PlayerGameModeChangeEvent event) {
    if (event.isCancelled()) {
        return;
    }

    Player player = event.getPlayer();
    Group group = groupManager.getGroupFromWorld(player.getWorld().getName());

    ConsoleLogger.debug("[GM PROCESS] Player '" + player.getName() + "' changed to GameMode '" +
            event.getNewGameMode().name() + "' in group '" + group.getName() + "'");

    playerManager.addPlayer(player, group);
    bukkitService.runTaskLater(() -> process.processGameModeChange(player, event.getNewGameMode(), group), 1L);
}
 
Example #10
Source File: ListenerGameMode.java    From CombatLogX with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler(priority=EventPriority.MONITOR, ignoreCancelled=true)
public void afterChangeGameMode(PlayerGameModeChangeEvent e) {
    FileConfiguration config = this.expansion.getConfig("cheat-prevention.yml");
    if(!config.getBoolean("game-mode.untag-on-change-game-mode")) return;

    Player player = e.getPlayer();
    ICombatManager manager = this.plugin.getCombatManager();
    if(!manager.isInCombat(player)) return;

    manager.untag(player, UntagReason.EXPIRE);
}
 
Example #11
Source File: GameModeChangeListener.java    From Plan with GNU Lesser General Public License v3.0 5 votes vote down vote up
@EventHandler(priority = EventPriority.MONITOR)
public void onGameModeChange(PlayerGameModeChangeEvent event) {
    if (event.isCancelled()) {
        return;
    }
    try {
        actOnEvent(event);
    } catch (Exception e) {
        errorLogger.log(L.ERROR, e, ErrorContext.builder().related(event, event.getPlayer().getGameMode() + "->" + event.getNewGameMode()).build());
    }
}
 
Example #12
Source File: PlayerListener.java    From WorldGuardExtraFlagsPlugin with MIT License 5 votes vote down vote up
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerGameModeChangeEvent(PlayerGameModeChangeEvent event)
{
	Player player = event.getPlayer();
	
	Session wgSession = this.plugin.getWorldGuardCommunicator().getSessionManager().getIfPresent(player);
	if (wgSession != null)
	{
		Boolean value = wgSession.getHandler(FlyFlagHandler.class).getCurrentValue();
		if (value != null)
		{
			new BukkitRunnable()
			{
				@Override
				public void run()
				{
					PlayerListener.this.checkFlyStatus(player);
				}
			}.runTask(WorldGuardExtraFlagsPlugin.getPlugin());
		}
	}
	else
	{
		new BukkitRunnable()
		{
			@Override
			public void run()
			{
				PlayerListener.this.checkFlyStatus(player);
			}
		}.runTask(WorldGuardExtraFlagsPlugin.getPlugin());
	}
}
 
Example #13
Source File: EvtGameMode.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean check(final Event e) {
	if (mode != null) {
		return mode.check(e, new Checker<GameMode>() {
			@Override
			public boolean check(final GameMode m) {
				return ((PlayerGameModeChangeEvent) e).getNewGameMode().equals(m);
			}
		});
	}
	return true;
}
 
Example #14
Source File: ExprGameMode.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void change(final Event e, final @Nullable Object[] delta, final ChangeMode mode) throws UnsupportedOperationException {
	final GameMode m = delta == null ? Bukkit.getDefaultGameMode() : (GameMode) delta[0];
	for (final Player p : getExpr().getArray(e)) {
		if (getTime() >= 0 && e instanceof PlayerGameModeChangeEvent && ((PlayerGameModeChangeEvent) e).getPlayer() == p && !Delay.isDelayed(e)) {
			if (((PlayerGameModeChangeEvent) e).getNewGameMode() != m)
				((PlayerGameModeChangeEvent) e).setCancelled(true);
		}
		p.setGameMode(m);
	}
}
 
Example #15
Source File: ExprGameMode.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected GameMode[] get(final Event e, final Player[] source) {
	return get(source, new Converter<Player, GameMode>() {
		@Override
		@Nullable
		public GameMode convert(final Player p) {
			if (getTime() >= 0 && e instanceof PlayerGameModeChangeEvent && ((PlayerGameModeChangeEvent) e).getPlayer() == p && !Delay.isDelayed(e))
				return ((PlayerGameModeChangeEvent) e).getNewGameMode();
			return p.getGameMode();
		}
	});
}
 
Example #16
Source File: GameModeChangeListener.java    From Plan with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void actOnEvent(PlayerGameModeChangeEvent event) {
    Player player = event.getPlayer();
    UUID uuid = player.getUniqueId();
    long time = System.currentTimeMillis();
    String gameMode = event.getNewGameMode().name();
    String worldName = player.getWorld().getName();

    dbSystem.getDatabase().executeTransaction(new WorldNameStoreTransaction(serverInfo.getServerUUID(), worldName));
    worldAliasSettings.addWorld(worldName);

    Optional<Session> cachedSession = SessionCache.getCachedSession(uuid);
    cachedSession.ifPresent(session -> session.changeState(worldName, gameMode, time));
}
 
Example #17
Source File: ExprGameMode.java    From Skript with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean setTime(final int time) {
	return super.setTime(time, PlayerGameModeChangeEvent.class, getExpr());
}