Skip to content

Commit

Permalink
implement bukkit-action and bukkit-action-builder
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Mar 12, 2024
1 parent 95cf6a5 commit 9ddb4bc
Show file tree
Hide file tree
Showing 14 changed files with 419 additions and 0 deletions.
26 changes: 26 additions & 0 deletions bukkit/bukkit-action-builder/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>me.hsgamer</groupId>
<artifactId>hscore-bukkit</artifactId>
<version>4.4.0-SNAPSHOT</version>
</parent>

<artifactId>hscore-bukkit-action-builder</artifactId>

<dependencies>
<dependency>
<groupId>me.hsgamer</groupId>
<artifactId>hscore-action-builder</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>me.hsgamer</groupId>
<artifactId>hscore-bukkit-action</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package me.hsgamer.hscore.bukkit.action.builder;

import me.hsgamer.hscore.action.builder.ActionBuilder;
import me.hsgamer.hscore.bukkit.action.*;
import org.bukkit.plugin.Plugin;

/**
* The utility class to register {@link me.hsgamer.hscore.action.common.Action} to the {@link ActionBuilder}
*/
public final class BukkitActionBuilder {
private BukkitActionBuilder() {
// EMPTY
}

/**
* Register the actions
*
* @param actionBuilder the action builder
* @param plugin the plugin
*/
public static void register(ActionBuilder actionBuilder, Plugin plugin) {
actionBuilder.register(input -> new BroadcastAction(input.getValue()), "broadcast");
actionBuilder.register(input -> new ConsoleAction(plugin, input.getValue()), "console");
actionBuilder.register(input -> new DelayAction(plugin, input.getValue()), "delay");
actionBuilder.register(input -> new OpAction(plugin, input.getValue()), "op");
actionBuilder.register(input -> new PermissionAction(plugin, input.getValue(), input.getOptionAsList()), "permission");
actionBuilder.register(input -> new PlayerAction(plugin, input.getValue()), "player");
actionBuilder.register(input -> new TellAction(input.getValue()), "tell");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Contains the class to register the {@link me.hsgamer.hscore.action.common.Action} in {@link me.hsgamer.hscore.bukkit.action} to {@link me.hsgamer.hscore.action.builder.ActionBuilder}
*/
package me.hsgamer.hscore.bukkit.action.builder;
41 changes: 41 additions & 0 deletions bukkit/bukkit-action/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>me.hsgamer</groupId>
<artifactId>hscore-bukkit</artifactId>
<version>4.4.0-SNAPSHOT</version>
</parent>

<artifactId>hscore-bukkit-action</artifactId>

<properties>
<minelib.version>1.1.2</minelib.version>
</properties>

<dependencies>
<dependency>
<groupId>me.hsgamer</groupId>
<artifactId>hscore-action-common</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.github.projectunified</groupId>
<artifactId>minelib-scheduler-entity</artifactId>
<version>${minelib.version}</version>
</dependency>
<dependency>
<groupId>io.github.projectunified</groupId>
<artifactId>minelib-scheduler-global</artifactId>
<version>${minelib.version}</version>
</dependency>
<dependency>
<groupId>io.github.projectunified</groupId>
<artifactId>minelib-scheduler-async</artifactId>
<version>${minelib.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package me.hsgamer.hscore.bukkit.action;

import me.hsgamer.hscore.action.common.Action;
import me.hsgamer.hscore.common.StringReplacer;
import me.hsgamer.hscore.task.element.TaskProcess;
import org.bukkit.Bukkit;

import java.util.UUID;

/**
* The action to broadcast a message
*/
public class BroadcastAction implements Action {
private final String message;

/**
* Create a new action
*
* @param message the message
*/
public BroadcastAction(String message) {
this.message = message;
}

@Override
public void apply(UUID uuid, TaskProcess process, StringReplacer stringReplacer) {
String replaced = stringReplacer.replaceOrOriginal(message, uuid);
Bukkit.broadcastMessage(replaced);
process.next();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package me.hsgamer.hscore.bukkit.action;

import io.github.projectunified.minelib.scheduler.entity.EntityScheduler;
import me.hsgamer.hscore.action.common.Action;
import me.hsgamer.hscore.common.StringReplacer;
import me.hsgamer.hscore.task.element.TaskProcess;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

import java.util.UUID;

/**
* The action to execute a command
*/
public abstract class CommandAction implements Action {
protected final Plugin plugin;
private final String command;

/**
* Create a new action
*
* @param plugin the plugin
* @param command the command
*/
protected CommandAction(Plugin plugin, String command) {
this.plugin = plugin;
this.command = command;
}

private static String normalize(String command) {
return command.startsWith("/") ? command : "/" + command;
}

/**
* Accept the command
*
* @param player the player
* @param command the command
*/
protected abstract void accept(Player player, String command);

@Override
public void apply(UUID uuid, TaskProcess process, StringReplacer stringReplacer) {
String replaced = stringReplacer.replaceOrOriginal(command, uuid);
Player player = Bukkit.getPlayer(uuid);
if (player == null) {
process.next();
return;
}

String command = normalize(replaced);
EntityScheduler.get(plugin, player)
.run(() -> {
try {
accept(player, command);
} finally {
process.next();
}
}, process::next);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package me.hsgamer.hscore.bukkit.action;

import io.github.projectunified.minelib.scheduler.global.GlobalScheduler;
import me.hsgamer.hscore.action.common.Action;
import me.hsgamer.hscore.common.StringReplacer;
import me.hsgamer.hscore.task.element.TaskProcess;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;

import java.util.UUID;

/**
* The action to execute a command as console
*/
public class ConsoleAction implements Action {
private final Plugin plugin;
private final String command;

/**
* Create a new action
*
* @param plugin the plugin
* @param command the command
*/
public ConsoleAction(Plugin plugin, String command) {
this.plugin = plugin;
this.command = command;
}

@Override
public void apply(UUID uuid, TaskProcess process, StringReplacer stringReplacer) {
GlobalScheduler.get(plugin).run(() -> {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), stringReplacer.replaceOrOriginal(command, uuid));
process.next();
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package me.hsgamer.hscore.bukkit.action;

import io.github.projectunified.minelib.scheduler.entity.EntityScheduler;
import me.hsgamer.hscore.action.common.Action;
import me.hsgamer.hscore.common.StringReplacer;
import me.hsgamer.hscore.task.element.TaskProcess;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

import java.util.UUID;

/**
* The action to delay the next action
*/
public class DelayAction implements Action {
private final Plugin plugin;
private final String delay;

/**
* Create a new action
*
* @param plugin the plugin
* @param delay the delay in ticks
*/
public DelayAction(Plugin plugin, String delay) {
this.plugin = plugin;
this.delay = delay;
}

@Override
public void apply(UUID uuid, TaskProcess process, StringReplacer stringReplacer) {
Player player = Bukkit.getPlayer(uuid);
if (player == null) {
process.next();
return;
}

String value = stringReplacer.replaceOrOriginal(delay, uuid);
long delay;
try {
delay = Long.parseLong(value);
} catch (NumberFormatException e) {
plugin.getLogger().warning("Invalid delay: " + value);
process.next();
return;
}

EntityScheduler.get(plugin, player).runLater(process::next, process::next, delay);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package me.hsgamer.hscore.bukkit.action;

import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

/**
* The action to execute a command as op
*/
public class OpAction extends CommandAction {
/**
* Create a new action
*
* @param plugin the plugin
* @param command the command
*/
public OpAction(Plugin plugin, String command) {
super(plugin, command);
}

@Override
protected void accept(Player player, String command) {
if (player.isOp()) {
player.chat(command);
} else {
try {
player.setOp(true);
player.chat(command);
} finally {
player.setOp(false);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package me.hsgamer.hscore.bukkit.action;

import org.bukkit.entity.Player;
import org.bukkit.permissions.PermissionAttachment;
import org.bukkit.plugin.Plugin;

import java.util.List;
import java.util.stream.Collectors;

/**
* The action to execute a command with permissions
*/
public class PermissionAction extends CommandAction {
private final List<String> permissions;

/**
* Create a new action
*
* @param plugin the plugin
* @param command the command
* @param permissions the permissions
*/
public PermissionAction(Plugin plugin, String command, List<String> permissions) {
super(plugin, command);
this.permissions = permissions;
}

@Override
protected void accept(Player player, String command) {
List<PermissionAttachment> attachments = permissions.stream()
.filter(s -> !player.hasPermission(s))
.map(s -> player.addAttachment(plugin, s, true))
.collect(Collectors.toList());
try {
player.chat(command);
} finally {
attachments.forEach(player::removeAttachment);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package me.hsgamer.hscore.bukkit.action;

import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

/**
* The action to execute a command as the player
*/
public class PlayerAction extends CommandAction {
/**
* Create a new action
*
* @param plugin the plugin
* @param command the command
*/
public PlayerAction(Plugin plugin, String command) {
super(plugin, command);
}

@Override
protected void accept(Player player, String command) {
player.chat(command);
}
}
Loading

0 comments on commit 9ddb4bc

Please sign in to comment.