Skip to content

Commit

Permalink
more getters, progress displays on shift, nicer tooltips
Browse files Browse the repository at this point in the history
  • Loading branch information
sisby-folk committed Jul 27, 2024
1 parent 605f192 commit 524ad7c
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package net.modfest.scatteredshards;

import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.modfest.scatteredshards.api.ScatteredShardsAPI;
import net.modfest.scatteredshards.load.ShardTypeLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
import net.fabricmc.fabric.api.resource.ResourcePackActivationType;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.util.Identifier;
import net.modfest.scatteredshards.api.shard.ShardType;
import net.modfest.scatteredshards.command.ShardCommand;
Expand All @@ -30,11 +28,11 @@ public static String permission(String path) {

@Override
public void onInitialize() {
//ScatteredShardsAPI.init();
ShardType.register();
ShardTypeLoader.register();
ShardCommand.register();
ScatteredShardsNetworking.register();
ScatteredShardsContent.register();
ServerLifecycleEvents.SERVER_STOPPED.register(ScatteredShardsAPI::serverStopped);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.UUID;

import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.server.MinecraftServer;
import org.jetbrains.annotations.ApiStatus;

import net.fabricmc.api.EnvType;
Expand All @@ -27,12 +28,12 @@ public class ScatteredShardsAPI {
private static ShardCollectionPersistentState collectionPersistentState;
private static final ShardLibrary serverShardLibrary = new ShardLibraryImpl();
private static Map<UUID, ShardCollection> serverCollections = new HashMap<>();
public static ShardLibrary clientShardLibrary = null;
public static ShardCollection clientShardCollection = null;
public static GlobalCollection clientGlobalCollection = null;
private static ShardLibrary clientShardLibrary = null;
private static ShardCollection clientShardCollection = null;
private static GlobalCollection clientGlobalCollection = null;
private static Thread serverThread = null;
private static Thread clientThread = null;
public static GlobalCollection serverGlobalCollection = null;
private static GlobalCollection serverGlobalCollection = null;


public static ShardLibrary getServerLibrary() {
Expand All @@ -52,6 +53,10 @@ public static ShardLibrary getClientLibrary() {
return clientShardLibrary;
}

public static void updateClientShardLibrary(ShardLibrary library) {
clientShardLibrary = library;
}

public static ShardCollection getServerCollection(UUID uuid) {
var collection = serverCollections.get(uuid);
if (collection == null) {
Expand All @@ -62,7 +67,16 @@ public static ShardCollection getServerCollection(UUID uuid) {
return collection;
}

public static GlobalCollection getServerGlobalCollection() {
if (serverThread != null && !Thread.currentThread().equals(serverThread)) {
throw new IllegalStateException("getServerGlobalCollection called from thread '" + Thread.currentThread().getName() + "'. This method can only be accessed from the server thread.");
}

return serverGlobalCollection;
}

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

Expand Down Expand Up @@ -93,6 +107,21 @@ public static ShardCollection getClientCollection() {
return clientShardCollection;
}

public static void updateClientShardCollection(ShardCollection collection) {
clientShardCollection = collection;
}

public static GlobalCollection getClientGlobalCollection() {
if (clientThread != null && !Thread.currentThread().equals(clientThread)) {
throw new IllegalStateException("getClientGlobalCollection called from thread '" + Thread.currentThread().getName() + "'. This method can only be accessed from the client thread.");
}

return clientGlobalCollection;
}

public static void updateClientGlobalCollection(GlobalCollection collection) {
clientGlobalCollection = collection;
}

public static boolean triggerShardCollection(ServerPlayerEntity player, Identifier shardId) {
var collection = getServerCollection(player);
Expand Down Expand Up @@ -127,6 +156,16 @@ public static void init() {
serverThread = Thread.currentThread();
}

public static void serverStopped(MinecraftServer server) {
serverShardLibrary.clearAll();
serverCollections = new HashMap<>();
clientShardLibrary = null;
clientShardCollection = null;
clientGlobalCollection = null;
serverThread = null;
serverGlobalCollection = null;
}

@ApiStatus.Internal
@Environment(EnvType.CLIENT)
public static void initClient() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.minecraft.client.toast.SystemToast;
import net.minecraft.client.toast.Toast;

import net.minecraft.client.util.InputUtil;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
Expand Down Expand Up @@ -40,7 +41,7 @@ public static void triggerShardCollectAnimation(Identifier shardId) {
var collection = ScatteredShardsAPI.getClientCollection();

Shard shard = library.shards().get(shardId).orElse(Shard.MISSING_SHARD);
if (shard == null) {
if (shard == Shard.MISSING_SHARD) {
ScatteredShards.LOGGER.warn("Received shard collection event with ID '" + shardId + "' but it does not exist on this client");
return;
}
Expand Down Expand Up @@ -78,4 +79,8 @@ public static void openShardTablet() {
client.getSoundManager().play(PositionedSoundInstance.master(SoundEvents.ITEM_BOOK_PAGE_TURN, 1.0f, 1.0f));
});
}

public static boolean hasShiftDown() {
return InputUtil.isKeyPressed(MinecraftClient.getInstance().getWindow().getHandle(), 340) || InputUtil.isKeyPressed(MinecraftClient.getInstance().getWindow().getHandle(), 344);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.modfest.scatteredshards.client.screen;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;

Expand All @@ -14,6 +15,8 @@
import io.github.cottonmc.cotton.gui.widget.data.Axis;
import io.github.cottonmc.cotton.gui.widget.data.Insets;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.minecraft.text.Text;
import net.minecraft.util.Colors;
import net.minecraft.util.Identifier;
import net.modfest.scatteredshards.api.ShardCollection;
import net.modfest.scatteredshards.api.ShardLibrary;
Expand All @@ -22,6 +25,7 @@
import net.modfest.scatteredshards.client.screen.widget.WLeftRightPanel;
import net.modfest.scatteredshards.client.screen.widget.WShardPanel;
import net.modfest.scatteredshards.client.screen.widget.WShardSetPanel;
import net.modfest.scatteredshards.client.screen.widget.scalable.WScaledLabel;
import net.modfest.scatteredshards.networking.C2SRequestGlobalCollection;

public class ShardTabletGuiDescription extends LightweightGuiDescription {
Expand All @@ -45,12 +49,39 @@ public ShardTabletGuiDescription(ShardCollection collection, ShardLibrary librar
List<Identifier> ids = new ArrayList<>(this.library.shardSets().keySet());
ids.sort(Comparator.comparing(Identifier::getNamespace));

shardSelector = new WListPanel<Identifier, WShardSetPanel>(ids, WShardSetPanel::new, this::configurePanel);
shardSelector = new WListPanel<>(ids, WShardSetPanel::new, this::configurePanel);
selectorPanel.setInsets(Insets.ROOT_PANEL);

WLeftRightPanel root = new WLeftRightPanel(selectorPanel, shardPanel);
selectorPanel.add(shardSelector, 0, 0, getLayoutWidth(selectorPanel), getLayoutHeight(selectorPanel));

int panelHeight = selectorPanel.getHeight();

WScaledLabel progressVisited = new WScaledLabel(() -> {
if (!ScatteredShardsClient.hasShiftDown()) return Text.empty();
int visitedSets = 0;
for (Collection<Identifier> set : library.shardSets().asMap().values()) {
for (Identifier identifier : set) {
if (collection.contains(identifier)) {
visitedSets++;
break;
}
}
}
return Text.translatable("gui.scattered_shards.tablet.label.progress.started", "%.0f%%".formatted(100 * visitedSets / (float) library.shardSets().asMap().keySet().size()));
}, 1.0f).setColor(Colors.LIGHT_GRAY);
selectorPanel.add(progressVisited, 0, 0);
progressVisited.setSize(80, 10);
progressVisited.setLocation(13, panelHeight - 20);

WScaledLabel progressTotal = new WScaledLabel(() -> {
if (!ScatteredShardsClient.hasShiftDown()) return Text.empty();
return Text.translatable("gui.scattered_shards.tablet.label.progress.total", "%.0f%%".formatted(100 * collection.size() / (float) library.shards().size()));
}, 1.0f).setColor(Colors.LIGHT_GRAY);
selectorPanel.add(progressTotal, 0, 0);
progressTotal.setSize(80, 10);
progressTotal.setLocation(selectorPanel.getWidth() - 72, panelHeight - 20);

ClientPlayNetworking.send(C2SRequestGlobalCollection.INSTANCE);

this.setRootPanel(root);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;
import net.modfest.scatteredshards.ScatteredShards;
import net.modfest.scatteredshards.api.ScatteredShardsAPI;
Expand Down Expand Up @@ -75,13 +76,21 @@ public void paint(DrawContext context, int x, int y, int mouseX, int mouseY) {
}

}

@Override
public void addTooltip(TooltipBuilder tooltip) {
var globalCollection = ScatteredShardsAPI.clientGlobalCollection;

tooltip.add(Text.translatable("gui.scattered_shards.tablet.tooltip.global_collection", globalCollection.getCount(shardId), globalCollection.totalPlayers()));


if (ScatteredShardsClient.hasShiftDown()) {
tooltip.add(shard.name());
}
tooltip.add(ShardType.getDescription(shard.shardTypeId()).copy().withColor(shardType.textColor()));
if (ScatteredShardsClient.hasShiftDown()) {
var globalCollection = ScatteredShardsAPI.getClientGlobalCollection();
if (globalCollection != null) {
tooltip.add(Text.translatable("gui.scattered_shards.tablet.tooltip.global_collection", "%.1f%%".formatted(100 * globalCollection.getCount(shardId) / (float) globalCollection.totalPlayers())).formatted(Formatting.GRAY));
}
}

super.addTooltip(tooltip);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ public class WShardSetPanel extends WPanelWithInsets {

protected Consumer<Shard> shardConsumer = (it) -> {};

private WScaledLabel sourceLabel = new WScaledLabel(Text.literal(""), 0.8f)
private final WScaledLabel sourceLabel = new WScaledLabel(Text.literal(""), 0.8f)
.setColor(0xFF_CCFFCC)
.setShadow(true);
private List<WMiniShard> shards = new ArrayList<>();
private final List<WMiniShard> shards = new ArrayList<>();

public WShardSetPanel() {
this.setInsets(new Insets(2));
}
Expand All @@ -51,17 +52,16 @@ public int layoutHeight() {
}

public void setShardSet(Identifier setId, ShardLibrary library, ShardCollection collection) {
List<Identifier> shardSet = new ArrayList<>();
shardSet.addAll(library.shardSets().get(setId));
List<Identifier> shardSet = new ArrayList<>(library.shardSets().get(setId));
shardSet.sort((a, b) -> {
int aPriority = library.shards().get(a)
.map(it -> it.shardTypeId())
.map(Shard::shardTypeId)
.flatMap(library.shardTypes()::get)
.map(ShardType::listOrder)
.orElse(Integer.MAX_VALUE);

int bPriority = library.shards().get(b)
.map(it -> it.shardTypeId())
.map(Shard::shardTypeId)
.flatMap(library.shardTypes()::get)
.map(ShardType::listOrder)
.orElse(Integer.MAX_VALUE);
Expand All @@ -76,7 +76,7 @@ public void setShardSet(Identifier setId, ShardLibrary library, ShardCollection
//Start fresh on this panel's actual children
this.children.clear();
this.add(sourceLabel, 0, 96, 18);
sourceLabel.setLocation(0+this.insets.left(), 2+this.insets.top());
sourceLabel.setLocation(this.insets.left(), 2+this.insets.top());
sourceLabel.setText(Shard.getSourceForSourceId(setId));

//The actual remaining layout width is less the label and the width of the card itself
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class WScaledText extends WScalableWidget {

protected Supplier<Text> text;
protected IntSupplier color = () -> 0xFF_FFFFFF;
protected Supplier<List<OrderedText>> hover = () -> List.of();
protected Supplier<List<OrderedText>> hover = List::of;
protected boolean shadow = false;
protected int backgroundColor = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class C2SRequestGlobalCollection implements CustomPayload {
public static final PacketCodec<RegistryByteBuf, C2SRequestGlobalCollection> PACKET_CODEC = PacketCodec.unit(INSTANCE);

public static void receive(C2SRequestGlobalCollection payload, ServerPlayNetworking.Context context) {
ServerPlayNetworking.send(context.player(), new S2CSyncGlobalCollection(ScatteredShardsAPI.serverGlobalCollection));
ServerPlayNetworking.send(context.player(), new S2CSyncGlobalCollection(ScatteredShardsAPI.getServerGlobalCollection()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public record S2CSyncCollection(ShardCollection collection) implements CustomPay
public static void receive(S2CSyncCollection payload, ClientPlayNetworking.Context context) {
context.client().execute(() -> {
ScatteredShards.LOGGER.info("Syncing ShardCollection with {} shards collected.", payload.collection().size());
ScatteredShardsAPI.clientShardCollection = payload.collection(); // TODO: bad, fix
ScatteredShardsAPI.updateClientShardCollection(payload.collection());
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ public static void receive(S2CSyncGlobalCollection payload, ClientPlayNetworking
ScatteredShards.LOGGER.info("Syncing GlobalShardCollection...");

context.client().execute(() -> {
ScatteredShardsAPI.clientGlobalCollection = payload.globalCollection(); // bad, TODO: fix

ScatteredShards.LOGGER.info("Sync complete.");
ScatteredShardsAPI.updateClientGlobalCollection(payload.globalCollection());
ScatteredShards.LOGGER.info("Sync complete. Received data for {} players.", payload.globalCollection.totalPlayers());
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static void receive(S2CSyncLibrary payload, ClientPlayNetworking.Context
ScatteredShards.LOGGER.info("Syncing ShardLibrary...");

context.client().execute(() -> {
ScatteredShardsAPI.clientShardLibrary = payload.library(); // bad, TODO: fix
ScatteredShardsAPI.updateClientShardLibrary(payload.library());
ShardLibrary library = ScatteredShardsAPI.getClientLibrary();

//Tidy up in case MISSING got dropped
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,8 @@ public static void register() {
ServerPlayNetworking.send(handler.getPlayer(), new S2CSyncLibrary(ScatteredShardsAPI.getServerLibrary()));
ShardCollectionPersistentState.get(server); // Trigger the PersistentState load if it hasn't yet
ServerPlayNetworking.send(handler.getPlayer(), new S2CSyncCollection(ScatteredShardsAPI.getServerCollection(handler.getPlayer())));


if (ScatteredShardsAPI.serverGlobalCollection == null) {
ScatteredShardsAPI.calculateShardProgress();
}

ServerPlayNetworking.send(handler.getPlayer(), new S2CSyncGlobalCollection(ScatteredShardsAPI.serverGlobalCollection));
ScatteredShardsAPI.calculateShardProgress();
ServerPlayNetworking.send(handler.getPlayer(), new S2CSyncGlobalCollection(ScatteredShardsAPI.getServerGlobalCollection()));
});

}
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/assets/scattered_shards/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
"gui.scattered_shards.creator.toggle.mod_icon": "Use mod icon",
"gui.scattered_shards.creator.button.save": "Save",
"gui.scattered_shards.tablet.click_on_a_shard": "Click on a Shard to the left",
"gui.scattered_shards.tablet.tooltip.global_collection": "%s/%s Players have collected this shard",
"gui.scattered_shards.tablet.tooltip.global_collection": "%s of players have this shard",
"gui.scattered_shards.tablet.label.progress.started": "%s Started",
"gui.scattered_shards.tablet.label.progress.total": "%s Total",
"toast.scattered_shards.shard_mod.title": "Shard Modification",
"toast.scattered_shards.shard_mod.success": "Successfully modified '%s'",
"toast.scattered_shards.shard_mod.fail": "Failed to modify '%s'",
Expand Down

0 comments on commit 524ad7c

Please sign in to comment.