Add game state handlers
This commit is contained in:
parent
91c1a6c840
commit
c0751511a5
5 changed files with 130 additions and 0 deletions
|
@ -1,6 +1,25 @@
|
||||||
package gent.zeus.mc13dtl;
|
package gent.zeus.mc13dtl;
|
||||||
|
|
||||||
|
import org.bukkit.plugin.Plugin;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
|
import gent.zeus.mc13dtl.gamestate.GameStateCommand;
|
||||||
|
import gent.zeus.mc13dtl.gamestate.GameStateHandler;
|
||||||
|
import io.papermc.paper.command.brigadier.Commands;
|
||||||
|
import io.papermc.paper.plugin.lifecycle.event.LifecycleEventManager;
|
||||||
|
import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents;
|
||||||
|
|
||||||
public class MC13DTL extends JavaPlugin {
|
public class MC13DTL extends JavaPlugin {
|
||||||
|
public static MC13DTL instance;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onEnable() {
|
||||||
|
instance = this;
|
||||||
|
getServer().getPluginManager().registerEvents(new GameStateHandler(), this);
|
||||||
|
LifecycleEventManager<Plugin> manager = this.getLifecycleManager();
|
||||||
|
manager.registerEventHandler(LifecycleEvents.COMMANDS, event -> {
|
||||||
|
Commands commands = event.registrar();
|
||||||
|
commands.register("start-game", "Start a game of 13 days to live", new GameStateCommand());
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,38 @@
|
||||||
|
package gent.zeus.mc13dtl.gamestate;
|
||||||
|
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.NamespacedKey;
|
||||||
|
import org.bukkit.OfflinePlayer;
|
||||||
|
import org.bukkit.World;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.persistence.PersistentDataType;
|
||||||
|
import org.bukkit.potion.PotionEffect;
|
||||||
|
|
||||||
|
import gent.zeus.mc13dtl.MC13DTL;
|
||||||
|
import io.papermc.paper.command.brigadier.BasicCommand;
|
||||||
|
import io.papermc.paper.command.brigadier.CommandSourceStack;
|
||||||
|
|
||||||
|
public class GameStateCommand implements BasicCommand {
|
||||||
|
@Override
|
||||||
|
public void execute(CommandSourceStack stack, String[] args) {
|
||||||
|
World world = Bukkit.getWorld("world");
|
||||||
|
NamespacedKey key = new NamespacedKey(MC13DTL.instance, "game-state");
|
||||||
|
world.getPersistentDataContainer().set(key, PersistentDataType.STRING, "active");
|
||||||
|
GameStateUtil.loadWorld(world);
|
||||||
|
for (OfflinePlayer i : Bukkit.getOfflinePlayers()) {
|
||||||
|
Player player = i.getPlayer();
|
||||||
|
player.getInventory().clear();
|
||||||
|
player.heal(100);
|
||||||
|
player.getActivePotionEffects().clear();
|
||||||
|
for (PotionEffect effect : player.getActivePotionEffects()) {
|
||||||
|
player.removePotionEffect(effect.getType());
|
||||||
|
}
|
||||||
|
GameStateUtil.loadPlayer(player);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String permission() {
|
||||||
|
return "13dtl.manage-game";
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
package gent.zeus.mc13dtl.gamestate;
|
||||||
|
|
||||||
|
import org.bukkit.World;
|
||||||
|
import org.bukkit.event.EventHandler;
|
||||||
|
import org.bukkit.event.Listener;
|
||||||
|
import org.bukkit.event.player.PlayerJoinEvent;
|
||||||
|
import org.bukkit.event.world.WorldLoadEvent;
|
||||||
|
|
||||||
|
public class GameStateHandler implements Listener {
|
||||||
|
@EventHandler
|
||||||
|
void onPlayerJoin(PlayerJoinEvent event) {
|
||||||
|
GameStateUtil.loadPlayer(event.getPlayer());
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
void onWorldLoad(WorldLoadEvent event) {
|
||||||
|
World world = event.getWorld();
|
||||||
|
if (!world.getName().equals("world")) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
GameStateUtil.loadWorld(world);
|
||||||
|
}
|
||||||
|
}
|
49
src/main/java/gent/zeus/mc13dtl/gamestate/GameStateUtil.java
Normal file
49
src/main/java/gent/zeus/mc13dtl/gamestate/GameStateUtil.java
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
package gent.zeus.mc13dtl.gamestate;
|
||||||
|
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.Difficulty;
|
||||||
|
import org.bukkit.GameMode;
|
||||||
|
import org.bukkit.GameRule;
|
||||||
|
import org.bukkit.NamespacedKey;
|
||||||
|
import org.bukkit.World;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.persistence.PersistentDataType;
|
||||||
|
|
||||||
|
import gent.zeus.mc13dtl.MC13DTL;
|
||||||
|
import net.md_5.bungee.api.ChatColor;
|
||||||
|
|
||||||
|
public class GameStateUtil {
|
||||||
|
static void loadWorld(World world) {
|
||||||
|
NamespacedKey key = new NamespacedKey(MC13DTL.instance, "game-state");
|
||||||
|
if (!world.getPersistentDataContainer().has(key)) {
|
||||||
|
world.getPersistentDataContainer().set(key, PersistentDataType.STRING, "lobby");
|
||||||
|
}
|
||||||
|
String gameState = world.getPersistentDataContainer().get(key, PersistentDataType.STRING);
|
||||||
|
if (gameState.equals("lobby")) {
|
||||||
|
world.setDifficulty(Difficulty.PEACEFUL);
|
||||||
|
world.setGameRule(GameRule.DO_DAYLIGHT_CYCLE, false);
|
||||||
|
world.setFullTime(0);
|
||||||
|
world.getWorldBorder().setSize(200);
|
||||||
|
}
|
||||||
|
if (gameState.equals("active")) {
|
||||||
|
world.setDifficulty(Difficulty.NORMAL);
|
||||||
|
world.setGameRule(GameRule.DO_DAYLIGHT_CYCLE, true);
|
||||||
|
world.getWorldBorder().setSize(10000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void loadPlayer(Player player) {
|
||||||
|
NamespacedKey key = new NamespacedKey(MC13DTL.instance, "game-state");
|
||||||
|
String gameState = Bukkit.getWorld("world").getPersistentDataContainer().get(key, PersistentDataType.STRING);
|
||||||
|
if (gameState.equals("lobby")) {
|
||||||
|
player.setGameMode(GameMode.ADVENTURE);
|
||||||
|
player.setInvulnerable(true);
|
||||||
|
player.sendMessage(ChatColor.DARK_PURPLE + "Game hasn't started yet, please wait...");
|
||||||
|
}
|
||||||
|
if (gameState.equals("active")) {
|
||||||
|
player.setGameMode(GameMode.SURVIVAL);
|
||||||
|
player.setInvulnerable(false);
|
||||||
|
player.sendMessage(ChatColor.DARK_PURPLE + "Game is active, have fun!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,3 +2,4 @@ name: 13DTL
|
||||||
version: 0.0.1
|
version: 0.0.1
|
||||||
main: gent.zeus.mc13dtl.MC13DTL
|
main: gent.zeus.mc13dtl.MC13DTL
|
||||||
api-version: '1.21.1'
|
api-version: '1.21.1'
|
||||||
|
load: STARTUP
|
||||||
|
|
Loading…
Reference in a new issue