Skip to content

Commit

Permalink
feat(log): added config to control log
Browse files Browse the repository at this point in the history
fix(log): only log once per click
fix(gameplay): summon anvil entity directly with precise position using mixin.
  • Loading branch information
nad2040 committed Oct 7, 2023
1 parent bac7925 commit e1b13ad
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 45 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
53 changes: 48 additions & 5 deletions src/main/java/com/nad2040/elytrabombing/ElytraBombingMod.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
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.
// It is considered best practice to use your mod id as the logger's name.
// 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() {
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
71 changes: 32 additions & 39 deletions src/main/java/com/nad2040/elytrabombing/mixin/ItemMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<TypedActionResult<ItemStack>> 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()));
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/elytrabombing.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"package": "com.nad2040.elytrabombing.mixin",
"compatibilityLevel": "JAVA_17",
"mixins": [
"FallingBlockEntityMixin",
"ItemMixin"
],
"injectors": {
Expand Down

0 comments on commit e1b13ad

Please sign in to comment.