From e1b13ad39b5369eb79e0a00415f3d0e65dcf03f1 Mon Sep 17 00:00:00 2001 From: nad <58147198+nad2040@users.noreply.github.com> Date: Sat, 7 Oct 2023 11:31:21 -0400 Subject: [PATCH] feat(log): added config to control log fix(log): only log once per click fix(gameplay): summon anvil entity directly with precise position using mixin. --- gradle.properties | 2 +- .../elytrabombing/ElytraBombingMod.java | 53 ++++++++++++-- .../mixin/FallingBlockEntityMixin.java | 17 +++++ .../elytrabombing/mixin/ItemMixin.java | 71 +++++++++---------- src/main/resources/elytrabombing.mixins.json | 1 + 5 files changed, 99 insertions(+), 45 deletions(-) create mode 100644 src/main/java/com/nad2040/elytrabombing/mixin/FallingBlockEntityMixin.java diff --git a/gradle.properties b/gradle.properties index 897a362..cc92009 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,6 +9,6 @@ org.gradle.parallel=true loader_version=0.14.21 # Mod Properties - mod_version = 1.0.1 + mod_version = 1.1.0 maven_group = com.nad2040 archives_base_name = elytrabombing \ No newline at end of file diff --git a/src/main/java/com/nad2040/elytrabombing/ElytraBombingMod.java b/src/main/java/com/nad2040/elytrabombing/ElytraBombingMod.java index 84809f7..b265b27 100644 --- a/src/main/java/com/nad2040/elytrabombing/ElytraBombingMod.java +++ b/src/main/java/com/nad2040/elytrabombing/ElytraBombingMod.java @@ -1,10 +1,20 @@ package com.nad2040.elytrabombing; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; import net.fabricmc.api.ModInitializer; +import net.minecraft.block.BlockState; +import net.minecraft.item.ItemStack; +import net.minecraft.util.Hand; import net.minecraft.util.math.Vec3d; -import net.minecraft.util.math.Vec3i; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.gson.Gson; + +import java.io.*; +import java.util.Objects; public class ElytraBombingMod implements ModInitializer { // This logger is used to write text to the console and the log file. @@ -12,10 +22,7 @@ public class ElytraBombingMod implements ModInitializer { // That way, it's clear which mod wrote info, warnings, and errors. public static final String MODID = "elytrabombing"; public static final Logger LOGGER = LoggerFactory.getLogger(MODID); - - public static Vec3i VEC3D_TO_3I(Vec3d vec) { - return new Vec3i((int) vec.x, (int) vec.y, (int) vec.z); - } + public static final Boolean SHOULD_LOG = Objects.requireNonNull(load_config("should_log")).getAsBoolean(); @Override public void onInitialize() { @@ -25,4 +32,40 @@ public void onInitialize() { LOGGER.info("Elytra Bombing Mod initialized!"); } + + public static void log(Hand hand, Hand other_hand, ItemStack usedItemStack, ItemStack otherItemStack, Vec3d position, Vec3d velocity) { + LOGGER.info("right click action detected"); + LOGGER.info("hand is " + ((hand == Hand.MAIN_HAND) ? "main hand" : "off hand")); + LOGGER.info("other hand is " + ((other_hand == Hand.MAIN_HAND) ? "main hand" : "off hand")); + LOGGER.info("used item: " + usedItemStack); + LOGGER.info("other item: " + otherItemStack); + LOGGER.info("player pos: " + position); + LOGGER.info("player vel: " + velocity); + } + + private static JsonElement load_config(String key) { + Gson gson = new Gson(); + File dir = new File("config"); + if (dir.mkdir()) LOGGER.info("created config directory"); + File f = new File(dir, MODID+".json"); + if (!f.exists()) { + try (FileWriter fw = new FileWriter(f)) { + JsonObject new_conf = new JsonObject(); + new_conf.addProperty("should_log",true); + gson.toJson(new_conf, new JsonWriter(fw)); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + try (FileReader fr = new FileReader(f)) { + JsonObject json = gson.fromJson(new JsonReader(fr), JsonObject.class); + return json.has(key) ? json.get(key) : null; + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public interface FBEInterface { + void setBlock(BlockState state); + } } diff --git a/src/main/java/com/nad2040/elytrabombing/mixin/FallingBlockEntityMixin.java b/src/main/java/com/nad2040/elytrabombing/mixin/FallingBlockEntityMixin.java new file mode 100644 index 0000000..dabcc6f --- /dev/null +++ b/src/main/java/com/nad2040/elytrabombing/mixin/FallingBlockEntityMixin.java @@ -0,0 +1,17 @@ +package com.nad2040.elytrabombing.mixin; + +import com.nad2040.elytrabombing.ElytraBombingMod; +import net.minecraft.block.BlockState; +import net.minecraft.entity.FallingBlockEntity; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; + +@Mixin(FallingBlockEntity.class) +public abstract class FallingBlockEntityMixin implements ElytraBombingMod.FBEInterface { + @Shadow + private BlockState block; + + public void setBlock(BlockState block) { + this.block = block; + } +} diff --git a/src/main/java/com/nad2040/elytrabombing/mixin/ItemMixin.java b/src/main/java/com/nad2040/elytrabombing/mixin/ItemMixin.java index 6ad2258..496905c 100644 --- a/src/main/java/com/nad2040/elytrabombing/mixin/ItemMixin.java +++ b/src/main/java/com/nad2040/elytrabombing/mixin/ItemMixin.java @@ -2,19 +2,16 @@ import com.nad2040.elytrabombing.ElytraBombingMod; import net.minecraft.block.Blocks; +import net.minecraft.entity.EntityType; import net.minecraft.entity.FallingBlockEntity; import net.minecraft.entity.TntEntity; import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.item.FlintAndSteelItem; -import net.minecraft.item.Item; -import net.minecraft.item.ItemStack; -import net.minecraft.item.Items; +import net.minecraft.item.*; import net.minecraft.sound.SoundCategory; import net.minecraft.sound.SoundEvents; import net.minecraft.stat.Stats; import net.minecraft.util.Hand; import net.minecraft.util.TypedActionResult; -import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Vec3d; import net.minecraft.world.World; import net.minecraft.world.event.GameEvent; @@ -25,48 +22,44 @@ @Mixin(Item.class) public class ItemMixin { - @Inject(at = @At("HEAD"), method = "use", cancellable = true) + @Inject(at = @At("TAIL"), method = "use", cancellable = true) public void use(World world, PlayerEntity user, Hand hand, CallbackInfoReturnable> cir) { if (user.isFallFlying()) { - ElytraBombingMod.LOGGER.info("right click action detected"); Hand other_hand = (hand == Hand.MAIN_HAND) ? Hand.OFF_HAND : Hand.MAIN_HAND; - ElytraBombingMod.LOGGER.info("hand is " + ((hand == Hand.MAIN_HAND) ? "main hand" : "off hand")); - ElytraBombingMod.LOGGER.info("other hand is " + ((other_hand == Hand.MAIN_HAND) ? "main hand" : "off hand")); - ItemStack usedItemStack = user.getStackInHand(hand), otherItemStack = user.getStackInHand(other_hand); - ElytraBombingMod.LOGGER.info("used item: " + usedItemStack); - ElytraBombingMod.LOGGER.info("other item: " + otherItemStack); - Vec3d position = user.getPos(), velocity = user.getVelocity(); - ElytraBombingMod.LOGGER.info("player pos: " + position); - ElytraBombingMod.LOGGER.info("player vel: " + velocity); - if (!world.isClient && usedItemStack.isOf(Items.FLINT_AND_STEEL) && otherItemStack.isOf(Items.TNT)) { - TntEntity tntEntity = new TntEntity(world, position.x, position.y, position.z, user); - tntEntity.setVelocity(velocity.multiply(1.2)); - world.spawnEntity(tntEntity); - world.playSound(null, tntEntity.getX(), tntEntity.getY(), tntEntity.getZ(), SoundEvents.ENTITY_TNT_PRIMED, SoundCategory.BLOCKS, 1.0f, 1.0f); - world.emitGameEvent(user, GameEvent.PRIME_FUSE, position); - if (!user.getAbilities().creativeMode) { - usedItemStack.damage(1, user, p -> p.sendToolBreakStatus(hand)); - otherItemStack.decrement(1); + if (ElytraBombingMod.SHOULD_LOG && world.isClient) { + ElytraBombingMod.log(hand,other_hand,usedItemStack,otherItemStack,position,velocity); + } + if (usedItemStack.isOf(Items.FLINT_AND_STEEL) && otherItemStack.isOf(Items.TNT)) { + if (!world.isClient) { + TntEntity tntEntity = new TntEntity(world, position.x, position.y, position.z, user); + tntEntity.setVelocity(velocity.multiply(1.2)); + world.spawnEntity(tntEntity); + world.playSound(null, tntEntity.getX(), tntEntity.getY(), tntEntity.getZ(), SoundEvents.ENTITY_TNT_PRIMED, SoundCategory.BLOCKS, 1.0f, 1.0f); + world.emitGameEvent(user, GameEvent.PRIME_FUSE, position); + if (!user.getAbilities().creativeMode) { + usedItemStack.damage(1, user, p -> p.sendToolBreakStatus(hand)); + otherItemStack.decrement(1); + } + user.incrementStat(Stats.USED.getOrCreateStat((FlintAndSteelItem) (Object) this)); } - user.incrementStat(Stats.USED.getOrCreateStat((FlintAndSteelItem) (Object) this)); cir.setReturnValue(TypedActionResult.success(user.getStackInHand(hand), world.isClient())); - } else if (!world.isClient && (usedItemStack.isOf(Items.ANVIL) || usedItemStack.isOf(Items.CHIPPED_ANVIL) || usedItemStack.isOf(Items.DAMAGED_ANVIL)) && otherItemStack.isEmpty()) { - FallingBlockEntity anvilEntity = null; - if (usedItemStack.isOf(Items.ANVIL)) - anvilEntity = FallingBlockEntity.spawnFromBlock(world, new BlockPos(ElytraBombingMod.VEC3D_TO_3I(position)), Blocks.ANVIL.getDefaultState()); - else if (usedItemStack.isOf(Items.CHIPPED_ANVIL)) - anvilEntity = FallingBlockEntity.spawnFromBlock(world, new BlockPos(ElytraBombingMod.VEC3D_TO_3I(position)), Blocks.CHIPPED_ANVIL.getDefaultState()); - else if (usedItemStack.isOf(Items.DAMAGED_ANVIL)) - anvilEntity = FallingBlockEntity.spawnFromBlock(world, new BlockPos(ElytraBombingMod.VEC3D_TO_3I(position)), Blocks.DAMAGED_ANVIL.getDefaultState()); - assert anvilEntity != null; - anvilEntity.setVelocity(velocity.multiply(1.2)); - world.spawnEntity(anvilEntity); - world.playSound(null, anvilEntity.getX(), anvilEntity.getY(), anvilEntity.getZ(), SoundEvents.BLOCK_ANVIL_FALL, SoundCategory.BLOCKS, 1.0f, 1.0f); - if (!user.getAbilities().creativeMode) { - usedItemStack.decrement(1); + } else if ((usedItemStack.isOf(Items.ANVIL) || usedItemStack.isOf(Items.CHIPPED_ANVIL) || usedItemStack.isOf(Items.DAMAGED_ANVIL)) && otherItemStack.isEmpty()) { + if (!world.isClient) { + FallingBlockEntity anvilEntity = new FallingBlockEntity(EntityType.FALLING_BLOCK, world); + anvilEntity.setPosition(position); + anvilEntity.setVelocity(velocity.multiply(1.2)); + if (usedItemStack.isOf(Items.ANVIL)) ((ElytraBombingMod.FBEInterface) anvilEntity).setBlock(Blocks.ANVIL.getDefaultState()); + if (usedItemStack.isOf(Items.CHIPPED_ANVIL)) ((ElytraBombingMod.FBEInterface) anvilEntity).setBlock(Blocks.CHIPPED_ANVIL.getDefaultState()); + if (usedItemStack.isOf(Items.DAMAGED_ANVIL)) ((ElytraBombingMod.FBEInterface) anvilEntity).setBlock(Blocks.DAMAGED_ANVIL.getDefaultState()); + world.spawnEntity(anvilEntity); + world.playSound(null, anvilEntity.getX(), anvilEntity.getY(), anvilEntity.getZ(), SoundEvents.BLOCK_ANVIL_FALL, SoundCategory.BLOCKS, 1.0f, 1.0f); + if (!user.getAbilities().creativeMode) { + usedItemStack.decrement(1); + } } + cir.setReturnValue(TypedActionResult.success(user.getStackInHand(hand), world.isClient())); } } } diff --git a/src/main/resources/elytrabombing.mixins.json b/src/main/resources/elytrabombing.mixins.json index eadac28..b2fb30e 100644 --- a/src/main/resources/elytrabombing.mixins.json +++ b/src/main/resources/elytrabombing.mixins.json @@ -4,6 +4,7 @@ "package": "com.nad2040.elytrabombing.mixin", "compatibilityLevel": "JAVA_17", "mixins": [ + "FallingBlockEntityMixin", "ItemMixin" ], "injectors": {