Skip to content

Commit

Permalink
Simple whitelist system
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaming32 committed May 22, 2024
1 parent 45b56a0 commit ef351c3
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,15 @@ public class WorldHostConfig {
private String serverIp = DEFAULT_SERVER_IP;

private OnlineStatusLocation onlineStatusLocation = OnlineStatusLocation.RIGHT;

private boolean enableFriends = true;

private boolean enableReconnectionToasts = false;

private boolean noUPnP = false;

private boolean useShortIp = false;

private boolean showOutdatedWorldHost = true;

private boolean shareButton = true;

private boolean allowFriendRequests = true;

private boolean announceFriendsOnline = true;
private boolean whitelistJoins = false;

private final Set<UUID> friends = Collections.synchronizedSet(new LinkedHashSet<>());

Expand Down Expand Up @@ -203,6 +196,15 @@ public void setAnnounceFriendsOnline(boolean announceFriendsOnline) {
this.announceFriendsOnline = announceFriendsOnline;
}

@ConfigProperty(order = 10)
public boolean isWhitelistJoins() {
return whitelistJoins;
}

public void setWhitelistJoins(boolean whitelistJoins) {
this.whitelistJoins = whitelistJoins;
}

public Set<UUID> getFriends() {
return friends;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.github.gaming32.worldhost.mixin;

import com.mojang.authlib.GameProfile;
import io.github.gaming32.worldhost.WorldHost;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.players.PlayerList;
import org.spongepowered.asm.mixin.Final;
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(PlayerList.class)
public class MixinPlayerList {
@Shadow @Final private MinecraftServer server;

@Inject(method = "isWhiteListed", at = @At("HEAD"), cancellable = true)
private void whitelistFriends(GameProfile profile, CallbackInfoReturnable<Boolean> cir) {
if (!WorldHost.CONFIG.isWhitelistJoins()) return;
if (!server.isSingleplayerOwner(profile) && !WorldHost.isFriend(profile.getId())) {
cir.setReturnValue(false);
}
}
}
2 changes: 1 addition & 1 deletion src/main/resources/assets/world-host/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"world-host.config.announceFriendsOnline": "Popup Friends Online",
"world-host.config.announceFriendsOnline.tooltip": "When a friend goes online, you will get a popup. You can use this option to disable that.",
"world-host.config.whitelistJoins": "Whitelist Joins",
"world-host.config.whitelistJoins.tooltip": "Only allow friends to join your worlds. This will also affect LAN.",
"world-host.config.whitelistJoins.tooltip": "Only allow friends to join your worlds. This affects LAN and direct IPs.",
"world-host.friends": "Friends",
"world-host.friends.add_silently": "Add Friend Silently",
"world-host.friends.add_silently.tooltip": "This will add the person as a friend, without notifying them.\nNote that you must both friend each other in order to join each other's games.",
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/world-host.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"MixinCommands",
"MixinInputConstants",
"MixinLevelSummary",
"MixinPlayerList",
"MixinPublishCommand",
"MixinServerConnectionListener_1",
"ServerConnectionListenerAccessor"
Expand Down

0 comments on commit ef351c3

Please sign in to comment.