Skip to content

Commit

Permalink
Add Mixins
Browse files Browse the repository at this point in the history
  • Loading branch information
Killarexe committed Feb 3, 2024
1 parent 29679d7 commit 9ffa900
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package github.killarexe.copper_extension.item;

import java.util.Random;

import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.RandomSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Player;
Expand Down Expand Up @@ -30,7 +29,7 @@ public void inventoryTick(ItemStack itemStack, Level level, Entity entity, int s
}
}

public static void rustEntityStack(Item nextItem, ItemStack stack, Level level, ItemEntity entity, Random random) {
public static <T extends Item> void rustEntityStack(T nextItem, ItemStack stack, Level level, ItemEntity entity, RandomSource random) {
int count = stack.getCount();
if(level.random.nextFloat() < level.getGameRules().getInt(null) * BASE_CHANCE / count) {
Vec3 pos = entity.position();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package github.killarexe.copper_extension.fabric.mixin;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import github.killarexe.copper_extension.fabric.registry.CEItems;
import github.killarexe.copper_extension.item.RustableItem;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.OwnableEntity;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level;

@Mixin(ItemEntity.class)
public abstract class ItemEntityMixin extends Entity implements OwnableEntity{

public ItemEntityMixin(EntityType<?> type, Level level) {
super(type, level);
}

@Inject(method = "tick()V", at = @At("HEAD"))
public void tick(CallbackInfo callbackInfo) {
ItemEntity itemEntity = ItemEntity.class.cast(this);
ItemStack stack = itemEntity.getItem();
if(stack.getItem() == Items.COPPER_INGOT) {
RustableItem.rustEntityStack(CEItems.EXPOSED_COPPER_INGOT, stack, level(), itemEntity, random);
} else if(item instanceof RustableItem rustableItem) {
RustableItem.rustEntityStack(rustableItem.getRustItem(), stack, level(), itemEntity, random);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package github.killarexe.copper_extension.fabric.mixin;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import github.killarexe.copper_extension.fabric.registry.CEGameRules;
import github.killarexe.copper_extension.fabric.registry.CEItems;
import github.killarexe.copper_extension.item.RustableItem;
import github.killarexe.copper_extension.item.WaxableItem;
import net.fabricmc.fabric.api.item.v1.FabricItem;
import net.minecraft.core.BlockPos;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.flag.FeatureElement;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.BeehiveBlock;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.Vec3;

@Mixin(Item.class)
public abstract class ItemMixin implements FeatureElement, ItemLike, FabricItem {

@Inject(method = "inventoryTick(Lnet/minecraft/item/ItemStack;Lnet/minecraft/world/World;Lnet/minecraft/entity/Entity;IZ)V", at = @At("HEAD"))
public void inventoryTick(ItemStack stack, Level level, Entity entity, int slot, boolean selected, CallbackInfo callbackInfo) {
if(entity instanceof Player player && stack.getItem() == Items.COPPER_INGOT) {
int count = stack.getCount();
if(player.getRandom().nextFloat() < level.getGameRules().getInt(CEGameRules.COPPER_OXIDATION_CHANCE) * RustableItem.BASE_CHANCE / count) {
player.getInventory().setItem(slot, new ItemStack(CEItems.EXPOSED_COPPER_INGOT, count));
}
}
}

@Inject(method = "useOn(Lnet/minecraft/item/context/UseOnContext;)Lnet/minecraft/world/InteractionResult;", at = @At("HEAD"), cancellable = true)
public void useOn(UseOnContext context, CallbackInfoReturnable<InteractionResult> callbackInfoReturnable) {
if(context.getItemInHand().getItem() == Items.COPPER_INGOT) {
Level level = context.getLevel();
BlockPos pos = context.getClickedPos();
BlockState state = level.getBlockState(pos);
if(state.hasProperty(BeehiveBlock.HONEY_LEVEL)) {
int currentValue = state.getValue(BeehiveBlock.HONEY_LEVEL);
if(currentValue <= 1) {
Player player = context.getPlayer();
Vec3 playerPos = player.position();
ItemStack stack = context.getItemInHand();
int amount = player.isShiftKeyDown() ? currentValue : 1;
WaxableItem.waxStack(CEItems.WAXED_COPPER_INGOT, level, stack, playerPos, amount);
level.setBlock(pos, state.setValue(BeehiveBlock.HONEY_LEVEL, currentValue - amount), Block.UPDATE_ALL_IMMEDIATE);
callbackInfoReturnable.setReturnValue(InteractionResult.SUCCESS);
}
}
callbackInfoReturnable.setReturnValue(InteractionResult.PASS);
}
}
}

0 comments on commit 9ffa900

Please sign in to comment.