-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
260 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
144 changes: 144 additions & 0 deletions
144
src/main/java/me/alexdevs/solstice/modules/jail/JailModule.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,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); | ||
} | ||
|
||
}); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/me/alexdevs/solstice/modules/jail/commands/DeleteJailCommand.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 @@ | ||
package me.alexdevs.solstice.modules.jail.commands; | ||
|
||
public class DeleteJailCommand { | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/me/alexdevs/solstice/modules/jail/commands/JailCommand.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 @@ | ||
package me.alexdevs.solstice.modules.jail.commands; | ||
|
||
public class JailCommand { | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/me/alexdevs/solstice/modules/jail/commands/SetJailCommand.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,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; | ||
}) | ||
); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/me/alexdevs/solstice/modules/jail/commands/UnjailCommand.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 @@ | ||
package me.alexdevs.solstice.modules.jail.commands; | ||
|
||
public class UnjailCommand { | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/me/alexdevs/solstice/modules/jail/data/JailConfig.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,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" | ||
); | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/me/alexdevs/solstice/modules/jail/data/JailLocale.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,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>") | ||
); | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/me/alexdevs/solstice/modules/jail/data/JailPlayerData.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,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; | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/me/alexdevs/solstice/modules/jail/data/JailServerData.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,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<>(); | ||
} |