- Move
version
to constructor | Initialize it onEnable() - Store Flight Boolean and GameMode variables onPlayerQuit | onPlayerJoin set Flight based on GameMode
- Check SystemCommandEvent for GameMode command | Cancel the event | Pass arguments to a custom method to set Flight and GameMode
- Add Permission check -> See if player is allowed to change their GameMode manually
Last active
March 1, 2016 07:57
-
-
Save cFerg/8663ff4505ccfbc059c1 to your computer and use it in GitHub Desktop.
GameMode Fly Checking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package elite.gm; | |
import java.lang.reflect.Constructor; | |
import java.lang.reflect.InvocationTargetException; | |
import java.util.List; | |
import org.bukkit.Bukkit; | |
import org.bukkit.ChatColor; | |
import org.bukkit.GameMode; | |
import org.bukkit.entity.Player; | |
import org.bukkit.event.EventHandler; | |
import org.bukkit.event.Listener; | |
import org.bukkit.event.player.PlayerCommandPreprocessEvent; | |
import org.bukkit.plugin.java.JavaPlugin; | |
public class Main extends JavaPlugin implements Listener{ | |
@Override | |
public void onEnable(){ | |
Bukkit.getServer().getPluginManager().registerEvents(this, this); | |
} | |
@EventHandler | |
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent e) { | |
Player p = e.getPlayer(); | |
String[] args = e.getMessage().toLowerCase().split("\\s+"); | |
if (args.length > 1){ | |
if (args[0].equals("/gamemode")) { | |
if (args.length == 2){ | |
//Check if player typing command has permission | |
e.setCancelled(true); | |
Boolean pFlying = p.isFlying(); | |
switch (args[1]) { | |
case "survival": | |
case "0": | |
p.setAllowFlight(true); //Change value depending on server | |
gameModeChange(p, GameMode.SURVIVAL); | |
p.setFlying(pFlying); | |
break; | |
case "creative": | |
case "1": | |
p.setAllowFlight(true); | |
gameModeChange(p, GameMode.CREATIVE); | |
p.setFlying(pFlying); | |
break; | |
case "adventure": | |
case "2": | |
p.setAllowFlight(false); //Change value depending on server | |
gameModeChange(p, GameMode.ADVENTURE); | |
p.setFlying(false); //Keep set flying as variable, unless you don't want them to fly | |
break; | |
case "spectator": | |
case "3": | |
p.setAllowFlight(true); | |
gameModeChange(p, GameMode.SPECTATOR); | |
p.setFlying(pFlying); | |
break; | |
default: | |
p.sendMessage(ChatColor.RED + "Usage: /gamemode <mode> [player]"); | |
p.sendMessage(ChatColor.YELLOW + "Survival | Creative | Adventure | Spectator"); | |
break; | |
} | |
} | |
if (args.length == 3){ | |
e.setCancelled(true); | |
//Also check if player typing command has permission | |
List<Player> players = p.getWorld().getPlayers(); | |
if (players.contains(Bukkit.getPlayer(args[2]))){ | |
Player target = Bukkit.getPlayer(args[2]); | |
Boolean pFlying = target.isFlying(); | |
switch (args[1]) { | |
case "survival": | |
case "0": | |
target.setAllowFlight(true); //Change value depending on server | |
gameModeChange(target, GameMode.SURVIVAL); | |
target.setFlying(pFlying); | |
break; | |
case "creative": | |
case "1": | |
target.setAllowFlight(true); | |
gameModeChange(target, GameMode.CREATIVE); | |
target.setFlying(pFlying); | |
break; | |
case "adventure": | |
case "2": | |
target.setAllowFlight(false); //Change value depending on server | |
gameModeChange(target, GameMode.ADVENTURE); | |
target.setFlying(false); //Keep set flying as variable, unless you don't want them to fly | |
break; | |
case "spectator": | |
case "3": | |
target.setAllowFlight(true); | |
gameModeChange(target, GameMode.SPECTATOR); | |
target.setFlying(pFlying); | |
break; | |
default: | |
p.sendMessage(ChatColor.RED + "Usage: /gamemode <mode> [player]"); | |
p.sendMessage(ChatColor.YELLOW + "Survival | Creative | Adventure | Spectator"); | |
break; | |
} | |
}else{ | |
p.sendMessage(ChatColor.RED + "Cannot set GameMode of: " + ChatColor.WHITE + args[2]); | |
p.sendMessage(ChatColor.RED + "Usage: /gamemode <mode> [player]"); | |
p.sendMessage(ChatColor.YELLOW + "Survival | Creative | Adventure | Spectator"); | |
} | |
} | |
} | |
} | |
} | |
public void gameModeChange(Player p, GameMode g){ | |
if (g.equals(GameMode.SURVIVAL)){ | |
sendGameUpdate(p, 0); | |
} | |
if (g.equals(GameMode.CREATIVE)){ | |
sendGameUpdate(p, 1); | |
} | |
if (g.equals(GameMode.ADVENTURE)){ | |
sendGameUpdate(p, 2); | |
} | |
if (g.equals(GameMode.SPECTATOR)){ | |
sendGameUpdate(p, 3); | |
} | |
} | |
public void sendGameUpdate(Player p, int gameMode){ | |
try { | |
Constructor<?> gmConstruct= getNMSClass("PacketPlayOutGameStateChange").getConstructor(int.class, float.class); | |
Object packet = gmConstruct.newInstance(3, gameMode); | |
sendPacket(p, packet); | |
} catch (SecurityException | IllegalArgumentException | IllegalAccessException | InvocationTargetException | NoSuchMethodException | InstantiationException ex) { | |
} | |
} | |
public void sendPacket(Player p, Object packet) { | |
try { | |
Object handle = p.getClass().getMethod("getHandle").invoke(p); | |
Object playerConnection = handle.getClass().getField("playerConnection").get(handle); | |
playerConnection.getClass().getMethod("sendPacket", getNMSClass("Packet")).invoke(playerConnection, packet); | |
} | |
catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchFieldException ex) { | |
} | |
} | |
public Class<?> getNMSClass(String name) { | |
String version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3]; | |
try { | |
return Class.forName("net.minecraft.server." + version + "." + name); | |
} | |
catch (ClassNotFoundException ex) { | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment