Skip to content

Commit

Permalink
Woah code
Browse files Browse the repository at this point in the history
  • Loading branch information
enjarai committed Jun 12, 2024
1 parent 0e1b464 commit f7b2098
Show file tree
Hide file tree
Showing 27 changed files with 527 additions and 24 deletions.
33 changes: 33 additions & 0 deletions src/client/java/dev/enjarai/trickster/ModKeyBindings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package dev.enjarai.trickster;

import dev.enjarai.trickster.item.ModItems;
import dev.enjarai.trickster.net.MladyPacket;
import dev.enjarai.trickster.net.ModNetworking;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.entity.EquipmentSlot;
import org.lwjgl.glfw.GLFW;

public class ModKeyBindings {
public static final KeyBinding TAKE_HAT = new KeyBinding("key.trickster.take_hat", GLFW.GLFW_KEY_G, "trickster");

public static void register() {
KeyBindingHelper.registerKeyBinding(TAKE_HAT);

ClientTickEvents.END_CLIENT_TICK.register(client -> {
var player = client.player;
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));
}
} else {
if (player.getEquippedStack(EquipmentSlot.HEAD).isEmpty() && player.getOffHandStack().isIn(ModItems.HOLDABLE_HAT)) {
ModNetworking.MLADY.clientHandle().send(new MladyPacket(false));
}
}
}
});
}
}
1 change: 1 addition & 0 deletions src/client/java/dev/enjarai/trickster/TricksterClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class TricksterClient implements ClientModInitializer {
@Override
public void onInitializeClient() {
ModHandledScreens.register();
ModKeyBindings.register();

UIParsing.registerFactory(Trickster.id("glyph"), GlyphComponent::parse);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
public class ModHandledScreens {
public static void register() {
HandledScreens.register(ModScreenHandlers.SCROLL_AND_QUILL, ScrollAndQuillScreen::new);
HandledScreens.register(ModScreenHandlers.SCROLL_CONTAINER, ScrollContainerScreen::new);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package dev.enjarai.trickster.screen;

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.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 final int rows;

public ScrollContainerScreen(ScrollContainerScreenHandler handler, PlayerInventory inventory, Text title) {
super(handler, inventory, title);
this.rows = handler.getRows();
this.backgroundHeight = 114 + this.rows * 18;
this.playerInventoryTitleY = this.backgroundHeight - 94;
}

public void render(DrawContext context, int mouseX, int mouseY, float delta) {
super.render(context, mouseX, mouseY, delta);
this.drawMouseoverTooltip(context, mouseX, mouseY);
}

protected void drawBackground(DrawContext context, float delta, int mouseX, int mouseY) {
int i = (this.width - this.backgroundWidth) / 2;
int j = (this.height - this.backgroundHeight) / 2;
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);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class SpellPartWidget extends AbstractParentElement implements Drawable,
public static final Pattern DELETE_CIRCLE_GLYPH = Pattern.of(0, 4, 8);
public static final Pattern CLEAR_DISABLED_GLYPH = Pattern.of(0, 4, 8, 5, 2, 1, 0, 3, 6, 7, 8);
public static final Pattern COPY_OFFHAND_LITERAL = Pattern.of(4, 0, 1, 4, 2, 1);
public static final Pattern COPY_OFFHAND_LITERAL_INNER = Pattern.of(1, 2, 4, 1, 0, 4, 7);
public static final Pattern COPY_OFFHAND_EXECUTE = Pattern.of(4, 3, 0, 4, 5, 2, 4, 1);

private SpellPart spellPart;
Expand Down Expand Up @@ -426,6 +427,8 @@ protected void stopDrawing() {
} else {
setSubPartInTree(drawingPart, Optional.of(otherHandSpellSupplier.get().deepClone()), spellPart);
}
} else if (compiled.equals(COPY_OFFHAND_LITERAL_INNER)) {
drawingPart.glyph = otherHandSpellSupplier.get().deepClone();
} else if (compiled.equals(COPY_OFFHAND_EXECUTE)) {
toBeReplaced = drawingPart;
initializeReplace.run();
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/dev/enjarai/trickster/Trickster.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dev.enjarai.trickster.block.ModBlocks;
import dev.enjarai.trickster.item.ModItems;
import dev.enjarai.trickster.item.component.ModComponents;
import dev.enjarai.trickster.net.ModNetworking;
import dev.enjarai.trickster.screen.ModScreenHandlers;
import dev.enjarai.trickster.spell.tricks.Tricks;
import net.fabricmc.api.ModInitializer;
Expand All @@ -28,6 +29,7 @@ public void onInitialize() {
ModComponents.register();
ModItems.register();
ModScreenHandlers.register();
ModNetworking.register();
Tricks.register();
}

Expand Down
50 changes: 50 additions & 0 deletions src/main/java/dev/enjarai/trickster/item/EvaluationMirrorItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package dev.enjarai.trickster.item;

import dev.enjarai.trickster.item.component.ModComponents;
import dev.enjarai.trickster.item.component.SpellComponent;
import dev.enjarai.trickster.screen.ScrollAndQuillScreenHandler;
import dev.enjarai.trickster.spell.SpellPart;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.NamedScreenHandlerFactory;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.text.Text;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.world.World;

public class EvaluationMirrorItem extends Item {
public EvaluationMirrorItem(Settings settings) {
super(settings);
}

@Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
var stack = user.getStackInHand(hand);
var otherStack = user.getStackInHand(hand == Hand.MAIN_HAND ? Hand.OFF_HAND : Hand.MAIN_HAND);

if (!user.isSneaking()) {
user.openHandledScreen(new NamedScreenHandlerFactory() {
@Override
public Text getDisplayName() {
return Text.translatable("trickster.screen.mirror_of_evaluation");
}

@Override
public ScreenHandler createMenu(int syncId, PlayerInventory playerInventory, PlayerEntity player) {
return new ScrollAndQuillScreenHandler(
syncId, playerInventory, stack, otherStack,
hand == Hand.MAIN_HAND ? EquipmentSlot.MAINHAND : EquipmentSlot.OFFHAND, true
);
}
});
} else {
stack.set(ModComponents.SPELL, new SpellComponent(new SpellPart()));
}

return TypedActionResult.success(stack);
}
}
11 changes: 11 additions & 0 deletions src/main/java/dev/enjarai/trickster/item/ModItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,24 @@
import net.minecraft.item.Item;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.tag.TagKey;

public class ModItems {
public static final LavenderBookItem TOME_OF_TOMFOOLERY = LavenderBookItem.registerForBook(
Trickster.id("tome_of_tomfoolery"), new Item.Settings().maxCount(1));
public static final ScrollAndQuillItem SCROLL_AND_QUILL = register("scroll_and_quill",
new ScrollAndQuillItem(new Item.Settings().maxCount(16)
.component(ModComponents.SPELL, new SpellComponent(new SpellPart()))));
public static final EvaluationMirrorItem MIRROR_OF_EVALUATION = register("mirror_of_evaluation",
new EvaluationMirrorItem(new Item.Settings().maxCount(1)
.component(ModComponents.SPELL, new SpellComponent(new SpellPart()))));
public static final TrickHatItem TOP_HAT = register("top_hat",
new TrickHatItem(new Item.Settings().maxCount(1)));

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"));
public static final TagKey<Item> SCROLLS = TagKey.of(RegistryKeys.ITEM, Trickster.id("scrolls"));

private static <T extends Item> T register(String name, T item) {
return Registry.register(Registries.ITEM, Trickster.id(name), item);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import dev.enjarai.trickster.item.component.ModComponents;
import dev.enjarai.trickster.screen.ScrollAndQuillScreenHandler;
import dev.enjarai.trickster.spell.PlayerSpellContext;
import dev.enjarai.trickster.spell.SpellContext;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.Item;
Expand All @@ -24,6 +26,7 @@ public ScrollAndQuillItem(Settings settings) {
public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
var stack = user.getStackInHand(hand);
var otherStack = user.getStackInHand(hand == Hand.MAIN_HAND ? Hand.OFF_HAND : Hand.MAIN_HAND);
var slot = hand == Hand.MAIN_HAND ? EquipmentSlot.MAINHAND : EquipmentSlot.OFFHAND;

if (user.isSneaking()) {
user.openHandledScreen(new NamedScreenHandlerFactory() {
Expand All @@ -34,13 +37,13 @@ public Text getDisplayName() {

@Override
public ScreenHandler createMenu(int syncId, PlayerInventory playerInventory, PlayerEntity player) {
return new ScrollAndQuillScreenHandler(syncId, playerInventory, stack, otherStack);
return new ScrollAndQuillScreenHandler(syncId, playerInventory, stack, otherStack, slot, false);
}
});
} else if (!world.isClient()) {
} else if (!world.isClient()) { // TODO remove
var spell = stack.get(ModComponents.SPELL);
if (spell != null) {
spell.spell().runSafely(new SpellContext((ServerPlayerEntity) user));
spell.spell().runSafely(new PlayerSpellContext((ServerPlayerEntity) user, slot));
}
}

Expand Down
30 changes: 30 additions & 0 deletions src/main/java/dev/enjarai/trickster/item/TrickHatItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package dev.enjarai.trickster.item;

import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Equipment;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.world.World;

public class TrickHatItem extends Item implements Equipment {
public TrickHatItem(Settings settings) {
super(settings);
}

@Override
public EquipmentSlot getSlotType() {
return EquipmentSlot.HEAD;
}

@Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
var stack = user.getStackInHand(hand);

// TODO

return TypedActionResult.success(stack);
}
}
4 changes: 4 additions & 0 deletions src/main/java/dev/enjarai/trickster/net/MladyPacket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package dev.enjarai.trickster.net;

public record MladyPacket(boolean hold) {
}
32 changes: 32 additions & 0 deletions src/main/java/dev/enjarai/trickster/net/ModNetworking.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package dev.enjarai.trickster.net;

import dev.enjarai.trickster.Trickster;
import dev.enjarai.trickster.item.ModItems;
import io.wispforest.owo.network.OwoNetChannel;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.item.ItemStack;

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

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

if (packet.hold()) {
if (player.getEquippedStack(EquipmentSlot.HEAD).isIn(ModItems.HOLDABLE_HAT) && player.getOffHandStack().isEmpty()) {
var headStack = inventory.getArmorStack(EquipmentSlot.HEAD.getEntitySlotId());
inventory.armor.set(EquipmentSlot.HEAD.getEntitySlotId(), ItemStack.EMPTY);
inventory.offHand.set(0, headStack);
}
} else {
if (player.getEquippedStack(EquipmentSlot.HEAD).isEmpty() && player.getOffHandStack().isIn(ModItems.HOLDABLE_HAT)) {
var offHandStack = inventory.offHand.getFirst();
inventory.offHand.set(0, ItemStack.EMPTY);
inventory.armor.set(EquipmentSlot.HEAD.getEntitySlotId(), offHandStack);
}
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
public class ModScreenHandlers {
public static final ScreenHandlerType<ScrollAndQuillScreenHandler> SCROLL_AND_QUILL =
new ScreenHandlerType<>(ScrollAndQuillScreenHandler::new, FeatureSet.empty());
public static final ScreenHandlerType<ScrollContainerScreenHandler> SCROLL_CONTAINER =
new ScreenHandlerType<>(ScrollContainerScreenHandler::new, FeatureSet.empty());

public static void register() {
Registry.register(Registries.SCREEN_HANDLER, Trickster.id("scroll_and_quill"), SCROLL_AND_QUILL);
Registry.register(Registries.SCREEN_HANDLER, Trickster.id("scroll_container"), SCROLL_CONTAINER);
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package dev.enjarai.trickster.screen;

import dev.enjarai.trickster.item.ModItems;
import dev.enjarai.trickster.item.component.ModComponents;
import dev.enjarai.trickster.item.component.SpellComponent;
import dev.enjarai.trickster.spell.Fragment;
import dev.enjarai.trickster.spell.PlayerSpellContext;
import dev.enjarai.trickster.spell.SpellContext;
import dev.enjarai.trickster.spell.SpellPart;
import dev.enjarai.trickster.spell.fragment.VoidFragment;
import io.wispforest.endec.Endec;
import io.wispforest.owo.client.screens.SyncedProperty;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Hand;

import java.util.function.Consumer;

Expand All @@ -24,14 +28,19 @@ public class ScrollAndQuillScreenHandler extends ScreenHandler {

public Consumer<Fragment> replacerCallback;

public final EquipmentSlot slot;
public final boolean greedyEvaluation;

public ScrollAndQuillScreenHandler(int syncId, PlayerInventory playerInventory) {
this(syncId, playerInventory, null, null);
this(syncId, playerInventory, null, null, null, false);
}

public ScrollAndQuillScreenHandler(int syncId, PlayerInventory playerInventory, ItemStack scrollStack, ItemStack otherHandStack) {
public ScrollAndQuillScreenHandler(int syncId, PlayerInventory playerInventory, ItemStack scrollStack, ItemStack otherHandStack, EquipmentSlot slot, boolean greedyEvaluation) {
super(ModScreenHandlers.SCROLL_AND_QUILL, syncId);

this.scrollStack = scrollStack;
this.slot = slot;
this.greedyEvaluation = greedyEvaluation;

if (scrollStack != null) {
var spell = scrollStack.get(ModComponents.SPELL);
Expand All @@ -58,7 +67,17 @@ public ScrollAndQuillScreenHandler(int syncId, PlayerInventory playerInventory,

public void updateSpell(SpellPart spell) {
if (scrollStack != null) {
scrollStack.set(ModComponents.SPELL, new SpellComponent(spell));
var server = player().getServer();
if (server != null) {
server.execute(() -> {
if (greedyEvaluation) {
spell.runSafely(new PlayerSpellContext((ServerPlayerEntity) player(), slot).setDestructive(), err -> {});
this.spell.set(spell);
}

scrollStack.set(ModComponents.SPELL, new SpellComponent(spell));
});
}
} else {
// var result = SpellPart.CODEC.encodeStart(JsonOps.INSTANCE, spell).result().get();
// Trickster.LOGGER.warn(result.toString());
Expand All @@ -70,10 +89,12 @@ public void executeOffhand() {
var server = player().getServer();
if (server != null) {
server.execute(() -> {
var fragment = otherHandSpell.get()
.runSafely(new SpellContext((ServerPlayerEntity) player()))
.orElse(VoidFragment.INSTANCE);
sendMessage(new Replace(fragment));
if (player().getInventory().contains(ModItems.CAN_EVALUATE_DYNAMICALLY)) {
var fragment = otherHandSpell.get()
.runSafely(new PlayerSpellContext((ServerPlayerEntity) player(), slot))
.orElse(VoidFragment.INSTANCE);
sendMessage(new Replace(fragment));
}
});
} else {
sendMessage(new ExecuteOffhand());
Expand Down
Loading

0 comments on commit f7b2098

Please sign in to comment.