Skip to content

Commit

Permalink
Make /pronounsset OP-only, /spawnset also sets player respawn, strict…
Browse files Browse the repository at this point in the history
…er /nick verification
  • Loading branch information
kivattt committed Oct 21, 2023
1 parent 4421e66 commit 54bba8d
Show file tree
Hide file tree
Showing 13 changed files with 162 additions and 14 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ Displays mod version
`Container.updateInventory` is overwritten by this mod,\
keep this in mind if you are writing mixins for this method

# Known issues
Doing `/sethome` then covering up the home location with blocks, unloading the chunk its in,
then doing `/home` can make you fall into an underground cave
this will also affect other commands like `/spawn`, since it's really an underlying issue with teleporting

# Other
`/sethome` doesn't follow the naming convention `...set` because it's such a common command name in Minecraft servers

Expand Down
6 changes: 5 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
- Better system for configuration, less confusing commands (something like /gamerule \<key> \<value>)
- In `/spawn`, `/teleport`, `/home` commands check if the target chunk is loaded by the player
and if not, warn them to try relogging if they're stuck in air


- Dimension support for spawning (respawning/first join) in the nether
Calculate the actual spawn point since it's probably chunk-based and just translated back to normal coords which aren't the exact same\
prob need some explanation to the user about it

- Improve the 100% CPU fix to not sleep on server close (saving chunks?)

More descriptive logging?
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ buildscript {
maven { url 'https://jitpack.io/' }
}
dependencies {
classpath('com.github.Fox2Code.FoxLoader:dev:1.2.19')
classpath('com.github.Fox2Code.FoxLoader:dev:1.2.23')
}
}

apply plugin: 'foxloader.dev'

version '1.1.0'
version '1.2.0'

foxloader {
// forceReload = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class KivaServerUtils extends Mod {

public static HashMap<String, Coordinate> playerHomes = new HashMap<>();
public static Coordinate spawnCommandLocation = null;
public static String version = "1.1.0";
public static String version = "1.2.0";
public static HashMap<String, Boolean> config = new HashMap<>();
public static String handleWindowClickLatestPlayerUsername;

Expand Down
34 changes: 34 additions & 0 deletions src/server/java/com/kiva/kivaserverutils/NicknameAllowed.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.kiva.kivaserverutils;

import com.fox2code.foxloader.loader.ServerMod;
import net.minecraft.src.game.entity.player.EntityPlayerMP;

import java.util.Map;

public class NicknameAllowed {
public static boolean nicknameIsAllowed(final String nickname){
for (Map.Entry<String, String> playerNick : KivaServerUtils.playerNicknames.entrySet()){
// Check if someone already has the nickname
if (playerNick.getValue().equalsIgnoreCase(nickname))
return false;

// Check if nickname matches a username in playerNicknames
// Not perfect, we'd have to store a null value for players with no nicknames to
// keep a username history, though this could better be achieved by parsing server.log
// but that's pretty hacky and would only fix this small issue where
// someone could set their nickname to a players username if that player
// 1. Doesn't have a nickname
// 2. Isn't currently on the server
if (playerNick.getKey().equalsIgnoreCase(nickname))
return false;
}

// Check if nickname matches any username of currently online players
for (EntityPlayerMP p : ServerMod.getOnlinePlayers()){
if (p.username.equalsIgnoreCase(nickname))
return false;
}

return true;
}
}
19 changes: 19 additions & 0 deletions src/server/java/com/kiva/kivaserverutils/commands/Home.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.fox2code.foxloader.registry.CommandCompat;
import com.kiva.kivaserverutils.Coordinate;
import com.kiva.kivaserverutils.KivaServerUtils;
import net.minecraft.src.game.block.Block;
import net.minecraft.src.game.level.WorldServer;

public class Home extends CommandCompat{
public Home(){
Expand Down Expand Up @@ -39,6 +41,23 @@ public void onExecute(final String[] args, final NetworkPlayer commandExecutor)
return;
}

// Scrapped code attempting to fix the issue mention in the README.md Known Issues
// It seems getBlockId() doesn't work when the chunk isnt loaded, defeating the purpose entirely
// Also Math.round() didnt seem to work out, maybe checking a 2x2 area would be necessary?
// Would probably be a better solution to just pre-load the chunks somehow before the player reaches unloaded chunks
/*int yWithOffset = (int)Math.round(homeCoordinate.y);
// 0 = Overworld
// I assume ServerMod.getGameInstance().worldMngr[0] is always the overworld, but this is to make sure
for (WorldServer worldServer : ServerMod.getGameInstance().worldMngr){
if (worldServer.dimension != 0)
continue;
for (; worldServer.getBlockId((int) Math.round(homeCoordinate.x), yWithOffset, (int)Math.round(homeCoordinate.z)) != Block.air.blockID; yWithOffset++){}
}
System.out.println("yOffset: " + yWithOffset);*/

commandExecutor.teleportRegistered(homeCoordinate.x, homeCoordinate.y, homeCoordinate.z);
commandExecutor.displayChatMessage(ChatColors.GREEN + "Teleported to home!");
}
Expand Down
5 changes: 3 additions & 2 deletions src/server/java/com/kiva/kivaserverutils/commands/Nick.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fox2code.foxloader.network.NetworkPlayer;
import com.fox2code.foxloader.registry.CommandCompat;
import com.kiva.kivaserverutils.KivaServerUtils;
import com.kiva.kivaserverutils.NicknameAllowed;

import static com.kiva.kivaserverutils.UsageMessage.sendUsageMessage;

Expand Down Expand Up @@ -38,8 +39,8 @@ public void onExecute(final String[] args, final NetworkPlayer commandExecutor){
}
}

if (KivaServerUtils.playerNicknames.containsValue(nickname)){
commandExecutor.displayChatMessage("Someone already has that nickname, ask a mod to force change it?");
if (!commandExecutor.isOperator() && !NicknameAllowed.nicknameIsAllowed(nickname)){
commandExecutor.displayChatMessage("Someone already has that nickname/username, ask a mod to force change it?");
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

public class PronounsSet extends CommandCompat{
public PronounsSet(){
super("pronounsset", false);
super("pronounsset", true);
}

public String commandSyntax(){
Expand Down
19 changes: 19 additions & 0 deletions src/server/java/com/kiva/server/mixins/MixinNetLoginHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.kiva.server.mixins;

import net.minecraft.src.game.entity.player.EntityPlayerMP;
import net.minecraft.src.server.packets.NetLoginHandler;
import net.minecraft.src.server.packets.Packet1Login;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

@Mixin(NetLoginHandler.class)
public class MixinNetLoginHandler {
// Scrapped spawn location dimension code
/*@Inject(method = "doLogin", at = @At("RETURN"), locals = LocalCapture.CAPTURE_FAILSOFT)
public void debugMoment(Packet1Login packet1Login, CallbackInfo ci, EntityPlayerMP entityPlayerMP){
System.out.println("Dimension in doLogin in the end: " + entityPlayerMP.dimension);
}*/
}
14 changes: 10 additions & 4 deletions src/server/java/com/kiva/server/mixins/MixinNetServerHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
import net.minecraft.src.server.packets.Packet3Chat;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import org.spongepowered.asm.mixin.injection.*;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(NetServerHandler.class)
Expand Down Expand Up @@ -50,4 +47,13 @@ public abstract class MixinNetServerHandler {
public void storeLatestPlayerUsername(Packet102WindowClick packet102WindowClick, CallbackInfo ci){
KivaServerUtils.handleWindowClickLatestPlayerUsername = this.playerEntity.username;
}

// Scrapped spawn location dimension code
/*@ModifyArg(method = "handleRespawnPacket", at = @At(value = "INVOKE", target = "Lnet/minecraft/src/server/ServerConfigurationManager;recreatePlayerEntity(Lnet/minecraft/src/game/entity/player/EntityPlayerMP;I)Lnet/minecraft/src/game/entity/player/EntityPlayerMP;"), index = 1)
public int setDimensionOnRespawn(int arg2){
if (KivaServerUtils.spawnCommandLocation == null)
return 0; // Vanilla behaviour
return KivaServerUtils.spawnCommandLocation.dimension;
}*/
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.kiva.server.mixins;

import com.kiva.kivaserverutils.KivaServerUtils;
import net.minecraft.server.MinecraftServer;
import net.minecraft.src.game.entity.player.EntityPlayerMP;
import net.minecraft.src.server.ServerConfigurationManager;
import net.minecraft.src.server.packets.NetLoginHandler;
import net.minecraft.src.server.player.PlayerController;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(ServerConfigurationManager.class)
public class MixinServerConfigurationManager {
// Scrapped spawn location dimension code
/*@Shadow private MinecraftServer mcServer;
// If this is the player's first time joining, spawn in the dimension specified with /spawnset
@Inject(method = "login", at = @At("TAIL"), cancellable = true)
public void handlePlayerFirstJoined(NetLoginHandler netLoginHandler, String arg2, CallbackInfoReturnable<EntityPlayerMP> cir){
if (KivaServerUtils.spawnCommandLocation == null)
return;
cir.setReturnValue(new EntityPlayerMP(
this.mcServer,
this.mcServer.getWorldManager(KivaServerUtils.spawnCommandLocation.dimension),
arg2,
new PlayerController(this.mcServer.getWorldManager(KivaServerUtils.spawnCommandLocation.dimension))
));
cir.cancel();
}*/
}
25 changes: 25 additions & 0 deletions src/server/java/com/kiva/server/mixins/MixinWorld.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.kiva.server.mixins;

import com.kiva.kivaserverutils.KivaServerUtils;
import net.minecraft.src.game.level.World;
import net.minecraft.src.game.level.chunk.ChunkCoordinates;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(World.class)
public class MixinWorld {
@Inject(method = "getSpawnPoint", at = @At("HEAD"), cancellable = true)
public void overRideTheSpawnPoint(CallbackInfoReturnable<ChunkCoordinates> cir){
if (KivaServerUtils.spawnCommandLocation == null)
return;

// Don't override the spawn location if spawn was set in the nether
if (KivaServerUtils.spawnCommandLocation.dimension != 0)
return;

cir.setReturnValue(new ChunkCoordinates((int) KivaServerUtils.spawnCommandLocation.x, (int) KivaServerUtils.spawnCommandLocation.y, (int) KivaServerUtils.spawnCommandLocation.z));
cir.cancel();
}
}
7 changes: 4 additions & 3 deletions src/server/resources/kivaserverutils.server.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
"package": "com.kiva.server.mixins",
"compatibilityLevel": "JAVA_8",
"mixins": [
"MixinNetServerHandler",
"MixinContainer",
"MixinExplosion",
"MixinItemDynamite",
"MixinContainer",
"MixinNetServerHandler",
"MixinSpawnerAnimals",
"MixinThreadHeavy",
"MixinSpawnerAnimals"
"MixinWorld"
]
}

0 comments on commit 54bba8d

Please sign in to comment.