Skip to content

Commit

Permalink
added textWidgets and ButtonWidgets
Browse files Browse the repository at this point in the history
button action is still w.i.p. (only works client side), currently working on alignment for widgets
  • Loading branch information
megadoxs committed Jun 21, 2024
1 parent b36fa13 commit 8e24e85
Show file tree
Hide file tree
Showing 24 changed files with 578 additions and 105 deletions.
23 changes: 17 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ repositories {
name = 'Ladysnake Mods'
url = 'https://maven.ladysnake.org/releases'
}
maven {
name = 'Ladysnake Mods (snapshots)'
url = 'https://maven.ladysnake.org/snapshots'
}
maven {
url = 'https://maven.cafeteria.dev'
content {
Expand Down Expand Up @@ -93,17 +97,24 @@ dependencies {

// Fabric API. This is technically optional, but you probably want it anyway.
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
modImplementation "com.github.apace100:origins-fabric:${project.origins_version}"

modApi "com.github.apace100:apoli:${project.apoli_version}"
include "com.github.apace100:apoli:${project.apoli_version}"

modApi("dev.onyxstudios.cardinal-components-api:cardinal-components-base:${cca_version}") {
modApi("dev.onyxstudios.cardinal-components-api:cardinal-components-base:${project.cca_version}") {
exclude(group: "net.fabricmc.fabric-api")
}
modApi("dev.onyxstudios.cardinal-components-api:cardinal-components-entity:${cca_version}") {
include "dev.onyxstudios.cardinal-components-api:cardinal-components-base:${project.cca_version}"

modApi("dev.onyxstudios.cardinal-components-api:cardinal-components-entity:${project.cca_version}") {
exclude(group: "net.fabricmc.fabric-api")
}
include "dev.onyxstudios.cardinal-components-api:cardinal-components-entity:${project.cca_version}"

modApi "io.github.ladysnake:PlayerAbilityLib:${pal_version}"
include "io.github.ladysnake:PlayerAbilityLib:${pal_version}"

modApi "io.github.apace100:apoli:${project.apoli_version}"
include "io.github.apace100:apoli:${project.apoli_version}"

modImplementation "io.github.apace100:origins-fabric:${project.origins_version}"

include modApi("com.jamieswhiteshirt:reach-entity-attributes:${project.reach_version}")
}
Expand Down
15 changes: 8 additions & 7 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ org.gradle.parallel=true

# Fabric Properties
# check these on https://fabricmc.net/develop
minecraft_version=1.20.2
yarn_mappings=1.20.2+build.4
minecraft_version=1.20.4
yarn_mappings=1.20.4+build.3
loader_version=0.15.11

# Mod Properties
Expand All @@ -14,8 +14,9 @@ maven_group=com.megadoxs.megalib
archives_base_name=megalib

# Dependencies
fabric_version=0.91.6+1.20.2
cca_version=5.3.0
apoli_version=2.11.11
reach_version=2.4.0
origins_version=1.12.3
fabric_version=0.97.1+1.20.4
cca_version=5.4.0
pal_version=1.9.0
apoli_version=2.12.0-alpha.4+mc.1.20.4
reach_version=2.5.0
origins_version=1.13.0-alpha.3+mc.1.20.4
6 changes: 4 additions & 2 deletions src/main/java/com/megadoxs/megalib/Megalib.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.megadoxs.megalib;

import com.megadoxs.megalib.registry.MegalibRegistries;
import com.megadoxs.megalib.registry.factory.MegalibPowers;
import com.megadoxs.megalib.screen_element.ScreenElements;
import net.fabricmc.api.ModInitializer;

import net.minecraft.util.Identifier;
Expand All @@ -13,13 +15,13 @@ public class Megalib implements ModInitializer {
// That way, it's clear which mod wrote info, warnings, and errors.

public static final String MOD_ID = "megalib";
public static final Logger LOGGER = LoggerFactory.getLogger("Megalib.class");

@Override
public void onInitialize() {
ScreenElements.register();

MegalibPowers.register();

LOGGER.info("Megalib has initialized. Ready to power up your game!");
}
public static Identifier identifier(String path) {
return new Identifier(MOD_ID, path);
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/megadoxs/megalib/MegalibClient.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package com.megadoxs.megalib;

import com.megadoxs.megalib.networking.packet.c2s.PerformButtonActionsC2SPacket;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.minecraft.util.Identifier;

import java.util.LinkedList;
import java.util.List;

@Environment(EnvType.CLIENT)
public class MegalibClient implements ClientModInitializer {
@Override
public void onInitializeClient() {
}

public static void performButtonActions(Identifier identifier) {
ClientPlayNetworking.send(new PerformButtonActionsC2SPacket(identifier));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.megadoxs.megalib.access;

import com.megadoxs.megalib.data.UserInterfaceData;
import io.github.apace100.apoli.power.PowerType;

public interface UserInterfaceViewer {
void megalib$showInterface(UserInterfaceData toastData);
void megalib$showInterface(UserInterfaceData interfaceData, PowerType<?> powerType);
}
78 changes: 78 additions & 0 deletions src/main/java/com/megadoxs/megalib/data/MegalibDataTypes.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,83 @@
package com.megadoxs.megalib.data;

import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import com.megadoxs.megalib.registry.MegalibRegistries;
import com.megadoxs.megalib.screen_element.ScreenElementFactory;
import com.megadoxs.megalib.screen_element.ScreenElements;
import com.megadoxs.megalib.util.Screen.Size;
import io.github.apace100.calio.util.IdentifierAlias;
import io.github.apace100.calio.ClassUtil;
import io.github.apace100.calio.data.SerializableData;
import io.github.apace100.calio.data.SerializableDataType;
import io.github.apace100.calio.data.SerializableDataTypes;
import io.github.apace100.calio.util.DynamicIdentifier;
import net.minecraft.registry.Registry;
import net.minecraft.util.Identifier;
import net.minecraft.util.JsonHelper;

import java.util.List;
import java.util.function.BiFunction;

public class MegalibDataTypes {
public static final SerializableDataType<Size> SIZE = SerializableDataType.compound(
Size.class,
new SerializableData()
.add("unit", SerializableDataTypes.STRING, "percents")
.add("width", SerializableDataTypes.INT, 0)
.add("height", SerializableDataTypes.INT, 0),
(data) -> new Size(
data.get("unit"),
data.get("width"),
data.get("height")
),
(serializableData, Size) -> {
SerializableData.Instance data = serializableData.new Instance();

data.set("unit", Size.unit());
data.set("width", Size.width());
data.set("height", Size.height());

return data;
}
);

public static final SerializableDataType<ScreenElementFactory.Instance> SCREEN_ELEMENT = element(MegalibRegistries.SCREEN_ELEMENT_FACTORY, ScreenElements.ALIASES, "Screen Element");
public static final SerializableDataType<List<ScreenElementFactory.Instance>> SCREEN_ELEMENTS = SerializableDataType.list(SCREEN_ELEMENT);

public static <T> SerializableDataType<ScreenElementFactory.Instance> element(Registry<ScreenElementFactory> registry, String name) {
return element(registry, IdentifierAlias.GLOBAL, name);
}

public static <T> SerializableDataType<ScreenElementFactory.Instance> element(Registry<ScreenElementFactory> registry, IdentifierAlias aliases, String name) {
return element(registry, aliases, (conditionFactories, id) -> new IllegalArgumentException(name + " \"" + id + "\" is not registered"));
}

public static <T> SerializableDataType<ScreenElementFactory.Instance> element(Registry<ScreenElementFactory> registry, IdentifierAlias aliases, BiFunction<Registry<ScreenElementFactory>, Identifier, RuntimeException> errorHandler) {
return new SerializableDataType<>(
ClassUtil.castClass(ScreenElementFactory.Instance.class),
(buf, instance) -> instance.write(buf),
buf -> {
Identifier factoryId = buf.readIdentifier();
return registry
.getOrEmpty(aliases.resolveAlias(factoryId, registry::containsId))
.orElseThrow(() -> errorHandler.apply(registry, factoryId))
.read(buf);
},
jsonElement -> {

if (!(jsonElement instanceof JsonObject jsonObject)) {
throw new JsonSyntaxException("Expected a JSON object.");
}

Identifier factoryId = DynamicIdentifier.of(JsonHelper.getString(jsonObject, "type"));
return registry
.getOrEmpty(aliases.resolveAlias(factoryId, registry::containsId))
.orElseThrow(() -> errorHandler.apply(registry, factoryId))
.read(jsonObject);

},
ScreenElementFactory.Instance::toJson
);
}
}
21 changes: 13 additions & 8 deletions src/main/java/com/megadoxs/megalib/data/UserInterfaceData.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package com.megadoxs.megalib.data;

import com.megadoxs.megalib.screen_element.ScreenElementFactory;
import com.megadoxs.megalib.util.Screen.Size;
import io.github.apace100.calio.data.SerializableData;
import io.github.apace100.calio.data.SerializableDataType;
import io.github.apace100.calio.data.SerializableDataTypes;

public record UserInterfaceData(String title, int height, int width) {
public record UserInterfaceData(String title, Size size, String location, ScreenElementFactory.Instance screenElements) {

public static final SerializableData DATA = new SerializableData()
.add("title", SerializableDataTypes.STRING)
.add("height", SerializableDataTypes.INT)
.add("width", SerializableDataTypes.INT);
.add("title", SerializableDataTypes.STRING, "UserInterface.Title")
.add("size", MegalibDataTypes.SIZE, new Size("percents", 100, 100))
.add("location", SerializableDataTypes.STRING, "centered")
.add("element", MegalibDataTypes.SCREEN_ELEMENT, null);

public static final SerializableDataType<UserInterfaceData> DATA_TYPE = SerializableDataType.compound(
UserInterfaceData.class,
Expand All @@ -21,8 +24,9 @@ public record UserInterfaceData(String title, int height, int width) {
public static UserInterfaceData fromData(SerializableData.Instance data) {
return new UserInterfaceData(
data.get("title"),
data.get("height"),
data.get("width")
data.get("size"),
data.get("location"),
data.get("element")
);
}

Expand All @@ -31,8 +35,9 @@ public SerializableData.Instance toData() {
SerializableData.Instance data = DATA.new Instance();

data.set("title", this.title());
data.set("height", this.height());
data.set("width", this.width());
data.set("size", this.size());
data.set("location", this.location());
data.set("element", this.screenElements());

return data;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.megadoxs.megalib.data.UserInterfaceData;
import com.megadoxs.megalib.screen.UserInterface.UserInterface;
import com.mojang.authlib.GameProfile;
import io.github.apace100.apoli.power.PowerType;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.AbstractClientPlayerEntity;
import net.minecraft.client.network.ClientPlayerEntity;
Expand All @@ -17,10 +18,10 @@ public ClientPlayerEntityMixin(ClientWorld world, GameProfile profile) {
}

@Override
public void megalib$showInterface(UserInterfaceData InterfaceData) {
public void megalib$showInterface(UserInterfaceData InterfaceData, PowerType<?> powerType) {
MinecraftClient client = MinecraftClient.getInstance();
client.execute(() -> {
UserInterface userInterface = new UserInterface(InterfaceData);
UserInterface userInterface = new UserInterface(InterfaceData, powerType);
client.setScreen(userInterface);
});
}
Expand Down
39 changes: 0 additions & 39 deletions src/main/java/com/megadoxs/megalib/networking/ModPacketsS2C.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.megadoxs.megalib.networking.packet.c2s;

import com.megadoxs.megalib.Megalib;
import net.fabricmc.fabric.api.networking.v1.FabricPacket;
import net.fabricmc.fabric.api.networking.v1.PacketType;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.Identifier;

public record PerformButtonActionsC2SPacket(Identifier identifier) implements FabricPacket {

public static final PacketType<PerformButtonActionsC2SPacket> TYPE = PacketType.create(
Megalib.identifier("c2s/perform_button_action"), PerformButtonActionsC2SPacket::read
);

private static PerformButtonActionsC2SPacket read(PacketByteBuf buffer) {
return new PerformButtonActionsC2SPacket(buffer.readIdentifier());
}
@Override
public void write(PacketByteBuf buffer) {
buffer.writeIdentifier(identifier);
}

@Override
public PacketType<?> getType() {
return TYPE;
}
}

This file was deleted.

Loading

0 comments on commit 8e24e85

Please sign in to comment.