feat: added rudimentary friendmanager

This commit is contained in:
əlemi 2023-03-02 01:07:58 +01:00
parent 320b1b4f4e
commit b480fd3a79
Signed by: alemi
GPG key ID: A4895B84D311642C
2 changed files with 130 additions and 0 deletions

View file

@ -3,6 +3,7 @@ package ftbsc.bscv;
import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.CommandSyntaxException;
import ftbsc.bscv.api.IModule; import ftbsc.bscv.api.IModule;
import ftbsc.bscv.system.Friends;
import ftbsc.bscv.system.ModManager; import ftbsc.bscv.system.ModManager;
import net.minecraft.client.entity.player.ClientPlayerEntity; import net.minecraft.client.entity.player.ClientPlayerEntity;
import net.minecraft.client.gui.screen.IngameMenuScreen; import net.minecraft.client.gui.screen.IngameMenuScreen;
@ -39,6 +40,9 @@ public class Boscovicino implements ICommons {
public static ForgeConfigSpec spec; public static ForgeConfigSpec spec;
private static Friends friends;
public static Friends friends() { return Boscovicino.friends; }
public Boscovicino() { public Boscovicino() {
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::clientSetup); FMLJavaModLoadingContext.get().getModEventBus().addListener(this::clientSetup);
@ -50,8 +54,12 @@ public class Boscovicino implements ICommons {
Boscovicino.spec = cfg.build(); Boscovicino.spec = cfg.build();
ForgeConfigSpec.Builder friendSpec = new ForgeConfigSpec.Builder();
Boscovicino.friends = new Friends(friendSpec, dp);
// register config handler // register config handler
ModLoadingContext.get().registerConfig(Type.CLIENT, spec, "bscv.toml"); ModLoadingContext.get().registerConfig(Type.CLIENT, spec, "bscv.toml");
ModLoadingContext.get().registerConfig(Type.CLIENT, friendSpec.build(), "friends.toml");
// Register ourselves for server and other game events we are interested in // Register ourselves for server and other game events we are interested in
MinecraftForge.EVENT_BUS.register(this); MinecraftForge.EVENT_BUS.register(this);

View file

@ -0,0 +1,122 @@
package ftbsc.bscv.system;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.annotation.Nullable;
import com.google.common.collect.Lists;
import com.mojang.authlib.GameProfile;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import ftbsc.bscv.Boscovicino;
import ftbsc.bscv.ICommons;
import net.minecraft.client.network.play.ClientPlayNetHandler;
import net.minecraft.client.network.play.NetworkPlayerInfo;
import net.minecraft.command.CommandSource;
import net.minecraft.command.Commands;
import net.minecraftforge.common.ForgeConfigSpec;
public class Friends implements ICommons {
@Nullable
public static GameProfile fromName(String name) {
ClientPlayNetHandler connection = MC.getConnection();
if (connection == null) return null;
for (NetworkPlayerInfo info : connection.getOnlinePlayers()) {
if (info.getProfile().getName().equals(name)) {
return new GameProfile(info.getProfile().getId(), info.getProfile().getName());
}
}
return null;
}
private ForgeConfigSpec.ConfigValue<List<? extends String>> store;
public Friends(ForgeConfigSpec.Builder builder, CommandDispatcher<CommandSource> dispatcher) {
this.store = builder.comment("actual friend list").defineList("data", () -> new ArrayList<String>(), u -> {
try{
UUID.fromString(u.toString());
return true;
} catch (Exception e) {
return false;
}
});
dispatcher.register(
Commands.literal("friends")
.then(
Commands.literal("add")
.then(
Commands.argument("name", StringArgumentType.word())
.executes( ctx -> {
String name = ctx.getArgument("name", String.class);
GameProfile profile = Friends.fromName(name);
if (profile == null) return 0;
this.addFriend(profile);
Boscovicino.log(String.format("> added %s <", profile.getName()));
return 1;
})
)
)
.then(
Commands.literal("remove")
.then(
Commands.argument("name", StringArgumentType.word())
.executes( ctx -> {
String name = ctx.getArgument("name", String.class);
GameProfile profile = Friends.fromName(name);
if (profile == null) return 0;
this.removeFriend(profile);
Boscovicino.log(String.format("> removed %s <", profile.getName()));
return 1;
})
)
)
.executes(ctx -> {
for (String friend : this.store.get()) {
Boscovicino.log(String.format("%s", friend));
}
return 1;
})
);
}
public boolean isFriend(String name) {
GameProfile usr = fromName(name);
if (usr != null) {
return isFriend(usr);
}
return false;
}
public boolean isFriend(GameProfile friend) { return isFriend(friend.getId()); }
public boolean isFriend(UUID friend) {
return this.store.get().contains(friend.toString());
}
public boolean removeFriend(GameProfile friend) {
if (this.store.get().contains(friend.getId().toString())) {
this.store.get().remove(friend.getId().toString());
this.store.save();
return true;
}
return false;
}
public boolean addFriend(GameProfile friend) {
if (!this.store.get().contains(friend.getId().toString())) {
ArrayList<String> botch = Lists.newArrayList(this.store.get());
botch.add(friend.getId().toString());
this.store.set((List<? extends String>) botch);
this.store.save();
return true;
}
return false;
}
}