<commit_before>package com.gmail.stefvanschiedev.buildinggame.utils.plot;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.WeatherType;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;

import com.gmail.stefvanschiedev.buildinggame.Main;
import com.gmail.stefvanschiedev.buildinggame.managers.arenas.ArenaManager;
import com.gmail.stefvanschiedev.buildinggame.managers.files.SettingsManager;
import com.gmail.stefvanschiedev.buildinggame.managers.id.IDDecompiler;
import com.gmail.stefvanschiedev.buildinggame.managers.mainspawn.MainSpawnManager;
import com.gmail.stefvanschiedev.buildinggame.managers.messages.MessageManager;
import com.gmail.stefvanschiedev.buildinggame.utils.GameState;
import com.gmail.stefvanschiedev.buildinggame.utils.ItemBuilder;
import com.gmail.stefvanschiedev.buildinggame.utils.Time;
import com.gmail.stefvanschiedev.buildinggame.utils.Vote;
import com.gmail.stefvanschiedev.buildinggame.utils.arena.Arena;
import com.gmail.stefvanschiedev.buildinggame.utils.arena.ArenaMode;
import com.gmail.stefvanschiedev.buildinggame.utils.gameplayer.GamePlayer;
import com.gmail.stefvanschiedev.buildinggame.utils.gameplayer.GamePlayerType;
import com.gmail.stefvanschiedev.buildinggame.utils.guis.buildmenu.BuildMenu;
import com.gmail.stefvanschiedev.buildinggame.utils.guis.spectatormenu.SpectatorMenu;
import com.gmail.stefvanschiedev.buildinggame.utils.nbt.entity.NbtFactory;
import com.gmail.stefvanschiedev.buildinggame.utils.nbt.entity.NmsClasses;
import com.gmail.stefvanschiedev.buildinggame.utils.nbt.entity.NbtFactory.NbtCompound;
import com.gmail.stefvanschiedev.buildinggame.utils.particle.Particle;

public class Plot {

	private Arena arena;
	private boolean raining = false;
	private Boundary boundary;
	private Floor floor;
	private final int ID;
	private final List<GamePlayer> gamePlayers = new ArrayList<>();
	private final List<BlockState> blocks = new ArrayList<>();
	private final Map<Entity, Location> entities;
	private final List<Vote> votes = new ArrayList<>();
	private final List<Particle> particles = new ArrayList<>();
	private Location location;
	private final Map<Player, Integer> timesVoted = new HashMap<>();
	private Time time = Time.AM6;
	private final BuildMenu buildMenu;
	
	public Plot(int ID) {
		this.ID = ID;
		
		this.buildMenu = new BuildMenu();
		this.entities = new HashMap<>();
	}
	
	public boolean addEntity(Entity entity) {
		YamlConfiguration config = SettingsManager.getInstance().getConfig();
		
		if (getArena().getState() == GameState.WAITING || getArena().getState() == GameState.STARTING)
			return false;
		
		if (!config.getBoolean("mobs.allow"))
			return false;
		
		if (config.getStringList("blocked-entities").contains(entity.getType().toString().toLowerCase()))
			return false;
		
		if (config.getBoolean("mobs.enable-noai")) {
			NbtCompound nbt = NbtFactory.createCompound();
			nbt.put("NoAI", 1);
			NmsClasses.setTag(entity, nbt.getHandle());
		}
		
		entities.put(entity, entity.getLocation());
		return true;
	}
	
	public void addParticle(Particle particle, Player player) {
		YamlConfiguration config = SettingsManager.getInstance().getConfig();
		YamlConfiguration messages = SettingsManager.getInstance().getMessages();
		
		if (getParticles().size() != config.getInt("max-particles"))
			particles.add(particle);
		else
			MessageManager.getInstance().send(player, messages.getStringList("particle.max-particles"));
	}
	
	public void addSpectator(final Player spectator, GamePlayer spectates) {
		YamlConfiguration config = SettingsManager.getInstance().getConfig();
		YamlConfiguration messages = SettingsManager.getInstance().getMessages();
		
		final GamePlayer gamePlayer = new GamePlayer(spectator, GamePlayerType.SPECTATOR);
		gamePlayer.setSpectates(spectates);
		
		getAllGamePlayers().add(gamePlayer);
		
		for (GamePlayer player : getAllGamePlayers())
			player.getPlayer().hidePlayer(spectator);
		
		spectator.getInventory().setItem(config.getInt("leave-item.slot"), IDDecompiler.getInstance().decompile(spectator, config.getString("leave-item.id")).setDisplayName(MessageManager.translate(messages.getString("leave-item.name"))).setClickEvent(event -> {
            gamePlayer.connect(MainSpawnManager.getInstance().getServer(), MainSpawnManager.getInstance().getMainSpawn());
            removeSpectator(gamePlayer);
            MessageManager.getInstance().send(spectator, ChatColor.GREEN + "Stopped spectating");
            return true;
        }));
		spectator.getInventory().setItem(8, new ItemBuilder(spectator, Material.EMERALD).setDisplayName(ChatColor.GREEN + "Spectator menu").setClickEvent(event -> {
            new SpectatorMenu().open(spectator);
            return true;
        }));
		
		spectator.teleport(spectates.getPlayer().getLocation());
		spectator.setGameMode(GameMode.CREATIVE);
		spectator.setCanPickupItems(false);
	}
	
	public void addVote(Vote vote) {
		YamlConfiguration config = SettingsManager.getInstance().getConfig();
		YamlConfiguration messages = SettingsManager.getInstance().getMessages();
		
		if (getArena().getState() != GameState.VOTING) {
			return;
		}
		
		for (GamePlayer gamePlayer : getGamePlayers()) {
			if (vote.getSender().getName().equals("stefvanschie")) {
				break;
			} else {
				if (gamePlayer.getPlayer() == vote.getSender()) {
					MessageManager.getInstance().send(vote.getSender(), messages.getStringList("vote.own-plot"));
					return;
				}
			}
		}
		
		//check how many times voted
		if (getTimesVoted(vote.getSender()) == config.getInt("max-vote-change")) {
			for (String message : messages.getStringList("vote.maximum-votes")) {
				MessageManager.getInstance().send(vote.getSender(), message
						.replace("%max_votes%", config.getInt("max-votes-change") + ""));
			}
			return;
		}
		
		getTimesVoted().put(vote.getSender(), getTimesVoted(vote.getSender()) + 1);
		
		for (String message : messages.getStringList("vote.message")) {
			MessageManager.getInstance().send(vote.getSender(), message
					.replace("%playerplot%", getArena().getVotingPlot().getPlayerFormat())
					.replace("%points%", vote.getPoints() + ""));
		}
		
		for (String message : messages.getStringList("vote.receiver")) {
			for (GamePlayer player : getArena().getVotingPlot().getGamePlayers())
				MessageManager.getInstance().send(player.getPlayer(), message
						.replace("%points%", vote.getPoints() + "")
						.replace("%sender%", vote.getSender().getName()));
		}
		
		for (GamePlayer player : ArenaManager.getInstance().getArena(vote.getSender()).getPlot(vote.getSender()).getGamePlayers()) {
			player.addTitleAndSubtitle(messages.getString("vote.title")
					.replace("%points%", vote.getPoints() + ""), messages.getString("vote.subtitle")
					.replace("%points%", vote.getPoints() + ""));
		}
		
		if (hasVoted(vote.getSender())) {
			getVotes().remove(getVote(vote.getSender()));
		}
		
		votes.add(vote);
		
		if (!config.getBoolean("scoreboards.vote.text"))
			arena.getVoteScoreboard().setScore(getPlayerFormat(), getPoints());
		
		if (!config.getBoolean("names-after-voting") && config.getBoolean("scoreboards.vote.enable")) {
			for (Plot p : arena.getPlots()) {
				if (!p.getGamePlayers().isEmpty()) {
					for (GamePlayer player : getGamePlayers()) {
						arena.getVoteScoreboard().show(player.getPlayer());
					}
				}
			}
		}
	}
	
	public List<GamePlayer> getAllGamePlayers() {
		return gamePlayers;
	}
	
	private Arena getArena() {
		return arena;
	}
	
	public Boundary getBoundary() {
		return boundary;
	}
	
	public BuildMenu getBuildMenu() {<commit_msg>Fix NPE when fetching arena from sender of a vote<commit_after>package com.gmail.stefvanschiedev.buildinggame.utils.plot;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.WeatherType;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;

import com.gmail.stefvanschiedev.buildinggame.Main;
import com.gmail.stefvanschiedev.buildinggame.managers.arenas.ArenaManager;
import com.gmail.stefvanschiedev.buildinggame.managers.files.SettingsManager;
import com.gmail.stefvanschiedev.buildinggame.managers.id.IDDecompiler;
import com.gmail.stefvanschiedev.buildinggame.managers.mainspawn.MainSpawnManager;
import com.gmail.stefvanschiedev.buildinggame.managers.messages.MessageManager;
import com.gmail.stefvanschiedev.buildinggame.utils.GameState;
import com.gmail.stefvanschiedev.buildinggame.utils.ItemBuilder;
import com.gmail.stefvanschiedev.buildinggame.utils.Time;
import com.gmail.stefvanschiedev.buildinggame.utils.Vote;
import com.gmail.stefvanschiedev.buildinggame.utils.arena.Arena;
import com.gmail.stefvanschiedev.buildinggame.utils.arena.ArenaMode;
import com.gmail.stefvanschiedev.buildinggame.utils.gameplayer.GamePlayer;
import com.gmail.stefvanschiedev.buildinggame.utils.gameplayer.GamePlayerType;
import com.gmail.stefvanschiedev.buildinggame.utils.guis.buildmenu.BuildMenu;
import com.gmail.stefvanschiedev.buildinggame.utils.guis.spectatormenu.SpectatorMenu;
import com.gmail.stefvanschiedev.buildinggame.utils.nbt.entity.NbtFactory;
import com.gmail.stefvanschiedev.buildinggame.utils.nbt.entity.NmsClasses;
import com.gmail.stefvanschiedev.buildinggame.utils.nbt.entity.NbtFactory.NbtCompound;
import com.gmail.stefvanschiedev.buildinggame.utils.particle.Particle;

public class Plot {

	private Arena arena;
	private boolean raining = false;
	private Boundary boundary;
	private Floor floor;
	private final int ID;
	private final List<GamePlayer> gamePlayers = new ArrayList<>();
	private final List<BlockState> blocks = new ArrayList<>();
	private final Map<Entity, Location> entities;
	private final List<Vote> votes = new ArrayList<>();
	private final List<Particle> particles = new ArrayList<>();
	private Location location;
	private final Map<Player, Integer> timesVoted = new HashMap<>();
	private Time time = Time.AM6;
	private final BuildMenu buildMenu;
	
	public Plot(int ID) {
		this.ID = ID;
		
		this.buildMenu = new BuildMenu();
		this.entities = new HashMap<>();
	}
	
	public boolean addEntity(Entity entity) {
		YamlConfiguration config = SettingsManager.getInstance().getConfig();
		
		if (getArena().getState() == GameState.WAITING || getArena().getState() == GameState.STARTING)
			return false;
		
		if (!config.getBoolean("mobs.allow"))
			return false;
		
		if (config.getStringList("blocked-entities").contains(entity.getType().toString().toLowerCase()))
			return false;
		
		if (config.getBoolean("mobs.enable-noai")) {
			NbtCompound nbt = NbtFactory.createCompound();
			nbt.put("NoAI", 1);
			NmsClasses.setTag(entity, nbt.getHandle());
		}
		
		entities.put(entity, entity.getLocation());
		return true;
	}
	
	public void addParticle(Particle particle, Player player) {
		YamlConfiguration config = SettingsManager.getInstance().getConfig();
		YamlConfiguration messages = SettingsManager.getInstance().getMessages();
		
		if (getParticles().size() != config.getInt("max-particles"))
			particles.add(particle);
		else
			MessageManager.getInstance().send(player, messages.getStringList("particle.max-particles"));
	}
	
	public void addSpectator(final Player spectator, GamePlayer spectates) {
		YamlConfiguration config = SettingsManager.getInstance().getConfig();
		YamlConfiguration messages = SettingsManager.getInstance().getMessages();
		
		final GamePlayer gamePlayer = new GamePlayer(spectator, GamePlayerType.SPECTATOR);
		gamePlayer.setSpectates(spectates);
		
		getAllGamePlayers().add(gamePlayer);
		
		for (GamePlayer player : getAllGamePlayers())
			player.getPlayer().hidePlayer(spectator);
		
		spectator.getInventory().setItem(config.getInt("leave-item.slot"), IDDecompiler.getInstance().decompile(spectator, config.getString("leave-item.id")).setDisplayName(MessageManager.translate(messages.getString("leave-item.name"))).setClickEvent(event -> {
            gamePlayer.connect(MainSpawnManager.getInstance().getServer(), MainSpawnManager.getInstance().getMainSpawn());
            removeSpectator(gamePlayer);
            MessageManager.getInstance().send(spectator, ChatColor.GREEN + "Stopped spectating");
            return true;
        }));
		spectator.getInventory().setItem(8, new ItemBuilder(spectator, Material.EMERALD).setDisplayName(ChatColor.GREEN + "Spectator menu").setClickEvent(event -> {
            new SpectatorMenu().open(spectator);
            return true;
        }));
		
		spectator.teleport(spectates.getPlayer().getLocation());
		spectator.setGameMode(GameMode.CREATIVE);
		spectator.setCanPickupItems(false);
	}
	
	public void addVote(Vote vote) {
		YamlConfiguration config = SettingsManager.getInstance().getConfig();
		YamlConfiguration messages = SettingsManager.getInstance().getMessages();
		
		if (getArena().getState() != GameState.VOTING)
			return;
		
		for (GamePlayer gamePlayer : getGamePlayers()) {
			if (vote.getSender().getName().equals("stefvanschie"))
				break;
			else {
				if (gamePlayer.getPlayer() == vote.getSender()) {
					MessageManager.getInstance().send(vote.getSender(), messages.getStringList("vote.own-plot"));
					return;
				}
			}
		}
		
		//check how many times voted
		if (getTimesVoted(vote.getSender()) == config.getInt("max-vote-change")) {
			for (String message : messages.getStringList("vote.maximum-votes"))
				MessageManager.getInstance().send(vote.getSender(), message
						.replace("%max_votes%", config.getInt("max-votes-change") + ""));

			return;
		}
		
		getTimesVoted().put(vote.getSender(), getTimesVoted(vote.getSender()) + 1);
		
		for (String message : messages.getStringList("vote.message"))
			MessageManager.getInstance().send(vote.getSender(), message
					.replace("%playerplot%", getArena().getVotingPlot().getPlayerFormat())
					.replace("%points%", vote.getPoints() + ""));
		
		for (String message : messages.getStringList("vote.receiver")) {
			for (GamePlayer player : getArena().getVotingPlot().getGamePlayers())
				MessageManager.getInstance().send(player.getPlayer(), message
						.replace("%points%", vote.getPoints() + "")
						.replace("%sender%", vote.getSender().getName()));
		}

		Arena senderArena = ArenaManager.getInstance().getArena(vote.getSender());

		if (senderArena != null) {
            for (GamePlayer player : senderArena.getPlot(vote.getSender()).getGamePlayers())
                player.addTitleAndSubtitle(messages.getString("vote.title")
                        .replace("%points%", vote.getPoints() + ""), messages.getString("vote.subtitle")
                        .replace("%points%", vote.getPoints() + ""));
        }
		
		if (hasVoted(vote.getSender()))
			getVotes().remove(getVote(vote.getSender()));
		
		votes.add(vote);
		
		if (!config.getBoolean("scoreboards.vote.text"))
			arena.getVoteScoreboard().setScore(getPlayerFormat(), getPoints());
		
		if (!config.getBoolean("names-after-voting") && config.getBoolean("scoreboards.vote.enable")) {
			for (Plot p : arena.getPlots()) {
				if (!p.getGamePlayers().isEmpty()) {
					for (GamePlayer player : getGamePlayers())
						arena.getVoteScoreboard().show(player.getPlayer());
				}
			}
		}
	}
	
	public List<GamePlayer> getAllGamePlayers() {
		return gamePlayers;
	}
	
	private Arena getArena() {
		return arena;
	}
	
	public Boundary getBoundary() {
		return boundary;
	}
	
	public BuildMenu getBuildMenu() {
{'fraction_non_alphanumeric': 0.1365958251876143, 'fraction_numerical': 0.0005045090496310778, 'mean_word_length': 4.981893625047152, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 33, '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': '5398480', 'n_tokens_mistral': 5286, 'n_tokens_neox': 4785, 'n_words': 765}