Essentia/plugin/src/main/java/com/alttd/essentia/user/EssentiaUser.java
2024-07-28 14:47:49 +02:00

168 lines
3.9 KiB
Java

package com.alttd.essentia.user;
import com.alttd.essentia.api.model.Home;
import com.alttd.essentia.api.model.UserSettings;
import com.alttd.essentia.api.request.Request;
import com.alttd.essentia.api.user.User;
import com.alttd.essentia.model.EssentiaHome;
import org.bukkit.Location;
import java.util.*;
import java.util.stream.Collectors;
public class EssentiaUser implements User {
protected final UUID uuid;
protected Location backLocation;
protected Location deathLocation;
protected Map<String, Home> homes;
protected UserSettings userSettings;
protected Request request;
private boolean saving;
private boolean needsSaving; // can we use a decorator for this to set it to true on every change?
private EssentiaUser(Builder builder) {
this.uuid = builder.uuid;
this.backLocation = builder.backLocation;
this.deathLocation = builder.deathLocation;
this.homes = builder.homes;
this.userSettings = builder.userSettings;
}
@Override
public UUID getUUID() {
return uuid;
}
@Override
public Location getBackLocation(boolean death) {
return death ? deathLocation : backLocation;
}
@Override
public void setBackLocation(boolean death, Location location) {
if (death) {
deathLocation = location;
return;
}
backLocation = location;
needsSaving = true;
}
@Override
public boolean hasHome(String name) {
return homes.containsKey(name);
}
@Override
public Home getHome(String name) {
return homes.get(name);
}
@Override
public void setHome(String name, Location location) {
homes.put(name, new EssentiaHome(name, location));
needsSaving = true;
}
@Override
public void removeHome(String name) {
homes.remove(name);
needsSaving = true;
}
@Override
public int getHomeCount() {
return homes.size();
}
@Override
public List<String> getMatchingHomeNames(String homeName) {
return getHomes().stream()
.filter(home -> home.toLowerCase().startsWith(homeName.toLowerCase()))
.collect(Collectors.toList());
}
@Override
public Map<String, Home> getHomeData() {
return homes;
}
@Override
public Set<String> getHomes() {
return homes.keySet();
}
@Override
public UserSettings getUserSettings() {
return userSettings;
}
public boolean saving() {
return saving;
}
public void saving(boolean saving) {
this.saving = saving;
}
public boolean needsSaving() {
return needsSaving;
}
public void needsSaving(boolean needsSaving) {
this.needsSaving = needsSaving;
}
@Override
public Request request() {
return request;
}
@Override
public void request(Request request) {
this.request = request;
}
public static class Builder {
protected UUID uuid;
protected Location backLocation = null;
protected Location deathLocation = null;
protected Map<String, Home> homes = new HashMap<>();
protected UserSettings userSettings = null;
public Builder uuid(UUID uuid) {
this.uuid = uuid;
return this;
}
public Builder backLocation(Location location) {
this.backLocation = location;
return this;
}
public Builder deathLocation(Location location) {
this.deathLocation = location;
return this;
}
public Builder homes(Map<String, Home> homes) {
this.homes = homes;
return this;
}
public Builder userSettings(UserSettings userSettings) {
this.userSettings = userSettings;
return this;
}
public EssentiaUser build() {
return new EssentiaUser(this);
}
}
}