Stage 12: Data storage system
This commit is contained in:
parent
82ed455cd2
commit
4fd7955d56
62 changed files with 5613 additions and 0 deletions
93
src/main/java/dev/loki/chest/ChestManager.java
Normal file
93
src/main/java/dev/loki/chest/ChestManager.java
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
package dev.loki.chest;
|
||||
|
||||
import dev.loki.config.LocationsConfig;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
public class ChestManager {
|
||||
|
||||
private final JavaPlugin plugin;
|
||||
private final Map<UUID, Chest> chests;
|
||||
private final LocationsConfig locationsConfig;
|
||||
|
||||
public ChestManager(JavaPlugin plugin, LocationsConfig locationsConfig) {
|
||||
this.plugin = plugin;
|
||||
this.chests = new HashMap<>();
|
||||
this.locationsConfig = locationsConfig;
|
||||
loadChests();
|
||||
}
|
||||
|
||||
private void loadChests() {
|
||||
List<Location> locations = locationsConfig.getChestLocations();
|
||||
for (Location loc : locations) {
|
||||
Chest chest = new Chest(loc);
|
||||
chests.put(chest.getId(), chest);
|
||||
}
|
||||
plugin.getLogger().info("Loaded " + chests.size() + " chests");
|
||||
}
|
||||
|
||||
public Chest getChest(UUID id) {
|
||||
return chests.get(id);
|
||||
}
|
||||
|
||||
public List<Chest> getAllChests() {
|
||||
return new ArrayList<>(chests.values());
|
||||
}
|
||||
|
||||
public Chest getNearestChest(Location location, double maxRange) {
|
||||
Chest nearest = null;
|
||||
double minDistance = maxRange;
|
||||
|
||||
for (Chest chest : chests.values()) {
|
||||
double distance = chest.getDistance(location);
|
||||
if (distance < minDistance) {
|
||||
minDistance = distance;
|
||||
nearest = chest;
|
||||
}
|
||||
}
|
||||
|
||||
return nearest;
|
||||
}
|
||||
|
||||
public List<Chest> getAvailableChests() {
|
||||
List<Chest> available = new ArrayList<>();
|
||||
for (Chest chest : chests.values()) {
|
||||
if (chest.isAvailable()) {
|
||||
available.add(chest);
|
||||
}
|
||||
}
|
||||
return available;
|
||||
}
|
||||
|
||||
public void resetAllChests() {
|
||||
for (Chest chest : chests.values()) {
|
||||
chest.setOpened(false);
|
||||
chest.setEmpty(false);
|
||||
}
|
||||
plugin.getLogger().info("Reset all chests");
|
||||
}
|
||||
|
||||
public void clearChests() {
|
||||
chests.clear();
|
||||
}
|
||||
|
||||
public void addChest(Location location) {
|
||||
Chest chest = new Chest(location);
|
||||
chests.put(chest.getId(), chest);
|
||||
locationsConfig.addChestLocation(location);
|
||||
}
|
||||
|
||||
public int getTotalChests() {
|
||||
return chests.size();
|
||||
}
|
||||
|
||||
public int getRemainingChests() {
|
||||
return (int) chests.values().stream().filter(Chest::isAvailable).count();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue