Skip to content

Commit

Permalink
Added base for jail module
Browse files Browse the repository at this point in the history
  • Loading branch information
Ale32bit committed Jan 8, 2025
1 parent 2cc6e7a commit 0543ba7
Show file tree
Hide file tree
Showing 10 changed files with 260 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import me.alexdevs.solstice.modules.afk.AfkModule;
import me.alexdevs.solstice.modules.autoAnnouncement.AutoAnnouncementModule;
import me.alexdevs.solstice.modules.item.ItemModule;
import me.alexdevs.solstice.modules.jail.JailModule;
import me.alexdevs.solstice.modules.kit.KitModule;
import me.alexdevs.solstice.modules.note.NoteModule;
import me.alexdevs.solstice.modules.restart.RestartModule;
Expand Down Expand Up @@ -80,6 +81,7 @@ public class ModuleProvider implements ModuleEntrypoint {
new InfoModule(),
new InventorySeeModule(),
new ItemModule(),
new JailModule(),
new KickModule(),
new KitModule(),
new MailModule(),
Expand Down
144 changes: 144 additions & 0 deletions src/main/java/me/alexdevs/solstice/modules/jail/JailModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package me.alexdevs.solstice.modules.jail;

import me.alexdevs.solstice.Solstice;
import me.alexdevs.solstice.api.ServerPosition;
import me.alexdevs.solstice.api.events.CommandEvents;
import me.alexdevs.solstice.api.module.ModuleBase;
import me.alexdevs.solstice.modules.jail.commands.SetJailCommand;
import me.alexdevs.solstice.modules.jail.data.JailConfig;
import me.alexdevs.solstice.modules.jail.data.JailLocale;
import me.alexdevs.solstice.modules.jail.data.JailPlayerData;
import me.alexdevs.solstice.modules.jail.data.JailServerData;
import net.fabricmc.fabric.api.entity.event.v1.ServerPlayerEvents;
import net.fabricmc.fabric.api.event.player.*;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.ActionResult;
import net.minecraft.util.TypedActionResult;

import java.util.Map;
import java.util.UUID;

public class JailModule extends ModuleBase {
public static final String ID = "jail";

public JailModule() {
super(ID);

Solstice.configManager.registerData(ID, JailConfig.class, JailConfig::new);
Solstice.localeManager.registerModule(ID, JailLocale.MODULE);
Solstice.playerData.registerData(ID, JailPlayerData.class, JailPlayerData::new);
Solstice.serverData.registerData(ID, JailServerData.class, JailServerData::new);

commands.add(new SetJailCommand(this));

ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> {
Solstice.nextTick(() -> {
if (isPlayerJailed(handler.getPlayer().getUuid())) {
sendToJail(handler.getPlayer());
}
});
});

ServerPlayerEvents.AFTER_RESPAWN.register((oldPlayer, player, alive) -> {
if (isPlayerJailed(player.getUuid())) {
sendToJail(player);
}
});

CommandEvents.ALLOW_COMMAND.register((source, command) -> {
if (!source.isExecutedByPlayer())
return true;

if (isPlayerJailed(source.getPlayer().getUuid())) {
var config = getConfig();
var cmd = command.split(" ")[0];
var canRun = config.allowedCommands.contains(cmd);
if (!canRun) {
source.sendFeedback(() -> locale().get("commandNotAllowed"), false);
}
return canRun;
}

return true;
});

AttackBlockCallback.EVENT.register((player, world, hand, blockPos, direction) -> {
if (isPlayerJailed(player.getUuid())) {
player.sendMessage(locale().get("cannotBreakBlocks"));
return ActionResult.FAIL;
}
return ActionResult.PASS;
});

AttackEntityCallback.EVENT.register((player, world, hand, entity, entityHitResult) -> {
if (isPlayerJailed(player.getUuid())) {
player.sendMessage(locale().get("cannotAttackEntities"));
return ActionResult.FAIL;
}
return ActionResult.PASS;
});

PlayerBlockBreakEvents.BEFORE.register((world, player, blockPos, blockState, blockEntity) -> {
if (isPlayerJailed(player.getUuid())) {
player.sendMessage(locale().get("cannotBreakBlocks"));
return false;
}

return true;
});

UseBlockCallback.EVENT.register((player, world, hand, blockHitResult) -> {
if (isPlayerJailed(player.getUuid())) {
player.sendMessage(locale().get("cannotUseBlocks"));
return ActionResult.FAIL;
}
return ActionResult.PASS;
});

UseEntityCallback.EVENT.register((player, world, hand, entity, entityHitResult) -> {
if (isPlayerJailed(player.getUuid())) {
player.sendMessage(locale().get("cannotUseEntities"));
return ActionResult.FAIL;
}
return ActionResult.PASS;
});

UseItemCallback.EVENT.register((player, world, hand) -> {
var stack = player.getStackInHand(hand);
if (isPlayerJailed(player.getUuid())) {
player.sendMessage(locale().get("cannotUseItems"));
return TypedActionResult.fail(stack);
}
return TypedActionResult.pass(stack);
});
}

public JailConfig getConfig() {
return Solstice.configManager.getData(JailConfig.class);
}

public Map<String, ServerPosition> getJails() {
return Solstice.serverData.getData(JailServerData.class).jails;
}

public JailPlayerData getPlayer(UUID uuid) {
return Solstice.playerData.get(uuid).getData(JailPlayerData.class);
}

public boolean isPlayerJailed(UUID uuid) {
return getPlayer(uuid).jailed;
}

public void sendToJail(ServerPlayerEntity player) {
Solstice.nextTick(() -> {
var data = getPlayer(player.getUuid());
var jails = getJails();
var jail = jails.get(data.jailName);
if (jail != null) {
jail.teleport(player);
}

});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package me.alexdevs.solstice.modules.jail.commands;

public class DeleteJailCommand {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package me.alexdevs.solstice.modules.jail.commands;

public class JailCommand {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package me.alexdevs.solstice.modules.jail.commands;

import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import me.alexdevs.solstice.api.ServerPosition;
import me.alexdevs.solstice.api.module.ModCommand;
import me.alexdevs.solstice.modules.jail.JailModule;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;

import java.util.List;

public class SetJailCommand extends ModCommand<JailModule> {
public SetJailCommand(JailModule module) {
super(module);
}

@Override
public List<String> getNames() {
return List.of("setjail");
}

@Override
public LiteralArgumentBuilder<ServerCommandSource> command(String name) {
return CommandManager.literal(name)
.requires(require("set", 3))
.then(CommandManager.argument("name", StringArgumentType.word())
.executes(context -> {
var player = context.getSource().getPlayerOrThrow();
var jailName = StringArgumentType.getString(context, "name");

var position = new ServerPosition(player);

var jails = module.getJails();
if(jails.containsKey(jailName)) {
return 0;
}

jails.put(jailName, position);

return 1;
})
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package me.alexdevs.solstice.modules.jail.commands;

public class UnjailCommand {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package me.alexdevs.solstice.modules.jail.data;

import org.spongepowered.configurate.objectmapping.ConfigSerializable;
import org.spongepowered.configurate.objectmapping.meta.Comment;

import java.util.List;

@ConfigSerializable
public class JailConfig {
@Comment("List of commands the jailed players can execute.")
public List<String> allowedCommands = List.of(
"afk",
"ignore",
"msg", "tell", "w", "dm",
"mail",
"info", "motd", "rules"
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package me.alexdevs.solstice.modules.jail.data;

import java.util.Map;

public class JailLocale {
public static final Map<String, String> MODULE = Map.ofEntries(
Map.entry("cannotRunCommands", "<red>You are not allowed to run this command in jail!</red>"),
Map.entry("cannotBreakBlocks", "<red>You are not allowed to break blocks in jail!</red>"),
Map.entry("cannotAttackEntities", "<red>You are not allowed to attack entities in jail!</red>"),
Map.entry("cannotUseBlocks", "<red>You are not allowed to use blocks in jail!</red>"),
Map.entry("cannotUseEntities", "<red>You are not allowed to interact with entities in jail!</red>"),
Map.entry("cannotUseItems", "<red>You are not allowed to use items in jail!</red>")
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package me.alexdevs.solstice.modules.jail.data;

import org.jetbrains.annotations.Nullable;

import java.util.Date;
import java.util.UUID;

public class JailPlayerData {
public boolean jailed = false;
public @Nullable String jailName = null;
public @Nullable UUID jailedBy = null;
public @Nullable Date jailedOn = null;
public int jailTime = 0;
public @Nullable String jailReason = null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package me.alexdevs.solstice.modules.jail.data;

import me.alexdevs.solstice.api.ServerPosition;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class JailServerData {
public Map<String, ServerPosition> jails = new ConcurrentHashMap<>();
}

0 comments on commit 0543ba7

Please sign in to comment.