refactor: significantly refactor internals, remove Sponge (#708)

* feat: start database refactor

Adds database migrations

* refactor: refactor database system

Now supports migrations

* refactor: begin major internals rewrite

move to using provider-style interfaces similar to huskclaims
bukkit done, still need to do fabric and sponge

* refactor: remove sponge, checkstyle

Low usage and high maintenance time cost. We will examine Forge instead in the future.

* refactor: implement refactoring changes on fabric

also fix various refactor quirks on Bukkit

* refactor: tidy metrics logic

* build: bump deps
This commit is contained in:
William authored and GitHub committed 2024-10-28 20:49:57 +00:00
1 parent 05bcad9249
commit ffe995ef5f
162 files changed
+3890 -5730

No files matched your search

@@ -27,6 +27,7 @@ import net.william278.huskhomes.position.World;
import net.william278.huskhomes.teleport.Teleport;
import net.william278.huskhomes.teleport.TeleportBuilder;
import net.william278.huskhomes.user.OnlineUser;
import net.william278.huskhomes.user.User;
import net.william278.huskhomes.util.TransactionResolver;
import org.jetbrains.annotations.NotNull;
@@ -87,11 +88,11 @@ public abstract class Broker {
.type(Message.Type.PLAYER_LIST)
.scope(Message.Scope.SERVER)
.target(message.getSourceServer())
.payload(Payload.withStringList(plugin.getLocalPlayerList()))
.payload(Payload.withUserList(plugin.getOnlineUsers().stream().map(u -> (User) u).toList()))
.build().send(this, receiver);
case PLAYER_LIST -> message.getPayload()
.getStringList()
.ifPresent(players -> plugin.setPlayerList(message.getSourceServer(), players));
.getUserList()
.ifPresent(players -> plugin.setUserList(message.getSourceServer(), players));
case UPDATE_HOME -> message.getPayload().getString()
.map(UUID::fromString)
.ifPresent(homeId -> {
@@ -148,11 +149,11 @@ public abstract class Broker {
if (world.isEmpty()) {
plugin.log(Level.SEVERE, "%s requested a position in a world we don't have! World: %s"
.formatted(message.getSourceServer(), request.getWorldName()));
Message.builder()
plugin.getBroker().ifPresent(b -> Message.builder()
.type(Message.Type.RTP_LOCATION)
.target(request.getUsername())
.payload(Payload.empty())
.build().send(plugin.getMessenger(), request.getUsername());
.build().send(b, request.getUsername()));
return;
}
plugin.getRandomTeleportEngine().getRandomPosition(world.get(), null)
@@ -166,7 +167,7 @@ public abstract class Broker {
builder.payload(Payload.withRTPResponse(
Payload.RTPResponse.of(request.getUsername(), position.get())));
}
builder.build().send(plugin.getMessenger(), request.getUsername());
plugin.getBroker().ifPresent(b -> builder.build().send(b, request.getUsername()));
});
});
}
@@ -209,7 +210,11 @@ public abstract class Broker {
// Get the formatted channel ID for the broker
@NotNull
protected String getSubChannelId() {
final String version = String.format("%s.%s", plugin.getVersion().getMajor(), plugin.getVersion().getMinor());
final String version = String.format(
"%s.%s",
plugin.getPluginVersion().getMajor(),
plugin.getPluginVersion().getMinor()
);
try {
return plugin.getKey(
plugin.getSettings().getCrossServer().getClusterId().toLowerCase(Locale.ENGLISH),
@@ -0,0 +1,53 @@
/*
* This file is part of HuskHomes, licensed under the Apache License 2.0.
*
* Copyright (c) William278 <[email protected]>
* Copyright (c) contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.william278.huskhomes.network;
import net.william278.huskhomes.HuskHomes;
import org.jetbrains.annotations.NotNull;
import java.util.Optional;
public interface BrokerProvider {
Optional<Broker> getBroker();
void setBroker(@NotNull Broker broker);
void closeBroker();
void setupPluginMessagingChannels();
default void loadBroker() {
if (!getPlugin().getSettings().getCrossServer().isEnabled()) {
return;
}
switch (getPlugin().getSettings().getCrossServer().getBrokerType()) {
case REDIS -> setBroker(new RedisBroker(getPlugin()));
case PLUGIN_MESSAGE -> setBroker(new PluginMessageBroker(getPlugin()));
}
getBroker().ifPresent(Broker::initialize);
}
@NotNull
HuskHomes getPlugin();
}
@@ -70,7 +70,7 @@ public class Message {
}
public void send(@NotNull Broker broker, @NotNull OnlineUser sender) {
this.sender = sender.getUsername();
this.sender = sender.getName();
this.sourceServer = broker.getServer();
broker.send(this, sender);
}
@@ -21,10 +21,12 @@ package net.william278.huskhomes.network;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import lombok.NoArgsConstructor;
import lombok.Value;
import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.position.World;
import net.william278.huskhomes.teleport.TeleportRequest;
import net.william278.huskhomes.user.User;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -34,6 +36,7 @@ import java.util.Optional;
/**
* Represents a payload sent in a cross-server {@link Message}.
*/
@NoArgsConstructor
public class Payload {
@Nullable
@@ -68,6 +71,11 @@ public class Payload {
@SerializedName("string_list")
private List<String> stringList;
@Nullable
@Expose
@SerializedName("user_list")
private List<User> userList;
/**
* Returns an empty cross-server message payload.
*
@@ -137,6 +145,16 @@ public class Payload {
return payload;
}
/**
* A usr list field.
*/
@NotNull
public static Payload withUserList(@NotNull List<User> target) {
final Payload payload = new Payload();
payload.userList = target;
return payload;
}
/**
* An RTP Response field.
*/
@@ -157,9 +175,6 @@ public class Payload {
return payload;
}
private Payload() {
}
/**
* A position field.
*/
@@ -195,6 +210,13 @@ public class Payload {
return Optional.ofNullable(stringList);
}
/**
* A string list field.
*/
public Optional<List<User>> getUserList() {
return Optional.ofNullable(userList);
}
/**
* An RTP response.
*/
@@ -49,7 +49,7 @@ public class PluginMessageBroker extends Broker {
@Override
public void initialize() throws IllegalStateException {
plugin.initializePluginChannels();
plugin.setupPluginMessagingChannels();
}
public final void onReceive(@NotNull String channel, @NotNull OnlineUser user, byte[] message) {
@@ -214,7 +214,7 @@ public class RedisBroker extends PluginMessageBroker {
if (message.getScope() == Message.Scope.PLAYER) {
broker.plugin.getOnlineUsers().stream()
.filter(online -> message.getTarget().equals(Message.TARGET_ALL)
|| online.getUsername().equals(message.getTarget()))
|| online.getName().equals(message.getTarget()))
.forEach(receiver -> broker.handle(receiver, message));
return;
}