Skip to content

Commit

Permalink
Added conjured staves
Browse files Browse the repository at this point in the history
  • Loading branch information
miyucomics committed Feb 22, 2024
1 parent 825a2e4 commit 673f7f5
Show file tree
Hide file tree
Showing 11 changed files with 200 additions and 12 deletions.
28 changes: 25 additions & 3 deletions common/src/main/java/miyucomics/hexical/Hexical.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,40 @@
package miyucomics.hexical;

import miyucomics.hexical.registry.*;
import at.petrak.hexcasting.api.spell.iota.BooleanIota;
import at.petrak.hexcasting.api.spell.iota.Iota;
import dev.architectury.networking.NetworkManager;
import miyucomics.hexical.items.ConjuredStaffItem;
import miyucomics.hexical.registry.HexicalBlocks;
import miyucomics.hexical.registry.HexicalItems;
import miyucomics.hexical.registry.HexicalPatterns;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Identifier;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

public class Hexical {
public static final String MOD_ID = "hexical";
public static final Identifier CAST_CONJURED_STAFF_PACKET = new Identifier(MOD_ID, "cast_conjured_staff");

public static void init() {
HexicalAbstractions.initPlatformSpecific();
HexicalBlocks.init();
HexicalItems.init();
HexicalPatterns.init();

NetworkManager.registerReceiver(NetworkManager.Side.C2S, CAST_CONJURED_STAFF_PACKET, (buf, context) -> {
PlayerEntity player = context.getPlayer();
ItemStack stack = player.getMainHandStack();
if (stack.getItem() instanceof ConjuredStaffItem) {
int size = buf.readInt();
List<Iota> initStack = new ArrayList<>();
for (int i = 0; i < size; i++)
initStack.add(new BooleanIota(buf.readBoolean()));
((ConjuredStaffItem) stack.getItem()).cast(context.getPlayer().world, player, stack, initStack);
}
});
}

public static Identifier id(String string) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package miyucomics.hexical.casting.patterns.spells

import at.petrak.hexcasting.api.misc.MediaConstants
import at.petrak.hexcasting.api.spell.*
import at.petrak.hexcasting.api.spell.casting.CastingContext
import at.petrak.hexcasting.api.spell.iota.Iota
import at.petrak.hexcasting.xplat.IXplatAbstractions
import miyucomics.hexical.registry.HexicalItems
import net.minecraft.entity.ItemEntity
import net.minecraft.item.ItemStack
import net.minecraft.util.math.Vec3d

class OpConjureStaff : SpellAction {
override val argc = 4

override fun execute(args: List<Iota>, ctx: CastingContext): Triple<RenderedSpell, Int, List<ParticleSpray>> {
val position = args.getVec3(0, argc)
val media = args.getInt(1, argc)
val inputLength = args.getPositiveInt(2, argc)
val instructions = args.getList(3, argc).toList()
return Triple(Spell(position, media, inputLength, instructions), media + MediaConstants.SHARD_UNIT, listOf(ParticleSpray.burst(position, 1.0)))
}

private data class Spell(val position: Vec3d, val media: Int, val inputLength: Int, val instructions: List<Iota>) : RenderedSpell {
override fun cast(ctx: CastingContext) {
val stack = ItemStack(HexicalItems.CONJURED_STAFF_ITEM, 1)
stack.orCreateNbt.putInt("length", inputLength)
val hexHolder = IXplatAbstractions.INSTANCE.findHexHolder(stack)
hexHolder?.writeHex(instructions, media)
ctx.world.spawnEntity(ItemEntity(ctx.world, position.x, position.y, position.z, stack))
}
}
}
36 changes: 36 additions & 0 deletions common/src/main/java/miyucomics/hexical/items/ConjuredStaffItem.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package miyucomics.hexical.items

import at.petrak.hexcasting.api.spell.casting.CastingContext
import at.petrak.hexcasting.api.spell.casting.CastingHarness
import at.petrak.hexcasting.api.spell.iota.Iota
import at.petrak.hexcasting.common.items.magic.ItemPackagedHex
import net.minecraft.entity.LivingEntity
import net.minecraft.entity.player.PlayerEntity
import net.minecraft.item.ItemStack
import net.minecraft.server.network.ServerPlayerEntity
import net.minecraft.server.world.ServerWorld
import net.minecraft.util.Hand
import net.minecraft.util.TypedActionResult
import net.minecraft.world.World

class ConjuredStaffItem : ItemPackagedHex(Settings().maxCount(1)) {
override fun canDrawMediaFromInventory(stack: ItemStack?): Boolean {
return false
}

override fun breakAfterDepletion(): Boolean {
return true
}

override fun use(world: World?, player: PlayerEntity?, usedHand: Hand?): TypedActionResult<ItemStack> {
return TypedActionResult.pass(player?.getStackInHand(usedHand));
}

fun cast(world: World, user: LivingEntity, stack: ItemStack, initStack: MutableList<Iota>) {
val hex = getHex(stack, world as ServerWorld) ?: return
val harness = CastingHarness(CastingContext((user as ServerPlayerEntity), user.getActiveHand(), CastingContext.CastSource.PACKAGED_HEX))
harness.stack = initStack
println(initStack)
harness.executeIotas(hex, world)
}
}
1 change: 0 additions & 1 deletion common/src/main/java/miyucomics/hexical/items/LampItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class LampItem : ItemPackagedHex(Settings()) {
stack.nbt?.putLongArray("startRotation", player.rotationVector.serializeToNBT().longArray);
stack.nbt?.putLong("startTime", world.time);
}

player.setCurrentHand(usedHand)
return TypedActionResult.success(stack)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package miyucomics.hexical.mixin;

import at.petrak.hexcasting.common.lib.HexSounds;
import dev.architectury.networking.NetworkManager;
import io.netty.buffer.Unpooled;
import miyucomics.hexical.Hexical;
import miyucomics.hexical.items.ConjuredStaffItem;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.sound.SoundCategory;
import net.minecraft.util.Hand;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.ArrayList;
import java.util.List;

@Mixin(MinecraftClient.class)
public class MinecraftClientMixin {
@Unique private static final int COOLDOWN = 10;
@Unique private int timer = 0;
@Shadow @Nullable public ClientPlayerEntity player;
@Unique private final List<Boolean> clicks = new ArrayList<>();

@Inject(method = "tick", at = @At("HEAD"))
public void tick(CallbackInfo info) {
if (player == null)
return;
if (timer < 0) {
clicks.clear();
return;
}
timer--;

if (!(player.getMainHandStack().getItem() instanceof ConjuredStaffItem))
return;

int neededLength = player.getMainHandStack().getOrCreateNbt().getInt("length");
if (neededLength == 0)
return;

if (clicks.size() == neededLength) {
timer = 0;
player.world.playSound(player, player.getX(), player.getY(), player.getZ(), HexSounds.CAST_THOTH, SoundCategory.PLAYERS, 1f, 1f);

PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
buf.writeInt(neededLength);
for (int i = 0; i < neededLength; i++)
buf.writeBoolean(clicks.get(i));
NetworkManager.sendToServer(Hexical.CAST_CONJURED_STAFF_PACKET, buf);

clicks.clear();
}
}

@Inject(method = "handleInputEvents", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/MinecraftClient;doAttack()Z", ordinal = 0), cancellable = true)
public void onLeftClick(CallbackInfo info) {
if (player == null || player.isSpectator())
return;
if (player.getMainHandStack().getItem() instanceof ConjuredStaffItem) {
timer = COOLDOWN;
clicks.add(false);
player.swingHand(Hand.MAIN_HAND);
player.world.playSound(player, player.getX(), player.getY(), player.getZ(), HexSounds.SPELL_CIRCLE_CAST, SoundCategory.PLAYERS, 1f, 1f);
info.cancel();
}
}

@Inject(method = "handleInputEvents", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/MinecraftClient;doItemUse()V", ordinal = 0), cancellable = true)
public void onRightClick(CallbackInfo info) {
if (player == null || player.isSpectator())
return;
if (player.getMainHandStack().getItem() instanceof ConjuredStaffItem) {
timer = COOLDOWN;
clicks.add(true);
player.swingHand(Hand.MAIN_HAND);
player.world.playSound(player, player.getX(), player.getY(), player.getZ(), HexSounds.SPELL_CIRCLE_CAST, SoundCategory.PLAYERS, 1f, 1f);
info.cancel();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package miyucomics.hexical.registry

import dev.architectury.registry.registries.DeferredRegister
import miyucomics.hexical.Hexical
import miyucomics.hexical.items.ConjuredStaffItem
import miyucomics.hexical.items.LampItem
import net.minecraft.item.BlockItem
import net.minecraft.item.Item
Expand All @@ -10,10 +11,12 @@ import net.minecraft.util.registry.Registry
object HexicalItems {
private val ITEMS: DeferredRegister<Item> = DeferredRegister.create(Hexical.MOD_ID, Registry.ITEM_KEY)
val LAMP_ITEM: LampItem = LampItem()
val CONJURED_STAFF_ITEM: ConjuredStaffItem = ConjuredStaffItem()

@JvmStatic
fun init() {
ITEMS.register("lamp") { LAMP_ITEM }
ITEMS.register("conjured_staff") { CONJURED_STAFF_ITEM }
ITEMS.register("conjured_bouncy_block") { BlockItem(HexicalBlocks.CONJURED_BOUNCY_BLOCK, Item.Settings()) }
ITEMS.register("conjured_slippery_block") { BlockItem(HexicalBlocks.CONJURED_SLIPPERY_BLOCK, Item.Settings()) }
ITEMS.register("conjured_volatile_block") { BlockItem(HexicalBlocks.CONJURED_VOLATILE_BLOCK, Item.Settings()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import at.petrak.hexcasting.api.spell.math.HexPattern
import miyucomics.hexical.Hexical
import miyucomics.hexical.casting.patterns.operators.eval.OpNephthys
import miyucomics.hexical.casting.patterns.operators.lamp.OpGetLampData
import miyucomics.hexical.casting.patterns.spells.OpChorusBlink
import miyucomics.hexical.casting.patterns.spells.OpConjureBlock
import miyucomics.hexical.casting.patterns.spells.OpPing
import miyucomics.hexical.casting.patterns.spells.OpProgramLamp
import miyucomics.hexical.casting.patterns.spells.*
import net.minecraft.util.Identifier

object HexicalPatterns {
Expand All @@ -27,9 +24,11 @@ object HexicalPatterns {
var PROGRAM_LAMP: HexPattern = register(HexPattern.fromAngles("wwqqqqq", HexDir.EAST), "program_lamp", OpProgramLamp())
var PING: HexPattern = register(HexPattern.fromAngles("eweeewedqdeqqqqqwaeeee", HexDir.NORTH_EAST), "ping", OpPing())

var ROD_LOOK: HexPattern = register(HexPattern.fromAngles("qwddeda", HexDir.SOUTH_WEST), "get_lamp_start_position", OpGetLampData(0))
var ROD_POS: HexPattern = register(HexPattern.fromAngles("qwddedq", HexDir.SOUTH_WEST), "get_lamp_start_rotation", OpGetLampData(1))
var ROD_STAMP: HexPattern = register(HexPattern.fromAngles("qwddedw", HexDir.SOUTH_WEST), "get_lamp_use_time", OpGetLampData(2))
var CONJURE_STAFF: HexPattern = register(HexPattern.fromAngles("wweeeed", HexDir.NORTH_EAST), "conjure_staff", OpConjureStaff())

var LAMP_POSITION: HexPattern = register(HexPattern.fromAngles("qwddeda", HexDir.SOUTH_WEST), "get_lamp_start_position", OpGetLampData(0))
var LAMP_ROTATION: HexPattern = register(HexPattern.fromAngles("qwddedq", HexDir.SOUTH_WEST), "get_lamp_start_rotation", OpGetLampData(1))
var LAMP_USE_TIME: HexPattern = register(HexPattern.fromAngles("qwddedw", HexDir.SOUTH_WEST), "get_lamp_use_time", OpGetLampData(2))

@JvmStatic
fun init() {
Expand Down
1 change: 1 addition & 0 deletions common/src/main/resources/assets/hexical/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"item.hexical.lamp": "Genieless Lamp",
"item.hexical.conjured_staff": "Conjured Staff",

"block.hexical.conjured_bouncy_block": "Conjured Bouncy Block",
"block.hexical.conjured_slippery_block": "Conjured Slippery Block",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "minecraft:item/generated",
"textures": {
"layer0": "hexical:item/conjured_staff"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion common/src/main/resources/hexical-common.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"package": "miyucomics.hexical.mixin",
"compatibilityLevel": "JAVA_17",
"minVersion": "0.8",
"client": [],
"client": [
"MinecraftClientMixin"
],
"mixins": [
"CastingHarnessMixin"
],
Expand Down

0 comments on commit 673f7f5

Please sign in to comment.