Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Playerdata #17

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/main/java/com/hackclub/hackcraft/parkour/ParkourPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
import com.hackclub.hackcraft.parkour.listeners.ComputerMapListener;
import com.hackclub.hackcraft.parkour.listeners.PlayerMoveListener;
import com.hackclub.hackcraft.parkour.objects.ParkourMap;
import com.hackclub.hackcraft.parkour.utils.DataManager;
import com.hackclub.hackcraft.parkour.utils.ParkourUtil;
import org.bukkit.Bukkit;
import org.bukkit.configuration.serialization.ConfigurationSerialization;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;

public class ParkourPlugin extends JavaPlugin {

public ParkourUtil parkourUtil;
private DataManager dataManager;

@Override
public void onEnable() {
Expand All @@ -23,8 +24,10 @@ public void onEnable() {
ConfigurationSerialization.registerClass(ParkourMap.class);

// initialize custom objects
parkourUtil = new ParkourUtil(this);
this.parkourUtil = new ParkourUtil(this);
parkourUtil.loadParkourMaps();
this.dataManager = new DataManager(this);


// register commands
this.getCommand("pkadmin").setExecutor(new ParkourAdminCommand(this));
Expand All @@ -33,18 +36,25 @@ public void onEnable() {
getServer().getPluginManager().registerEvents(new PlayerMoveListener(this), this);
getServer().getPluginManager().registerEvents(new CheckpointListener(this), this);



Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
@Override
public void run() {
parkourUtil.spawnCheckpointParticles();
}
}, 1L, 20L);

this.getDataManager().registerAll();

}

@Override
public void onDisable() {
getLogger().info("Parkour deactivated!");
this.getDataManager().unregisterAll();
}

public DataManager getDataManager() {
return dataManager;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public CheckpointListener(ParkourPlugin pl) {

@EventHandler
public void onCheckpoint(CheckpointEvent e) {
e.getPlayer().playSound(e.getPlayer().getLocation(), Sound.BLOCK_NOTE_BLOCK_CHIME, 1.0F, 8.0F);
e.getPlayer().playSound(e.getPlayer().getLocation(), Sound.BLOCK_NOTE_BLOCK_CHIME, 1.0F,
8.0F);
pl.getDataManager().getData(e.getPlayer()).getCurrentRun()
.setLastCheckpoint(e.getCheckpointIndex());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.hackclub.hackcraft.parkour.objects;

import com.hackclub.hackcraft.parkour.ParkourPlugin;
import org.bukkit.entity.Player;

public class PlayerData {
private final ParkourPlugin plugin;
private final Player player;


// Session data (will be cleared when player quits)
private ParkourRun currentRun = null;
private ParkourMap currentMap = null;

public PlayerData(ParkourPlugin plugin, Player player) {
this.plugin = plugin;
this.player = player;

}

public Player getPlayer() {
return player;
}

public ParkourRun getCurrentRun() {
return currentRun;
}

public void setCurrentRun(ParkourRun currentRun) {
this.currentRun = currentRun;
}

public ParkourMap getCurrentMap() {
return currentMap;
}

public void setCurrentMap(ParkourMap currentMap) {
this.currentMap = currentMap;
}



}

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.hackclub.hackcraft.parkour.utils;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import com.hackclub.hackcraft.parkour.ParkourPlugin;
import com.hackclub.hackcraft.parkour.objects.PlayerData;
import org.bukkit.entity.Player;

public class DataManager {
private final ParkourPlugin plugin;
private final String dataFolder;
private final Map<UUID, PlayerData> players = new HashMap<>();

public DataManager(ParkourPlugin plugin) {
this.plugin = plugin;
this.dataFolder = plugin.getDataFolder() + File.separator + "players";

File folder = new File(this.dataFolder);
folder.mkdirs();
}

public String getDataFolder() {
return this.dataFolder;
}

public PlayerData getData(Player player) {
return this.players.get(player.getUniqueId());
}

public void registerPlayer(Player player) {
this.players.put(player.getUniqueId(), new PlayerData(this.plugin, player));
}

public void registerAll() {
for (Player player : this.plugin.getServer().getOnlinePlayers()) {
this.registerPlayer(player);
}
}

public void unregisterPlayer(Player player) {

this.players.remove(player.getUniqueId());
}

public void unregisterAll() {
for (Player player : this.plugin.getServer().getOnlinePlayers()) {
this.unregisterPlayer(player);
}
}
}
30 changes: 30 additions & 0 deletions src/main/java/com/hackclub/hackcraft/parkour/utils/PlayerUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.hackclub.hackcraft.parkour.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import com.hackclub.hackcraft.parkour.ParkourPlugin;
import com.hackclub.hackcraft.parkour.objects.ParkourMap;
import com.hackclub.hackcraft.parkour.objects.PlayerData;
import org.bukkit.entity.Player;

public class PlayerUtil {

private ParkourPlugin plugin;
private final Map<UUID, PlayerData> players = new HashMap<>();


// teleports player to the start of a parkour map
public void teleportParkour(Player player, ParkourMap map) {
player.teleport(map.getSpawn());
plugin.getDataManager().getData(player).setCurrentMap(map);
}

// probably should be teleport lobby or something idk
public void teleportSpawn(Player player) {
player.teleport(plugin.getServer().getWorld("world").getSpawnLocation());
plugin.getDataManager().getData(player).setCurrentMap(null);
// TODO: create a parkour world class or some way to store this data flexibly
}

}