added EventRepository
This commit is contained in:
parent
696c4bc260
commit
184617e9c3
@ -0,0 +1,29 @@
|
|||||||
|
package eu.mhsl.craftattack.spawn.api.client.repositories;
|
||||||
|
|
||||||
|
import eu.mhsl.craftattack.spawn.api.client.HttpRepository;
|
||||||
|
import eu.mhsl.craftattack.spawn.api.client.ReqResp;
|
||||||
|
import eu.mhsl.craftattack.spawn.util.api.EventApiUtil;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class EventRepository extends HttpRepository {
|
||||||
|
public EventRepository() {
|
||||||
|
super(EventApiUtil.getBaseUri());
|
||||||
|
}
|
||||||
|
|
||||||
|
public record CreatedRoom(UUID uuid) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public record QueueRoom(UUID player, UUID room) {
|
||||||
|
public record Response(String error) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ReqResp<CreatedRoom> createSession() {
|
||||||
|
return this.post("room", null, CreatedRoom.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ReqResp<QueueRoom.Response> queueRoom(QueueRoom request) {
|
||||||
|
return this.post("queueRoom", request, QueueRoom.Response.class);
|
||||||
|
}
|
||||||
|
}
|
@ -3,7 +3,7 @@ package eu.mhsl.craftattack.spawn.api.client.repositories;
|
|||||||
import com.google.common.reflect.TypeToken;
|
import com.google.common.reflect.TypeToken;
|
||||||
import eu.mhsl.craftattack.spawn.api.client.HttpRepository;
|
import eu.mhsl.craftattack.spawn.api.client.HttpRepository;
|
||||||
import eu.mhsl.craftattack.spawn.api.client.ReqResp;
|
import eu.mhsl.craftattack.spawn.api.client.ReqResp;
|
||||||
import eu.mhsl.craftattack.spawn.util.api.ApiUtil;
|
import eu.mhsl.craftattack.spawn.util.api.WebsiteApiUtil;
|
||||||
|
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -12,7 +12,7 @@ import java.util.UUID;
|
|||||||
|
|
||||||
public class FeedbackRepository extends HttpRepository {
|
public class FeedbackRepository extends HttpRepository {
|
||||||
public FeedbackRepository() {
|
public FeedbackRepository() {
|
||||||
super(ApiUtil.getBaseUri(), ApiUtil::withAuthorizationSecret);
|
super(WebsiteApiUtil.getBaseUri(), WebsiteApiUtil::withAuthorizationSecret);
|
||||||
}
|
}
|
||||||
|
|
||||||
public record Request(String event, List<UUID> users) {}
|
public record Request(String event, List<UUID> users) {}
|
||||||
|
@ -2,7 +2,7 @@ package eu.mhsl.craftattack.spawn.api.client.repositories;
|
|||||||
|
|
||||||
import eu.mhsl.craftattack.spawn.api.client.HttpRepository;
|
import eu.mhsl.craftattack.spawn.api.client.HttpRepository;
|
||||||
import eu.mhsl.craftattack.spawn.api.client.ReqResp;
|
import eu.mhsl.craftattack.spawn.api.client.ReqResp;
|
||||||
import eu.mhsl.craftattack.spawn.util.api.ApiUtil;
|
import eu.mhsl.craftattack.spawn.util.api.WebsiteApiUtil;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
@ -11,7 +11,7 @@ import java.util.UUID;
|
|||||||
|
|
||||||
public class ReportRepository extends HttpRepository {
|
public class ReportRepository extends HttpRepository {
|
||||||
public ReportRepository() {
|
public ReportRepository() {
|
||||||
super(ApiUtil.getBaseUri(), ApiUtil::withAuthorizationSecret);
|
super(WebsiteApiUtil.getBaseUri(), WebsiteApiUtil::withAuthorizationSecret);
|
||||||
}
|
}
|
||||||
|
|
||||||
public record ReportCreationInfo(@NotNull UUID reporter, @Nullable UUID reported, String reason) {
|
public record ReportCreationInfo(@NotNull UUID reporter, @Nullable UUID reported, String reason) {
|
||||||
|
@ -2,13 +2,13 @@ package eu.mhsl.craftattack.spawn.api.client.repositories;
|
|||||||
|
|
||||||
import eu.mhsl.craftattack.spawn.api.client.HttpRepository;
|
import eu.mhsl.craftattack.spawn.api.client.HttpRepository;
|
||||||
import eu.mhsl.craftattack.spawn.api.client.ReqResp;
|
import eu.mhsl.craftattack.spawn.api.client.ReqResp;
|
||||||
import eu.mhsl.craftattack.spawn.util.api.ApiUtil;
|
import eu.mhsl.craftattack.spawn.util.api.WebsiteApiUtil;
|
||||||
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
public class WhitelistRepository extends HttpRepository {
|
public class WhitelistRepository extends HttpRepository {
|
||||||
public WhitelistRepository() {
|
public WhitelistRepository() {
|
||||||
super(ApiUtil.getBaseUri(), ApiUtil::withAuthorizationSecret);
|
super(WebsiteApiUtil.getBaseUri(), WebsiteApiUtil::withAuthorizationSecret);
|
||||||
}
|
}
|
||||||
|
|
||||||
public record UserData(
|
public record UserData(
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
package eu.mhsl.craftattack.spawn.appliances.event;
|
package eu.mhsl.craftattack.spawn.appliances.event;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
|
||||||
import eu.mhsl.craftattack.spawn.Main;
|
import eu.mhsl.craftattack.spawn.Main;
|
||||||
|
import eu.mhsl.craftattack.spawn.api.client.ReqResp;
|
||||||
|
import eu.mhsl.craftattack.spawn.api.client.repositories.EventRepository;
|
||||||
import eu.mhsl.craftattack.spawn.api.server.HttpServer;
|
import eu.mhsl.craftattack.spawn.api.server.HttpServer;
|
||||||
import eu.mhsl.craftattack.spawn.appliance.Appliance;
|
import eu.mhsl.craftattack.spawn.appliance.Appliance;
|
||||||
import eu.mhsl.craftattack.spawn.appliance.ApplianceCommand;
|
import eu.mhsl.craftattack.spawn.appliance.ApplianceCommand;
|
||||||
@ -10,6 +11,7 @@ import eu.mhsl.craftattack.spawn.appliances.customAdvancements.CustomAdvancement
|
|||||||
import eu.mhsl.craftattack.spawn.appliances.event.command.*;
|
import eu.mhsl.craftattack.spawn.appliances.event.command.*;
|
||||||
import eu.mhsl.craftattack.spawn.appliances.event.listener.ApplyPendingRewardsListener;
|
import eu.mhsl.craftattack.spawn.appliances.event.listener.ApplyPendingRewardsListener;
|
||||||
import eu.mhsl.craftattack.spawn.util.IteratorUtil;
|
import eu.mhsl.craftattack.spawn.util.IteratorUtil;
|
||||||
|
import eu.mhsl.craftattack.spawn.util.api.HttpStatus;
|
||||||
import eu.mhsl.craftattack.spawn.util.entity.DisplayVillager;
|
import eu.mhsl.craftattack.spawn.util.entity.DisplayVillager;
|
||||||
import eu.mhsl.craftattack.spawn.util.listener.DismissInventoryOpenFromHolder;
|
import eu.mhsl.craftattack.spawn.util.listener.DismissInventoryOpenFromHolder;
|
||||||
import eu.mhsl.craftattack.spawn.util.listener.PlayerInteractAtEntityEventListener;
|
import eu.mhsl.craftattack.spawn.util.listener.PlayerInteractAtEntityEventListener;
|
||||||
@ -27,12 +29,6 @@ import org.bukkit.inventory.ItemStack;
|
|||||||
import org.bukkit.inventory.meta.ItemMeta;
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.URI;
|
|
||||||
import java.net.URISyntaxException;
|
|
||||||
import java.net.http.HttpClient;
|
|
||||||
import java.net.http.HttpRequest;
|
|
||||||
import java.net.http.HttpResponse;
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class Event extends Appliance {
|
public class Event extends Appliance {
|
||||||
@ -57,7 +53,6 @@ public class Event extends Appliance {
|
|||||||
private boolean isOpen = false;
|
private boolean isOpen = false;
|
||||||
private AdvertisementStatus advertiseStatus = AdvertisementStatus.BEFORE;
|
private AdvertisementStatus advertiseStatus = AdvertisementStatus.BEFORE;
|
||||||
private UUID roomId;
|
private UUID roomId;
|
||||||
private final HttpClient eventServerClient = HttpClient.newHttpClient();
|
|
||||||
private final List<Reward> pendingRewards = new ArrayList<>();
|
private final List<Reward> pendingRewards = new ArrayList<>();
|
||||||
|
|
||||||
record RewardConfiguration(String memorialMaterial, String memorialTitle, String memorialLore, List<UUID> memorials,
|
record RewardConfiguration(String memorialMaterial, String memorialTitle, String memorialLore, List<UUID> memorials,
|
||||||
@ -74,85 +69,62 @@ public class Event extends Appliance {
|
|||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
this.villager = new DisplayVillager.ConfigBound(
|
this.villager = new DisplayVillager.ConfigBound(
|
||||||
localConfig(),
|
this.localConfig(),
|
||||||
villager -> {
|
villager -> {
|
||||||
villager.customName(Component.text("Events", NamedTextColor.GOLD));
|
villager.customName(Component.text("Events", NamedTextColor.GOLD));
|
||||||
villager.setProfession(Villager.Profession.LIBRARIAN);
|
villager.setProfession(Villager.Profession.LIBRARIAN);
|
||||||
villager.setVillagerType(Villager.Type.SNOW);
|
villager.setVillagerType(Villager.Type.SNOW);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
this.isOpen = localConfig().getBoolean("enabled", false);
|
this.isOpen = this.localConfig().getBoolean("enabled", false);
|
||||||
if(this.isOpen) this.roomId = UUID.fromString(localConfig().getString("roomId", ""));
|
if(this.isOpen) this.roomId = UUID.fromString(this.localConfig().getString("roomId", ""));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void openEvent() throws URISyntaxException, IOException, InterruptedException {
|
public void openEvent() {
|
||||||
if(isOpen) throw new ApplianceCommand.Error("Es läuft derzeit bereits ein Event!");
|
if(this.isOpen) throw new ApplianceCommand.Error("Es läuft derzeit bereits ein Event!");
|
||||||
HttpRequest createRoomRequest = HttpRequest.newBuilder()
|
|
||||||
.uri(new URI(localConfig().getString("api") + "/room"))
|
|
||||||
.POST(HttpRequest.BodyPublishers.noBody())
|
|
||||||
.build();
|
|
||||||
|
|
||||||
HttpResponse<String> rawResponse = eventServerClient.send(createRoomRequest, HttpResponse.BodyHandlers.ofString());
|
ReqResp<EventRepository.CreatedRoom> sessionResponse = this.queryRepository(EventRepository.class).createSession();
|
||||||
if(rawResponse.statusCode() != 200)
|
|
||||||
throw new ApplianceCommand.Error("Event-Server meldet Fehler: " + rawResponse.statusCode());
|
|
||||||
|
|
||||||
record Response(UUID uuid) {
|
if(sessionResponse.status() != HttpStatus.OK)
|
||||||
}
|
throw new ApplianceCommand.Error("Event-Server meldet Fehler: " + sessionResponse.status());
|
||||||
Response response = new Gson().fromJson(rawResponse.body(), Response.class);
|
|
||||||
|
|
||||||
isOpen = true;
|
this.isOpen = true;
|
||||||
roomId = response.uuid;
|
this.roomId = sessionResponse.data().uuid();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void joinEvent(Player p) {
|
public void joinEvent(Player p) {
|
||||||
if(!isOpen) {
|
if(!this.isOpen) {
|
||||||
p.sendMessage(Component.text("Zurzeit ist kein Event geöffnet.", NamedTextColor.RED));
|
p.sendMessage(Component.text("Zurzeit ist kein Event geöffnet.", NamedTextColor.RED));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!p.hasPermission("admin") && advertiseStatus == AdvertisementStatus.BEFORE) {
|
if(!p.hasPermission("admin") && this.advertiseStatus == AdvertisementStatus.BEFORE) {
|
||||||
p.sendMessage(Component.text("Die Event befinden sich noch in der Vorbereitung.", NamedTextColor.RED));
|
p.sendMessage(Component.text("Die Event befinden sich noch in der Vorbereitung.", NamedTextColor.RED));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!p.hasPermission("admin") && advertiseStatus == AdvertisementStatus.DONE) {
|
if(!p.hasPermission("admin") && this.advertiseStatus == AdvertisementStatus.DONE) {
|
||||||
p.sendMessage(Component.text("Die Events laufen bereits. Ein nachträgliches Beitreten ist nicht möglich.", NamedTextColor.RED));
|
p.sendMessage(Component.text("Die Events laufen bereits. Ein nachträgliches Beitreten ist nicht möglich.", NamedTextColor.RED));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
Main.instance().getLogger().info("Verbinde mit eventserver: " + p.getName());
|
Main.instance().getLogger().info("Verbinde mit eventserver: " + p.getName());
|
||||||
p.sendMessage(Component.text("Authentifiziere...", NamedTextColor.GREEN));
|
p.sendMessage(Component.text("Authentifiziere...", NamedTextColor.GREEN));
|
||||||
record Request(UUID player, UUID room) {
|
ReqResp<EventRepository.QueueRoom.Response> queueRespone = this.queryRepository(EventRepository.class)
|
||||||
}
|
.queueRoom(new EventRepository.QueueRoom(p.getUniqueId(), this.roomId));
|
||||||
Request request = new Request(p.getUniqueId(), this.roomId);
|
|
||||||
HttpRequest queueRoomRequest = HttpRequest.newBuilder()
|
|
||||||
.uri(new URI(localConfig().getString("api") + "/queueRoom"))
|
|
||||||
.POST(HttpRequest.BodyPublishers.ofString(new Gson().toJson(request)))
|
|
||||||
.build();
|
|
||||||
|
|
||||||
record Response(String error) {
|
if(queueRespone.status() != HttpStatus.OK || queueRespone.data().error() != null) {
|
||||||
}
|
p.sendMessage(Component.text("Fehler beim Betreten: " + queueRespone.data().error(), NamedTextColor.RED));
|
||||||
HttpResponse<String> rawResponse = eventServerClient.send(queueRoomRequest, HttpResponse.BodyHandlers.ofString());
|
|
||||||
Main.instance().getLogger().info("Response: " + rawResponse.body());
|
|
||||||
Response response = new Gson().fromJson(rawResponse.body(), Response.class);
|
|
||||||
|
|
||||||
if(rawResponse.statusCode() != 200 || response.error != null) {
|
|
||||||
p.sendMessage(Component.text("Fehler beim Betreten: " + response.error, NamedTextColor.RED));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
p.sendMessage(Component.text("Betrete...", NamedTextColor.GREEN));
|
p.sendMessage(Component.text("Betrete...", NamedTextColor.GREEN));
|
||||||
PluginMessage.connect(p, localConfig().getString("connect-server-name"));
|
PluginMessage.connect(p, this.localConfig().getString("connect-server-name"));
|
||||||
|
|
||||||
} catch(URISyntaxException | IOException | InterruptedException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void endEvent() {
|
public void endEvent() {
|
||||||
if(!isOpen) throw new ApplianceCommand.Error("Es läuft derzeit kein Event!");
|
if(!this.isOpen) throw new ApplianceCommand.Error("Es läuft derzeit kein Event!");
|
||||||
isOpen = false;
|
this.isOpen = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void rewardPlayers(RewardConfiguration rewardConfiguration) {
|
private void rewardPlayers(RewardConfiguration rewardConfiguration) {
|
||||||
@ -160,10 +132,10 @@ public class Event extends Appliance {
|
|||||||
Reward reward = new Reward(uuid, new ItemStack(Objects.requireNonNull(Material.matchMaterial(rewardConfiguration.material)), amount));
|
Reward reward = new Reward(uuid, new ItemStack(Objects.requireNonNull(Material.matchMaterial(rewardConfiguration.material)), amount));
|
||||||
|
|
||||||
if(Bukkit.getPlayer(uuid) == null) {
|
if(Bukkit.getPlayer(uuid) == null) {
|
||||||
pendingRewards.add(reward);
|
this.pendingRewards.add(reward);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
giveReward(reward);
|
this.giveReward(reward);
|
||||||
});
|
});
|
||||||
|
|
||||||
rewardConfiguration.memorials.forEach(uuid -> {
|
rewardConfiguration.memorials.forEach(uuid -> {
|
||||||
@ -177,10 +149,10 @@ public class Event extends Appliance {
|
|||||||
Main.instance().getAppliance(CustomAdvancements.class).grantAdvancement(Advancements.participateEvent, uuid);
|
Main.instance().getAppliance(CustomAdvancements.class).grantAdvancement(Advancements.participateEvent, uuid);
|
||||||
|
|
||||||
if(Bukkit.getPlayer(uuid) == null) {
|
if(Bukkit.getPlayer(uuid) == null) {
|
||||||
pendingRewards.add(memorial);
|
this.pendingRewards.add(memorial);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
giveReward(memorial);
|
this.giveReward(memorial);
|
||||||
});
|
});
|
||||||
|
|
||||||
rewardConfiguration.rewards.keySet().stream()
|
rewardConfiguration.rewards.keySet().stream()
|
||||||
@ -208,7 +180,7 @@ public class Event extends Appliance {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void advertise() {
|
public void advertise() {
|
||||||
advertiseCountdown.cancelIfRunning();
|
this.advertiseCountdown.cancelIfRunning();
|
||||||
this.advertiseStatus = AdvertisementStatus.ADVERTISED;
|
this.advertiseStatus = AdvertisementStatus.ADVERTISED;
|
||||||
IteratorUtil.onlinePlayers(player -> player.sendMessage(
|
IteratorUtil.onlinePlayers(player -> player.sendMessage(
|
||||||
Component.text()
|
Component.text()
|
||||||
@ -219,7 +191,7 @@ public class Event extends Appliance {
|
|||||||
.append(Component.text(", um dem Event beizutreten!")).appendNewline()
|
.append(Component.text(", um dem Event beizutreten!")).appendNewline()
|
||||||
.append(Component.text("-".repeat(10), NamedTextColor.GRAY)).appendNewline()
|
.append(Component.text("-".repeat(10), NamedTextColor.GRAY)).appendNewline()
|
||||||
));
|
));
|
||||||
advertiseCountdown.start();
|
this.advertiseCountdown.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -247,7 +219,7 @@ public class Event extends Appliance {
|
|||||||
protected List<Listener> listeners() {
|
protected List<Listener> listeners() {
|
||||||
return List.of(
|
return List.of(
|
||||||
new ApplyPendingRewardsListener(),
|
new ApplyPendingRewardsListener(),
|
||||||
new PlayerInteractAtEntityEventListener(this.villager.getUniqueId(), playerInteractAtEntityEvent -> joinEvent(playerInteractAtEntityEvent.getPlayer())),
|
new PlayerInteractAtEntityEventListener(this.villager.getUniqueId(), playerInteractAtEntityEvent -> this.joinEvent(playerInteractAtEntityEvent.getPlayer())),
|
||||||
new DismissInventoryOpenFromHolder(this.villager.getUniqueId())
|
new DismissInventoryOpenFromHolder(this.villager.getUniqueId())
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,23 @@
|
|||||||
|
package eu.mhsl.craftattack.spawn.util.api;
|
||||||
|
|
||||||
|
import eu.mhsl.craftattack.spawn.config.Configuration;
|
||||||
|
import org.bukkit.configuration.ConfigurationSection;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public class EventApiUtil {
|
||||||
|
private final static ConfigurationSection apiConfig = Objects.requireNonNull(Configuration.cfg.getConfigurationSection("event"));
|
||||||
|
public final static String basePath = apiConfig.getString("api");
|
||||||
|
|
||||||
|
public static URI getBaseUri() {
|
||||||
|
Objects.requireNonNull(basePath);
|
||||||
|
try {
|
||||||
|
return new URI(basePath);
|
||||||
|
} catch(URISyntaxException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -8,7 +8,7 @@ import java.net.URI;
|
|||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
public class ApiUtil {
|
public class WebsiteApiUtil {
|
||||||
private final static ConfigurationSection apiConfig = Objects.requireNonNull(Configuration.cfg.getConfigurationSection("api"));
|
private final static ConfigurationSection apiConfig = Objects.requireNonNull(Configuration.cfg.getConfigurationSection("api"));
|
||||||
public final static String basePath = apiConfig.getString("baseurl");
|
public final static String basePath = apiConfig.getString("baseurl");
|
||||||
public final static String apiSecret = apiConfig.getString("secret");
|
public final static String apiSecret = apiConfig.getString("secret");
|
Loading…
x
Reference in New Issue
Block a user