Skip to content

Commit

Permalink
Finished Scrap & patch BlockItem interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
Killarexe committed Sep 17, 2024
1 parent 32a3a07 commit 66b608c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.tags.ItemTags;
import net.minecraft.util.RandomSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Player;
Expand All @@ -19,13 +22,14 @@
import net.minecraft.world.phys.Vec3;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.Map;
import java.util.Optional;

public class CEActions {

public static final float BASE_CHANCE = 0.0013666F;

public static void scrap(Item scarpItem, ItemStack currentStack, ItemStack otherStack, ServerPlayer serverPlayer, int count) {
private static void scrap(Item scarpItem, ItemStack currentStack, ItemStack otherStack, ServerPlayer serverPlayer, int count) {
int damage = otherStack.getMaxDamage() - otherStack.getDamageValue();
int amount = Math.min(Math.min(count, currentStack.getCount()), damage);

Expand All @@ -40,6 +44,36 @@ public static void scrap(Item scarpItem, ItemStack currentStack, ItemStack other
serverPlayer.getCooldowns().addCooldown(otherStack.getItem(), amount * 8);
}

private static Optional<Item> getScrapItem(Item item) {
for (Map.Entry<Item, Item> entry: CEMaps.OXIDATION_MAP_ITEMS.entrySet()) {
if (entry.getValue() == item) {
return Optional.of(entry.getKey());
}
}
for (Map.Entry<Item, Item> entry: CEMaps.WAXING_MAP_ITEMS.entrySet()) {
if (entry.getValue() == item) {
return Optional.of(entry.getKey());
}
}
return Optional.empty();
}

public static void scrapUse(Player player, InteractionHand hand, CallbackInfoReturnable<InteractionResultHolder<ItemStack>> callbackInfo) {
ItemStack currentHandStack = player.getItemInHand(hand);
ItemStack otherHandStack = player.getOffhandItem();
if (CEMaps.OXIDATION_MAP_ITEMS.containsValue(currentHandStack.getItem()) && otherHandStack.is(ItemTags.AXES)) {
Optional<Item> scrapItem = getScrapItem(currentHandStack.getItem());
if (scrapItem.isEmpty()) {
callbackInfo.setReturnValue(InteractionResultHolder.fail(currentHandStack));
}
if (player instanceof ServerPlayer serverPlayer && !player.getCooldowns().isOnCooldown(otherHandStack.getItem())) {
scrap(scrapItem.get(), currentHandStack, otherHandStack, serverPlayer, serverPlayer.isShiftKeyDown() ? currentHandStack.getCount() : 1);
callbackInfo.setReturnValue(InteractionResultHolder.success(currentHandStack));
}
callbackInfo.setReturnValue(InteractionResultHolder.consume(currentHandStack));
}
}

public static <T extends Item> void rustEntityStack(
T nextItem, ItemStack stack, Level level,
ItemEntity entity, GameRules.Key<GameRules.IntegerValue> oxidationGameRule, RandomSource random)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public class CEMaps {
OXIDATION_MAP_ITEMS.put(Items.WEATHERED_CHISELED_COPPER, Items.OXIDIZED_CHISELED_COPPER);


WAXING_MAP_ITEMS.put(Items.COPPER_BLOCK, Items.WAXED_COPPER_BLOCK);
WAXING_MAP_ITEMS.put(Items.EXPOSED_COPPER, Items.WAXED_EXPOSED_COPPER);
WAXING_MAP_ITEMS.put(Items.WEATHERED_COPPER, Items.WAXED_WEATHERED_COPPER);
WAXING_MAP_ITEMS.put(Items.OXIDIZED_COPPER, Items.WAXED_WEATHERED_COPPER);

WAXING_MAP_ITEMS.put(Items.CUT_COPPER, Items.WAXED_CUT_COPPER);
WAXING_MAP_ITEMS.put(Items.EXPOSED_CUT_COPPER, Items.WAXED_EXPOSED_CUT_COPPER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@
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;

import java.util.Map;

@Mixin(Item.class)
public abstract class ItemMixin implements FeatureElement, ItemLike, FabricItem {
Expand All @@ -49,26 +43,7 @@ public void inventoryTick(ItemStack stack, Level level, Entity entity, int slot,

@Inject(method = "use", at = @At("HEAD"), cancellable = true)
public void use(Level level, Player player, InteractionHand interactionHand, CallbackInfoReturnable<InteractionResultHolder<ItemStack>> callbackInfo) {
ItemStack currentHandStack = player.getItemInHand(interactionHand);
ItemStack otherHandStack = player.getOffhandItem();
if (CEMaps.OXIDATION_MAP_ITEMS.containsValue(currentHandStack.getItem()) && otherHandStack.is(ItemTags.AXES)) {
Item scrapItem = null;
for (Map.Entry<Item, Item> entry: CEMaps.OXIDATION_MAP_ITEMS.entrySet()) {
if (entry.getValue() == currentHandStack.getItem()) {
scrapItem = entry.getKey();
break;
}
}
if (scrapItem == null) {
callbackInfo.setReturnValue(InteractionResultHolder.fail(currentHandStack));
}
if (player instanceof ServerPlayer serverPlayer && !player.getCooldowns().isOnCooldown(otherHandStack.getItem())) {
CEActions.scrap(scrapItem, currentHandStack, otherHandStack, serverPlayer, serverPlayer.isShiftKeyDown() ? currentHandStack.getCount() : 1);
callbackInfo.setReturnValue(InteractionResultHolder.success(currentHandStack));
}

callbackInfo.setReturnValue(InteractionResultHolder.consume(currentHandStack));
}
CEActions.scrapUse(player, interactionHand, callbackInfo);
}

@Inject(method = "useOn(Lnet/minecraft/world/item/context/UseOnContext;)Lnet/minecraft/world/InteractionResult;", at = @At("HEAD"), cancellable = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,10 @@
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;

import java.util.Map;

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

@Shadow @Final protected boolean canRepair;

@Inject(method = "inventoryTick(Lnet/minecraft/world/item/ItemStack;Lnet/minecraft/world/level/Level;Lnet/minecraft/world/entity/Entity;IZ)V", at = @At("HEAD"))
public void inventoryTick(ItemStack stack, Level level, Entity entity, int slot, boolean selected, CallbackInfo callbackInfo) {
Item item = stack.getItem();
Expand All @@ -53,26 +45,7 @@ public void inventoryTick(ItemStack stack, Level level, Entity entity, int slot,

@Inject(method = "use", at = @At("HEAD"), cancellable = true)
public void use(Level level, Player player, InteractionHand interactionHand, CallbackInfoReturnable<InteractionResultHolder<ItemStack>> callbackInfo) {
ItemStack currentHandStack = player.getItemInHand(interactionHand);
ItemStack otherHandStack = player.getOffhandItem();
if (CEMaps.OXIDATION_MAP_ITEMS.containsValue(currentHandStack.getItem()) && otherHandStack.is(ItemTags.AXES)) {
Item scrapItem = null;
for (Map.Entry<Item, Item> entry: CEMaps.OXIDATION_MAP_ITEMS.entrySet()) {
if (entry.getValue() == currentHandStack.getItem()) {
scrapItem = entry.getKey();
break;
}
}
if (scrapItem == null) {
callbackInfo.setReturnValue(InteractionResultHolder.fail(currentHandStack));
}
if (player instanceof ServerPlayer serverPlayer && !player.getCooldowns().isOnCooldown(otherHandStack.getItem())) {
CEActions.scrap(scrapItem, currentHandStack, otherHandStack, serverPlayer, serverPlayer.isShiftKeyDown() ? currentHandStack.getCount() : 1);
callbackInfo.setReturnValue(InteractionResultHolder.success(currentHandStack));
}

callbackInfo.setReturnValue(InteractionResultHolder.consume(currentHandStack));
}
CEActions.scrapUse(player, interactionHand, callbackInfo);
}

@Inject(method = "useOn(Lnet/minecraft/world/item/context/UseOnContext;)Lnet/minecraft/world/InteractionResult;", at = @At("HEAD"), cancellable = true)
Expand Down

0 comments on commit 66b608c

Please sign in to comment.