Skip to content

Commit

Permalink
feat: add initial minestom support
Browse files Browse the repository at this point in the history
  • Loading branch information
yusshu committed Aug 26, 2023
1 parent 7173aa0 commit 5a43985
Show file tree
Hide file tree
Showing 34 changed files with 1,289 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {

tasks {
processResources {
filesMatching("**.yml") {
filesMatching(listOf("**plugin.yml", "**extension.json")) {
expand("project" to project)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.command.PluginCommand;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;
Expand All @@ -36,17 +35,22 @@
import team.unnamed.creative.ResourcePack;
import team.unnamed.creative.central.CreativeCentral;
import team.unnamed.creative.central.CreativeCentralProvider;
import team.unnamed.creative.central.bukkit.action.ActionManager;
import team.unnamed.creative.central.bukkit.command.MainCommand;
import team.unnamed.creative.central.bukkit.listener.CreativeResourcePackStatusListener;
import team.unnamed.creative.central.bukkit.listener.ResourcePackSendListener;
import team.unnamed.creative.central.bukkit.listener.ResourcePackStatusListener;
import team.unnamed.creative.central.bukkit.request.BukkitResourcePackRequestSender;
import team.unnamed.creative.central.bukkit.util.Components;
import team.unnamed.creative.central.bukkit.util.PluginResources;
import team.unnamed.creative.central.common.config.Configuration;
import team.unnamed.creative.central.common.config.ExportConfiguration;
import team.unnamed.creative.central.common.config.YamlConfigurationLoader;
import team.unnamed.creative.central.common.util.Components;
import team.unnamed.creative.central.common.event.EventBusImpl;
import team.unnamed.creative.central.common.event.EventExceptionHandler;
import team.unnamed.creative.central.common.export.ResourcePackExporterFactory;
import team.unnamed.creative.central.common.server.CommonResourcePackServer;
import team.unnamed.creative.central.common.util.LocalAddressProvider;
import team.unnamed.creative.central.common.util.Monitor;
import team.unnamed.creative.central.common.util.Streams;
import team.unnamed.creative.central.event.EventBus;
import team.unnamed.creative.central.event.pack.ResourcePackGenerateEvent;
Expand Down Expand Up @@ -76,18 +80,21 @@ public final class CreativeCentralPlugin extends JavaPlugin implements CreativeC
private ResourcePackRequestSender requestSender;
private CentralResourcePackServer resourcePackServer;

private Monitor<Configuration> configurationMonitor;

@Override
public void onEnable() {
saveDefaultConfig();
Configuration config = YamlConfigurationLoader.load(PluginResources.get(this, "config.yml"));
this.configurationMonitor = Monitor.monitor(config);

serveOptions = new ServeOptions();
eventBus = new EventBusImpl<>(Plugin.class, getLogger());
eventBus = new EventBusImpl<>(Plugin.class, EventExceptionHandler.logging(getLogger()));
requestSender = BukkitResourcePackRequestSender.bukkit();
resourcePackServer = new CommonResourcePackServer();

// load serve/send options
serveOptions.serve(true);
serveOptions.delay(getConfig().getInt("send.delay"));
serveOptions.delay(config.send().delay());

// register event listeners
listen(
Expand All @@ -103,10 +110,7 @@ public void onEnable() {
command.setTabCompleter(mainCommandHandler);

// load actions
ActionManager actionManager = new ActionManager();
ConfigurationSection feedbackSection = getConfig().getConfigurationSection("feedback");
actionManager.load(feedbackSection);
eventBus.listen(this, ResourcePackStatusEvent.class, new CreativeResourcePackStatusListener(actionManager));
eventBus.listen(this, ResourcePackStatusEvent.class, new CreativeResourcePackStatusListener(configurationMonitor));

// start resource pack server if enabled
loadResourcePackServer();
Expand All @@ -123,6 +127,10 @@ public void onEnable() {
}, 1L);
}

public Monitor<Configuration> config() {
return configurationMonitor;
}

private void registerService() {
Bukkit.getServicesManager().register(CreativeCentral.class, this, this, ServicePriority.High);
CreativeCentralProvider.set(this);
Expand All @@ -134,22 +142,21 @@ private void unregisterService() {
}

private void loadResourcePackServer() {
ConfigurationSection config = getConfig().getConfigurationSection("export.localhost");
boolean enabled = config != null && config.getBoolean("enabled");
ExportConfiguration.LocalHostExportConfiguration config = configurationMonitor.get().export().localHost();

if (!enabled) {
if (!config.enabled()) {
return;
}

getLogger().info("Resource-pack server enabled, starting...");

String address = getConfig().getString("export.localhost.address", "");
int port = getConfig().getInt("export.localhost.port");
String address = config.address();
int port = config.port();

// if address is empty, automatically detect the server's address
if (address.trim().isEmpty()) {
try {
address = LocalAddressProvider.getLocalAddress(getConfig().getStringList("--what-is-my-ip-services"));
address = LocalAddressProvider.getLocalAddress(configurationMonitor.get().whatIsMyIpServices());
} catch (IOException e) {
getLogger().log(Level.SEVERE, "An exception was caught when trying to get the local server address", e);
}
Expand All @@ -175,6 +182,8 @@ public ResourcePack generateSync() {
" generate the resource pack. Is the server shutting down?");
}

Configuration config = configurationMonitor.get();

getLogger().info("Generating resource-pack...");

File resourcesFolder = new File(getDataFolder(), "resources");
Expand Down Expand Up @@ -227,7 +236,7 @@ public ResourcePack generateSync() {
@Nullable ResourcePackLocation location = null;
{
getLogger().info("Exporting resource pack...");
String exportType = getConfig().getString("export.type", "mcpacks");
String exportType = config.export().type();
ResourcePackExporter exporter = ResourcePackExporterFactory.create(exportType, getDataFolder(), resourcePackServer, getLogger());
try {
location = exporter.export(resourcePack);
Expand All @@ -240,8 +249,8 @@ public ResourcePack generateSync() {
serveOptions.request(ResourcePackRequest.of(
location.url(),
location.hash(),
getConfig().getBoolean("send.request.required"),
Components.deserialize(getConfig().getString("send.request.prompt", "prompt"))
config.send().request().required(),
Components.deserialize(config.send().request().prompt())
));
} else {
serveOptions.request(null);
Expand Down Expand Up @@ -316,4 +325,4 @@ public EventBus eventBus() {
return eventBus;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,31 @@
*/
package team.unnamed.creative.central.bukkit.action;

import org.bukkit.configuration.ConfigurationSection;
import team.unnamed.creative.central.pack.ResourcePackStatus;
import org.bukkit.entity.Player;
import team.unnamed.creative.central.common.action.Action;
import team.unnamed.creative.central.common.action.ActionExecutor;
import team.unnamed.creative.central.common.action.AudienceActionExecutor;
import team.unnamed.creative.central.common.action.KickAction;

import java.util.Collections;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
public final class BukkitActionExecutor extends AudienceActionExecutor<Player> {

public class ActionManager {
private static final ActionExecutor<Player> INSTANCE = new BukkitActionExecutor();

private final Map<ResourcePackStatus, List<Action>> actionsByStatus = new EnumMap<>(ResourcePackStatus.class);
private BukkitActionExecutor() {
}

public void load(ConfigurationSection config) {
actionsByStatus.put(ResourcePackStatus.DECLINED, ActionParser.parse(config, "declined"));
actionsByStatus.put(ResourcePackStatus.ACCEPTED, ActionParser.parse(config, "accepted"));
actionsByStatus.put(ResourcePackStatus.FAILED, ActionParser.parse(config, "failed"));
actionsByStatus.put(ResourcePackStatus.LOADED, ActionParser.parse(config, "success"));
@Override
public void execute(Action action, Player player) {
super.execute(action, player);
if (action instanceof KickAction kickAction) {
player.kick(kickAction.reason());
} else {
throw new IllegalArgumentException("Unknown action type: '" + action + "'");
}
}

public List<Action> actions(ResourcePackStatus status) {
return actionsByStatus.getOrDefault(status, Collections.emptyList());
public static ActionExecutor<Player> bukkit() {
return INSTANCE;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Nullable;
import team.unnamed.creative.central.bukkit.CreativeCentralPlugin;
import team.unnamed.creative.central.bukkit.util.Components;
import team.unnamed.creative.central.common.util.Components;
import team.unnamed.creative.central.request.ResourcePackRequest;

import javax.annotation.ParametersAreNonnullByDefault;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,33 @@
package team.unnamed.creative.central.bukkit.listener;

import org.bukkit.entity.Player;
import team.unnamed.creative.central.bukkit.action.Action;
import team.unnamed.creative.central.bukkit.action.ActionManager;
import team.unnamed.creative.central.bukkit.action.BukkitActionExecutor;
import team.unnamed.creative.central.common.action.Action;
import team.unnamed.creative.central.common.config.Configuration;
import team.unnamed.creative.central.common.util.Monitor;
import team.unnamed.creative.central.event.EventListener;
import team.unnamed.creative.central.event.pack.ResourcePackStatusEvent;
import team.unnamed.creative.central.pack.ResourcePackStatus;

import java.util.Collections;
import java.util.List;

public class CreativeResourcePackStatusListener implements EventListener<ResourcePackStatusEvent> {

private final ActionManager actionManager;
private final Monitor<Configuration> config;

public CreativeResourcePackStatusListener(ActionManager actionManager) {
this.actionManager = actionManager;
public CreativeResourcePackStatusListener(Monitor<Configuration> config) {
this.config = config;
}

@Override
public void on(ResourcePackStatusEvent event) {
ResourcePackStatus status = event.status();
Player player = (Player) event.player();

List<Action> actions = actionManager.actions(status);

List<Action> actions = config.get().feedback().getOrDefault(status, Collections.emptyList());
for (Action action : actions) {
action.execute(player);
BukkitActionExecutor.bukkit().execute(action, player);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* This file is part of creative, licensed under the MIT license
*
* Copyright (c) 2021-2023 Unnamed Team
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package team.unnamed.creative.central.bukkit.util;

import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

public final class PluginResources {

private PluginResources() {
}

public static @Nullable InputStream get(Plugin plugin, String path) {
File file = new File(plugin.getDataFolder(), path);
if (!file.exists()) {
// file doesn't exist in the plugin's data folder,
// try to get it from the jar and copy it to the
// data folder
InputStream stream = plugin.getResource(path);
if (stream == null) {
// file doesn't exist in the jar either
return null;
}
plugin.saveResource(path, false);
return stream;
}

// file exists in the plugin's data folder,
// read the resource from there
try {
return new FileInputStream(file);
} catch (FileNotFoundException e) {
// should never happen
throw new IllegalStateException("File doesn't exist", e);
}
}

}
4 changes: 4 additions & 0 deletions common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ dependencies {
implementation(project(":creative-central-api"))
implementation("team.unnamed:creative-serializer-minecraft:$version")
implementation("team.unnamed:creative-server:0.7.2-SNAPSHOT")

// -- provided by server or plugin implementations --
compileOnly("net.kyori:adventure-text-minimessage:4.13.1")
compileOnly("org.yaml:snakeyaml:2.0")
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package team.unnamed.creative.central.bukkit.action;

import org.bukkit.entity.Player;
package team.unnamed.creative.central.common.action;

public interface Action {

void execute(Player player);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* This file is part of creative, licensed under the MIT license
*
* Copyright (c) 2021-2023 Unnamed Team
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package team.unnamed.creative.central.common.action;

public interface ActionExecutor<T> {

void execute(Action action, T target);

}
Loading

0 comments on commit 5a43985

Please sign in to comment.