Skip to content

Commit

Permalink
Progress in work
Browse files Browse the repository at this point in the history
  • Loading branch information
enjarai committed Jun 14, 2024
1 parent 958edb9 commit d57da2b
Show file tree
Hide file tree
Showing 26 changed files with 343 additions and 40 deletions.
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id 'fabric-loom' version '1.6-SNAPSHOT'
id 'maven-publish'
id 'me.fallenbreath.yamlang' version '1.3.1'
}

version = project.mod_version
Expand Down Expand Up @@ -79,6 +80,11 @@ jar {
}
}

yamlang {
targetSourceSets = [sourceSets.main]
inputDir = "assets/trickster/lang"
}

// configure the maven publication
publishing {
publications {
Expand Down
17 changes: 15 additions & 2 deletions src/client/java/dev/enjarai/trickster/ModKeyBindings.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package dev.enjarai.trickster;

import dev.enjarai.trickster.item.ModItems;
import dev.enjarai.trickster.item.component.ModComponents;
import dev.enjarai.trickster.net.MladyPacket;
import dev.enjarai.trickster.net.ModNetworking;
import dev.enjarai.trickster.net.ScrollInGamePacket;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.entity.EquipmentSlot;
import org.lwjgl.glfw.GLFW;
Expand All @@ -20,14 +23,24 @@ public static void register() {
if (player != null && client.currentScreen == null) {
if (TAKE_HAT.isPressed()) {
if (player.getEquippedStack(EquipmentSlot.HEAD).isIn(ModItems.HOLDABLE_HAT) && player.getEquippedStack(EquipmentSlot.OFFHAND).isEmpty()) {
ModNetworking.MLADY.clientHandle().send(new MladyPacket(true));
ModNetworking.CHANNEL.clientHandle().send(new MladyPacket(true));
}
} else {
if (player.getEquippedStack(EquipmentSlot.HEAD).isEmpty() && player.getOffHandStack().isIn(ModItems.HOLDABLE_HAT)) {
ModNetworking.MLADY.clientHandle().send(new MladyPacket(false));
ModNetworking.CHANNEL.clientHandle().send(new MladyPacket(false));
}
}
}
});
}

public static boolean interceptScroll(float amount) {
var player = MinecraftClient.getInstance().player;
if (player != null && (player.getOffHandStack().contains(ModComponents.SELECTED_SLOT)
|| (player.isSneaking() && player.getMainHandStack().contains(ModComponents.SELECTED_SLOT)))) {
ModNetworking.CHANNEL.clientHandle().send(new ScrollInGamePacket(amount));
return true;
}
return false;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package dev.enjarai.trickster.mixin.client;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import dev.enjarai.trickster.item.ModItems;
import net.minecraft.client.render.item.HeldItemRenderer;
import net.minecraft.item.ItemStack;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;

@Mixin(HeldItemRenderer.class)
public class HeldItemRendererMixin {
@WrapOperation(
method = "updateHeldItems",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/item/ItemStack;areEqual(Lnet/minecraft/item/ItemStack;Lnet/minecraft/item/ItemStack;)Z"
)
)
private boolean cancelItemSwapAnimation(ItemStack left, ItemStack right, Operation<Boolean> original) {
var originalValue = original.call(left, right);

if (left.isIn(ModItems.HOLDABLE_HAT) && right.isIn(ModItems.HOLDABLE_HAT)) {
return true;
}

return originalValue;
}
}
27 changes: 27 additions & 0 deletions src/client/java/dev/enjarai/trickster/mixin/client/MouseMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dev.enjarai.trickster.mixin.client;

import com.llamalad7.mixinextras.injector.v2.WrapWithCondition;
import dev.enjarai.trickster.ModKeyBindings;
import dev.enjarai.trickster.screen.SpellCircleScreen;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.Mouse;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.text.Text;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(Mouse.class)
public class MouseMixin {
@WrapWithCondition(
method = "onMouseScroll",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/entity/player/PlayerInventory;scrollInHotbar(D)V"
)
)
private boolean interceptMouseScroll(PlayerInventory instance, double scrollAmount) {
return !ModKeyBindings.interceptScroll((float) scrollAmount);
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package dev.enjarai.trickster.screen;

import dev.enjarai.trickster.Trickster;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
import net.minecraft.client.gui.screen.ingame.ScreenHandlerProvider;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.screen.slot.Slot;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

@Environment(EnvType.CLIENT)
public class ScrollContainerScreen extends HandledScreen<ScrollContainerScreenHandler> implements ScreenHandlerProvider<ScrollContainerScreenHandler> {
private static final Identifier TEXTURE = Identifier.ofVanilla("textures/gui/container/generic_54.png");
private static final Identifier TEXTURE = Trickster.id("textures/gui/scroll_54.png");
// private static final Identifier TEXTURE = Identifier.ofVanilla("textures/gui/container/generic_54.png");
private static final Identifier SELECTED_SLOT_TEXTURE = Trickster.id("textures/gui/selected_slot.png");
private final int rows;

public ScrollContainerScreen(ScrollContainerScreenHandler handler, PlayerInventory inventory, Text title) {
Expand All @@ -32,5 +36,17 @@ protected void drawBackground(DrawContext context, float delta, int mouseX, int
context.drawTexture(TEXTURE, i, j, 0, 0, this.backgroundWidth, this.rows * 18 + 17);
context.drawTexture(TEXTURE, i, j + this.rows * 18 + 17, 0, 126, this.backgroundWidth, 96);
}

@Override
protected void drawSlot(DrawContext context, Slot slot) {
super.drawSlot(context, slot);

context.getMatrices().push();
context.getMatrices().translate(0.0F, 0.0F, 110.0F);
if (slot.id == handler.selectedSlot.get()) {
context.drawTexture(SELECTED_SLOT_TEXTURE, slot.x - 4, slot.y - 4, 0, 0, 24, 24, 24, 24);
}
context.getMatrices().pop();
}
}

16 changes: 9 additions & 7 deletions src/client/resources/trickster.client.mixins.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{
"required": true,
"package": "dev.enjarai.trickster.mixin.client",
"compatibilityLevel": "JAVA_21",
"client": [
],
"injectors": {
"defaultRequire": 1
"required": true,
"package": "dev.enjarai.trickster.mixin.client",
"compatibilityLevel": "JAVA_21",
"client": [
"HeldItemRendererMixin",
"MouseMixin"
],
"injectors": {
"defaultRequire": 1
}
}
4 changes: 3 additions & 1 deletion src/main/java/dev/enjarai/trickster/item/ModItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dev.enjarai.trickster.Trickster;
import dev.enjarai.trickster.item.component.ModComponents;
import dev.enjarai.trickster.item.component.SelectedSlotComponent;
import dev.enjarai.trickster.item.component.SpellComponent;
import dev.enjarai.trickster.spell.SpellPart;
import io.wispforest.lavender.book.LavenderBookItem;
Expand All @@ -27,7 +28,8 @@ public class ModItems {
public static final TrickHatItem TOP_HAT = register("top_hat",
new TrickHatItem(new Item.Settings().maxCount(1)
.component(DataComponentTypes.CONTAINER,
ContainerComponent.fromStacks(DefaultedList.ofSize(27, ItemStack.EMPTY)))));
ContainerComponent.fromStacks(DefaultedList.ofSize(27, ItemStack.EMPTY)))
.component(ModComponents.SELECTED_SLOT, new SelectedSlotComponent(0, 27))));

public static final TagKey<Item> CAN_EVALUATE_DYNAMICALLY = TagKey.of(RegistryKeys.ITEM, Trickster.id("can_evaluate_dynamically"));
public static final TagKey<Item> HOLDABLE_HAT = TagKey.of(RegistryKeys.ITEM, Trickster.id("holdable_hat"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
public class ModComponents {
public static final ComponentType<SpellComponent> SPELL =
register("spell", builder -> builder.codec(SpellComponent.CODEC).cache());
public static final ComponentType<SelectedSlotComponent> SELECTED_SLOT =
register("selected_slot", builder -> builder.codec(SelectedSlotComponent.CODEC).cache());

private static <T> ComponentType<T> register(String id, UnaryOperator<ComponentType.Builder<T>> builderOperator) {
return Registry.register(Registries.DATA_COMPONENT_TYPE, Trickster.id(id), (builderOperator.apply(ComponentType.builder())).build());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package dev.enjarai.trickster.item.component;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;

public record SelectedSlotComponent(int slot, int maxSlot) {
public static final Codec<SelectedSlotComponent> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.INT.fieldOf("slot").forGetter(SelectedSlotComponent::slot),
Codec.INT.fieldOf("max_slot").forGetter(SelectedSlotComponent::maxSlot)
).apply(instance, SelectedSlotComponent::new));
}
48 changes: 46 additions & 2 deletions src/main/java/dev/enjarai/trickster/net/ModNetworking.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

import dev.enjarai.trickster.Trickster;
import dev.enjarai.trickster.item.ModItems;
import dev.enjarai.trickster.item.component.ModComponents;
import dev.enjarai.trickster.item.component.SelectedSlotComponent;
import io.wispforest.owo.network.OwoNetChannel;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.item.ItemStack;
import net.minecraft.text.Text;

public class ModNetworking {
public static final OwoNetChannel MLADY = OwoNetChannel.create(Trickster.id("mlady"));
public static final OwoNetChannel CHANNEL = OwoNetChannel.create(Trickster.id("main"));

public static void register() {
MLADY.registerServerbound(MladyPacket.class, (packet, access) -> {
CHANNEL.registerServerbound(MladyPacket.class, (packet, access) -> {
var player = access.player();
var inventory = player.getInventory();

Expand All @@ -28,5 +32,45 @@ public static void register() {
}
}
});

CHANNEL.registerServerbound(ScrollInGamePacket.class, (packet, access) -> {
var player = access.player();

if (Math.abs(packet.amount()) >= 1f) {
ItemStack stack = null;

if (player.isSneaking() && player.getMainHandStack().contains(ModComponents.SELECTED_SLOT)) {
stack = player.getMainHandStack();
} else if (player.getOffHandStack().contains(ModComponents.SELECTED_SLOT)) {
stack = player.getOffHandStack();
}

if (stack != null) {
var current = stack.get(ModComponents.SELECTED_SLOT);
var container = stack.get(DataComponentTypes.CONTAINER);

if (current != null && container != null) {
var newSlot = Math.round(current.slot() + packet.amount());
int maxSlot = (int) Math.min(current.maxSlot(), container.stream().count());

if (maxSlot > 0) {
while (newSlot < 0) {
newSlot += maxSlot;
}
while (newSlot >= maxSlot) {
newSlot -= maxSlot;
}
} else {
newSlot = 0;
}

stack.set(ModComponents.SELECTED_SLOT,
new SelectedSlotComponent(newSlot, current.maxSlot()));

player.sendMessage(Text.of("TODO: " + newSlot), true);
}
}
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package dev.enjarai.trickster.net;

public record ScrollInGamePacket(float amount) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mojang.datafixers.util.Pair;
import dev.enjarai.trickster.Trickster;
import dev.enjarai.trickster.item.ModItems;
import dev.enjarai.trickster.item.component.ModComponents;
import io.wispforest.owo.client.screens.SyncedProperty;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.ContainerComponent;
Expand All @@ -25,6 +26,7 @@ public class ScrollContainerScreenHandler extends ScreenHandler {
private final ItemStack containerStack;

private final SyncedProperty<Integer> lockedSlot = createProperty(Integer.class, -1);
public final SyncedProperty<Integer> selectedSlot = createProperty(Integer.class, 0);

public ScrollContainerScreenHandler(int syncId, PlayerInventory playerInventory) {
this(syncId, playerInventory, null);
Expand All @@ -47,6 +49,11 @@ public ScrollContainerScreenHandler(int syncId, PlayerInventory playerInventory,
});
}

var selected = containerStack.get(ModComponents.SELECTED_SLOT);
if (selected != null) {
selectedSlot.set(selected.slot());
}

for (int j = 0; j < 9; ++j) {
if (playerInventory.getStack(j) == containerStack) {
lockedSlot.set(j);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,13 @@ public RoundableFragment ceil() throws BlunderException {
public RoundableFragment round() throws BlunderException {
return new NumberFragment(Math.round(number));
}

@Override
public boolean equals(Object obj) {
if (obj instanceof NumberFragment n) {
var precision = 1 / 16d;
return n.number > number - precision && n.number < number + precision;
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,12 @@ public RoundableFragment ceil() throws BlunderException {
public RoundableFragment round() throws BlunderException {
return new VectorFragment(vector.round(new Vector3d()));
}

@Override
public boolean equals(Object obj) {
if (obj instanceof VectorFragment v) {
return v.vector.equals(vector, 1 / 16d);
}
return false;
}
}
6 changes: 6 additions & 0 deletions src/main/java/dev/enjarai/trickster/spell/tricks/Tricks.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mojang.serialization.Lifecycle;
import dev.enjarai.trickster.Trickster;
import dev.enjarai.trickster.spell.Pattern;
import dev.enjarai.trickster.spell.tricks.bool.*;
import dev.enjarai.trickster.spell.tricks.math.*;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
Expand Down Expand Up @@ -46,6 +47,11 @@ public class Tricks {

// Boolean
public static final IfElseTrick IF_ELSE = register("if_else", new IfElseTrick());
public static final EqualsTrick EQUALS = register("equals", new EqualsTrick());
public static final NotEqualsTrick NOT_EQUALS = register("not_equals", new NotEqualsTrick());
public static final AllTrick ALL = register("all", new AllTrick());
public static final AnyTrick ANY = register("any", new AnyTrick());
public static final NoneTrick NONE = register("none", new NoneTrick());

private static <T extends Trick> T register(String path, T trick) {
LOOKUP.put(trick.getPattern(), trick);
Expand Down
Loading

0 comments on commit d57da2b

Please sign in to comment.