-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add /schedulerestart and related features
- Loading branch information
1 parent
bfe9ead
commit ab9f18e
Showing
7 changed files
with
300 additions
and
26 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
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
41 changes: 41 additions & 0 deletions
41
src/main/java/com/github/mori01231/lifecore/command/GCListenerRestartExtendTimeCommand.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,41 @@ | ||
package com.github.mori01231.lifecore.command; | ||
|
||
import com.github.mori01231.lifecore.LifeCore; | ||
import org.bukkit.ChatColor; | ||
import org.bukkit.command.Command; | ||
import org.bukkit.command.CommandSender; | ||
import org.bukkit.command.TabExecutor; | ||
import org.bukkit.entity.Player; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.*; | ||
|
||
public class GCListenerRestartExtendTimeCommand implements TabExecutor { | ||
private final LifeCore plugin; | ||
private final Set<UUID> players = new HashSet<>(); | ||
|
||
public GCListenerRestartExtendTimeCommand(@NotNull LifeCore plugin) { | ||
this.plugin = plugin; | ||
} | ||
|
||
@Override | ||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { | ||
if (!plugin.getGcListener().isTriggered()) return true; | ||
if (!(sender instanceof Player)) { | ||
return true; | ||
} | ||
Player player = (Player) sender; | ||
players.add(player.getUniqueId()); | ||
sender.sendMessage(ChatColor.GREEN + "投票しました。"); | ||
if (sender.hasPermission("lifecore.extend-time-immediately") || players.size() == 5) { | ||
ScheduleRestartCommand.schedule(30); | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) { | ||
return Collections.emptyList(); | ||
} | ||
} |
135 changes: 135 additions & 0 deletions
135
src/main/java/com/github/mori01231/lifecore/command/ScheduleRestartCommand.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,135 @@ | ||
package com.github.mori01231.lifecore.command; | ||
|
||
import com.github.mori01231.lifecore.LifeCore; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.ChatColor; | ||
import org.bukkit.command.Command; | ||
import org.bukkit.command.CommandSender; | ||
import org.bukkit.command.TabExecutor; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.scheduler.BukkitTask; | ||
import org.jetbrains.annotations.Contract; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.*; | ||
|
||
public class ScheduleRestartCommand implements TabExecutor { | ||
private static final Map<Integer, Set<Action>> ACTIONS = new HashMap<>(); | ||
private static final List<BukkitTask> tasks = new ArrayList<>(); | ||
private final LifeCore plugin; | ||
|
||
public ScheduleRestartCommand(@NotNull LifeCore plugin) { | ||
this.plugin = plugin; | ||
} | ||
|
||
@Override | ||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { | ||
if (args.length == 0) { | ||
sender.sendMessage(ChatColor.RED + "/schedulerestart <minutes>"); | ||
return true; | ||
} | ||
if (args[0].equalsIgnoreCase("gc")) { | ||
plugin.getGcListener().triggerNow(); | ||
return true; | ||
} | ||
int minutes = Integer.parseInt(args[0]); | ||
schedule(minutes); | ||
return true; | ||
} | ||
|
||
public static void schedule(int minutes) { | ||
for (BukkitTask task : tasks) { | ||
task.cancel(); | ||
} | ||
tasks.clear(); | ||
Bukkit.broadcastMessage("§6[§dお知らせ§6] §aこのサーバーは§d" + minutes + "分後" + "§aに再起動されます。"); | ||
int maxTicks = minutes * 60 * 20; | ||
ACTIONS.forEach((seconds, actions) -> { | ||
if (seconds < minutes * 60) { | ||
BukkitTask task = Bukkit.getScheduler().runTaskLater(LifeCore.getPlugin(LifeCore.class), () -> { | ||
for (Action action : actions) { | ||
action.execute(seconds); | ||
} | ||
}, maxTicks - seconds * 20); | ||
tasks.add(task); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) { | ||
return Collections.singletonList("gc"); | ||
} | ||
|
||
public enum Action { | ||
BROADCAST { | ||
@Override | ||
public void execute(int seconds) { | ||
if (seconds >= 60) { | ||
int minutes = seconds / 60; | ||
Bukkit.broadcastMessage("§6[§dお知らせ§6] §aこのサーバーは§d" + minutes + "分後" + "§aに再起動されます。"); | ||
} else { | ||
Bukkit.broadcastMessage("§6[§dお知らせ§6] §aこのサーバーは§c" + seconds + "秒後" + "§aに再起動されます。"); | ||
} | ||
} | ||
}, | ||
ENABLE_WHITELIST { | ||
@Override | ||
public void execute(int seconds) { | ||
Bukkit.setWhitelist(true); | ||
} | ||
}, | ||
SAVE_AND_KICK_ALL_PLAYERS { | ||
@Override | ||
public void execute(int seconds) { | ||
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "mpdb saveAndKick"); | ||
for (Player player : Bukkit.getOnlinePlayers()) { | ||
player.kickPlayer("Server closed"); | ||
} | ||
} | ||
}, | ||
SCHEDULE_SHUTDOWN_SERVER { | ||
@Override | ||
public void execute(int seconds) { | ||
Bukkit.getScheduler().runTaskLater(LifeCore.getPlugin(LifeCore.class), Bukkit::shutdown, 20 * 60); | ||
} | ||
}, | ||
; | ||
|
||
public abstract void execute(int seconds); | ||
} | ||
|
||
@NotNull | ||
@Contract("_ -> new") | ||
private static Set<Action> setOf(@NotNull Action... actions) { | ||
return new HashSet<>(Arrays.asList(actions)); | ||
} | ||
|
||
static { | ||
ACTIONS.put(60 * 60, setOf(Action.BROADCAST)); | ||
ACTIONS.put(55 * 60, setOf(Action.BROADCAST)); | ||
ACTIONS.put(50 * 60, setOf(Action.BROADCAST)); | ||
ACTIONS.put(45 * 60, setOf(Action.BROADCAST)); | ||
ACTIONS.put(40 * 60, setOf(Action.BROADCAST)); | ||
ACTIONS.put(35 * 60, setOf(Action.BROADCAST)); | ||
ACTIONS.put(30 * 60, setOf(Action.BROADCAST)); | ||
ACTIONS.put(25 * 60, setOf(Action.BROADCAST)); | ||
ACTIONS.put(20 * 60, setOf(Action.BROADCAST)); | ||
ACTIONS.put(15 * 60, setOf(Action.BROADCAST)); | ||
ACTIONS.put(10 * 60, setOf(Action.BROADCAST)); | ||
ACTIONS.put(5 * 60, setOf(Action.BROADCAST)); | ||
ACTIONS.put(4 * 60, setOf(Action.BROADCAST)); | ||
ACTIONS.put(3 * 60, setOf(Action.BROADCAST)); | ||
ACTIONS.put(2 * 60, setOf(Action.BROADCAST, Action.ENABLE_WHITELIST)); | ||
ACTIONS.put(60, setOf(Action.BROADCAST)); | ||
ACTIONS.put(30, setOf(Action.BROADCAST)); | ||
ACTIONS.put(10, setOf(Action.BROADCAST)); | ||
ACTIONS.put(5, setOf(Action.BROADCAST)); | ||
ACTIONS.put(4, setOf(Action.BROADCAST)); | ||
ACTIONS.put(3, setOf(Action.BROADCAST)); | ||
ACTIONS.put(2, setOf(Action.BROADCAST)); | ||
ACTIONS.put(1, setOf(Action.BROADCAST)); | ||
ACTIONS.put(0, setOf(Action.SAVE_AND_KICK_ALL_PLAYERS, Action.SCHEDULE_SHUTDOWN_SERVER)); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/main/java/com/github/mori01231/lifecore/listener/item/LavaSpongeItemListener.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,54 @@ | ||
package com.github.mori01231.lifecore.listener.item; | ||
|
||
import com.github.mori01231.lifecore.util.ItemUtil; | ||
import org.bukkit.*; | ||
import org.bukkit.block.Block; | ||
import org.bukkit.event.Event; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.EventPriority; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.block.Action; | ||
import org.bukkit.event.block.BlockBreakEvent; | ||
import org.bukkit.event.player.PlayerInteractEvent; | ||
import org.bukkit.util.RayTraceResult; | ||
|
||
public class LavaSpongeItemListener implements Listener { | ||
private static final String ITEM_ID = "56fabea9-e1f9-4e7f-ae78-83e07e8b8767"; | ||
|
||
@EventHandler(priority = EventPriority.MONITOR) | ||
public void onPlayerInteract(PlayerInteractEvent e) { | ||
if (e.getAction() != Action.RIGHT_CLICK_AIR && e.getAction() != Action.RIGHT_CLICK_BLOCK) { | ||
return; | ||
} | ||
if (e.getAction() == Action.RIGHT_CLICK_BLOCK && e.useItemInHand() == Event.Result.DENY) return; | ||
RayTraceResult result = e.getPlayer().rayTraceBlocks(4, FluidCollisionMode.ALWAYS); | ||
if (result == null || result.getHitBlock() == null) { | ||
return; | ||
} | ||
if (!ITEM_ID.equals(ItemUtil.getStringTag(e.getItem(), "LifeItemId"))) { | ||
// wrong item | ||
return; | ||
} | ||
int affected = 0; | ||
int radius = 2; | ||
for (int dx = -radius; dx <= radius; dx++) { | ||
for (int dy = -radius; dy <= radius; dy++) { | ||
for (int dz = -radius; dz <= radius; dz++) { | ||
Location loc = result.getHitBlock().getLocation().clone().add(dx, dy, dz); | ||
Block block = loc.getBlock(); | ||
if (block.getType() != Material.LAVA && block.getType() != Material.WATER) { | ||
// wrong block | ||
continue; | ||
} | ||
if (new BlockBreakEvent(block, e.getPlayer()).callEvent()) { | ||
block.setType(Material.AIR, true); | ||
affected++; | ||
} | ||
} | ||
} | ||
} | ||
if (affected > 0 && e.getPlayer().getGameMode() != GameMode.CREATIVE) { | ||
// TODO: reduce durability | ||
} | ||
} | ||
} |
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
Oops, something went wrong.