Remove json, implement mutual exclusives, adjustments & fix bugs

This commit is contained in:
2025-12-05 18:41:32 +01:00
parent bb8bc7b479
commit 9ce984a54a
18 changed files with 48 additions and 96 deletions
@@ -23,12 +23,6 @@ public class PlayerSkillData {
public boolean isUnlocked(ResourceLocation nodeId) {
return unlockedNodes.contains(nodeId);
}
public boolean hasTag(ResourceLocation tag) {
return activeTags.contains(tag);
}
public Set<ResourceLocation> getActiveTags() {
return activeTags;
}
private final ResourceLocation[] abilitySlots = new ResourceLocation[3];
@@ -57,10 +51,10 @@ public class PlayerSkillData {
}
tag.put("Tags", tagList);
ListTag abilityList = new ListTag();
for (int i = 0; i < abilitySlots.length; i++) {
for (ResourceLocation abilitySlot : abilitySlots) {
CompoundTag t = new CompoundTag();
if (abilitySlots[i] != null) {
t.putString("Id", abilitySlots[i].toString());
if (abilitySlot != null) {
t.putString("Id", abilitySlot.toString());
} else {
t.putString("Id", "");
}
@@ -130,9 +124,12 @@ public class PlayerSkillData {
}
public void bindAbility(int slot, ResourceLocation abilityId) {
bindHelperFunc(slot, abilityId, abilitySlots);
}
public static void bindHelperFunc(int slot, ResourceLocation abilityId, ResourceLocation[] abilitySlots) {
int index = slot - 1;
if (index < 0 || index >= abilitySlots.length) return;
// Ensure the same ability is not present in multiple slots
if (abilityId != null) {
for (int i = 0; i < abilitySlots.length; i++) {
if (abilityId.equals(abilitySlots[i])) {
@@ -143,12 +140,6 @@ public class PlayerSkillData {
abilitySlots[index] = abilityId;
}
public ResourceLocation getAbilityInSlot(int slot) {
int index = slot - 1;
if (index < 0 || index >= abilitySlots.length) return null;
return abilitySlots[index];
}
public ResourceLocation[] getAbilitySlots() {
return abilitySlots.clone();
}
@@ -9,9 +9,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class PlayerSkillProvider implements ICapabilityProvider, ICapabilitySerializable<CompoundTag> {
public static final ResourceLocation ID = new ResourceLocation("skillengine", "player_skill_data");
private final PlayerSkillData backend = new PlayerSkillData();
private final LazyOptional<PlayerSkillData> optional = LazyOptional.of(() -> backend);
@@ -19,7 +19,7 @@ public class SkillEngineCapabilities {
@SubscribeEvent
public static void onAttachCapabilities(AttachCapabilitiesEvent<Entity> event) {
if (event.getObject() instanceof Player player) {
if (event.getObject() instanceof Player) {
SkillEngine.LOGGER.info("Attaching PlayerSkillData capability");
event.addCapability(
new ResourceLocation(SkillEngine.MODID, "player_skill_data"),
@@ -10,6 +10,7 @@ import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.NotNull;
public class AbilityBindingScreen extends Screen {
@@ -74,7 +75,7 @@ public class AbilityBindingScreen extends Screen {
}
@Override
public void render(GuiGraphics gfx, int mouseX, int mouseY, float partialTick) {
public void render(@NotNull GuiGraphics gfx, int mouseX, int mouseY, float partialTick) {
this.renderBackground(gfx);
int x = (width - PANEL_WIDTH) / 2;
int y = (height - PANEL_HEIGHT) / 2;
@@ -10,19 +10,17 @@ import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
public class AbilitySelectScreen extends Screen {
private final Screen parent;
private final int slot; // 1..3
private final int slot;
private final List<AbilityNode> unlockedAbilities = new ArrayList<>();
private int page = 0;
private static final int PAGE_SIZE = 8;
private static final int PANEL_WIDTH = 260;
private static final int PANEL_HEIGHT = 220;
@@ -54,7 +52,6 @@ public class AbilitySelectScreen extends Screen {
for (int i = start; i < end; i++) {
AbilityNode node = unlockedAbilities.get(i);
String label = node.getTitle().getString();
// Disable if already in this slot to avoid redundant bind
boolean alreadyInThisSlot = SkilltreeClientState.getSlotOfAbility(node.getId()) == slot;
Button btn = Button.builder(Component.literal(label), b -> select(node))
.pos(x + 15, rowY)
@@ -67,7 +64,6 @@ public class AbilitySelectScreen extends Screen {
addRenderableWidget(btn);
rowY += 24;
}
// paging controls
if (page > 0) {
addRenderableWidget(Button.builder(Component.literal("< Prev"), b -> {
page--;
@@ -86,7 +82,6 @@ public class AbilitySelectScreen extends Screen {
private void select(AbilityNode node) {
ResourceLocation id = node.getId();
// optimistic client update, then send to server
SkilltreeClientState.bindAbilityLocal(slot, id);
PacketHandler.CHANNEL.sendToServer(new ServerboundBindAbilityPacket(id, slot));
back();
@@ -107,7 +102,7 @@ public class AbilitySelectScreen extends Screen {
}
@Override
public void render(GuiGraphics gfx, int mouseX, int mouseY, float partialTick) {
public void render(@NotNull GuiGraphics gfx, int mouseX, int mouseY, float partialTick) {
this.renderBackground(gfx);
int x = (width - PANEL_WIDTH) / 2;
int y = (height - PANEL_HEIGHT) / 2;
@@ -8,6 +8,7 @@ import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.NotNull;
public class AbilitySlotsScreen extends Screen {
private static final int PANEL_WIDTH = 260;
@@ -24,7 +25,6 @@ public class AbilitySlotsScreen extends Screen {
int buttonW = 80;
int buttonH = 20;
int buttonX = x + PANEL_WIDTH - buttonW - 15;
// Y slot = 1
addRenderableWidget(Button.builder(Component.literal("Rebind"), b -> openSelect(1))
.pos(buttonX, rowY).size(buttonW, buttonH).build());
@@ -34,7 +34,6 @@ public class AbilitySlotsScreen extends Screen {
// C slot = 3
addRenderableWidget(Button.builder(Component.literal("Rebind"), b -> openSelect(3))
.pos(buttonX, rowY + 70).size(buttonW, buttonH).build());
addRenderableWidget(Button.builder(Component.literal("Close"), b -> onClose())
.pos(x + PANEL_WIDTH - 70, y + PANEL_HEIGHT - 28).size(60, 20).build());
}
@@ -54,20 +53,17 @@ public class AbilitySlotsScreen extends Screen {
}
@Override
public void render(GuiGraphics gfx, int mouseX, int mouseY, float partialTick) {
public void render(@NotNull GuiGraphics gfx, int mouseX, int mouseY, float partialTick) {
this.renderBackground(gfx);
int x = (width - PANEL_WIDTH) / 2;
int y = (height - PANEL_HEIGHT) / 2;
gfx.fill(0, 0, width, height, 0xAA000000);
gfx.fill(x, y, x + PANEL_WIDTH, y + PANEL_HEIGHT, 0xFF222222);
gfx.drawCenteredString(font, Component.literal("Ability Slots"), x + PANEL_WIDTH / 2, y + 12, 0xFFFFFF);
int rowY = y + 40;
drawSlotRow(gfx, x + 15, rowY, 1, "Y");
drawSlotRow(gfx, x + 15, rowY + 35, 2, "X");
drawSlotRow(gfx, x + 15, rowY + 70, 3, "C");
super.render(gfx, mouseX, mouseY, partialTick);
}
@@ -11,6 +11,7 @@ import net.minecraftforge.fml.common.Mod;
modid = SkillEngine.MODID,
value = Dist.CLIENT
)
public class ClientEvents {
@SubscribeEvent
public static void onKeyInput(InputEvent.Key event) {
@@ -1,6 +1,7 @@
package net.fretux.skillengine.client;
import net.fretux.skillengine.SkillEngine;
import net.fretux.skillengine.capability.PlayerSkillData;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.toasts.SystemToast;
import net.minecraft.network.chat.Component;
@@ -65,14 +66,7 @@ public class SkilltreeClientState {
}
public static void bindAbilityLocal(int slot, ResourceLocation abilityId) {
int index = slot - 1;
if (index < 0 || index >= abilitySlots.length) return;
if (abilityId != null) {
for (int i = 0; i < abilitySlots.length; i++) {
if (abilityId.equals(abilitySlots[i])) abilitySlots[i] = null;
}
}
abilitySlots[index] = abilityId;
PlayerSkillData.bindHelperFunc(slot, abilityId, abilitySlots);
}
public static void setAbilitySlots(ResourceLocation[] slots) {
@@ -17,33 +17,23 @@ import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.FormattedText;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
public class SkilltreeScreen extends Screen {
private static final int NODE_RADIUS = 12;
private static final int ABILITY_RADIUS = 12;
private static final int OVERLAY_WIDTH = 240;
private static final int OVERLAY_HEIGHT = 180;
private static final int OVERLAY_CONTENT_HEIGHT = 160;
private static final float ZOOM_STEP = 0.1f;
private static final float LINE_THICKNESS = 2.0f;
private float zoom = 1.0f;
private final float minZoom = 0.5f;
private final float maxZoom = 2.0f;
private double panX = 0;
private double panY = 0;
private boolean dragging = false;
private double lastMouseX;
private double lastMouseY;
private SkillNode hoveredNode = null;
private SkillNode selectedNode = null;
private AbilityNode hoveredAbility = null;
@@ -60,7 +50,9 @@ public class SkilltreeScreen extends Screen {
@Override
public boolean mouseScrolled(double mouseX, double mouseY, double delta) {
zoom += delta * ZOOM_STEP;
zoom += (float) (delta * ZOOM_STEP);
float minZoom = 0.5f;
float maxZoom = 2.0f;
zoom = Mth.clamp(zoom, minZoom, maxZoom);
return true;
}
@@ -74,6 +66,7 @@ public class SkilltreeScreen extends Screen {
SkillNode clickedNode = findNodeAt(mouseX, mouseY);
if (clickedNode != null) {
if (isLockedByExclusivity(clickedNode)) {
assert Minecraft.getInstance().player != null;
Minecraft.getInstance().player.playSound(
net.minecraft.sounds.SoundEvents.VILLAGER_NO, 1f, 1f
);
@@ -97,8 +90,6 @@ public class SkilltreeScreen extends Screen {
return true;
}
dragging = true;
lastMouseX = mouseX;
lastMouseY = mouseY;
}
return super.mouseClicked(mouseX, mouseY, button);
}
@@ -125,7 +116,7 @@ public class SkilltreeScreen extends Screen {
}
@Override
public void render(GuiGraphics gfx, int mouseX, int mouseY, float partialTicks) {
public void render(@NotNull GuiGraphics gfx, int mouseX, int mouseY, float partialTicks) {
this.renderBackground(gfx);
renderSkillPointHeader(gfx);
hoveredNode = findNodeAt(mouseX, mouseY);
@@ -244,6 +235,7 @@ public class SkilltreeScreen extends Screen {
if (!node.getPrereqAttributes().isEmpty()) {
tooltip.add(Component.literal("Requirements:")
.withStyle(ChatFormatting.GOLD));
assert Minecraft.getInstance().player != null;
Minecraft.getInstance().player.getCapability(PlayerStatsProvider.PLAYER_STATS)
.ifPresent(stats -> node.getPrereqAttributes().forEach((attr, required) -> {
int current = stats.getAttributeLevel(attr);
@@ -287,6 +279,7 @@ public class SkilltreeScreen extends Screen {
0xFFFFFF
);
textY[0] += 12;
assert Minecraft.getInstance().player != null;
Minecraft.getInstance().player.getCapability(PlayerStatsProvider.PLAYER_STATS)
.ifPresent(stats -> node.getPrereqAttributes().forEach((attr, required) -> {
int current = stats.getAttributeLevel(attr);
@@ -14,8 +14,7 @@ import net.minecraftforge.network.PacketDistributor;
public class SkillEnginePlayerEvents {
private static void sync(ServerPlayer player) {
player.getCapability(SkillEngineCapabilities.PLAYER_SKILLS).ifPresent(data -> {
PacketHandler.CHANNEL.send(
player.getCapability(SkillEngineCapabilities.PLAYER_SKILLS).ifPresent(data -> PacketHandler.CHANNEL.send(
PacketDistributor.PLAYER.with(() -> player),
new ClientboundSyncSkillsPacket(
data.getUnlockedNodes(),
@@ -23,8 +22,7 @@ public class SkillEnginePlayerEvents {
data.getSkillPoints(),
data.getAbilitySlots()
)
);
});
));
}
@SubscribeEvent
@@ -9,7 +9,6 @@ import net.fretux.skillengine.client.SkilltreeClientState;
import java.util.function.Supplier;
public class ClientboundNodeUnlockedPacket {
private final ResourceLocation id;
private final int newSkillPoints;
@@ -13,7 +13,7 @@ public class PacketHandler {
private static final String PROTOCOL = "1";
public static final SimpleChannel CHANNEL = NetworkRegistry.ChannelBuilder
.named(new ResourceLocation(SkillEngine.MODID, "network"))
.named(ResourceLocation.fromNamespaceAndPath(SkillEngine.MODID, "network"))
.clientAcceptedVersions(PROTOCOL::equals)
.serverAcceptedVersions(PROTOCOL::equals)
.networkProtocolVersion(() -> PROTOCOL)
@@ -2,7 +2,6 @@ package net.fretux.skillengine.network;
import net.fretux.skillengine.capability.SkillEngineCapabilities;
import net.minecraft.server.level.ServerPlayer;
import net.fretux.skillengine.network.PacketHandler;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.network.NetworkEvent;
@@ -36,7 +35,6 @@ public class ServerboundBindAbilityPacket {
player.getCapability(SkillEngineCapabilities.PLAYER_SKILLS).ifPresent(data ->
data.bindAbility(msg.slot, msg.abilityId)
);
// Sync back to client so UI reflects updated bindings
PacketHandler.syncSkillsTo(player);
});
ctx.get().setPacketHandled(true);
@@ -12,13 +12,10 @@ import net.minecraftforge.network.PacketDistributor;
import java.util.function.Supplier;
public class ServerboundUnlockNodePacket {
private final ResourceLocation nodeId;
public ServerboundUnlockNodePacket(ResourceLocation nodeId) {
this.nodeId = nodeId;
}
public static void encode(ServerboundUnlockNodePacket msg, FriendlyByteBuf buf) {
buf.writeResourceLocation(msg.nodeId);
}
@@ -73,7 +70,6 @@ public class ServerboundUnlockNodePacket {
}
});
});
ctx.get().setPacketHandled(true);
}
}
@@ -13,6 +13,7 @@ import net.minecraft.util.profiling.ProfilerFiller;
import net.minecraftforge.event.AddReloadListenerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
@@ -34,38 +35,28 @@ public class AbilityNodeLoader extends SimpleJsonResourceReloadListener {
@Override
protected void apply(Map<ResourceLocation, JsonElement> jsons,
ResourceManager resourceManager,
ProfilerFiller profiler) {
@NotNull ResourceManager resourceManager,
@NotNull ProfilerFiller profiler) {
SkillEngine.LOGGER.info("Loading ability nodes...");
AbilityNodeRegistry.clear();
jsons.forEach((id, json) -> {
JsonObject obj = json.getAsJsonObject();
Component title = Component.literal(obj.get("title").getAsString());
Component description = Component.literal(obj.get("description").getAsString());
JsonObject pos = obj.getAsJsonObject("position");
float x = pos.get("x").getAsFloat();
float y = pos.get("y").getAsFloat();
List<ResourceLocation> links = new ArrayList<>();
obj.getAsJsonArray("links").forEach(e ->
links.add(new ResourceLocation(e.getAsString())));
List<ResourceLocation> tags = new ArrayList<>();
obj.getAsJsonArray("tags").forEach(e ->
tags.add(new ResourceLocation(e.getAsString())));
ResourceLocation icon = new ResourceLocation(obj.get("icon").getAsString());
int cooldown = obj.has("cooldown") ? obj.get("cooldown").getAsInt() : 0;
AbilityNode node = new AbilityNode(id, title, description, x, y, links, tags, icon, cooldown);
AbilityNodeRegistry.put(node);
});
SkillEngine.LOGGER.info("Loaded {} active ability nodes", AbilityNodeRegistry.all().size());
}
}
@@ -7,21 +7,16 @@ import java.util.LinkedHashMap;
import java.util.Map;
public class AbilityNodeRegistry {
private static final Map<ResourceLocation, AbilityNode> ABILITIES = new LinkedHashMap<>();
public static void clear() {
ABILITIES.clear();
}
public static void put(AbilityNode node) {
ABILITIES.put(node.getId(), node);
}
public static AbilityNode get(ResourceLocation id) {
return ABILITIES.get(id);
}
public static Collection<AbilityNode> all() {
return ABILITIES.values();
}
@@ -10,6 +10,7 @@ import net.minecraft.util.profiling.ProfilerFiller;
import net.minecraftforge.event.AddReloadListenerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
@@ -32,8 +33,8 @@ public class SkillNodeLoader extends SimpleJsonResourceReloadListener {
@Override
protected void apply(Map<ResourceLocation, JsonElement> jsons,
ResourceManager resourceManager,
ProfilerFiller profiler) {
@NotNull ResourceManager resourceManager,
@NotNull ProfilerFiller profiler) {
SkillEngine.LOGGER.info("Loading skill nodes...");
SkillNodeRegistry.clear();
jsons.forEach((id, element) -> {
@@ -0,0 +1,5 @@
{
"key.skillengine.openskilltree": "Open Skilltree",
"key.skillengine.openabilities": "Open Ability Screen"
}