mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-02 06:28:49 +00:00
fabric: Only build 1.20.1
This commit is contained in:
85 files changed
+15
-5585
No files matched your search
@@ -0,0 +1,484 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import io.netty.buffer.ByteBufUtil;
|
||||
import net.fabricmc.api.DedicatedServerModInitializer;
|
||||
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
|
||||
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
|
||||
import net.fabricmc.fabric.api.networking.v1.PacketSender;
|
||||
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
import net.fabricmc.loader.api.ModContainer;
|
||||
import net.kyori.adventure.platform.fabric.FabricServerAudiences;
|
||||
import net.minecraft.network.PacketByteBuf;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.network.ServerPlayNetworkHandler;
|
||||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.william278.annotaml.Annotaml;
|
||||
import net.william278.desertwell.util.Version;
|
||||
import net.william278.huskhomes.command.Command;
|
||||
import net.william278.huskhomes.command.FabricCommand;
|
||||
import net.william278.huskhomes.config.Locales;
|
||||
import net.william278.huskhomes.config.Server;
|
||||
import net.william278.huskhomes.config.Settings;
|
||||
import net.william278.huskhomes.config.Spawn;
|
||||
import net.william278.huskhomes.database.Database;
|
||||
import net.william278.huskhomes.database.MySqlDatabase;
|
||||
import net.william278.huskhomes.database.SqLiteDatabase;
|
||||
import net.william278.huskhomes.event.FabricEventDispatcher;
|
||||
import net.william278.huskhomes.hook.Hook;
|
||||
import net.william278.huskhomes.listener.EventListener;
|
||||
import net.william278.huskhomes.listener.FabricEventListener;
|
||||
import net.william278.huskhomes.manager.Manager;
|
||||
import net.william278.huskhomes.network.Broker;
|
||||
import net.william278.huskhomes.network.PluginMessageBroker;
|
||||
import net.william278.huskhomes.network.RedisBroker;
|
||||
import net.william278.huskhomes.position.Location;
|
||||
import net.william278.huskhomes.position.World;
|
||||
import net.william278.huskhomes.random.NormalDistributionEngine;
|
||||
import net.william278.huskhomes.random.RandomTeleportEngine;
|
||||
import net.william278.huskhomes.user.ConsoleUser;
|
||||
import net.william278.huskhomes.user.FabricUser;
|
||||
import net.william278.huskhomes.user.OnlineUser;
|
||||
import net.william278.huskhomes.user.SavedUser;
|
||||
import net.william278.huskhomes.util.FabricSafetyResolver;
|
||||
import net.william278.huskhomes.util.FabricTask;
|
||||
import net.william278.huskhomes.util.UnsafeBlocks;
|
||||
import net.william278.huskhomes.util.Validator;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.slf4j.spi.LoggingEventBuilder;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.nio.file.Files;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.logging.Level;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class FabricHuskHomes implements DedicatedServerModInitializer, HuskHomes, FabricTask.Supplier,
|
||||
FabricEventDispatcher, FabricSafetyResolver, ServerPlayNetworking.PlayChannelHandler {
|
||||
|
||||
public static final Logger LOGGER = LoggerFactory.getLogger("HuskHomes");
|
||||
private final ModContainer modContainer = FabricLoader.getInstance().getModContainer("huskhomes")
|
||||
.orElseThrow(() -> new RuntimeException("Failed to get Mod Container"));
|
||||
|
||||
private MinecraftServer minecraftServer;
|
||||
private Map<String, Boolean> permissions;
|
||||
private Set<SavedUser> savedUsers;
|
||||
private Settings settings;
|
||||
private Locales locales;
|
||||
private Database database;
|
||||
private Validator validator;
|
||||
private Manager manager;
|
||||
private EventListener eventListener;
|
||||
private RandomTeleportEngine randomTeleportEngine;
|
||||
private Spawn serverSpawn;
|
||||
private UnsafeBlocks unsafeBlocks;
|
||||
private List<Hook> hooks;
|
||||
private List<Command> commands;
|
||||
private ConcurrentHashMap<String, List<String>> globalPlayerList;
|
||||
private Set<UUID> currentlyOnWarmup;
|
||||
private Server server;
|
||||
@Nullable
|
||||
private Broker broker;
|
||||
private FabricServerAudiences audiences;
|
||||
|
||||
@Override
|
||||
public void onInitializeServer() {
|
||||
// Get plugin version from mod container
|
||||
this.permissions = new HashMap<>();
|
||||
this.savedUsers = new HashSet<>();
|
||||
this.globalPlayerList = new ConcurrentHashMap<>();
|
||||
this.currentlyOnWarmup = new HashSet<>();
|
||||
this.validator = new Validator(this);
|
||||
|
||||
// Load settings and locales
|
||||
initialize("plugin config & locale files", (plugin) -> {
|
||||
if (!loadConfigs()) {
|
||||
throw new IllegalStateException("Failed to load config files. Please check the console for errors");
|
||||
}
|
||||
});
|
||||
|
||||
// Pre-register commands
|
||||
initialize("commands", (plugin) -> this.commands = registerCommands());
|
||||
|
||||
ServerLifecycleEvents.SERVER_STARTING.register(server -> {
|
||||
this.minecraftServer = server;
|
||||
this.onEnable();
|
||||
});
|
||||
ServerLifecycleEvents.SERVER_STOPPING.register(server -> this.onDisable());
|
||||
}
|
||||
|
||||
private void onEnable() {
|
||||
// Create adventure audience
|
||||
this.audiences = FabricServerAudiences.of(minecraftServer);
|
||||
|
||||
// Temporarily log about sounds being disabled - todo: Remove when adventure-platform-fabric is updated
|
||||
if (getSettings().doPlaySoundEffects()) {
|
||||
log(Level.WARNING, "Sound effects are currently disabled for HuskHomes v" + getVersion()
|
||||
+ " on Fabric servers running Minecraft " + getMinecraftServer().getVersion());
|
||||
}
|
||||
|
||||
// Initialize the database
|
||||
initialize(getSettings().getDatabaseType().getDisplayName() + " database connection", (plugin) -> {
|
||||
this.database = switch (getSettings().getDatabaseType()) {
|
||||
case MYSQL, MARIADB -> new MySqlDatabase(this);
|
||||
case SQLITE -> new SqLiteDatabase(this);
|
||||
};
|
||||
|
||||
database.initialize();
|
||||
});
|
||||
|
||||
// Initialize the manager
|
||||
this.manager = new Manager(this);
|
||||
|
||||
// Initialize the network messenger if proxy mode is enabled
|
||||
if (getSettings().doCrossServer()) {
|
||||
initialize(settings.getBrokerType().getDisplayName() + " message broker", (plugin) -> {
|
||||
broker = switch (settings.getBrokerType()) {
|
||||
case PLUGIN_MESSAGE -> new PluginMessageBroker(this);
|
||||
case REDIS -> new RedisBroker(this);
|
||||
};
|
||||
broker.initialize();
|
||||
});
|
||||
}
|
||||
|
||||
setRandomTeleportEngine(new NormalDistributionEngine(this));
|
||||
|
||||
// Register plugin hooks (Economy, Maps, Plan)
|
||||
initialize("hooks", (plugin) -> {
|
||||
this.registerHooks();
|
||||
|
||||
if (hooks.size() > 0) {
|
||||
hooks.forEach(hook -> {
|
||||
try {
|
||||
hook.initialize();
|
||||
} catch (Throwable e) {
|
||||
log(Level.WARNING, "Failed to initialize " + hook.getName() + " hook", e);
|
||||
}
|
||||
});
|
||||
log(Level.INFO, "Registered " + hooks.size() + " mod hooks: " + hooks.stream()
|
||||
.map(Hook::getName)
|
||||
.collect(Collectors.joining(", ")));
|
||||
}
|
||||
});
|
||||
|
||||
// Register events
|
||||
initialize("events", (plugin) -> this.eventListener = new FabricEventListener(this));
|
||||
|
||||
this.checkForUpdates();
|
||||
}
|
||||
|
||||
private void onDisable() {
|
||||
if (this.eventListener != null) {
|
||||
this.eventListener.handlePluginDisable();
|
||||
}
|
||||
if (database != null) {
|
||||
database.terminate();
|
||||
}
|
||||
if (broker != null) {
|
||||
broker.close();
|
||||
}
|
||||
if (audiences != null) {
|
||||
audiences.close();
|
||||
audiences = null;
|
||||
}
|
||||
cancelTasks();
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public ConsoleUser getConsole() {
|
||||
return new ConsoleUser(audiences.console());
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public List<OnlineUser> getOnlineUsers() {
|
||||
return minecraftServer.getPlayerManager().getPlayerList()
|
||||
.stream().map(user -> (OnlineUser) FabricUser.adapt(user, this))
|
||||
.toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public Set<SavedUser> getSavedUsers() {
|
||||
return savedUsers;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public Settings getSettings() {
|
||||
return settings;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSettings(@NotNull Settings settings) {
|
||||
this.settings = settings;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public Locales getLocales() {
|
||||
return locales;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLocales(@NotNull Locales locales) {
|
||||
this.locales = locales;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Spawn> getServerSpawn() {
|
||||
return Optional.ofNullable(serverSpawn);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setServerSpawn(@NotNull Spawn spawn) {
|
||||
this.serverSpawn = spawn;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setServerSpawn(@NotNull Location location) {
|
||||
try {
|
||||
// Create or update the spawn.yml file
|
||||
final File spawnFile = new File(getDataFolder(), "spawn.yml");
|
||||
if (spawnFile.exists() && !spawnFile.delete()) {
|
||||
log(Level.WARNING, "Failed to delete the existing spawn.yml file");
|
||||
}
|
||||
this.serverSpawn = Annotaml.create(spawnFile, new Spawn(location)).get();
|
||||
|
||||
// Update the world spawn location, too
|
||||
minecraftServer.getWorlds().forEach(world -> {
|
||||
if (world.getRegistryKey().getValue().asString().equals(location.getWorld().getName())) {
|
||||
world.setSpawnPos(BlockPos.ofFloored(location.getX(), location.getY(), location.getZ()), 0);
|
||||
}
|
||||
});
|
||||
} catch (IOException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
|
||||
log(Level.WARNING, "Failed to save the server spawn.yml file", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getServerName() {
|
||||
return server.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setServer(@NotNull Server server) {
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUnsafeBlocks(@NotNull UnsafeBlocks unsafeBlocks) {
|
||||
this.unsafeBlocks = unsafeBlocks;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public UnsafeBlocks getUnsafeBlocks() {
|
||||
return unsafeBlocks;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public Database getDatabase() {
|
||||
return database;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public Validator getValidator() {
|
||||
return validator;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public Manager getManager() {
|
||||
return manager;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public Broker getMessenger() {
|
||||
if (broker == null) {
|
||||
throw new IllegalStateException("Attempted to access message broker when it was not initialized");
|
||||
}
|
||||
return broker;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public RandomTeleportEngine getRandomTeleportEngine() {
|
||||
return randomTeleportEngine;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRandomTeleportEngine(@NotNull RandomTeleportEngine randomTeleportEngine) {
|
||||
this.randomTeleportEngine = randomTeleportEngine;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public List<Hook> getHooks() {
|
||||
return hooks;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setHooks(@NotNull List<Hook> hooks) {
|
||||
this.hooks = hooks;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public InputStream getResource(@NotNull String name) {
|
||||
return this.modContainer.findPath(name)
|
||||
.map(path -> {
|
||||
try {
|
||||
return Files.newInputStream(path);
|
||||
} catch (IOException e) {
|
||||
log(Level.WARNING, "Failed to load resource: " + name, e);
|
||||
}
|
||||
return null;
|
||||
})
|
||||
.orElse(this.getClass().getClassLoader().getResourceAsStream(name));
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public File getDataFolder() {
|
||||
return FabricLoader.getInstance().getConfigDir().resolve("huskhomes").toFile();
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public List<World> getWorlds() {
|
||||
final List<World> worlds = new ArrayList<>();
|
||||
minecraftServer.getWorlds().forEach(world -> worlds.add(World.from(
|
||||
world.getRegistryKey().getValue().asString(),
|
||||
UUID.nameUUIDFromBytes(world.getRegistryKey().getValue().asString().getBytes())
|
||||
)));
|
||||
return worlds;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public Version getVersion() {
|
||||
return Version.fromString(modContainer.getMetadata().getVersion().getFriendlyString(), "-");
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public List<Command> getCommands() {
|
||||
return commands;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public List<Command> registerCommands() {
|
||||
final List<Command> commands = FabricCommand.Type.getCommands(getPlugin());
|
||||
CommandRegistrationCallback.EVENT.register((dispatcher, ignored, ignored2) ->
|
||||
commands.forEach(command -> new FabricCommand(command, this).register(dispatcher)));
|
||||
return commands;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDependencyLoaded(@NotNull String name) {
|
||||
return FabricLoader.getInstance().isModLoaded(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public Map<String, List<String>> getGlobalPlayerList() {
|
||||
return globalPlayerList;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public Set<UUID> getCurrentlyOnWarmup() {
|
||||
return currentlyOnWarmup;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerMetrics(int metricsId) {
|
||||
// No metrics for Fabric
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initializePluginChannels() {
|
||||
ServerPlayNetworking.registerGlobalReceiver(new Identifier("bungeecord", "main"), this);
|
||||
}
|
||||
|
||||
// When the server receives a plugin message
|
||||
@Override
|
||||
public void receive(@NotNull MinecraftServer server, @NotNull ServerPlayerEntity player,
|
||||
@NotNull ServerPlayNetworkHandler handler, @NotNull PacketByteBuf buf,
|
||||
@NotNull PacketSender responseSender) {
|
||||
if (broker instanceof PluginMessageBroker messenger
|
||||
&& getSettings().getBrokerType() == Broker.Type.PLUGIN_MESSAGE) {
|
||||
messenger.onReceive(
|
||||
PluginMessageBroker.BUNGEE_CHANNEL_ID,
|
||||
FabricUser.adapt(player, this),
|
||||
ByteBufUtil.getBytes(buf)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void log(@NotNull Level level, @NotNull String message, @NotNull Throwable... exceptions) {
|
||||
LoggingEventBuilder logEvent = LOGGER.makeLoggingEventBuilder(
|
||||
switch (level.getName()) {
|
||||
case "WARNING" -> org.slf4j.event.Level.WARN;
|
||||
case "SEVERE" -> org.slf4j.event.Level.ERROR;
|
||||
default -> org.slf4j.event.Level.INFO;
|
||||
}
|
||||
);
|
||||
if (exceptions.length >= 1) {
|
||||
logEvent = logEvent.setCause(exceptions[0]);
|
||||
}
|
||||
logEvent.log(message);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Map<String, Boolean> getPermissions() {
|
||||
return permissions;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public MinecraftServer getMinecraftServer() {
|
||||
return minecraftServer;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public FabricHuskHomes getPlugin() {
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
/*
|
||||
* 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.command;
|
||||
|
||||
import com.mojang.brigadier.CommandDispatcher;
|
||||
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
||||
import com.mojang.brigadier.tree.LiteralCommandNode;
|
||||
import me.lucko.fabric.api.permissions.v0.PermissionCheckEvent;
|
||||
import me.lucko.fabric.api.permissions.v0.Permissions;
|
||||
import net.fabricmc.fabric.api.util.TriState;
|
||||
import net.minecraft.server.command.ServerCommandSource;
|
||||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
import net.william278.huskhomes.FabricHuskHomes;
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.teleport.TeleportRequest;
|
||||
import net.william278.huskhomes.user.CommandUser;
|
||||
import net.william278.huskhomes.user.FabricUser;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import static com.mojang.brigadier.arguments.StringArgumentType.greedyString;
|
||||
import static net.minecraft.server.command.CommandManager.argument;
|
||||
import static net.minecraft.server.command.CommandManager.literal;
|
||||
|
||||
public class FabricCommand {
|
||||
|
||||
private final FabricHuskHomes plugin;
|
||||
private final Command command;
|
||||
|
||||
public FabricCommand(@NotNull Command command, @NotNull FabricHuskHomes plugin) {
|
||||
this.command = command;
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
public void register(@NotNull CommandDispatcher<ServerCommandSource> dispatcher) {
|
||||
// Register brigadier command
|
||||
final Predicate<ServerCommandSource> predicate = Permissions
|
||||
.require(command.getPermission(), command.isOperatorCommand() ? 3 : 0);
|
||||
final LiteralArgumentBuilder<ServerCommandSource> builder = literal(command.getName())
|
||||
.requires(predicate).executes(getBrigadierExecutor());
|
||||
plugin.getPermissions().put(command.getPermission(), command.isOperatorCommand());
|
||||
if (!command.getRawUsage().isBlank()) {
|
||||
builder.then(argument(command.getRawUsage().replaceAll("[<>\\[\\]]", ""), greedyString())
|
||||
.executes(getBrigadierExecutor())
|
||||
.suggests(getBrigadierSuggester()));
|
||||
}
|
||||
|
||||
// Register additional permissions
|
||||
final Map<String, Boolean> permissions = command.getAdditionalPermissions();
|
||||
permissions.forEach((permission, isOp) -> plugin.getPermissions().put(permission, isOp));
|
||||
PermissionCheckEvent.EVENT.register((player, node) -> {
|
||||
if (permissions.containsKey(node) && permissions.get(node) && player.hasPermissionLevel(3)) {
|
||||
return TriState.TRUE;
|
||||
}
|
||||
return TriState.DEFAULT;
|
||||
});
|
||||
|
||||
// Register aliases
|
||||
final LiteralCommandNode<ServerCommandSource> node = dispatcher.register(builder);
|
||||
dispatcher.register(literal("huskhomes:" + command.getName())
|
||||
.requires(predicate).executes(getBrigadierExecutor()).redirect(node));
|
||||
command.getAliases().forEach(alias -> dispatcher.register(literal(alias)
|
||||
.requires(predicate).executes(getBrigadierExecutor()).redirect(node)));
|
||||
}
|
||||
|
||||
private com.mojang.brigadier.Command<ServerCommandSource> getBrigadierExecutor() {
|
||||
return (context) -> {
|
||||
command.onExecuted(
|
||||
resolveExecutor(context.getSource()),
|
||||
command.removeFirstArg(context.getInput().split(" "))
|
||||
);
|
||||
return 1;
|
||||
};
|
||||
}
|
||||
|
||||
private com.mojang.brigadier.suggestion.SuggestionProvider<ServerCommandSource> getBrigadierSuggester() {
|
||||
if (!(command instanceof TabProvider provider)) {
|
||||
return (context, builder) -> com.mojang.brigadier.suggestion.Suggestions.empty();
|
||||
}
|
||||
return (context, builder) -> {
|
||||
final String[] args = command.removeFirstArg(context.getInput().split(" "));
|
||||
provider.getSuggestions(resolveExecutor(context.getSource()), args).stream()
|
||||
.map(suggestion -> {
|
||||
final String completedArgs = String.join(" ", args);
|
||||
int lastIndex = completedArgs.lastIndexOf(" ");
|
||||
if (lastIndex == -1) {
|
||||
return suggestion;
|
||||
}
|
||||
return completedArgs.substring(0, lastIndex + 1) + suggestion;
|
||||
})
|
||||
.forEach(builder::suggest);
|
||||
return builder.buildFuture();
|
||||
};
|
||||
}
|
||||
|
||||
private CommandUser resolveExecutor(@NotNull ServerCommandSource source) {
|
||||
if (source.getEntity() instanceof ServerPlayerEntity player) {
|
||||
return FabricUser.adapt(player, plugin);
|
||||
}
|
||||
return plugin.getConsole();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Commands available on the Fabric HuskHomes implementation.
|
||||
*/
|
||||
public enum Type {
|
||||
HOME_COMMAND(PrivateHomeCommand::new),
|
||||
SET_HOME_COMMAND(SetHomeCommand::new),
|
||||
HOME_LIST_COMMAND(PrivateHomeListCommand::new),
|
||||
DEL_HOME_COMMAND(DelHomeCommand::new),
|
||||
EDIT_HOME_COMMAND(EditHomeCommand::new),
|
||||
PUBLIC_HOME_COMMAND(PublicHomeCommand::new),
|
||||
PUBLIC_HOME_LIST_COMMAND(PublicHomeListCommand::new),
|
||||
WARP_COMMAND(WarpCommand::new),
|
||||
SET_WARP_COMMAND(SetWarpCommand::new),
|
||||
WARP_LIST_COMMAND(WarpListCommand::new),
|
||||
DEL_WARP_COMMAND(DelWarpCommand::new),
|
||||
EDIT_WARP_COMMAND(EditWarpCommand::new),
|
||||
TP_COMMAND(TpCommand::new),
|
||||
TP_HERE_COMMAND(TpHereCommand::new),
|
||||
TPA_COMMAND((plugin) -> new TeleportRequestCommand(plugin, TeleportRequest.Type.TPA)),
|
||||
TPA_HERE_COMMAND((plugin) -> new TeleportRequestCommand(plugin, TeleportRequest.Type.TPA_HERE)),
|
||||
TPACCEPT_COMMAND((plugin) -> new TpRespondCommand(plugin, true)),
|
||||
TPDECLINE_COMMAND((plugin) -> new TpRespondCommand(plugin, false)),
|
||||
RTP_COMMAND(RtpCommand::new),
|
||||
TP_IGNORE_COMMAND(TpIgnoreCommand::new),
|
||||
TP_OFFLINE_COMMAND(TpOfflineCommand::new),
|
||||
TP_ALL_COMMAND(TpAllCommand::new),
|
||||
TPA_ALL_COMMAND(TpaAllCommand::new),
|
||||
SPAWN_COMMAND(SpawnCommand::new),
|
||||
SET_SPAWN_COMMAND(SetSpawnCommand::new),
|
||||
BACK_COMMAND(BackCommand::new),
|
||||
HUSKHOMES_COMMAND(HuskHomesCommand::new);
|
||||
|
||||
private final Function<HuskHomes, Command> supplier;
|
||||
|
||||
Type(@NotNull Function<HuskHomes, Command> supplier) {
|
||||
this.supplier = supplier;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Command createCommand(@NotNull HuskHomes plugin) {
|
||||
return supplier.apply(plugin);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static List<Command> getCommands(@NotNull FabricHuskHomes plugin) {
|
||||
return Arrays.stream(values())
|
||||
.map(type -> type.createCommand(plugin))
|
||||
.map(command -> plugin.getSettings().isCommandDisabled(command) ? null : command)
|
||||
.filter(Objects::nonNull)
|
||||
.toList();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* 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.event;
|
||||
|
||||
import net.fabricmc.fabric.api.event.Event;
|
||||
import net.fabricmc.fabric.api.event.EventFactory;
|
||||
import net.minecraft.util.ActionResult;
|
||||
import net.william278.huskhomes.user.CommandUser;
|
||||
import net.william278.huskhomes.user.User;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
public interface DeleteAllHomesCallback extends FabricEventCallback<IDeleteAllHomesEvent> {
|
||||
|
||||
@NotNull
|
||||
Event<DeleteAllHomesCallback> EVENT = EventFactory.createArrayBacked(DeleteAllHomesCallback.class,
|
||||
(listeners) -> (event) -> {
|
||||
for (DeleteAllHomesCallback listener : listeners) {
|
||||
final ActionResult result = listener.invoke(event);
|
||||
if (event.isCancelled()) {
|
||||
return ActionResult.CONSUME;
|
||||
} else if (result != ActionResult.PASS) {
|
||||
event.setCancelled(true);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return ActionResult.PASS;
|
||||
});
|
||||
|
||||
@NotNull
|
||||
BiFunction<User, CommandUser, IDeleteAllHomesEvent> SUPPLIER = (owner, deleter) ->
|
||||
new IDeleteAllHomesEvent() {
|
||||
private boolean cancelled = false;
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public User getHomeOwner() {
|
||||
return owner;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CommandUser getDeleter() {
|
||||
return deleter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCancelled(boolean cancelled) {
|
||||
this.cancelled = cancelled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCancelled() {
|
||||
return cancelled;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Event<DeleteAllHomesCallback> getEvent() {
|
||||
return EVENT;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* 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.event;
|
||||
|
||||
import net.fabricmc.fabric.api.event.Event;
|
||||
import net.fabricmc.fabric.api.event.EventFactory;
|
||||
import net.minecraft.util.ActionResult;
|
||||
import net.william278.huskhomes.user.CommandUser;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
public interface DeleteAllWarpsCallback extends FabricEventCallback<IDeleteAllWarpsEvent> {
|
||||
|
||||
@NotNull
|
||||
Event<DeleteAllWarpsCallback> EVENT = EventFactory.createArrayBacked(DeleteAllWarpsCallback.class,
|
||||
(listeners) -> (event) -> {
|
||||
for (DeleteAllWarpsCallback listener : listeners) {
|
||||
final ActionResult result = listener.invoke(event);
|
||||
if (event.isCancelled()) {
|
||||
return ActionResult.CONSUME;
|
||||
} else if (result != ActionResult.PASS) {
|
||||
event.setCancelled(true);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return ActionResult.PASS;
|
||||
});
|
||||
|
||||
@NotNull
|
||||
Function<CommandUser, IDeleteAllWarpsEvent> SUPPLIER = (deleter) ->
|
||||
new IDeleteAllWarpsEvent() {
|
||||
private boolean cancelled = false;
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CommandUser getDeleter() {
|
||||
return deleter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCancelled(boolean cancelled) {
|
||||
this.cancelled = cancelled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCancelled() {
|
||||
return cancelled;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Event<DeleteAllWarpsCallback> getEvent() {
|
||||
return EVENT;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* 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.event;
|
||||
|
||||
import net.minecraft.util.ActionResult;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public interface FabricEventCallback<E extends Event> {
|
||||
|
||||
@NotNull
|
||||
ActionResult invoke(@NotNull E event);
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
/*
|
||||
* 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.event;
|
||||
|
||||
import net.minecraft.util.ActionResult;
|
||||
import net.william278.huskhomes.FabricHuskHomes;
|
||||
import net.william278.huskhomes.position.Home;
|
||||
import net.william278.huskhomes.position.Position;
|
||||
import net.william278.huskhomes.position.Warp;
|
||||
import net.william278.huskhomes.teleport.Teleport;
|
||||
import net.william278.huskhomes.teleport.TeleportRequest;
|
||||
import net.william278.huskhomes.teleport.TimedTeleport;
|
||||
import net.william278.huskhomes.user.CommandUser;
|
||||
import net.william278.huskhomes.user.OnlineUser;
|
||||
import net.william278.huskhomes.user.User;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public interface FabricEventDispatcher extends EventDispatcher {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
default <T extends Event> boolean fireIsCancelled(@NotNull T event) {
|
||||
try {
|
||||
final Method field = event.getClass().getDeclaredMethod("getEvent");
|
||||
field.setAccessible(true);
|
||||
|
||||
net.fabricmc.fabric.api.event.Event<?> fabricEvent =
|
||||
(net.fabricmc.fabric.api.event.Event<?>) field.invoke(event);
|
||||
|
||||
final FabricEventCallback<T> invoker = (FabricEventCallback<T>) fabricEvent.invoker();
|
||||
return invoker.invoke(event) == ActionResult.FAIL;
|
||||
} catch (InvocationTargetException | NoSuchMethodException | IllegalAccessException e) {
|
||||
getPlugin().log(Level.WARNING, "Failed to fire event (" + event.getClass().getName() + ")", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
default ITeleportEvent getTeleportEvent(@NotNull Teleport teleport) {
|
||||
return TeleportCallback.SUPPLIER.apply(teleport);
|
||||
}
|
||||
|
||||
@Override
|
||||
default @NotNull ITeleportWarmupEvent getTeleportWarmupEvent(@NotNull TimedTeleport teleport, int duration) {
|
||||
return TeleportWarmupCallback.SUPPLIER.apply(teleport, duration);
|
||||
}
|
||||
|
||||
@Override
|
||||
default @NotNull ISendTeleportRequestEvent getSendTeleportRequestEvent(@NotNull OnlineUser sender,
|
||||
@NotNull TeleportRequest request) {
|
||||
return SendTeleportRequestCallback.SUPPLIER.apply(sender, request);
|
||||
}
|
||||
|
||||
@Override
|
||||
default @NotNull IReceiveTeleportRequestEvent getReceiveTeleportRequestEvent(@NotNull OnlineUser recipient,
|
||||
@NotNull TeleportRequest request) {
|
||||
return ReceiveTeleportRequestCallback.SUPPLIER.apply(recipient, request);
|
||||
}
|
||||
|
||||
@Override
|
||||
default @NotNull IReplyTeleportRequestEvent getReplyTeleportRequestEvent(@NotNull OnlineUser recipient,
|
||||
@NotNull TeleportRequest request) {
|
||||
return ReplyTeleportRequestCallback.SUPPLIER.apply(recipient, request);
|
||||
}
|
||||
|
||||
@Override
|
||||
default @NotNull IHomeCreateEvent getHomeCreateEvent(@NotNull User owner, @NotNull String name,
|
||||
@NotNull Position position, @NotNull CommandUser creator) {
|
||||
return HomeCreateCallback.SUPPLIER.apply(owner, name, position, creator);
|
||||
}
|
||||
|
||||
@Override
|
||||
default @NotNull IHomeEditEvent getHomeEditEvent(@NotNull Home home, @NotNull CommandUser editor) {
|
||||
return HomeEditCallback.SUPPLIER.apply(home, editor);
|
||||
}
|
||||
|
||||
@Override
|
||||
default @NotNull IHomeDeleteEvent getHomeDeleteEvent(@NotNull Home home, @NotNull CommandUser deleter) {
|
||||
return HomeDeleteCallback.SUPPLIER.apply(home, deleter);
|
||||
}
|
||||
|
||||
@Override
|
||||
default @NotNull IWarpCreateEvent getWarpCreateEvent(@NotNull String name, @NotNull Position position,
|
||||
@NotNull CommandUser creator) {
|
||||
return WarpCreateCallback.SUPPLIER.apply(name, position, creator);
|
||||
}
|
||||
|
||||
@Override
|
||||
default @NotNull IWarpEditEvent getWarpEditEvent(@NotNull Warp warp, @NotNull CommandUser editor) {
|
||||
return WarpEditCallback.SUPPLIER.apply(warp, editor);
|
||||
}
|
||||
|
||||
@Override
|
||||
default @NotNull IWarpDeleteEvent getWarpDeleteEvent(@NotNull Warp warp, @NotNull CommandUser deleter) {
|
||||
return WarpDeleteCallback.SUPPLIER.apply(warp, deleter);
|
||||
}
|
||||
|
||||
@Override
|
||||
default @NotNull IHomeListEvent getViewHomeListEvent(@NotNull List<Home> homes, @NotNull CommandUser listViewer,
|
||||
boolean publicHomeList) {
|
||||
return HomeListCallback.SUPPLIER.apply(homes, listViewer, publicHomeList);
|
||||
}
|
||||
|
||||
@Override
|
||||
default @NotNull IWarpListEvent getViewWarpListEvent(@NotNull List<Warp> homes, @NotNull CommandUser listViewer) {
|
||||
return WarpListCallback.SUPPLIER.apply(homes, listViewer);
|
||||
}
|
||||
|
||||
@Override
|
||||
default @NotNull IDeleteAllHomesEvent getDeleteAllHomesEvent(@NotNull User user, @NotNull CommandUser deleter) {
|
||||
return DeleteAllHomesCallback.SUPPLIER.apply(user, deleter);
|
||||
}
|
||||
|
||||
@Override
|
||||
default @NotNull IDeleteAllWarpsEvent getDeleteAllWarpsEvent(@NotNull CommandUser deleter) {
|
||||
return DeleteAllWarpsCallback.SUPPLIER.apply(deleter);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
FabricHuskHomes getPlugin();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* 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.event;
|
||||
|
||||
import net.fabricmc.fabric.api.event.Event;
|
||||
import net.fabricmc.fabric.api.event.EventFactory;
|
||||
import net.minecraft.data.client.BlockStateVariantMap.QuadFunction;
|
||||
import net.minecraft.util.ActionResult;
|
||||
import net.william278.huskhomes.position.Position;
|
||||
import net.william278.huskhomes.user.CommandUser;
|
||||
import net.william278.huskhomes.user.User;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public interface HomeCreateCallback extends FabricEventCallback<IHomeCreateEvent> {
|
||||
|
||||
@NotNull
|
||||
Event<HomeCreateCallback> EVENT = EventFactory.createArrayBacked(HomeCreateCallback.class,
|
||||
(listeners) -> (event) -> {
|
||||
for (HomeCreateCallback listener : listeners) {
|
||||
final ActionResult result = listener.invoke(event);
|
||||
if (event.isCancelled()) {
|
||||
return ActionResult.CONSUME;
|
||||
} else if (result != ActionResult.PASS) {
|
||||
event.setCancelled(true);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return ActionResult.PASS;
|
||||
});
|
||||
|
||||
@NotNull
|
||||
QuadFunction<User, String, Position, CommandUser, IHomeCreateEvent> SUPPLIER = (user, name, position, creator) ->
|
||||
new IHomeCreateEvent() {
|
||||
private boolean cancelled = false;
|
||||
private String homeName = name;
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public User getOwner() {
|
||||
return user;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getName() {
|
||||
return homeName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setName(@NotNull String name) {
|
||||
this.homeName = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public Position getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPosition(@NotNull Position position) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull CommandUser getCreator() {
|
||||
return creator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCancelled(boolean cancelled) {
|
||||
this.cancelled = cancelled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCancelled() {
|
||||
return cancelled;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Event<HomeCreateCallback> getEvent() {
|
||||
return EVENT;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* 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.event;
|
||||
|
||||
import net.fabricmc.fabric.api.event.Event;
|
||||
import net.fabricmc.fabric.api.event.EventFactory;
|
||||
import net.minecraft.util.ActionResult;
|
||||
import net.william278.huskhomes.position.Home;
|
||||
import net.william278.huskhomes.user.CommandUser;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
public interface HomeDeleteCallback extends FabricEventCallback<IHomeDeleteEvent> {
|
||||
|
||||
@NotNull
|
||||
Event<HomeDeleteCallback> EVENT = EventFactory.createArrayBacked(HomeDeleteCallback.class,
|
||||
(listeners) -> (event) -> {
|
||||
for (HomeDeleteCallback listener : listeners) {
|
||||
final ActionResult result = listener.invoke(event);
|
||||
if (event.isCancelled()) {
|
||||
return ActionResult.CONSUME;
|
||||
} else if (result != ActionResult.PASS) {
|
||||
event.setCancelled(true);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return ActionResult.PASS;
|
||||
});
|
||||
|
||||
@NotNull
|
||||
BiFunction<Home, CommandUser, IHomeDeleteEvent> SUPPLIER = (home, deleter) ->
|
||||
new IHomeDeleteEvent() {
|
||||
private boolean cancelled = false;
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public Home getHome() {
|
||||
return home;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CommandUser getDeleter() {
|
||||
return deleter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCancelled(boolean cancelled) {
|
||||
this.cancelled = cancelled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCancelled() {
|
||||
return cancelled;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Event<HomeDeleteCallback> getEvent() {
|
||||
return EVENT;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* 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.event;
|
||||
|
||||
import net.fabricmc.fabric.api.event.Event;
|
||||
import net.fabricmc.fabric.api.event.EventFactory;
|
||||
import net.minecraft.util.ActionResult;
|
||||
import net.william278.huskhomes.position.Home;
|
||||
import net.william278.huskhomes.user.CommandUser;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
public interface HomeEditCallback extends FabricEventCallback<IHomeEditEvent> {
|
||||
|
||||
@NotNull
|
||||
Event<HomeEditCallback> EVENT = EventFactory.createArrayBacked(HomeEditCallback.class,
|
||||
(listeners) -> (event) -> {
|
||||
for (HomeEditCallback listener : listeners) {
|
||||
final ActionResult result = listener.invoke(event);
|
||||
if (event.isCancelled()) {
|
||||
return ActionResult.CONSUME;
|
||||
} else if (result != ActionResult.PASS) {
|
||||
event.setCancelled(true);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return ActionResult.PASS;
|
||||
});
|
||||
|
||||
@NotNull
|
||||
BiFunction<Home, CommandUser, IHomeEditEvent> SUPPLIER = (home, editor) ->
|
||||
new IHomeEditEvent() {
|
||||
private boolean cancelled = false;
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public Home getHome() {
|
||||
return home;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CommandUser getEditor() {
|
||||
return editor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCancelled(boolean cancelled) {
|
||||
this.cancelled = cancelled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCancelled() {
|
||||
return cancelled;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Event<HomeEditCallback> getEvent() {
|
||||
return EVENT;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* 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.event;
|
||||
|
||||
import net.fabricmc.fabric.api.event.Event;
|
||||
import net.fabricmc.fabric.api.event.EventFactory;
|
||||
import net.minecraft.util.ActionResult;
|
||||
import net.william278.huskhomes.position.Home;
|
||||
import net.william278.huskhomes.user.CommandUser;
|
||||
import org.apache.commons.lang3.function.TriFunction;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface HomeListCallback extends FabricEventCallback<IHomeListEvent> {
|
||||
|
||||
@NotNull
|
||||
Event<HomeListCallback> EVENT = EventFactory.createArrayBacked(HomeListCallback.class,
|
||||
(listeners) -> (event) -> {
|
||||
for (HomeListCallback listener : listeners) {
|
||||
final ActionResult result = listener.invoke(event);
|
||||
if (event.isCancelled()) {
|
||||
return ActionResult.CONSUME;
|
||||
} else if (result != ActionResult.PASS) {
|
||||
event.setCancelled(true);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return ActionResult.PASS;
|
||||
});
|
||||
|
||||
@NotNull
|
||||
TriFunction<List<Home>, CommandUser, Boolean, IHomeListEvent> SUPPLIER = (homes, viewer, publicHomes) ->
|
||||
new IHomeListEvent() {
|
||||
private boolean cancelled = false;
|
||||
private List<Home> listHomes = homes;
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public List<Home> getHomes() {
|
||||
return listHomes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setHomes(@NotNull List<Home> homes) {
|
||||
this.listHomes = homes;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CommandUser getListViewer() {
|
||||
return viewer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getIsPublicHomeList() {
|
||||
return publicHomes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCancelled(boolean cancelled) {
|
||||
this.cancelled = cancelled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCancelled() {
|
||||
return cancelled;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Event<HomeListCallback> getEvent() {
|
||||
return EVENT;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* 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.event;
|
||||
|
||||
import net.fabricmc.fabric.api.event.Event;
|
||||
import net.fabricmc.fabric.api.event.EventFactory;
|
||||
import net.minecraft.util.ActionResult;
|
||||
import net.william278.huskhomes.teleport.TeleportRequest;
|
||||
import net.william278.huskhomes.user.OnlineUser;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
public interface ReceiveTeleportRequestCallback extends FabricEventCallback<IReceiveTeleportRequestEvent> {
|
||||
|
||||
@NotNull
|
||||
Event<ReceiveTeleportRequestCallback> EVENT = EventFactory.createArrayBacked(ReceiveTeleportRequestCallback.class,
|
||||
(listeners) -> (event) -> {
|
||||
for (ReceiveTeleportRequestCallback listener : listeners) {
|
||||
final ActionResult result = listener.invoke(event);
|
||||
if (event.isCancelled()) {
|
||||
return ActionResult.CONSUME;
|
||||
} else if (result != ActionResult.PASS) {
|
||||
event.setCancelled(true);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return ActionResult.PASS;
|
||||
});
|
||||
|
||||
@NotNull
|
||||
BiFunction<OnlineUser, TeleportRequest, IReceiveTeleportRequestEvent> SUPPLIER = (recipient, request) ->
|
||||
new IReceiveTeleportRequestEvent() {
|
||||
private boolean cancelled = false;
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public OnlineUser getRecipient() {
|
||||
return recipient;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public TeleportRequest getRequest() {
|
||||
return request;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCancelled(boolean cancelled) {
|
||||
this.cancelled = cancelled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCancelled() {
|
||||
return cancelled;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Event<ReceiveTeleportRequestCallback> getEvent() {
|
||||
return EVENT;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* 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.event;
|
||||
|
||||
import net.fabricmc.fabric.api.event.Event;
|
||||
import net.fabricmc.fabric.api.event.EventFactory;
|
||||
import net.minecraft.util.ActionResult;
|
||||
import net.william278.huskhomes.teleport.TeleportRequest;
|
||||
import net.william278.huskhomes.user.OnlineUser;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
public interface ReplyTeleportRequestCallback extends FabricEventCallback<IReplyTeleportRequestEvent> {
|
||||
|
||||
@NotNull
|
||||
Event<ReplyTeleportRequestCallback> EVENT = EventFactory.createArrayBacked(ReplyTeleportRequestCallback.class,
|
||||
(listeners) -> (event) -> {
|
||||
for (ReplyTeleportRequestCallback listener : listeners) {
|
||||
final ActionResult result = listener.invoke(event);
|
||||
if (event.isCancelled()) {
|
||||
return ActionResult.CONSUME;
|
||||
} else if (result != ActionResult.PASS) {
|
||||
event.setCancelled(true);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return ActionResult.PASS;
|
||||
});
|
||||
|
||||
@NotNull
|
||||
BiFunction<OnlineUser, TeleportRequest, IReplyTeleportRequestEvent> SUPPLIER = (recipient, request) ->
|
||||
new IReplyTeleportRequestEvent() {
|
||||
private boolean cancelled = false;
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public OnlineUser getRecipient() {
|
||||
return recipient;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public TeleportRequest getRequest() {
|
||||
return request;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCancelled(boolean cancelled) {
|
||||
this.cancelled = cancelled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCancelled() {
|
||||
return cancelled;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Event<ReplyTeleportRequestCallback> getEvent() {
|
||||
return EVENT;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* 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.event;
|
||||
|
||||
import net.fabricmc.fabric.api.event.Event;
|
||||
import net.fabricmc.fabric.api.event.EventFactory;
|
||||
import net.minecraft.util.ActionResult;
|
||||
import net.william278.huskhomes.teleport.TeleportRequest;
|
||||
import net.william278.huskhomes.user.OnlineUser;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
public interface SendTeleportRequestCallback extends FabricEventCallback<ISendTeleportRequestEvent> {
|
||||
|
||||
@NotNull
|
||||
Event<SendTeleportRequestCallback> EVENT = EventFactory.createArrayBacked(SendTeleportRequestCallback.class,
|
||||
(listeners) -> (event) -> {
|
||||
for (SendTeleportRequestCallback listener : listeners) {
|
||||
final ActionResult result = listener.invoke(event);
|
||||
if (event.isCancelled()) {
|
||||
return ActionResult.CONSUME;
|
||||
} else if (result != ActionResult.PASS) {
|
||||
event.setCancelled(true);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return ActionResult.PASS;
|
||||
});
|
||||
|
||||
@NotNull
|
||||
BiFunction<OnlineUser, TeleportRequest, ISendTeleportRequestEvent> SUPPLIER = (sender, request) ->
|
||||
new ISendTeleportRequestEvent() {
|
||||
private boolean cancelled = false;
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public OnlineUser getSender() {
|
||||
return sender;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public TeleportRequest getRequest() {
|
||||
return request;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCancelled(boolean cancelled) {
|
||||
this.cancelled = cancelled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCancelled() {
|
||||
return cancelled;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Event<SendTeleportRequestCallback> getEvent() {
|
||||
return EVENT;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* 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.event;
|
||||
|
||||
import net.fabricmc.fabric.api.event.Event;
|
||||
import net.fabricmc.fabric.api.event.EventFactory;
|
||||
import net.minecraft.util.ActionResult;
|
||||
import net.william278.huskhomes.position.Position;
|
||||
import net.william278.huskhomes.teleport.Teleport;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
public interface TeleportBackCallback extends FabricEventCallback<ITeleportBackEvent> {
|
||||
|
||||
@NotNull
|
||||
Event<TeleportBackCallback> EVENT = EventFactory.createArrayBacked(TeleportBackCallback.class,
|
||||
(listeners) -> (event) -> {
|
||||
for (TeleportBackCallback listener : listeners) {
|
||||
final ActionResult result = listener.invoke(event);
|
||||
if (event.isCancelled()) {
|
||||
return ActionResult.FAIL;
|
||||
} else if (result == ActionResult.FAIL) {
|
||||
event.setCancelled(true);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return ActionResult.PASS;
|
||||
});
|
||||
|
||||
@NotNull
|
||||
Function<Teleport, ITeleportBackEvent> SUPPLIER = (teleport) ->
|
||||
new ITeleportBackEvent() {
|
||||
private boolean cancelled = false;
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public Position getLastPosition() {
|
||||
return (Position) getTeleport().getTarget();
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public Teleport getTeleport() {
|
||||
return teleport;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCancelled(boolean cancelled) {
|
||||
this.cancelled = cancelled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCancelled() {
|
||||
return cancelled;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Event<TeleportBackCallback> getEvent() {
|
||||
return EVENT;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* 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.event;
|
||||
|
||||
import net.fabricmc.fabric.api.event.Event;
|
||||
import net.fabricmc.fabric.api.event.EventFactory;
|
||||
import net.minecraft.util.ActionResult;
|
||||
import net.william278.huskhomes.teleport.Teleport;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
public interface TeleportCallback extends FabricEventCallback<ITeleportEvent> {
|
||||
|
||||
@NotNull
|
||||
Event<TeleportCallback> EVENT = EventFactory.createArrayBacked(TeleportCallback.class,
|
||||
(listeners) -> (event) -> {
|
||||
for (TeleportCallback listener : listeners) {
|
||||
final ActionResult result = listener.invoke(event);
|
||||
if (event.isCancelled()) {
|
||||
return ActionResult.FAIL;
|
||||
} else if (result == ActionResult.FAIL) {
|
||||
event.setCancelled(true);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return ActionResult.PASS;
|
||||
});
|
||||
|
||||
@NotNull
|
||||
Function<Teleport, ITeleportEvent> SUPPLIER = (teleport) ->
|
||||
new ITeleportEvent() {
|
||||
private boolean cancelled = false;
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public Teleport getTeleport() {
|
||||
return teleport;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCancelled(boolean cancelled) {
|
||||
this.cancelled = cancelled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCancelled() {
|
||||
return cancelled;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Event<TeleportCallback> getEvent() {
|
||||
return EVENT;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* 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.event;
|
||||
|
||||
import net.fabricmc.fabric.api.event.Event;
|
||||
import net.fabricmc.fabric.api.event.EventFactory;
|
||||
import net.minecraft.util.ActionResult;
|
||||
import net.william278.huskhomes.teleport.TimedTeleport;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
public interface TeleportWarmupCallback extends FabricEventCallback<ITeleportWarmupEvent> {
|
||||
@NotNull
|
||||
Event<TeleportWarmupCallback> EVENT = EventFactory.createArrayBacked(TeleportWarmupCallback.class,
|
||||
(listeners) -> (event) -> {
|
||||
for (TeleportWarmupCallback listener : listeners) {
|
||||
final ActionResult result = listener.invoke(event);
|
||||
if (event.isCancelled()) {
|
||||
return ActionResult.FAIL;
|
||||
} else if (result == ActionResult.FAIL) {
|
||||
event.setCancelled(true);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return ActionResult.PASS;
|
||||
});
|
||||
|
||||
@NotNull
|
||||
BiFunction<TimedTeleport, Integer, ITeleportWarmupEvent> SUPPLIER = (timedTeleport, duration) ->
|
||||
new ITeleportWarmupEvent() {
|
||||
private boolean cancelled = false;
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public TimedTeleport getTimedTeleport() {
|
||||
return timedTeleport;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWarmupDuration() {
|
||||
return duration;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCancelled(boolean cancelled) {
|
||||
this.cancelled = cancelled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCancelled() {
|
||||
return cancelled;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Event<TeleportWarmupCallback> getEvent() {
|
||||
return EVENT;
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* 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.event;
|
||||
|
||||
import net.fabricmc.fabric.api.event.Event;
|
||||
import net.fabricmc.fabric.api.event.EventFactory;
|
||||
import net.minecraft.util.ActionResult;
|
||||
import net.william278.huskhomes.position.Position;
|
||||
import net.william278.huskhomes.user.CommandUser;
|
||||
import org.apache.commons.lang3.function.TriFunction;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public interface WarpCreateCallback extends FabricEventCallback<IWarpCreateEvent> {
|
||||
|
||||
@NotNull
|
||||
Event<WarpCreateCallback> EVENT = EventFactory.createArrayBacked(WarpCreateCallback.class,
|
||||
(listeners) -> (event) -> {
|
||||
for (WarpCreateCallback listener : listeners) {
|
||||
final ActionResult result = listener.invoke(event);
|
||||
if (event.isCancelled()) {
|
||||
return ActionResult.CONSUME;
|
||||
} else if (result != ActionResult.PASS) {
|
||||
event.setCancelled(true);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return ActionResult.PASS;
|
||||
});
|
||||
|
||||
@NotNull
|
||||
TriFunction<String, Position, CommandUser, IWarpCreateEvent> SUPPLIER = (name, position, creator) ->
|
||||
new IWarpCreateEvent() {
|
||||
private boolean cancelled = false;
|
||||
private String homeName = name;
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getName() {
|
||||
return homeName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setName(@NotNull String name) {
|
||||
this.homeName = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public Position getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPosition(@NotNull Position position) {
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CommandUser getCreator() {
|
||||
return creator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCancelled(boolean cancelled) {
|
||||
this.cancelled = cancelled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCancelled() {
|
||||
return cancelled;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Event<WarpCreateCallback> getEvent() {
|
||||
return EVENT;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* 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.event;
|
||||
|
||||
import net.fabricmc.fabric.api.event.Event;
|
||||
import net.fabricmc.fabric.api.event.EventFactory;
|
||||
import net.minecraft.util.ActionResult;
|
||||
import net.william278.huskhomes.position.Warp;
|
||||
import net.william278.huskhomes.user.CommandUser;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
public interface WarpDeleteCallback extends FabricEventCallback<IWarpDeleteEvent> {
|
||||
|
||||
@NotNull
|
||||
Event<WarpDeleteCallback> EVENT = EventFactory.createArrayBacked(WarpDeleteCallback.class,
|
||||
(listeners) -> (event) -> {
|
||||
for (WarpDeleteCallback listener : listeners) {
|
||||
final ActionResult result = listener.invoke(event);
|
||||
if (event.isCancelled()) {
|
||||
return ActionResult.CONSUME;
|
||||
} else if (result != ActionResult.PASS) {
|
||||
event.setCancelled(true);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return ActionResult.PASS;
|
||||
});
|
||||
|
||||
@NotNull
|
||||
BiFunction<Warp, CommandUser, IWarpDeleteEvent> SUPPLIER = (warp, deleter) ->
|
||||
new IWarpDeleteEvent() {
|
||||
private boolean cancelled = false;
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public Warp getWarp() {
|
||||
return warp;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CommandUser getDeleter() {
|
||||
return deleter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCancelled(boolean cancelled) {
|
||||
this.cancelled = cancelled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCancelled() {
|
||||
return cancelled;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Event<WarpDeleteCallback> getEvent() {
|
||||
return EVENT;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* 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.event;
|
||||
|
||||
import net.fabricmc.fabric.api.event.Event;
|
||||
import net.fabricmc.fabric.api.event.EventFactory;
|
||||
import net.minecraft.util.ActionResult;
|
||||
import net.william278.huskhomes.position.Warp;
|
||||
import net.william278.huskhomes.user.CommandUser;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
public interface WarpListCallback extends FabricEventCallback<IWarpListEvent> {
|
||||
|
||||
@NotNull
|
||||
Event<WarpListCallback> EVENT = EventFactory.createArrayBacked(WarpListCallback.class,
|
||||
(listeners) -> (event) -> {
|
||||
for (WarpListCallback listener : listeners) {
|
||||
final ActionResult result = listener.invoke(event);
|
||||
if (event.isCancelled()) {
|
||||
return ActionResult.CONSUME;
|
||||
} else if (result != ActionResult.PASS) {
|
||||
event.setCancelled(true);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return ActionResult.PASS;
|
||||
});
|
||||
|
||||
@NotNull
|
||||
BiFunction<List<Warp>, CommandUser, IWarpListEvent> SUPPLIER = (warps, viewer) ->
|
||||
new IWarpListEvent() {
|
||||
private boolean cancelled = false;
|
||||
private List<Warp> listWarps = warps;
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public List<Warp> getWarps() {
|
||||
return listWarps;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setWarps(@NotNull List<Warp> homes) {
|
||||
this.listWarps = homes;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public CommandUser getListViewer() {
|
||||
return viewer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCancelled(boolean cancelled) {
|
||||
this.cancelled = cancelled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCancelled() {
|
||||
return cancelled;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Event<WarpListCallback> getEvent() {
|
||||
return EVENT;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* 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.listener;
|
||||
|
||||
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;
|
||||
import net.fabricmc.fabric.api.entity.event.v1.ServerPlayerEvents;
|
||||
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
|
||||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
import net.william278.huskhomes.FabricHuskHomes;
|
||||
import net.william278.huskhomes.user.FabricUser;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
// Note that the teleport event and update player respawn position events are not handled on Fabric.
|
||||
// The "update last position on teleport event" and "global respawn" features are not supported on Fabric.
|
||||
public class FabricEventListener extends EventListener {
|
||||
|
||||
public FabricEventListener(@NotNull FabricHuskHomes plugin) {
|
||||
super(plugin);
|
||||
this.registerEvents(plugin);
|
||||
}
|
||||
|
||||
// Register fabric event callback listeners to internal handlers
|
||||
private void registerEvents(@NotNull FabricHuskHomes plugin) {
|
||||
// Join event
|
||||
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> handlePlayerJoin(
|
||||
FabricUser.adapt(handler.player, plugin)
|
||||
));
|
||||
|
||||
// Quit event
|
||||
ServerPlayConnectionEvents.DISCONNECT.register((handler, server) -> handlePlayerLeave(
|
||||
FabricUser.adapt(handler.player, plugin)
|
||||
));
|
||||
|
||||
// Death event
|
||||
ServerLivingEntityEvents.AFTER_DEATH.register((entity, source) -> {
|
||||
if (entity instanceof ServerPlayerEntity player) {
|
||||
handlePlayerDeath(FabricUser.adapt(player, plugin));
|
||||
}
|
||||
});
|
||||
|
||||
// Respawn event
|
||||
ServerPlayerEvents.AFTER_RESPAWN.register((oldPlayer, newPlayer, alive) -> handlePlayerRespawn(
|
||||
FabricUser.adapt(newPlayer, plugin)
|
||||
));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
/*
|
||||
* 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.user;
|
||||
|
||||
import me.lucko.fabric.api.permissions.v0.Permissions;
|
||||
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.minecraft.network.PacketByteBuf;
|
||||
import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.william278.huskhomes.FabricHuskHomes;
|
||||
import net.william278.huskhomes.position.Location;
|
||||
import net.william278.huskhomes.position.Position;
|
||||
import net.william278.huskhomes.position.World;
|
||||
import net.william278.huskhomes.teleport.TeleportationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class FabricUser extends OnlineUser {
|
||||
|
||||
private final FabricHuskHomes plugin;
|
||||
private final ServerPlayerEntity player;
|
||||
|
||||
private FabricUser(@NotNull ServerPlayerEntity player, @NotNull FabricHuskHomes plugin) {
|
||||
super(player.getUuid(), player.getEntityName());
|
||||
this.player = player;
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static FabricUser adapt(@NotNull ServerPlayerEntity player, @NotNull FabricHuskHomes plugin) {
|
||||
return new FabricUser(player, plugin);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
return Position.at(
|
||||
player.getX(), player.getY(), player.getZ(),
|
||||
player.getYaw(), player.getPitch(),
|
||||
World.from(
|
||||
player.getWorld().getRegistryKey().getValue().asString(),
|
||||
UUID.nameUUIDFromBytes(player.getWorld().getRegistryKey().getValue().asString().getBytes())
|
||||
),
|
||||
plugin.getServerName()
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Position> getBedSpawnPosition() {
|
||||
final BlockPos spawn = player.getSpawnPointPosition();
|
||||
if (spawn == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
return Optional.of(Position.at(
|
||||
spawn.getX(), spawn.getY(), spawn.getZ(),
|
||||
player.getSpawnAngle(), 0,
|
||||
World.from(
|
||||
player.getSpawnPointDimension().getValue().asString(),
|
||||
UUID.nameUUIDFromBytes(player.getSpawnPointDimension().getValue().asString().getBytes())
|
||||
),
|
||||
plugin.getServerName()
|
||||
));
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getHealth() {
|
||||
return player.getHealth();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(@NotNull String node) {
|
||||
final boolean requiresOp = Boolean.TRUE.equals(plugin.getPermissions().getOrDefault(node, true));
|
||||
return Permissions.check(player, node, !requiresOp || player.hasPermissionLevel(3));
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public Map<String, Boolean> getPermissions() {
|
||||
return plugin.getPermissions().entrySet().stream()
|
||||
.filter(entry -> Permissions.check(player, entry.getKey(), entry.getValue()))
|
||||
.collect(HashMap::new, (map, entry) -> map.put(entry.getKey(), entry.getValue()), HashMap::putAll);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
protected List<Integer> getNumericalPermissions(@NotNull String nodePrefix) {
|
||||
final List<Integer> permissions = new ArrayList<>();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
if (hasPermission(nodePrefix + i)) {
|
||||
permissions.add(i);
|
||||
}
|
||||
}
|
||||
return permissions.stream().sorted(Collections.reverseOrder()).toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public Audience getAudience() {
|
||||
return player;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void playSound(@NotNull String soundEffect) throws IllegalArgumentException {
|
||||
// Do nothing - todo: Fix sounds when adventure-platform-fabric is updated.
|
||||
}
|
||||
|
||||
@Override
|
||||
public void teleportLocally(@NotNull Location location, boolean async) throws TeleportationException {
|
||||
final MinecraftServer server = player.getServer();
|
||||
assert server != null : "Server is null";
|
||||
final Identifier worldId = Identifier.tryParse(location.getWorld().getName());
|
||||
player.teleport(
|
||||
server.getWorld(server.getWorldRegistryKeys().stream()
|
||||
.filter(key -> key.getValue().equals(worldId)).findFirst().orElseThrow(
|
||||
() -> new TeleportationException(TeleportationException.Type.WORLD_NOT_FOUND, plugin))
|
||||
),
|
||||
location.getX(), location.getY(), location.getZ(),
|
||||
location.getYaw(), location.getPitch()
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendPluginMessage(@NotNull String channel, byte[] message) {
|
||||
final PacketByteBuf buf = PacketByteBufs.create();
|
||||
buf.writeBytes(message);
|
||||
final CustomPayloadS2CPacket packet = new CustomPayloadS2CPacket(parseIdentifier(channel), buf);
|
||||
player.networkHandler.sendPacket(packet);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMoving() {
|
||||
return player.isTouchingWater() || player.isFallFlying() || player.isSprinting() || player.isSneaking();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isVanished() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static Identifier parseIdentifier(@NotNull String channel) {
|
||||
if (channel.equals("BungeeCord")) {
|
||||
return new Identifier("bungeecord", "main");
|
||||
}
|
||||
return Optional.ofNullable(Identifier.tryParse(channel))
|
||||
.orElseThrow(() -> new IllegalArgumentException("Invalid channel name: " + channel));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* 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.util;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.FireBlock;
|
||||
import net.minecraft.block.FluidBlock;
|
||||
import net.minecraft.registry.Registries;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.world.ServerWorld;
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.math.Direction;
|
||||
import net.minecraft.world.BlockView;
|
||||
import net.william278.huskhomes.FabricHuskHomes;
|
||||
import net.william278.huskhomes.position.Location;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public interface FabricSafetyResolver extends SafetyResolver {
|
||||
|
||||
@Override
|
||||
default CompletableFuture<Optional<Location>> findSafeGroundLocation(@NotNull Location location) {
|
||||
final MinecraftServer server = ((FabricHuskHomes) getPlugin()).getMinecraftServer();
|
||||
final Identifier worldId = Identifier.tryParse(location.getWorld().getName());
|
||||
|
||||
// Ensure the location is on a valid world
|
||||
final Optional<ServerWorld> locationWorld = server.getWorldRegistryKeys().stream()
|
||||
.filter(key -> key.getValue().equals(worldId)).findFirst()
|
||||
.map(server::getWorld);
|
||||
if (locationWorld.isEmpty()) {
|
||||
return CompletableFuture.completedFuture(Optional.empty());
|
||||
}
|
||||
|
||||
// Ensure the location is within the world border
|
||||
final ServerWorld world = locationWorld.get();
|
||||
if (!world.getWorldBorder().contains(location.getX(), location.getZ())) {
|
||||
return CompletableFuture.completedFuture(Optional.empty());
|
||||
}
|
||||
|
||||
return CompletableFuture.completedFuture(findSafeLocationNear(location, world));
|
||||
}
|
||||
|
||||
/**
|
||||
* Search for a safe ground location near the given location.
|
||||
*
|
||||
* @param location The location to search around
|
||||
* @return An optional safe location, within 4 blocks of the given location
|
||||
*/
|
||||
private Optional<Location> findSafeLocationNear(@NotNull Location location, @NotNull ServerWorld world) {
|
||||
final BlockPos.Mutable blockPos = new BlockPos.Mutable(location.getX(), location.getY(), location.getZ());
|
||||
for (int x = -SEARCH_RADIUS; x <= SEARCH_RADIUS; x++) {
|
||||
for (int z = -SEARCH_RADIUS; z <= SEARCH_RADIUS; z++) {
|
||||
blockPos.set(location.getX() + x, location.getY(), location.getZ() + z);
|
||||
final int highestY = getHighestYAt(world, blockPos.getX(), blockPos.getY(), blockPos.getZ()) + 1;
|
||||
|
||||
final Block block = world.getBlockState(blockPos.withY(highestY - 1)).getBlock();
|
||||
final Identifier id = Registries.BLOCK.getId(block);
|
||||
|
||||
final Block bodyBlockType = world.getBlockState(blockPos.withY(highestY)).getBlock();
|
||||
final Identifier bodyBlockId = Registries.BLOCK.getId(bodyBlockType);
|
||||
|
||||
final Block headBlockType = world.getBlockState(blockPos.withY(highestY + 1)).getBlock();
|
||||
final Identifier headBlockId = Registries.BLOCK.getId(headBlockType);
|
||||
|
||||
if (!(block instanceof FluidBlock) && !(block instanceof FireBlock)
|
||||
&& isBlockSafeForStanding(id.toString()) && isBlockSafeForOccupation(bodyBlockId.toString())
|
||||
&& isBlockSafeForOccupation(headBlockId.toString())) {
|
||||
double locx = blockPos.getX();
|
||||
if (locx < 0) {
|
||||
locx += 1.5d;
|
||||
} else {
|
||||
locx = locx + 0.5d;
|
||||
}
|
||||
double locz = blockPos.getZ();
|
||||
if (locz < 0) {
|
||||
locz += 1.5d;
|
||||
} else {
|
||||
locz = locz + 0.5d;
|
||||
}
|
||||
return Optional.of(Location.at(
|
||||
locx,
|
||||
highestY,
|
||||
locz,
|
||||
location.getWorld()
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the highest Y value at the given X and Z coordinates.
|
||||
*
|
||||
* @param blockView The block view to search
|
||||
* @param x The X coordinate
|
||||
* @param y The Y coordinate
|
||||
* @param z The Z coordinate
|
||||
* @return The highest Y value at the given X and Z coordinates
|
||||
*/
|
||||
private int getHighestYAt(@NotNull BlockView blockView, int x, int y, int z) {
|
||||
final BlockPos.Mutable cursor = new BlockPos.Mutable(x, y, z);
|
||||
while (blockView.getBlockState(cursor).isAir() && cursor.getY() > blockView.getBottomY()) {
|
||||
cursor.move(Direction.DOWN);
|
||||
}
|
||||
return cursor.getY();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
/*
|
||||
* 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.util;
|
||||
|
||||
import net.william278.huskhomes.FabricHuskHomes;
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public interface FabricTask extends Task {
|
||||
|
||||
class Sync extends Task.Sync implements FabricTask {
|
||||
|
||||
protected Sync(@NotNull HuskHomes plugin, @NotNull Runnable runnable, long delayTicks) {
|
||||
super(plugin, runnable, delayTicks);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancel() {
|
||||
super.cancel();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (!cancelled) {
|
||||
Executors.newSingleThreadScheduledExecutor().schedule(
|
||||
() -> ((FabricHuskHomes) getPlugin()).getMinecraftServer().executeSync(runnable),
|
||||
delayTicks * 50,
|
||||
TimeUnit.MILLISECONDS
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Async extends Task.Async implements FabricTask {
|
||||
private CompletableFuture<Void> task;
|
||||
|
||||
protected Async(@NotNull HuskHomes plugin, @NotNull Runnable runnable) {
|
||||
super(plugin, runnable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancel() {
|
||||
if (task != null && !cancelled) {
|
||||
task.cancel(true);
|
||||
}
|
||||
super.cancel();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (!cancelled) {
|
||||
this.task = CompletableFuture.runAsync(runnable, ((FabricHuskHomes) getPlugin()).getMinecraftServer());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Repeating extends Task.Repeating implements FabricTask {
|
||||
|
||||
private ScheduledFuture<?> task;
|
||||
|
||||
protected Repeating(@NotNull HuskHomes plugin, @NotNull Runnable runnable, long repeatingTicks) {
|
||||
super(plugin, runnable, repeatingTicks);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancel() {
|
||||
if (task != null && !cancelled) {
|
||||
task.cancel(true);
|
||||
}
|
||||
super.cancel();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (!cancelled) {
|
||||
this.task = Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(
|
||||
runnable,
|
||||
0,
|
||||
repeatingTicks * 50,
|
||||
TimeUnit.MILLISECONDS
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
interface Supplier extends Task.Supplier {
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
default Task.Sync getSyncTask(@NotNull Runnable runnable, long delayTicks) {
|
||||
return new Sync(getPlugin(), runnable, delayTicks);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
default Task.Async getAsyncTask(@NotNull Runnable runnable) {
|
||||
return new Async(getPlugin(), runnable);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
default Task.Repeating getRepeatingTask(@NotNull Runnable runnable, long repeatingTicks) {
|
||||
return new Repeating(getPlugin(), runnable, repeatingTicks);
|
||||
}
|
||||
|
||||
@Override
|
||||
default void cancelTasks() {
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user