Skip to content

Commit

Permalink
Merge pull request #28 from ekulxam/1.21
Browse files Browse the repository at this point in the history
Cleanup commands, fix scrolling
  • Loading branch information
sisby-folk authored Dec 6, 2024
2 parents 644c0da + 2af3507 commit c04b766
Show file tree
Hide file tree
Showing 20 changed files with 195 additions and 161 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public HashMap<Identifier, Integer> collectionTracker() {
);

public int getCount(Identifier shard) {
var count = collectionTracker.get(shard);
Integer count = collectionTracker.get(shard);
return count != null ? count : 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void syncFromJson(JsonObject obj) {
public static <T> PacketCodec<RegistryByteBuf, MiniRegistry<T>> createPacketCodec(Codec<T> valueCodec) {
return PacketCodecs.map(HashMap::new, Identifier.PACKET_CODEC, PacketCodecs.codec(valueCodec)).xmap(
map -> {
var registry = new MiniRegistry<>(valueCodec);
MiniRegistry<T> registry = new MiniRegistry<>(valueCodec);
registry.putAll(map);
return registry;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static void updateClientShardLibrary(ShardLibrary library) {
}

public static ShardCollection getServerCollection(UUID uuid) {
var collection = serverCollections.get(uuid);
ShardCollection collection = serverCollections.get(uuid);
if (collection == null) {
collection = new ShardCollectionImpl();
serverCollections.put(uuid, collection);
Expand All @@ -74,8 +74,8 @@ public static GlobalCollection getServerGlobalCollection() {

public static void calculateShardProgress() {
if (serverGlobalCollection != null) return;
var shardCountMap = new HashMap<Identifier, Integer>();
var totalCount = serverCollections.size();
HashMap<Identifier, Integer> shardCountMap = new HashMap<>();
int totalCount = serverCollections.size();

serverCollections.forEach(((uuid, identifiers) -> identifiers.forEach(identifier -> shardCountMap.compute(identifier, (k, count) -> count != null ? 1 + count : 1))));

Expand Down Expand Up @@ -117,7 +117,7 @@ public static void updateClientGlobalCollection(GlobalCollection collection) {
}

public static boolean triggerShardCollection(ServerPlayerEntity player, Identifier shardId) {
var collection = getServerCollection(player);
ShardCollection collection = getServerCollection(player);
if (collection.add(shardId)) {
if (player.getServer() != null) collectionPersistentState.markDirty();

Expand All @@ -130,7 +130,7 @@ public static boolean triggerShardCollection(ServerPlayerEntity player, Identifi
}

public static boolean triggerShardUncollection(ServerPlayerEntity player, Identifier shardId) {
var collection = getServerCollection(player);
ShardCollection collection = getServerCollection(player);
if (collection.remove(shardId)) {
if (player.getServer() != null) collectionPersistentState.markDirty();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import net.modfest.scatteredshards.api.shard.ShardType;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;

/**
Expand Down Expand Up @@ -41,14 +43,14 @@ public interface ShardLibrary {
PacketCodecs.map(HashMap::new, Identifier.PACKET_CODEC, PacketCodecs.collection(ArrayList::new, Identifier.PACKET_CODEC)).xmap(
map -> {
SetMultimap<Identifier, Identifier> multimap = MultimapBuilder.hashKeys().hashSetValues(3).build();
for (var entry : map.entrySet()) {
for (Map.Entry<Identifier, ArrayList<Identifier>> entry : map.entrySet()) {
multimap.putAll(entry.getKey(), entry.getValue());
}
return multimap;
},
multimap -> {
HashMap<Identifier, ArrayList<Identifier>> map = new HashMap<>();
for (var entry : multimap.asMap().entrySet()) {
for (Map.Entry<Identifier, Collection<Identifier>> entry : multimap.asMap().entrySet()) {
map.put(entry.getKey(), new ArrayList<>(entry.getValue()));
}
return map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class ShardCollectionPersistentState extends PersistentState {
);

public static ShardCollectionPersistentState get(MinecraftServer server) {
var result = server.getOverworld().getPersistentStateManager().getOrCreate(TYPE, ScatteredShards.ID + "_collections");
ShardCollectionPersistentState result = server.getOverworld().getPersistentStateManager().getOrCreate(TYPE, ScatteredShards.ID + "_collections");
ScatteredShardsAPI.register(result);
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import net.modfest.scatteredshards.ScatteredShards;
import net.modfest.scatteredshards.ScatteredShardsContent;
import net.modfest.scatteredshards.api.ScatteredShardsAPI;
import net.modfest.scatteredshards.api.ShardCollection;
import net.modfest.scatteredshards.api.ShardLibrary;
import net.modfest.scatteredshards.api.shard.Shard;
import net.modfest.scatteredshards.api.shard.ShardType;
import net.modfest.scatteredshards.client.command.ClientShardCommand;
Expand Down Expand Up @@ -76,10 +78,10 @@ public static void triggerShardModificationToast(Identifier shardId, boolean suc
}

public static void openShardTablet() {
final var client = MinecraftClient.getInstance();
final MinecraftClient client = MinecraftClient.getInstance();
client.send(() -> {
final var library = ScatteredShardsAPI.getClientLibrary();
final var collection = ScatteredShardsAPI.getClientCollection();
final ShardLibrary library = ScatteredShardsAPI.getClientLibrary();
final ShardCollection collection = ScatteredShardsAPI.getClientCollection();

client.setScreen(new ShardTabletGuiDescription.Screen(collection, library));
client.getSoundManager().play(PositionedSoundInstance.master(SoundEvents.ITEM_BOOK_PAGE_TURN, 1.0f, 1.0f));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.modfest.scatteredshards.client.command;

import com.google.common.collect.SetMultimap;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
Expand All @@ -9,22 +10,31 @@
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import com.mojang.brigadier.tree.LiteralCommandNode;
import com.mojang.brigadier.tree.CommandNode;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
import net.minecraft.client.MinecraftClient;
import net.minecraft.command.argument.IdentifierArgumentType;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.modfest.scatteredshards.api.MiniRegistry;
import net.modfest.scatteredshards.api.ScatteredShardsAPI;
import net.modfest.scatteredshards.api.ShardCollection;
import net.modfest.scatteredshards.api.ShardLibrary;
import net.modfest.scatteredshards.api.impl.ShardLibraryImpl;
import net.modfest.scatteredshards.api.shard.Shard;
import net.modfest.scatteredshards.api.shard.ShardType;
import net.modfest.scatteredshards.client.screen.ShardCreatorGuiDescription;
import net.modfest.scatteredshards.client.screen.ShardTabletGuiDescription;
import net.modfest.scatteredshards.command.ShardCommand;

import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;

@SuppressWarnings("SameParameterValue")
public class ClientShardCommand {

private static final DynamicCommandExceptionType INVALID_SET_ID = new DynamicCommandExceptionType(
Expand All @@ -36,10 +46,28 @@ public class ClientShardCommand {

public static int view(CommandContext<FabricClientCommandSource> context) throws CommandSyntaxException {
Identifier id = context.getArgument("set_id", Identifier.class);
var shards = ScatteredShardsAPI.getClientLibrary().shardSets().get(id);
if (shards.isEmpty()) {
ShardLibrary realLibrary = ScatteredShardsAPI.getClientLibrary();
Set<Identifier> shardPackSet = realLibrary.shardSets().get(id);
if (shardPackSet.isEmpty()) {
throw INVALID_SET_ID.create(id);
}
MinecraftClient client = context.getSource().getClient();
ShardCollection shardCollection = ScatteredShardsAPI.getClientCollection();
ShardLibrary fakeLibrary = new ShardLibraryImpl();
MiniRegistry<Shard> realShardRegistry = realLibrary.shards();
MiniRegistry<Shard> fakeShardRegistry = fakeLibrary.shards();
SetMultimap<Identifier, Identifier> fakeShardSets = fakeLibrary.shardSets();
MiniRegistry<ShardType> fakeShardTypes = fakeLibrary.shardTypes();
for (Identifier shardId : shardPackSet) {
Optional<Shard> optionalShard = realShardRegistry.get(shardId);
if (optionalShard.isEmpty()) continue;
Shard shard = optionalShard.get();
fakeShardRegistry.put(shardId, shard);
fakeShardSets.put(shard.sourceId(), shardId);
}
realLibrary.shardTypes().forEach((fakeShardTypes::put));
fakeLibrary.shardDisplaySettings().copyFrom(realLibrary.shardDisplaySettings());
client.send(() -> client.setScreen(new ShardTabletGuiDescription.Screen(shardCollection, fakeLibrary)));
return Command.SINGLE_SUCCESS;
}

Expand All @@ -49,7 +77,7 @@ public static int creatorNew(CommandContext<FabricClientCommandSource> context)
ShardType shardType = ScatteredShardsAPI.getClientLibrary().shardTypes().get(shardTypeId)
.orElseThrow(() -> ShardCommand.INVALID_SHARD_TYPE.create(shardTypeId));

var client = context.getSource().getClient();
MinecraftClient client = context.getSource().getClient();
client.send(() -> client.setScreen(ShardCreatorGuiDescription.Screen.newShard(modId, shardType)));
return Command.SINGLE_SUCCESS;
}
Expand All @@ -59,23 +87,21 @@ public static int creatorEdit(CommandContext<FabricClientCommandSource> context)
Shard shard = ScatteredShardsAPI.getClientLibrary().shards().get(shardId)
.orElseThrow(() -> INVALID_SHARD_ID.create(shardId));

var client = context.getSource().getClient();
MinecraftClient client = context.getSource().getClient();
client.send(() -> client.setScreen(ShardCreatorGuiDescription.Screen.editShard(shard)));
return Command.SINGLE_SUCCESS;
}

public static int shards(CommandContext<FabricClientCommandSource> context) throws CommandSyntaxException {
var client = context.getSource().getClient();
var library = ScatteredShardsAPI.getClientLibrary();
var collection = ScatteredShardsAPI.getClientCollection();

MinecraftClient client = context.getSource().getClient();
ShardLibrary library = ScatteredShardsAPI.getClientLibrary();
ShardCollection collection = ScatteredShardsAPI.getClientCollection();
client.send(() -> client.setScreen(new ShardTabletGuiDescription.Screen(collection, library)));

return Command.SINGLE_SUCCESS;
}

public static CompletableFuture<Suggestions> suggestShardSets(CommandContext<FabricClientCommandSource> context, SuggestionsBuilder builder) {
for (var id : ScatteredShardsAPI.getClientLibrary().shardSets().keySet()) {
for (Identifier id : ScatteredShardsAPI.getClientLibrary().shardSets().keySet()) {
builder.suggest(id.toString());
}
return builder.buildFuture();
Expand All @@ -96,18 +122,14 @@ public static CompletableFuture<Suggestions> suggestShardTypes(CommandContext<Fa
}

public static CompletableFuture<Suggestions> suggestModIds(CommandContext<FabricClientCommandSource> context, SuggestionsBuilder builder) {
for (var mod : FabricLoader.getInstance().getAllMods()) {
for (ModContainer mod : FabricLoader.getInstance().getAllMods()) {
builder.suggest(mod.getMetadata().getId());
}
return builder.buildFuture();
}

private static LiteralCommandNode<FabricClientCommandSource> literal(String name) {
return LiteralArgumentBuilder.<FabricClientCommandSource>literal(name).build();
}

private static LiteralCommandNode<FabricClientCommandSource> literal(String name, Command<FabricClientCommandSource> command) {
return LiteralArgumentBuilder.<FabricClientCommandSource>literal(name).executes(command).build();
private static LiteralArgumentBuilder<FabricClientCommandSource> literal(String name) {
return LiteralArgumentBuilder.<FabricClientCommandSource>literal(name);
}

private static RequiredArgumentBuilder<FabricClientCommandSource, Identifier> identifierArgument(String name) {
Expand All @@ -120,46 +142,45 @@ private static RequiredArgumentBuilder<FabricClientCommandSource, String> string

public static void register() {
ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> {

var shardcRoot = literal("shardc");
dispatcher.getRoot().addChild(shardcRoot);
CommandNode<FabricClientCommandSource> shardcNode = literal("shardc").build();

//Usage: /shardc view <set_id>
var view = literal("view");
var setId = identifierArgument("set_id")
CommandNode<FabricClientCommandSource> view = literal("view").build();
CommandNode<FabricClientCommandSource> setId = identifierArgument("set_id")
.suggests(ClientShardCommand::suggestShardSets)
.executes(ClientShardCommand::view);
view.addChild(setId.build());
shardcRoot.addChild(view);
.executes(ClientShardCommand::view).build();

//Usage: /shardc creator
//-> new <mod_id> <shard_type>
//-> edit <shard_id>
var creator = literal("creator");

var creatorNew = literal("new");
var modId = stringArgument("mod_id")
.suggests(ClientShardCommand::suggestModIds);
var modIdBuild = modId.build();
var shardType = identifierArgument("shard_type")
CommandNode<FabricClientCommandSource> creator = literal("creator").requires((source) -> source.hasPermissionLevel(2)).build();
CommandNode<FabricClientCommandSource> creatorNew = literal("new").build();
CommandNode<FabricClientCommandSource> modId = stringArgument("mod_id")
.suggests(ClientShardCommand::suggestModIds)
.build();
CommandNode<FabricClientCommandSource> shardType = identifierArgument("shard_type")
.suggests(ClientShardCommand::suggestShardTypes)
.executes(ClientShardCommand::creatorNew);
modIdBuild.addChild(shardType.build());
creatorNew.addChild(modIdBuild);
.executes(ClientShardCommand::creatorNew)
.build();

var creatorEdit = literal("edit");
var shardId = identifierArgument("shard_id")
CommandNode<FabricClientCommandSource> creatorEdit = literal("edit").build();
CommandNode<FabricClientCommandSource> shardId = identifierArgument("shard_id")
.suggests(ClientShardCommand::suggestShards)
.executes(ClientShardCommand::creatorEdit);
creatorEdit.addChild(shardId.build());
.executes(ClientShardCommand::creatorEdit).build();

//Usage: /shards
CommandNode<FabricClientCommandSource> shardsCommand = literal("shards").executes(ClientShardCommand::shards).build();

dispatcher.getRoot().addChild(shardcNode);
shardcNode.addChild(view);
view.addChild(setId);
shardcNode.addChild(creator);
creator.addChild(creatorNew);
creatorNew.addChild(modId);
modId.addChild(shardType);
creator.addChild(creatorEdit);
creatorEdit.addChild(shardId);

shardcRoot.addChild(creator);

//Usage: /shards
var shardsCommand = literal("shards", ClientShardCommand::shards);
dispatcher.getRoot().addChild(shardsCommand);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import net.minecraft.component.ComponentChanges;
import net.minecraft.component.ComponentMap;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.Registries;
import net.minecraft.resource.Resource;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.modfest.scatteredshards.api.ScatteredShardsAPI;
Expand All @@ -34,6 +36,7 @@
import net.modfest.scatteredshards.util.ModMetaUtil;

import java.util.Objects;
import java.util.Optional;

public class ShardCreatorGuiDescription extends LightweightGuiDescription {
public static final Text TITLE_TEXT = Text.translatable("gui.scattered_shards.creator.title");
Expand Down Expand Up @@ -83,11 +86,11 @@ public static Identifier parseTexture(String path) {
if (path.isBlank()) {
return null;
}
var id = Identifier.tryParse(path);
Identifier id = Identifier.tryParse(path);
if (id == null) {
return null;
}
var resource = MinecraftClient.getInstance().getResourceManager().getResource(id);
Optional<Resource> resource = MinecraftClient.getInstance().getResourceManager().getResource(id);
return resource.isPresent() ? id : null;
}

Expand All @@ -106,7 +109,7 @@ public static Identifier parseTexture(String path) {
public WProtectableField itemField = new WProtectableField(ITEM_TEXT)
.setChangedListener((it) -> {
this.item = null;
var id = Identifier.tryParse(it);
Identifier id = Identifier.tryParse(it);
if (id != null) {
this.item = Registries.ITEM.containsId(id)
? Registries.ITEM.get(id)
Expand All @@ -119,7 +122,7 @@ public static Identifier parseTexture(String path) {
.setChangedListener((it) -> {
try {
this.itemComponents = ComponentMap.EMPTY;
var json = GSON.fromJson(it, JsonElement.class);
JsonElement json = GSON.fromJson(it, JsonElement.class);
this.itemComponents = ComponentMap.CODEC.decode(JsonOps.INSTANCE, json).getOrThrow().getFirst();
} catch (Exception ignored) {
}
Expand All @@ -140,7 +143,7 @@ private void updateItemIcon() {
shard.setIcon(Shard.MISSING_ICON);
return;
}
var stack = item.getDefaultStack();
ItemStack stack = item.getDefaultStack();
if (!itemComponents.isEmpty()) {
stack.applyComponentsFrom(itemComponents);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.modfest.scatteredshards.ScatteredShards;
Expand Down Expand Up @@ -148,7 +149,7 @@ public void paint(DrawContext context, int x, int y, int mouseX, int mouseY) {
int hoverX = (isRight) ? 0 : halfWidth - 1;
boolean hovered = (mouseX >= hoverX && mouseY >= 0 && mouseX < hoverX + halfWidth && mouseY < getHeight());

var matrices = context.getMatrices();
MatrixStack matrices = context.getMatrices();
matrices.push();
matrices.translate(x, y, 0);
NinePatch<Identifier> leftButton = map(recessedButton, button);
Expand Down
Loading

0 comments on commit c04b766

Please sign in to comment.