<commit_before>package no.atc.floyd.bukkit.cc;


import multiworld.MultiWorldPlugin;
import multiworld.api.MultiWorldAPI;
import multiworld.api.MultiWorldWorldData;
import multiworld.api.flag.FlagName;

import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Egg;
import org.bukkit.entity.Entity;
import org.bukkit.entity.ExperienceOrb;
import org.bukkit.entity.Horse;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.ThrownPotion;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.EntityPortalEnterEvent;
import org.bukkit.event.entity.ExpBottleEvent;
import org.bukkit.event.entity.PotionSplashEvent;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryCreativeEvent;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.player.PlayerChangedWorldEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerEggThrowEvent;
import org.bukkit.event.player.PlayerEvent;
import org.bukkit.event.player.PlayerExpChangeEvent;
import org.bukkit.event.player.PlayerGameModeChangeEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerInventoryEvent;
import org.bukkit.event.player.PlayerItemConsumeEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.PluginManager;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import org.bukkit.block.Block;
import org.bukkit.command.*;

/**
* Limited creative mode plugin for Bukkit/Spigot
*
* @author FloydATC
*/
public class CreativeControl extends JavaPlugin implements Listener {
    //public static Permissions Permissions = null;
    private static MultiWorldPlugin multiworld = null;
    private static ConcurrentHashMap<World, Boolean> is_creative = new ConcurrentHashMap<World, Boolean>();  
    private static ConcurrentHashMap<String, GameMode> player_inventory = new ConcurrentHashMap<String, GameMode>();  
    
	public static final String MSG_PREFIX = ChatColor.GRAY + "[" + ChatColor.GOLD + "CC" + ChatColor.GRAY + "] ";
	public static final ChatColor COLOR_INFO = ChatColor.AQUA;
	public static final ChatColor COLOR_WARNING = ChatColor.RED;
	public static final ChatColor COLOR_NAME = ChatColor.GOLD;
    
    public void onDisable() {
    	// Save all inventories to disk -- important for server restarts etc.
    	for (Player p : Bukkit.getOnlinePlayers()) {
	    	save_inventory(p, p.getGameMode());
    	}
    	
    	multiworld = null;
    }

    public void onEnable() {
    	
    	// Connect with MultiWorld
    	// multiworld.MultiWorldPlugin cannot be cast to multiworld.api.MultiWorldAPI
    	multiworld = (MultiWorldPlugin) getServer().getPluginManager().getPlugin("MultiWorld");
    	if (multiworld == null) {
    		getLogger().warning("Integration with MultiWorld failed, some security features unavailable");
    	} else {
    		getLogger().warning("Integration with MultiWorld enabled");

    		// Collect creative flag for each world and store in hash 'is_creative'
    		MultiWorldAPI api = multiworld.getApi();
    		for (MultiWorldWorldData mwd : api.getWorlds()) {
    			World w = mwd.getBukkitWorld();
    			Boolean creative = mwd.getOptionValue(FlagName.CREATIVEWORLD);
    			is_creative.put(w, creative);
        		getLogger().warning("World '"+w.getName()+"' "+(creative?"is":"is not")+" creative");
    		}
    	}
    	multiworld = null;
    	
    	// Get current player states (we have to assume none of them are on the move)
    	for (Player p : Bukkit.getOnlinePlayers()) {
    		player_inventory.put(p.getName(), p.getGameMode());
    		// Save inventory to disk so we have most recent information
    		save_inventory(p, p.getGameMode());
    	}
    	
    	// Register event handlers
        PluginManager pm = getServer().getPluginManager();
        pm.registerEvents(this, this);
    }

    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args ) {
    	String cmdname = cmd.getName().toLowerCase();
        Player player = null;
        if (sender instanceof Player) {
        	player = (Player) sender;
            //getLogger().info("player="+player+" cmd="+cmdname+" args="+Arrays.toString(args));
        }
        
        // See if any options were specified
        //Boolean force = false;
        Integer options = numOptions(args);
        if (options > 0) {
            String[] revised_args = new String[args.length - options];
            Integer index = 0;
            for (String s: args) {
            	if (s.startsWith("--")) {
            		// Supported options go here
            		//if (s.equalsIgnoreCase("--force")) {
            		//	force = true;
            		//}
            	} else {
            		revised_args[index] = s;
            		index++;
            	}
            }
        	//getLogger().info("done revising argument list");
            args = revised_args;
        }
        

        if (cmdname.equalsIgnoreCase("gm") && (player == null || player.hasPermission("creativecontrol.gm"))) {
        	if (args.length == 1) {
        		if (player == null) {
        			respond(player, MSG_PREFIX+COLOR_WARNING+"Must specify player");
        			return true;
        		}
        		
        		GameMode gm = GameMode.SURVIVAL;
        		if (args[0].equalsIgnoreCase("1")) gm = GameMode.CREATIVE;
        		if (args[0].equalsIgnoreCase("2")) gm = GameMode.SURVIVAL;
        		if (player.getGameMode() == gm) {
        			respond(player, MSG_PREFIX+COLOR_WARNING+"Already in "+gm.name()+" mode");
        		} else {
    		        save_inventory(player, player.getGameMode());
    		        load_inventory(player, gm, "");
        			player.setGameMode(gm);
        			respond(player, MSG_PREFIX+"Switched to "+gm.name()+" mode");
        			getLogger().info(player.getName()+" manually switched to "+gm.name()+" mode");
        		}
        		return true;
        	}
        	if (args.length == 2 && (player == null || player.hasPermission("creativecontrol.gm.other"))) {
        		Player p = getServer().getPlayer(args[1]);
        		if (p == null) {
        			respond(player, MSG_PREFIX+COLOR_WARNING+"Unknown player '"+args[1]+"'");
        			return true;
        		} else {
            		GameMode gm = GameMode.SURVIVAL;
            		if (args[0].equalsIgnoreCase("1")) gm = GameMode.CREATIVE;
            		if (args[0].equalsIgnoreCase("2")) gm = GameMode.SURVIVAL;
            		if (p.getGameMode() == gm) {
            			respond(player, MSG_PREFIX+COLOR_NAME+p.getName()+COLOR_INFO+" is already in "+gm.name()+" mode");
            		} else {
        		        save_inventory(player, p.getGameMode());
        		        load_inventory(p, gm, "");
            			p.setGameMode(gm);
            			if (player.equals(p)) {
                			respond(player, MSG_PREFIX+"Switched to "+gm.name()+" mode");
            			} else {
            				respond(player, MSG_PREFIX+COLOR_NAME+p.getName()+COLOR_INFO+" switched to "+gm.name()+" mode");
            				respond(p, MSG_PREFIX+COLOR_NAME+player.getName()+COLOR_INFO+" switched you to "+gm.name()+" mode");
            			}
            		    getLogger().info(p.getName()+" switched to "+gm.name()+" mode by "+player.getName());
        		    }
            		return true;
        		}
        	}
        	return false;
        }

        if (cmdname.equalsIgnoreCase("saveinventory") && (player == null || player.hasPermission("creativecontrol.save"))) {
            if (args.length > 0) {
            	for (String pname : args) {
            		Player p = getServer().getPlayer(pname);
            		if (p != null) {
            			save_inventory(p, p.getGameMode());
                		respond(player, MSG_PREFIX+"Saved "+pname);
            		} else {
                		respond(player, MSG_PREFIX+COLOR_WARNING+"Unknown player '"+pname+"'");
            		}
            	}
        		return true;
            } else {
            	if (player != null) {
            	    save_inventory(player, player.getGameMode());
            		respond(player, MSG_PREFIX+"Saved");
            		return true;
            	} else {
            		respond(player, MSG_PREFIX+COLOR_NAME+"Please specify player name");
            		return false;
            	}
            }
        }
        if (cmdname.equalsIgnoreCase("loadinventory") && (player == null || player.hasPermission("creativecontrol.load"))) {
    		if (args.length == 0) {
    			if (player == null) {
    				return false;
    			} else {
    				if (load_inventory(player, player.getGameMode(), "")) {
                		respond(player, MSG_PREFIX+"Loaded");
    				} else {
                		respond(player, MSG_PREFIX+COLOR_WARNING+"Error loading");
    				}
            		return true;
    			}
    		}
        	if (args.length == 1) {
    			String pname = args[0];
    			Player p = getServer().getPlayer(pname);
    			if (p != null) {
    				if (load_inventory(p, p.getGameMode(), "")) {
    					respond(player, MSG_PREFIX+"Loaded "+pname);
    				} else {
    					respond(player, MSG_PREFIX+COLOR_WARNING+"Error loading "+COLOR_NAME+pname);
    				}
    				return true;
    			} else {
    				respond(player, MSG_PREFIX+COLOR_WARNING+"Unknown player '"+pname+"'");
    				return true;
    			}
    		}
    		if (args.length == 2) {
    			String pname = args[0];
    			Player p = getServer().getPlayer(pname);
    			if (!args[1].equalsIgnoreCase("CREATIVE") && !args[1].equalsIgnoreCase("SURVIVAL")) {
    				respond(player, MSG_PREFIX+COLOR_WARNING+"Must specify gamemode CREATIVE or SURVIVAL");
    				return true;
    			}
    			GameMode gm = GameMode.valueOf(args[1]);
    			if (p != null) {
    				if (load_inventory(p, gm, "")) {
    					respond(player, MSG_PREFIX+"Loaded "+COLOR_NAME+pname);
    				} else {
    					respond(player, MSG_PREFIX+COLOR_WARNING+"Error loading "+pname);
    				}
    				return true;
    			} else {
    				respond(player, MSG_PREFIX+COLOR_WARNING+"Unknown player '"+pname+"'");
    				return true;
    			}
    		}
    		if (args.length == 3) {
    			String pname = args[0];
    			Player p = getServer().getPlayer(pname);
    			if (!args[1].equalsIgnoreCase("CREATIVE") && !args[1].equalsIgnoreCase("SURVIVAL")) {
    				respond(player, MSG_PREFIX+COLOR_WARNING+"Must specify gamemode CREATIVE or SURVIVAL");
    				return true;
    			}
    			GameMode gm = GameMode.valueOf(args[1]);
    			if (p != null) {
    				if (load_inventory(p, gm, args[2])) {
    					respond(player, MSG_PREFIX+"Loaded "+args[2]+" as "+COLOR_NAME+pname);
    				} else {
    					respond(player, MSG_PREFIX+COLOR_WARNING+"Error loading "+args[2]+" as "+pname);
    				}
    				return true;
    			} else {
    				respond(player, MSG_PREFIX+COLOR_WARNING+"Unknown player '"+pname+"'");
    				return true;
    			}
    		}
        }
        if (cmdname.equalsIgnoreCase("listinventory") && player == null) {
        	if (args.length == 1) {
    			String pname = args[0];
    			Player p = getServer().getPlayer(pname);
    			if (p != null) {
    				if (!list_inventory(p, p.getGameMode())) {
    					respond(player, MSG_PREFIX+COLOR_WARNING+"Error listing inventory of "+pname);
    				}
    				return true;
    			} else {
    				respond(player, MSG_PREFIX+COLOR_WARNING+"Unknown player '"+pname+"'");
    				return true;
    			}
    		}
        }
        return false;
    }
    
    @EventHandler
    public void onPlayerChangedWorldEvent( PlayerChangedWorldEvent event ) {
    	Player p = event.getPlayer();
    	Location loc = p.getLocation();
    	World w = loc.getWorld();
    	getLogger().info("Player "+p.getName()+" changed to world '"+w.getName()+"'");
    	if (is_creative.get(w) != (p.getGameMode() == GameMode.CREATIVE)) {
    		getLogger().info("Player "+p.getName()+" has incorrect game mode");
    		update_gamemode(p, w);
    	}
    }
    
    @EventHandler
    public boolean onPlayerJoin( PlayerJoinEvent event ) {
        Player player = event.getPlayer();
        String to_world = event.getPlayer().getLocation().getWorld().getName().toLowerCase();
        getLogger().info("player "+player.getName()+" joined world '"+to_world+"'");
        
        // Override game mode as it was on logout
        if (is_creative.get(event.getPlayer().getLocation().getWorld())) {
        	load_inventory(player, GameMode.CREATIVE, "");
        	player.setGameMode(GameMode.CREATIVE);
        } else {
        	load_inventory(player, GameMode.SURVIVAL, "");
        	player.setGameMode(GameMode.SURVIVAL);
        }
        //update_gamemode(player, player.getWorld());
    	return true;
    }

    @EventHandler
    public void onInventoryOpenEvent( InventoryOpenEvent event ) {
    	if (event.getPlayer() instanceof Player == false) { return; } 
    	Player p = (Player) event.getPlayer();
    	Location loc = p.getLocation();
    	World w = loc.getWorld();
    	// Sanity check -- does the player gamemode match the world gamemode?
    	verify_gamemode(p, w, event);
    }

    @EventHandler
    public void onPlayerMoveEvent( PlayerMoveEvent event ) {
    	if (event.getPlayer() instanceof Player == false) { return; } 
    	Player p = event.getPlayer();
    	Location loc = p.getLocation();
    	World w = loc.getWorld();
    	// Sanity check -- does the player gamemode match the world gamemode?
    	verify_gamemode(p, w, event);
    }

    @EventHandler
    public boolean onQuit( PlayerQuitEvent event ) {
    	// Save and unload locations from memory
        save_inventory(event.getPlayer(), event.getPlayer().getGameMode());
    	return true;
    }

    @EventHandler
    public void onPlayerGameModeChange( PlayerGameModeChangeEvent event ) {
    	Player p = event.getPlayer();
    	GameMode old_mode = p.getGameMode();
        GameMode new_mode = event.getNewGameMode();
        if (allow_gm_change(p, old_mode, new_mode)) {
        	getLogger().info("Player '"+p.getName()+"' in world '"+p.getWorld().getName()+"' is changing game mode from "+old_mode.name()+" to "+new_mode.name());
        	respond(p, MSG_PREFIX+"Entering "+new_mode.name()+" mode");
        	return;
        } else {
        	getLogger().info("Player '"+p.getName()+"' in world '"+p.getWorld().getName()+"' tried to change game mode from "+old_mode.name()+" to "+new_mode.name());
        	event.setCancelled(true);
        }
        return;
    }

    @EventHandler(priority = EventPriority.MONITOR) // Must process after TelePlugin
    public void onTeleport( PlayerTeleportEvent event ) {
    	if (event.isCancelled()) {
            Player p = event.getPlayer();
        	Location loc = p.getLocation();
        	World w = loc.getWorld();
        	// Sanity check -- does the player gamemode match the world gamemode?
        	verify_gamemode(p, w, event);
    		return ;
    	}
        Player player = event.getPlayer();
    	String from_world = event.getFrom().getWorld().getName().toLowerCase();
    	World w = event.getTo().getWorld();
    	String to_world = event.getTo().getWorld().getName().toLowerCase();
    	if (!from_world.equals(to_world)) {
            getLogger().info("Player "+player.getName()+" is teleporting from world '"+from_world+"' to '"+to_world+"'");
        	respond(player, MSG_PREFIX+"Teleporting to world '"+w.getName()+"'");
            update_gamemode(player, w);
    	}
    }    

    @EventHandler
    public void onEntityDeath(EntityDeathEvent event) {
    	if (event.getEntity() instanceof Player) {<commit_msg>Reordered some event handlers. Purely cosmetic.<commit_after>package no.atc.floyd.bukkit.cc;


import multiworld.MultiWorldPlugin;
import multiworld.api.MultiWorldAPI;
import multiworld.api.MultiWorldWorldData;
import multiworld.api.flag.FlagName;

import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Egg;
import org.bukkit.entity.Entity;
import org.bukkit.entity.ExperienceOrb;
import org.bukkit.entity.Horse;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.ThrownPotion;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.EntityPortalEnterEvent;
import org.bukkit.event.entity.ExpBottleEvent;
import org.bukkit.event.entity.PotionSplashEvent;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryCreativeEvent;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.player.PlayerChangedWorldEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerEggThrowEvent;
import org.bukkit.event.player.PlayerEvent;
import org.bukkit.event.player.PlayerExpChangeEvent;
import org.bukkit.event.player.PlayerGameModeChangeEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerInventoryEvent;
import org.bukkit.event.player.PlayerItemConsumeEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.PluginManager;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import org.bukkit.block.Block;
import org.bukkit.command.*;

/**
* Limited creative mode plugin for Bukkit/Spigot
*
* @author FloydATC
*/
public class CreativeControl extends JavaPlugin implements Listener {
    //public static Permissions Permissions = null;
    private static MultiWorldPlugin multiworld = null;
    private static ConcurrentHashMap<World, Boolean> is_creative = new ConcurrentHashMap<World, Boolean>();  
    private static ConcurrentHashMap<String, GameMode> player_inventory = new ConcurrentHashMap<String, GameMode>();  
    
	public static final String MSG_PREFIX = ChatColor.GRAY + "[" + ChatColor.GOLD + "CC" + ChatColor.GRAY + "] ";
	public static final ChatColor COLOR_INFO = ChatColor.AQUA;
	public static final ChatColor COLOR_WARNING = ChatColor.RED;
	public static final ChatColor COLOR_NAME = ChatColor.GOLD;
    
    public void onDisable() {
    	// Save all inventories to disk -- important for server restarts etc.
    	for (Player p : Bukkit.getOnlinePlayers()) {
	    	save_inventory(p, p.getGameMode());
    	}
    	
    	multiworld = null;
    }

    public void onEnable() {
    	
    	// Connect with MultiWorld
    	// multiworld.MultiWorldPlugin cannot be cast to multiworld.api.MultiWorldAPI
    	multiworld = (MultiWorldPlugin) getServer().getPluginManager().getPlugin("MultiWorld");
    	if (multiworld == null) {
    		getLogger().warning("Integration with MultiWorld failed, some security features unavailable");
    	} else {
    		getLogger().warning("Integration with MultiWorld enabled");

    		// Collect creative flag for each world and store in hash 'is_creative'
    		MultiWorldAPI api = multiworld.getApi();
    		for (MultiWorldWorldData mwd : api.getWorlds()) {
    			World w = mwd.getBukkitWorld();
    			Boolean creative = mwd.getOptionValue(FlagName.CREATIVEWORLD);
    			is_creative.put(w, creative);
        		getLogger().warning("World '"+w.getName()+"' "+(creative?"is":"is not")+" creative");
    		}
    	}
    	multiworld = null;
    	
    	// Get current player states (we have to assume none of them are on the move)
    	for (Player p : Bukkit.getOnlinePlayers()) {
    		player_inventory.put(p.getName(), p.getGameMode());
    		// Save inventory to disk so we have most recent information
    		save_inventory(p, p.getGameMode());
    	}
    	
    	// Register event handlers
        PluginManager pm = getServer().getPluginManager();
        pm.registerEvents(this, this);
    }

    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args ) {
    	String cmdname = cmd.getName().toLowerCase();
        Player player = null;
        if (sender instanceof Player) {
        	player = (Player) sender;
            //getLogger().info("player="+player+" cmd="+cmdname+" args="+Arrays.toString(args));
        }
        
        // See if any options were specified
        //Boolean force = false;
        Integer options = numOptions(args);
        if (options > 0) {
            String[] revised_args = new String[args.length - options];
            Integer index = 0;
            for (String s: args) {
            	if (s.startsWith("--")) {
            		// Supported options go here
            		//if (s.equalsIgnoreCase("--force")) {
            		//	force = true;
            		//}
            	} else {
            		revised_args[index] = s;
            		index++;
            	}
            }
        	//getLogger().info("done revising argument list");
            args = revised_args;
        }
        

        if (cmdname.equalsIgnoreCase("gm") && (player == null || player.hasPermission("creativecontrol.gm"))) {
        	if (args.length == 1) {
        		if (player == null) {
        			respond(player, MSG_PREFIX+COLOR_WARNING+"Must specify player");
        			return true;
        		}
        		
        		GameMode gm = GameMode.SURVIVAL;
        		if (args[0].equalsIgnoreCase("1")) gm = GameMode.CREATIVE;
        		if (args[0].equalsIgnoreCase("2")) gm = GameMode.SURVIVAL;
        		if (player.getGameMode() == gm) {
        			respond(player, MSG_PREFIX+COLOR_WARNING+"Already in "+gm.name()+" mode");
        		} else {
    		        save_inventory(player, player.getGameMode());
    		        load_inventory(player, gm, "");
        			player.setGameMode(gm);
        			respond(player, MSG_PREFIX+"Switched to "+gm.name()+" mode");
        			getLogger().info(player.getName()+" manually switched to "+gm.name()+" mode");
        		}
        		return true;
        	}
        	if (args.length == 2 && (player == null || player.hasPermission("creativecontrol.gm.other"))) {
        		Player p = getServer().getPlayer(args[1]);
        		if (p == null) {
        			respond(player, MSG_PREFIX+COLOR_WARNING+"Unknown player '"+args[1]+"'");
        			return true;
        		} else {
            		GameMode gm = GameMode.SURVIVAL;
            		if (args[0].equalsIgnoreCase("1")) gm = GameMode.CREATIVE;
            		if (args[0].equalsIgnoreCase("2")) gm = GameMode.SURVIVAL;
            		if (p.getGameMode() == gm) {
            			respond(player, MSG_PREFIX+COLOR_NAME+p.getName()+COLOR_INFO+" is already in "+gm.name()+" mode");
            		} else {
        		        save_inventory(player, p.getGameMode());
        		        load_inventory(p, gm, "");
            			p.setGameMode(gm);
            			if (player.equals(p)) {
                			respond(player, MSG_PREFIX+"Switched to "+gm.name()+" mode");
            			} else {
            				respond(player, MSG_PREFIX+COLOR_NAME+p.getName()+COLOR_INFO+" switched to "+gm.name()+" mode");
            				respond(p, MSG_PREFIX+COLOR_NAME+player.getName()+COLOR_INFO+" switched you to "+gm.name()+" mode");
            			}
            		    getLogger().info(p.getName()+" switched to "+gm.name()+" mode by "+player.getName());
        		    }
            		return true;
        		}
        	}
        	return false;
        }

        if (cmdname.equalsIgnoreCase("saveinventory") && (player == null || player.hasPermission("creativecontrol.save"))) {
            if (args.length > 0) {
            	for (String pname : args) {
            		Player p = getServer().getPlayer(pname);
            		if (p != null) {
            			save_inventory(p, p.getGameMode());
                		respond(player, MSG_PREFIX+"Saved "+pname);
            		} else {
                		respond(player, MSG_PREFIX+COLOR_WARNING+"Unknown player '"+pname+"'");
            		}
            	}
        		return true;
            } else {
            	if (player != null) {
            	    save_inventory(player, player.getGameMode());
            		respond(player, MSG_PREFIX+"Saved");
            		return true;
            	} else {
            		respond(player, MSG_PREFIX+COLOR_NAME+"Please specify player name");
            		return false;
            	}
            }
        }
        if (cmdname.equalsIgnoreCase("loadinventory") && (player == null || player.hasPermission("creativecontrol.load"))) {
    		if (args.length == 0) {
    			if (player == null) {
    				return false;
    			} else {
    				if (load_inventory(player, player.getGameMode(), "")) {
                		respond(player, MSG_PREFIX+"Loaded");
    				} else {
                		respond(player, MSG_PREFIX+COLOR_WARNING+"Error loading");
    				}
            		return true;
    			}
    		}
        	if (args.length == 1) {
    			String pname = args[0];
    			Player p = getServer().getPlayer(pname);
    			if (p != null) {
    				if (load_inventory(p, p.getGameMode(), "")) {
    					respond(player, MSG_PREFIX+"Loaded "+pname);
    				} else {
    					respond(player, MSG_PREFIX+COLOR_WARNING+"Error loading "+COLOR_NAME+pname);
    				}
    				return true;
    			} else {
    				respond(player, MSG_PREFIX+COLOR_WARNING+"Unknown player '"+pname+"'");
    				return true;
    			}
    		}
    		if (args.length == 2) {
    			String pname = args[0];
    			Player p = getServer().getPlayer(pname);
    			if (!args[1].equalsIgnoreCase("CREATIVE") && !args[1].equalsIgnoreCase("SURVIVAL")) {
    				respond(player, MSG_PREFIX+COLOR_WARNING+"Must specify gamemode CREATIVE or SURVIVAL");
    				return true;
    			}
    			GameMode gm = GameMode.valueOf(args[1]);
    			if (p != null) {
    				if (load_inventory(p, gm, "")) {
    					respond(player, MSG_PREFIX+"Loaded "+COLOR_NAME+pname);
    				} else {
    					respond(player, MSG_PREFIX+COLOR_WARNING+"Error loading "+pname);
    				}
    				return true;
    			} else {
    				respond(player, MSG_PREFIX+COLOR_WARNING+"Unknown player '"+pname+"'");
    				return true;
    			}
    		}
    		if (args.length == 3) {
    			String pname = args[0];
    			Player p = getServer().getPlayer(pname);
    			if (!args[1].equalsIgnoreCase("CREATIVE") && !args[1].equalsIgnoreCase("SURVIVAL")) {
    				respond(player, MSG_PREFIX+COLOR_WARNING+"Must specify gamemode CREATIVE or SURVIVAL");
    				return true;
    			}
    			GameMode gm = GameMode.valueOf(args[1]);
    			if (p != null) {
    				if (load_inventory(p, gm, args[2])) {
    					respond(player, MSG_PREFIX+"Loaded "+args[2]+" as "+COLOR_NAME+pname);
    				} else {
    					respond(player, MSG_PREFIX+COLOR_WARNING+"Error loading "+args[2]+" as "+pname);
    				}
    				return true;
    			} else {
    				respond(player, MSG_PREFIX+COLOR_WARNING+"Unknown player '"+pname+"'");
    				return true;
    			}
    		}
        }
        if (cmdname.equalsIgnoreCase("listinventory") && player == null) {
        	if (args.length == 1) {
    			String pname = args[0];
    			Player p = getServer().getPlayer(pname);
    			if (p != null) {
    				if (!list_inventory(p, p.getGameMode())) {
    					respond(player, MSG_PREFIX+COLOR_WARNING+"Error listing inventory of "+pname);
    				}
    				return true;
    			} else {
    				respond(player, MSG_PREFIX+COLOR_WARNING+"Unknown player '"+pname+"'");
    				return true;
    			}
    		}
        }
        return false;
    }
    
    // So tired of looking for this:
    // http://jd.bukkit.org/org/bukkit/event/player/PlayerEvent.html
    
    @EventHandler
    public void onPlayerChangedWorldEvent( PlayerChangedWorldEvent event ) {
    	Player p = event.getPlayer();
    	Location loc = p.getLocation();
    	World w = loc.getWorld();
    	getLogger().info("Player "+p.getName()+" changed to world '"+w.getName()+"'");
    	if (is_creative.get(w) != (p.getGameMode() == GameMode.CREATIVE)) {
    		getLogger().info("Player "+p.getName()+" has incorrect game mode");
    		update_gamemode(p, w);
    	}
    }
    
    @EventHandler
    public boolean onPlayerJoin( PlayerJoinEvent event ) {
        Player player = event.getPlayer();
        String to_world = event.getPlayer().getLocation().getWorld().getName().toLowerCase();
        getLogger().info("player "+player.getName()+" joined world '"+to_world+"'");
        
        // Override game mode as it was on logout
        if (is_creative.get(event.getPlayer().getLocation().getWorld())) {
        	load_inventory(player, GameMode.CREATIVE, "");
        	player.setGameMode(GameMode.CREATIVE);
        } else {
        	load_inventory(player, GameMode.SURVIVAL, "");
        	player.setGameMode(GameMode.SURVIVAL);
        }
        //update_gamemode(player, player.getWorld());
    	return true;
    }

    @EventHandler
    public boolean onQuit( PlayerQuitEvent event ) {
    	// Save and unload locations from memory
        save_inventory(event.getPlayer(), event.getPlayer().getGameMode());
    	return true;
    }

    @EventHandler
    public void onPlayerGameModeChange( PlayerGameModeChangeEvent event ) {
    	Player p = event.getPlayer();
    	GameMode old_mode = p.getGameMode();
        GameMode new_mode = event.getNewGameMode();
        if (allow_gm_change(p, old_mode, new_mode)) {
        	getLogger().info("Player '"+p.getName()+"' in world '"+p.getWorld().getName()+"' is changing game mode from "+old_mode.name()+" to "+new_mode.name());
        	respond(p, MSG_PREFIX+"Entering "+new_mode.name()+" mode");
        	return;
        } else {
        	getLogger().info("Player '"+p.getName()+"' in world '"+p.getWorld().getName()+"' tried to change game mode from "+old_mode.name()+" to "+new_mode.name());
        	event.setCancelled(true);
        }
        return;
    }

    @EventHandler(priority = EventPriority.MONITOR) // Must process after TelePlugin
    public void onTeleport( PlayerTeleportEvent event ) {
    	if (event.isCancelled()) {
            Player p = event.getPlayer();
        	Location loc = p.getLocation();
        	World w = loc.getWorld();
        	// Sanity check -- does the player gamemode match the world gamemode?
        	verify_gamemode(p, w, event);
    		return ;
    	}
        Player player = event.getPlayer();
    	String from_world = event.getFrom().getWorld().getName().toLowerCase();
    	World w = event.getTo().getWorld();
    	String to_world = event.getTo().getWorld().getName().toLowerCase();
    	if (!from_world.equals(to_world)) {
            getLogger().info("Player "+player.getName()+" is teleporting from world '"+from_world+"' to '"+to_world+"'");
        	respond(player, MSG_PREFIX+"Teleporting to world '"+w.getName()+"'");
            update_gamemode(player, w);
    	}
    }    
    
    
    @EventHandler
    public void onInventoryOpenEvent( InventoryOpenEvent event ) {
    	if (event.getPlayer() instanceof Player == false) { return; } 
    	Player p = (Player) event.getPlayer();
    	Location loc = p.getLocation();
    	World w = loc.getWorld();
    	// Sanity check -- does the player gamemode match the world gamemode?
    	verify_gamemode(p, w, event);
    }

    @EventHandler
    public void onPlayerMoveEvent( PlayerMoveEvent event ) {
    	if (event.getPlayer() instanceof Player == false) { return; } 
    	Player p = event.getPlayer();
    	Location loc = p.getLocation();
    	World w = loc.getWorld();
    	// Sanity check -- does the player gamemode match the world gamemode?
    	verify_gamemode(p, w, event);
    }


    @EventHandler
    public void onEntityDeath(EntityDeathEvent event) {
    	if (event.getEntity() instanceof Player) {
{'fraction_non_alphanumeric': 0.12990203421312801, 'fraction_numerical': 0.0019772911111776864, 'mean_word_length': 2.6449006333260536, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1077867', 'n_tokens_mistral': 10997, 'n_tokens_neox': 10567, 'n_words': 2124}