Skip to content

Commit

Permalink
npc stuff works. Made a lot of quality of life changes as well.
Browse files Browse the repository at this point in the history
  • Loading branch information
its-c10 committed Feb 27, 2022
1 parent 329f2c7 commit 6504d4d
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 42 deletions.
2 changes: 2 additions & 0 deletions src/main/java/net/dohaw/ironcraft/IronCraftCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
sender.sendMessage(StringUtils.colorString("&cThis is not a valid objective!"));
return false;
}
targetPlayer.sendMessage("Your objective has been set to " + objective);
targetPlayerData.setCurrentTutorialObjective(objective);
}else{
boolean isInGame;
Expand All @@ -78,6 +79,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
sender.sendMessage(StringUtils.colorString("&cThis is not a valid argument! It must be \"true\" or \"false\""));
return false;
}
targetPlayer.sendMessage("You have been moved to the game stage");
targetPlayerData.setInTutorial(isInGame);
targetPlayerData.initWorker(plugin);
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/net/dohaw/ironcraft/IronCraftPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.comphenix.protocol.events.ListenerPriority;
import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketEvent;
import net.citizensnpcs.npc.CitizensNPC;
import net.dohaw.corelib.CoreLib;
import net.dohaw.corelib.JPUtils;
import net.dohaw.corelib.StringUtils;
Expand Down Expand Up @@ -109,9 +110,19 @@ public void onEnable() {

@Override
public void onDisable() {

for(PlayerData pd : playerDataHandler.getPlayerDataList()){
CitizensNPC npc = pd.getManagerNPC();
if(npc != null){
npc.despawn();
npc.destroy();
}
}

baseConfig.saveChamberLocations(availableChamberLocations);
baseConfig.saveSpawnLocations(journeySpawnPoints);
playerDataHandler.saveAllData();

}

private void formPacketListeners() {
Expand Down
32 changes: 19 additions & 13 deletions src/main/java/net/dohaw/ironcraft/PlayerData.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package net.dohaw.ironcraft;

import net.citizensnpcs.npc.CitizensNPC;
import net.dohaw.corelib.StringUtils;
import net.dohaw.ironcraft.manager.ManagementType;
import net.dohaw.ironcraft.config.PlayerDataConfig;
import net.dohaw.ironcraft.data_collection.DataCollectionUtil;
import net.dohaw.ironcraft.manager.ManagerUtil;
import net.dohaw.ironcraft.prompt.AutonomySurveyPrompt;
import net.dohaw.ironcraft.util.LocationUtil;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.conversations.Conversation;
import org.bukkit.conversations.ConversationFactory;
import org.bukkit.entity.Entity;
import org.bukkit.entity.NPC;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
Expand Down Expand Up @@ -174,7 +174,7 @@ public class PlayerData {
/**
* The player's manager NPC.
*/
private NPC managerNPC;
private CitizensNPC managerNPC;

public PlayerData(UUID uuid, String providedID) {
this.providedID = providedID;
Expand All @@ -201,10 +201,13 @@ public void startTeleporter(IronCraftPlugin plugin){
teleporter = Bukkit.getScheduler().runTaskTimer(plugin, () -> {
if(isManager){
teleportToFocusedPlayer();
}else{
if(managementType == ManagementType.AI){
managerNPC.teleport(LocationUtil.getRelativeManagerLocation(getPlayer()));
}
}else if(managementType == ManagementType.AI){
Entity entity = managerNPC.getEntity();
Location tpLocation = ManagerUtil.getNPCManagerTPLocation(getPlayer());
Vector direction = getPlayer().getLocation().getDirection().clone().multiply(-1);
// Makes it to where the npc is looking at the user.
tpLocation.setDirection(direction);
entity.teleport(tpLocation);
}
}, 0L, 1L);
}
Expand Down Expand Up @@ -339,7 +342,7 @@ public int getNextGainIndex() {
public void teleportToFocusedPlayer(){
Player focusedPlayer = Bukkit.getPlayer(focusedPlayerUUID);
if(focusedPlayer == null) return;
getPlayer().teleport(LocationUtil.getRelativeManagerLocation(focusedPlayer));
getPlayer().teleport(ManagerUtil.getHumanManagerTPLocation(focusedPlayer));
}

public void initManager(IronCraftPlugin plugin){
Expand All @@ -361,9 +364,7 @@ public void initWorker(IronCraftPlugin plugin){
player.setInvisible(false);
player.setAllowFlight(false);
player.setFlying(false);
if(teleporter != null){
teleporter.cancel();
}
startTeleporter(plugin);
plugin.giveEssentialItems(player);
startGameTimeTracker(plugin);
}
Expand Down Expand Up @@ -400,6 +401,11 @@ public int getRoundsPlayed() {
return roundsPlayed;
}

public void setRoundsPlayed(int roundsPlayed) {
System.out.println("Rounds played: " + roundsPlayed);
this.roundsPlayed = roundsPlayed;
}

public Map<String, Integer> getItemToTimeStepGained() {
return itemToTimeStepGained;
}
Expand Down Expand Up @@ -559,11 +565,11 @@ public void setMinutesInGame(int minutesInGame) {
this.minutesInGame = minutesInGame;
}

public void setManagerNPC(NPC managerNPC) {
public void setManagerNPC(CitizensNPC managerNPC) {
this.managerNPC = managerNPC;
}

public NPC getManagerNPC() {
public CitizensNPC getManagerNPC() {
return managerNPC;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public PlayerData loadData() {
playerData.setChamberLocation(config.getLocation("Chamber Location"));
playerData.setCurrentTutorialObjective(Objective.valueOf(config.getString("Tutorial Objective")));
playerData.setMinutesInGame(config.getInt("Minutes In Game"));
playerData.setRoundsPlayed(config.getInt("Rounds Played"));

return playerData;
}
Expand All @@ -36,6 +37,7 @@ public void saveData(PlayerData data) {
config.set("Chamber Location", data.getChamberLocation());
config.set("Tutorial Objective", data.getCurrentTutorialObjective().toString());
config.set("Minutes In Game", data.getMinutesInGame());
config.set("Rounds Played", data.getRoundsPlayed());
saveConfig();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ public boolean hasDataLoaded(UUID uuid) {
return allPlayerData.containsKey(uuid);
}

public boolean hasDataLoaded(Player player){
return allPlayerData.containsKey(player.getUniqueId());
}

public PlayerData getData(UUID uuid) {
return allPlayerData.get(uuid);
}
Expand All @@ -96,4 +100,9 @@ public List<PlayerData> getPlayerDataList(Player player){
return list;
}

public boolean isManager(Player player){
if(!hasDataLoaded(player)) return false;
return getData(player).isManager();
}

}
64 changes: 58 additions & 6 deletions src/main/java/net/dohaw/ironcraft/listener/PlayerWatcher.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package net.dohaw.ironcraft.listener;

import net.citizensnpcs.npc.CitizensNPC;
import net.dohaw.corelib.StringUtils;
import net.dohaw.ironcraft.IronCraftPlugin;
import net.dohaw.ironcraft.Objective;
import net.dohaw.ironcraft.PlayerData;
import net.dohaw.ironcraft.Reason;
import net.dohaw.ironcraft.event.AssignManagerEvent;
import net.dohaw.ironcraft.IronCraftPlugin;
import net.dohaw.ironcraft.event.EndGameEvent;
import net.dohaw.ironcraft.handler.PlayerDataHandler;
import net.dohaw.ironcraft.PlayerData;
import net.dohaw.ironcraft.manager.ManagementType;
import net.dohaw.ironcraft.prompt.IDPrompt;
import net.dohaw.ironcraft.prompt.AutonomySurveyPrompt;
import net.dohaw.ironcraft.prompt.IDPrompt;
import net.dohaw.ironcraft.prompt.ManagerSurvey;
import org.bukkit.*;
import org.bukkit.block.Block;
Expand All @@ -25,6 +26,7 @@
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityPickupItemEvent;
import org.bukkit.event.inventory.FurnaceBurnEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType;
Expand All @@ -33,7 +35,10 @@
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;

import java.util.*;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.UUID;

public class PlayerWatcher implements Listener {

Expand Down Expand Up @@ -149,14 +154,43 @@ public void onPlayerJoin(PlayerJoinEvent e) {

}

/*
Ensures that the manager npc is removed when the player is kicked (The PlayerQuitEvent doesn't fire when they are kicked).
*/
@EventHandler
public void onPlayerKick(PlayerKickEvent e){

Player player = e.getPlayer();
PlayerDataHandler playerDataHandler = plugin.getPlayerDataHandler();
if(playerDataHandler.hasDataLoaded(player)){
PlayerData playerData = playerDataHandler.getData(player);
CitizensNPC managerNPC = playerData.getManagerNPC();
if(managerNPC != null){
managerNPC.despawn();
managerNPC.destroy();
}
}

}

@EventHandler
public void onPlayerLeave(PlayerQuitEvent e) {

Player player = e.getPlayer();
PlayerDataHandler playerDataHandler = plugin.getPlayerDataHandler();
PlayerData data = playerDataHandler.getData(player);
UUID playerUUID = player.getUniqueId();
if (playerDataHandler.hasDataLoaded(playerUUID)) {
plugin.getPlayerDataHandler().saveData(playerUUID);
if (playerDataHandler.hasDataLoaded(player)) {

playerDataHandler.saveData(playerUUID);

CitizensNPC managerNPC = data.getManagerNPC();
// this works
if(managerNPC != null){
managerNPC.despawn();
managerNPC.destroy();
}

}

player.getPersistentDataContainer().remove(IronCraftPlugin.IN_SURVEY_PDC_KEY);
Expand Down Expand Up @@ -241,6 +275,7 @@ public void onGameEnd(EndGameEvent e) {

playerData.incrementRoundsPlayed();
int roundsPlayed = playerData.getRoundsPlayed();
System.out.println("Rounds played: " + roundsPlayed);
if(playerData.getRoundsPlayed() < 3){

player.sendMessage(StringUtils.colorString("You have played " + roundsPlayed + " rounds. You have " + (3 - roundsPlayed) + " more round(s) to go!"));
Expand Down Expand Up @@ -342,6 +377,23 @@ public void onPlayerAttack(EntityDamageByEntityEvent e) {

}

/*
Doesn't allow managers to pickup items.
*/
@EventHandler
public void onManagerPickupItem(EntityPickupItemEvent e){
Entity entity = e.getEntity();
if(!(entity instanceof Player)) return;
Player player = (Player) entity;
PlayerDataHandler playerDataHandler = plugin.getPlayerDataHandler();
if(playerDataHandler.hasDataLoaded(player)){
PlayerData playerData = playerDataHandler.getData(player);
if(playerData.isManager()){
e.setCancelled(true);
}
}
}

/**
* Stops players from moving entirely if they are a manager and are overseeing users.
*/
Expand Down
61 changes: 54 additions & 7 deletions src/main/java/net/dohaw/ironcraft/manager/ManagerUtil.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package net.dohaw.ironcraft.manager;

import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.npc.CitizensNPC;
import net.dohaw.corelib.StringUtils;
import net.dohaw.ironcraft.IronCraftPlugin;
import net.dohaw.ironcraft.event.AssignManagerEvent;
import net.dohaw.ironcraft.PlayerData;
import net.dohaw.ironcraft.event.AssignManagerEvent;
import net.dohaw.ironcraft.util.LocationUtil;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.NPC;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;

import java.util.ArrayList;
Expand All @@ -29,6 +31,8 @@ public class ManagerUtil {
*/
public static void assignManager(PlayerData data) {

if(data.isManager()) return;

ThreadLocalRandom current = ThreadLocalRandom.current();
// TODO: switch this back
boolean hasAIManager = false /*current.nextBoolean()*/;
Expand All @@ -39,6 +43,8 @@ public static void assignManager(PlayerData data) {
data.setManagementType(ManagementType.HUMAN);
}

setupNPCManager(data);

List<PlayerData> allPlayerData = new ArrayList<>(IronCraftPlugin.getInstance().getPlayerDataHandler().getAllPlayerData().values());

// Filters out the people that aren't managers, already have the overseeing user limit, or if it's the player we are trying to assign a manager.
Expand Down Expand Up @@ -71,20 +77,61 @@ public static void assignManager(PlayerData data) {

private static void setupNPCManager(PlayerData user){

NPC npc = (NPC) CitizensAPI.getNPCRegistry().createNPC(EntityType.PLAYER, "C10_MC");
CitizensNPC npc = (CitizensNPC) CitizensAPI.getNPCRegistry().createNPC(EntityType.PLAYER, "C10_MC");
user.setManagerNPC(npc);
npc.spawn(getNPCManagerTPLocation(user.getPlayer()));

npc.setAI(false);
npc.setGravity(false);
npc.setCollidable(false);
npc.setAware(false);
LivingEntity entity = (LivingEntity) npc.getEntity();
entity.setAI(false);
entity.setGravity(false);
entity.setCollidable(false);

}

/**
* Ensures that all players that are in the game have a manager. If not, then it assigns them one.
* There is the possibility that they do not get assigned a manager at all if all the managers in the game are already managing a maximum amount of players (Currently is capped at 2)
*/
public static void ensurePlayersHaveManagers(IronCraftPlugin plugin){
for(PlayerData pd : plugin.getPlayerDataHandler().getPlayerDataList()){
System.out.println("Looping");
if(pd.getManager() == null && !pd.isManager() && !pd.isInTutorial()){
System.out.println("Assign him!");
ManagerUtil.assignManager(pd);
System.out.println("Managerment type: " + pd.getManagementType());
System.out.println("Manager: " + pd.getManagerNPC());
}
}
}

public static void sendManagerMessage(Player player){
player.sendRawMessage("You are a manager in the iron pickaxe factory. You will supervise 2 to 5 workers, who should make an iron pickaxe within 7 mins.");
player.sendRawMessage("As a manager, your task is to keep an eye on their performance and evaluate them after the 7-min session expires. You can rate each of them on three levels: Beginner level ($0), Intermediate level ($0.2), or Advanced level ($0.5). Your ratings will decide their pay in the session as shown in the brackets.");
player.sendRawMessage("Left click to switch your focus between players that you manage.");
}

/**
* Acquires the location that the <b>human manager</b> is supposed to be teleported to relative to the user.
* The location is supposed to be a birds eyes view of the user's view (Behind them and slightly higher than them).
* @param relativeUser The user playing the game.
* @return The location that the NPC manager is supposed to be relative to the user.
*/
public static Location getHumanManagerTPLocation(Player relativeUser){
Location focusedPlayerLoc = relativeUser.getLocation();
Location clone = focusedPlayerLoc.clone();
return clone.clone().add(clone.getDirection().multiply(-2.5)).add(0, 0.5, 0);
}

/**
* Acquires the location that the <b>npc manager</b> is supposed to be teleported to relative to the user.
* The location is supposed to be in front of the player so that they can see them.
* @param relativeUser The user playing the game.
* @return The location that the NPC manager is supposed to be relative to the user.
*/
public static Location getNPCManagerTPLocation(Player relativeUser){
Location focusedPlayerLoc = relativeUser.getLocation();
Location clone = focusedPlayerLoc.clone();
return LocationUtil.getLocationToLeft(clone.clone().add(clone.getDirection().multiply(2.5)).add(0, 2, 0), 2);
}

}
Loading

0 comments on commit 6504d4d

Please sign in to comment.