-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement bukkit-action and bukkit-action-builder
- Loading branch information
Showing
14 changed files
with
419 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
30 changes: 30 additions & 0 deletions
30
...on-builder/src/main/java/me/hsgamer/hscore/bukkit/action/builder/BukkitActionBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
...it-action-builder/src/main/java/me/hsgamer/hscore/bukkit/action/builder/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
31 changes: 31 additions & 0 deletions
31
bukkit/bukkit-action/src/main/java/me/hsgamer/hscore/bukkit/action/BroadcastAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
bukkit/bukkit-action/src/main/java/me/hsgamer/hscore/bukkit/action/CommandAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
bukkit/bukkit-action/src/main/java/me/hsgamer/hscore/bukkit/action/ConsoleAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
bukkit/bukkit-action/src/main/java/me/hsgamer/hscore/bukkit/action/DelayAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
bukkit/bukkit-action/src/main/java/me/hsgamer/hscore/bukkit/action/OpAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
bukkit/bukkit-action/src/main/java/me/hsgamer/hscore/bukkit/action/PermissionAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
bukkit/bukkit-action/src/main/java/me/hsgamer/hscore/bukkit/action/PlayerAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.