-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
102 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
fabric/src/main/java/github/killarexe/copper_extension/fabric/mixin/ItemEntityMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
fabric/src/main/java/github/killarexe/copper_extension/fabric/mixin/ItemMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |