Skip to content

Commit

Permalink
1.0.2: Fix server crashing when player left in an unlucky situation :(
Browse files Browse the repository at this point in the history
Minor cleanup
  • Loading branch information
onebeastchris committed Nov 12, 2023
1 parent 7d804e8 commit 55ef1d3
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 59 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ yarn_mappings=1.20+build.1
loader_version=0.14.21

# Mod Properties
mod_version=1.0.0
mod_version=1.0.2
maven_group=net.onebeastofchris
archives_base_name=visitors

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/onebeastchris/command/discordCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ public static LiteralCommandNode register(CommandDispatcher<ServerCommandSource>
}

public static int discordCommand(ServerCommandSource source) {
Supplier<Text> initialText = () -> Text.of(visitors.config.getDiscordInviteMessage());
Supplier<Text> mutableText = () -> {
Supplier<Text> discordInviteMessage = () -> Text.of(visitors.config.getDiscordInviteMessage());
Supplier<Text> discordLink = () -> {
Text link = Text.of("[" + visitors.config.getInviteLink() + "]");
MutableText text = link.copy().styled((style) -> style.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, visitors.config.getInviteLink())));
return text.setStyle(text.getStyle().withColor(Formatting.GOLD));
};

source.sendFeedback(initialText, false);
source.sendFeedback(mutableText, false);
source.sendFeedback(discordInviteMessage, false);
source.sendFeedback(discordLink, false);

return 1;
}
Expand Down
32 changes: 15 additions & 17 deletions src/main/java/onebeastchris/event/ServerJoinEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,21 @@ public static void register() {

if (PlayerDataUtil.isVisitor(profile)) {
visitors.LOGGER.info(profile.getName() + " joined the server as a visitor.");
if (PlayerDataUtil.isVisitor(profile)) {
PlayerDataUtil.addPlayer(profile, player);

tempStorage.put(player, new PlayerInfoStorage(
player.getCustomName(),
player.getServerWorld(),
new int[]{player.getBlockPos().getX(), player.getBlockPos().getY(), player.getBlockPos().getZ()})
);

player.changeGameMode(GameMode.SPECTATOR);
player.setCustomName(Text.of("Visitor: " + player.getGameProfile().getName()));
player.setCustomNameVisible(true);

// calling this once to ensure players see... something.
player.sendMessage(Text.of(visitors.config.getWelcomeVisitorMessage1()), true);
}
PlayerDataUtil.addVisitor(profile, player);

tempStorage.put(player, new PlayerInfoStorage(
player.getPlayerListName(),
player.getServerWorld(),
player.getPos())
);

player.changeGameMode(GameMode.SPECTATOR);
player.setCustomName(Text.of("Visitor: " + player.getGameProfile().getName()));
player.setCustomNameVisible(true);

// calling this once to ensure players see... something.
player.sendMessage(Text.of(visitors.config.getWelcomeVisitorMessage1()), true);
}
});
});
}
}
11 changes: 4 additions & 7 deletions src/main/java/onebeastchris/event/ServerLeaveEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,15 @@ public static void register() {

public static void onLeave(ServerPlayerEntity player, GameProfile profile){
PlayerDataUtil.removeVisitor(profile);
PlayerInfoStorage storage = ServerJoinEvent.tempStorage.get(player);
PlayerInfoStorage storage = ServerJoinEvent.tempStorage.remove(player);

int x = storage.position()[0];
int y = storage.position()[1];
int z = storage.position()[2];
double x = storage.position().getX();
double y = storage.position().getY();
double z = storage.position().getZ();

player.teleport(storage.world(), x, y, z, 0, 0);
player.changeGameMode(GameMode.DEFAULT);
player.sendMessage(Text.of(visitors.config.getWelcomeMemberMessage()), true);
player.setCustomName(storage.name());

PlayerDataUtil.removeVisitor(profile);
ServerJoinEvent.tempStorage.remove(player);
}
}
5 changes: 4 additions & 1 deletion src/main/java/onebeastchris/event/Timer.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ public static void register() {
}
public static void sendMessageToVisitors(String message){
for (ServerPlayerEntity player : PlayerDataUtil.visitorMap.values()){
player.sendMessage(Text.of(message), true);
// Could happen if unlucky, let's not crash the server.
if (player != null) {
player.sendMessage(Text.of(message), true);
}
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/onebeastchris/mixin/PlayerManagerMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class PlayerManagerMixin {
@Redirect(method = "checkCanJoin", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/PlayerManager;isWhitelisted(Lcom/mojang/authlib/GameProfile;)Z"))
private boolean visitors$isWhitelisted(PlayerManager instance, GameProfile profile) {
if (!instance.isWhitelisted(profile)) {
PlayerDataUtil.addVisitor(profile, null);
PlayerDataUtil.addFutureVisitor(profile);
}
return true;
}
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/onebeastchris/mixin/ServerConfigListMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ public abstract class ServerConfigListMixin {

@Inject(method = "add", at = @At("TAIL"))
private void add(ServerConfigEntry<?> entry, CallbackInfo ci) {
if (entry instanceof WhitelistEntry whitelistEntry) {
try {
GameProfile profile = ((ServerConfigEntryMixin.ServerConfigEntryInvoker<GameProfile>) whitelistEntry).callGetKey();
visitors.LOGGER.debug("WhitelistEntry: " + profile.getName());
PlayerDataUtil.changeStatus(profile);
} catch (Exception e) {
visitors.LOGGER.error("Error getting GameProfile from WhitelistEntry", e);
}
if (entry instanceof WhitelistEntry whitelistEntry) {
try {
GameProfile profile = ((ServerConfigEntryMixin.ServerConfigEntryInvoker<GameProfile>) whitelistEntry).callGetKey();
visitors.LOGGER.debug("WhitelistEntry: " + profile.getName());
PlayerDataUtil.changeStatus(profile);
} catch (Exception e) {
visitors.LOGGER.error("Error getting GameProfile from WhitelistEntry", e);
}
}
}
}
1 change: 0 additions & 1 deletion src/main/java/onebeastchris/util/ConfigUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

public class ConfigUtil {
private static final Logger LOGGER = LoggerFactory.getLogger("visitors");

private boolean discordCommand;
private String inviteLink;
//private boolean useCustomAdventure;
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/onebeastchris/util/PlayerDataUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ public class PlayerDataUtil {

public static HashMap<GameProfile, ServerPlayerEntity> visitorMap = new HashMap<>();

public static void addVisitor(GameProfile gameProfile, ServerPlayerEntity player){
visitorMap.put(gameProfile, player);
public static void addFutureVisitor(GameProfile gameProfile){
visitorMap.put(gameProfile, null);
}

public static void addPlayer(GameProfile gameProfile, ServerPlayerEntity player){
visitorMap.remove(gameProfile, null);
public static void addVisitor(GameProfile gameProfile, ServerPlayerEntity player){
visitorMap.put(gameProfile, player);
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/onebeastchris/util/PlayerInfoStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.Text;
import net.minecraft.util.math.Vec3d;

public record PlayerInfoStorage(Text name, ServerWorld world, int[] position) {
public record PlayerInfoStorage(Text name, ServerWorld world, Vec3d position) {
}
10 changes: 0 additions & 10 deletions src/main/java/onebeastchris/visitors.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,12 @@
import org.slf4j.LoggerFactory;

public class visitors implements ModInitializer {
// This logger is used to write text to the console and the log file.
// It is considered best practice to use your mod id as the logger's name.
// That way, it's clear which mod wrote info, warnings, and errors.
public static final Logger LOGGER = LoggerFactory.getLogger("visitors");

public static ConfigUtil config = new ConfigUtil();



@Override
public void onInitialize() {
// This code runs as soon as Minecraft is in a mod-load-ready state.
// However, some things (like resources) may still be uninitialized.
// Proceed with mild caution.


LOGGER.info("Loading visitors");
Register.registerAll();
}
Expand Down
5 changes: 2 additions & 3 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
"onebeastchris"
],
"contact": {
"homepage": "https://fabricmc.net/",
"sources": "https://github.com/FabricMC/fabric-example-mod"
"sources": "https://github.com/onebeastchris/visitors"
},
"license": "CC0-1.0",
"license": "MIT",
"icon": "assets/a_119.3test/icon.png",
"environment": "*",
"entrypoints": {
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/visitors.main.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
],
"injectors": {
"defaultRequire": 1
}
}
}

0 comments on commit 55ef1d3

Please sign in to comment.