Skip to content

Commit

Permalink
Add BlockItem support for waxing
Browse files Browse the repository at this point in the history
  • Loading branch information
Killarexe committed Sep 14, 2024
1 parent d384609 commit b7db690
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
package github.killarexe.copper_extension;

import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.RandomSource;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.GameRules;
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 org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

public class CEActions {

Expand Down Expand Up @@ -43,4 +51,32 @@ public static <T extends Item> void rustEntityStack(
entity.kill();
}
}

public static void waxUseOn(UseOnContext context, CallbackInfoReturnable<InteractionResult> callbackInfoReturnable) {
Item item = context.getItemInHand().getItem();
if(CEMaps.WAXING_MAP_ITEMS.containsKey(item)) {
Level level = context.getLevel();
BlockPos pos = context.getClickedPos();
BlockState state = level.getBlockState(pos);
if(!state.hasProperty(BeehiveBlock.HONEY_LEVEL)) {
callbackInfoReturnable.cancel();
}
int currentValue = state.getValue(BeehiveBlock.HONEY_LEVEL);
if(currentValue <= 1 && level instanceof ServerLevel serverLevel) {
Player player = context.getPlayer();
Vec3 playerPos = player.position();
int amount = player.isShiftKeyDown() ? currentValue : 1;
context.getItemInHand().shrink(amount);
ItemEntity entity = new ItemEntity(
serverLevel,
playerPos.x, playerPos.y, playerPos.z,
new ItemStack(CEMaps.WAXING_MAP_ITEMS.get(item), amount)
);
serverLevel.addFreshEntity(entity);
serverLevel.setBlock(pos, state.setValue(BeehiveBlock.HONEY_LEVEL, currentValue - amount), Block.UPDATE_ALL_IMMEDIATE);
callbackInfoReturnable.setReturnValue(InteractionResult.SUCCESS);
}
callbackInfoReturnable.setReturnValue(InteractionResult.PASS);
}
}
}
42 changes: 42 additions & 0 deletions common/src/main/java/github/killarexe/copper_extension/CEMaps.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,47 @@ public class CEMaps {
OXIDATION_MAP_ITEMS.put(Items.CHISELED_COPPER, Items.EXPOSED_CHISELED_COPPER);
OXIDATION_MAP_ITEMS.put(Items.EXPOSED_CHISELED_COPPER, Items.WEATHERED_CHISELED_COPPER);
OXIDATION_MAP_ITEMS.put(Items.WEATHERED_CHISELED_COPPER, Items.OXIDIZED_CHISELED_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);
WAXING_MAP_ITEMS.put(Items.WEATHERED_CUT_COPPER, Items.WAXED_WEATHERED_CUT_COPPER);
WAXING_MAP_ITEMS.put(Items.OXIDIZED_CUT_COPPER, Items.WAXED_OXIDIZED_CUT_COPPER);

WAXING_MAP_ITEMS.put(Items.CUT_COPPER_SLAB, Items.WAXED_CUT_COPPER_SLAB);
WAXING_MAP_ITEMS.put(Items.EXPOSED_CUT_COPPER_SLAB, Items.WAXED_EXPOSED_CUT_COPPER_SLAB);
WAXING_MAP_ITEMS.put(Items.WEATHERED_CUT_COPPER_SLAB, Items.WAXED_WEATHERED_CUT_COPPER_SLAB);
WAXING_MAP_ITEMS.put(Items.OXIDIZED_CUT_COPPER_SLAB, Items.WAXED_OXIDIZED_CUT_COPPER_SLAB);

WAXING_MAP_ITEMS.put(Items.CUT_COPPER_STAIRS, Items.WAXED_CUT_COPPER_STAIRS);
WAXING_MAP_ITEMS.put(Items.EXPOSED_CUT_COPPER_STAIRS, Items.WAXED_EXPOSED_CUT_COPPER_STAIRS);
WAXING_MAP_ITEMS.put(Items.WEATHERED_CUT_COPPER_STAIRS, Items.WAXED_WEATHERED_CUT_COPPER_STAIRS);
WAXING_MAP_ITEMS.put(Items.OXIDIZED_CUT_COPPER_STAIRS, Items.WAXED_OXIDIZED_CUT_COPPER_STAIRS);

WAXING_MAP_ITEMS.put(Items.COPPER_BULB, Items.WAXED_COPPER_BULB);
WAXING_MAP_ITEMS.put(Items.EXPOSED_COPPER_BULB, Items.WAXED_EXPOSED_COPPER_BULB);
WAXING_MAP_ITEMS.put(Items.WEATHERED_COPPER_BULB, Items.WAXED_WEATHERED_COPPER_BULB);
WAXING_MAP_ITEMS.put(Items.OXIDIZED_COPPER_BULB, Items.WAXED_OXIDIZED_COPPER_BULB);

WAXING_MAP_ITEMS.put(Items.COPPER_DOOR, Items.WAXED_COPPER_DOOR);
WAXING_MAP_ITEMS.put(Items.EXPOSED_COPPER_DOOR, Items.WAXED_EXPOSED_COPPER_DOOR);
WAXING_MAP_ITEMS.put(Items.WEATHERED_COPPER_DOOR, Items.WAXED_WEATHERED_COPPER_DOOR);
WAXING_MAP_ITEMS.put(Items.OXIDIZED_COPPER_DOOR, Items.WAXED_OXIDIZED_COPPER_DOOR);

WAXING_MAP_ITEMS.put(Items.COPPER_GRATE, Items.WAXED_COPPER_GRATE);
WAXING_MAP_ITEMS.put(Items.EXPOSED_COPPER_GRATE, Items.WAXED_EXPOSED_COPPER_GRATE);
WAXING_MAP_ITEMS.put(Items.WEATHERED_COPPER_GRATE, Items.WAXED_WEATHERED_COPPER_GRATE);
WAXING_MAP_ITEMS.put(Items.OXIDIZED_COPPER_GRATE, Items.WAXED_OXIDIZED_COPPER_GRATE);

WAXING_MAP_ITEMS.put(Items.COPPER_TRAPDOOR, Items.WAXED_COPPER_TRAPDOOR);
WAXING_MAP_ITEMS.put(Items.EXPOSED_COPPER_TRAPDOOR, Items.WAXED_EXPOSED_COPPER_TRAPDOOR);
WAXING_MAP_ITEMS.put(Items.WEATHERED_COPPER_TRAPDOOR, Items.WAXED_WEATHERED_COPPER_TRAPDOOR);
WAXING_MAP_ITEMS.put(Items.OXIDIZED_COPPER_TRAPDOOR, Items.WAXED_OXIDIZED_COPPER_TRAPDOOR);

WAXING_MAP_ITEMS.put(Items.CHISELED_COPPER, Items.WAXED_CHISELED_COPPER);
WAXING_MAP_ITEMS.put(Items.EXPOSED_CHISELED_COPPER, Items.WAXED_EXPOSED_CHISELED_COPPER);
WAXING_MAP_ITEMS.put(Items.WEATHERED_CHISELED_COPPER, Items.WAXED_WEATHERED_CHISELED_COPPER);
WAXING_MAP_ITEMS.put(Items.OXIDIZED_CHISELED_COPPER, Items.WAXED_OXIDIZED_CHISELED_COPPER);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package github.killarexe.copper_extension.fabric.mixin;

import github.killarexe.copper_extension.CEActions;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.context.UseOnContext;
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.CallbackInfoReturnable;

@Mixin(BlockItem.class)
public class BlockItemMixin {
@Inject(method = "useOn(Lnet/minecraft/world/item/context/UseOnContext;)Lnet/minecraft/world/InteractionResult;", at = @At("HEAD"), cancellable = true)
public void useOn(UseOnContext context, CallbackInfoReturnable<InteractionResult> callbackInfoReturnable) {
CEActions.waxUseOn(context, callbackInfoReturnable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,30 +73,6 @@ public void use(Level level, Player player, InteractionHand interactionHand, Cal

@Inject(method = "useOn(Lnet/minecraft/world/item/context/UseOnContext;)Lnet/minecraft/world/InteractionResult;", at = @At("HEAD"), cancellable = true)
public void useOn(UseOnContext context, CallbackInfoReturnable<InteractionResult> callbackInfoReturnable) {
Item item = context.getItemInHand().getItem();
if(CEMaps.WAXING_MAP_ITEMS.containsKey(item)) {
Level level = context.getLevel();
BlockPos pos = context.getClickedPos();
BlockState state = level.getBlockState(pos);
if(!state.hasProperty(BeehiveBlock.HONEY_LEVEL)) {
callbackInfoReturnable.cancel();
}
int currentValue = state.getValue(BeehiveBlock.HONEY_LEVEL);
if(currentValue <= 1 && level instanceof ServerLevel serverLevel) {
Player player = context.getPlayer();
Vec3 playerPos = player.position();
int amount = player.isShiftKeyDown() ? currentValue : 1;
context.getItemInHand().shrink(amount);
ItemEntity entity = new ItemEntity(
serverLevel,
playerPos.x, playerPos.y, playerPos.z,
new ItemStack(CEMaps.WAXING_MAP_ITEMS.get(item), amount)
);
serverLevel.addFreshEntity(entity);
serverLevel.setBlock(pos, state.setValue(BeehiveBlock.HONEY_LEVEL, currentValue - amount), Block.UPDATE_ALL_IMMEDIATE);
callbackInfoReturnable.setReturnValue(InteractionResult.SUCCESS);
}
callbackInfoReturnable.setReturnValue(InteractionResult.PASS);
}
CEActions.waxUseOn(context, callbackInfoReturnable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public static void register() {
CEMaps.OXIDATION_MAP_ITEMS.put(Items.COPPER_INGOT, EXPOSED_COPPER_INGOT);
CEMaps.OXIDATION_MAP_ITEMS.put(EXPOSED_COPPER_INGOT, WEATHERED_COPPER_INGOT);
CEMaps.OXIDATION_MAP_ITEMS.put(WEATHERED_COPPER_INGOT, OXIDIZED_COPPER_INGOT);
CEMaps.WAXING_MAP_ITEMS.put(Items.COPPER_INGOT, WAXED_COPPER_INGOT);
CEMaps.WAXING_MAP_ITEMS.put(EXPOSED_COPPER_INGOT, WAXED_EXPOSED_COPPER_INGOT);
CEMaps.WAXING_MAP_ITEMS.put(WEATHERED_COPPER_INGOT, WAXED_WEATHERED_COPPER_INGOT);
CEMaps.WAXING_MAP_ITEMS.put(OXIDIZED_COPPER_INGOT, WAXED_OXIDIZED_COPPER_INGOT);
}

private static <T extends Item> T createItem(String id, T item) {
Expand Down
6 changes: 4 additions & 2 deletions fabric/src/main/resources/copper_extension.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
"mixins": [],
"client": [
"ItemEntityMixin",
"ItemMixin"
"ItemMixin",
"BlockItemMixin"
],
"server": [
"ItemEntityMixin",
"ItemMixin"
"ItemMixin",
"BlockItemMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,10 @@

import github.killarexe.copper_extension.neoforge.CENeoForge;
import github.killarexe.copper_extension.neoforge.registry.CEItems;
import github.killarexe.copper_extension.item.WaxableItem;
import net.minecraft.world.item.CreativeModeTabs;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
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 net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.BuildCreativeModeTabContentsEvent;
import net.neoforged.neoforge.event.entity.player.PlayerInteractEvent;

public class CEEvents {
public static void registerEvents(IEventBus bus) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package github.killarexe.copper_extension.neoforge.mixin;

import github.killarexe.copper_extension.CEActions;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.context.UseOnContext;
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.CallbackInfoReturnable;

@Mixin(BlockItem.class)
public class BlockItemMixin {
@Inject(method = "useOn(Lnet/minecraft/world/item/context/UseOnContext;)Lnet/minecraft/world/InteractionResult;", at = @At("HEAD"), cancellable = true)
public void useOn(UseOnContext context, CallbackInfoReturnable<InteractionResult> callbackInfoReturnable) {
CEActions.waxUseOn(context, callbackInfoReturnable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.item.ItemEntity;
import net.neoforged.neoforge.common.extensions.IItemExtension;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
Expand All @@ -36,6 +38,8 @@
@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 Down Expand Up @@ -73,30 +77,6 @@ public void use(Level level, Player player, InteractionHand interactionHand, Cal

@Inject(method = "useOn(Lnet/minecraft/world/item/context/UseOnContext;)Lnet/minecraft/world/InteractionResult;", at = @At("HEAD"), cancellable = true)
public void useOn(UseOnContext context, CallbackInfoReturnable<InteractionResult> callbackInfoReturnable) {
Item item = context.getItemInHand().getItem();
if(CEMaps.WAXING_MAP_ITEMS.containsKey(item)) {
Level level = context.getLevel();
BlockPos pos = context.getClickedPos();
BlockState state = level.getBlockState(pos);
if(!state.hasProperty(BeehiveBlock.HONEY_LEVEL)) {
callbackInfoReturnable.cancel();
}
int currentValue = state.getValue(BeehiveBlock.HONEY_LEVEL);
if(currentValue <= 1 && level instanceof ServerLevel serverLevel) {
Player player = context.getPlayer();
Vec3 playerPos = player.position();
int amount = player.isShiftKeyDown() ? currentValue : 1;
context.getItemInHand().shrink(amount);
ItemEntity entity = new ItemEntity(
serverLevel,
playerPos.x, playerPos.y, playerPos.z,
new ItemStack(CEMaps.WAXING_MAP_ITEMS.get(item), amount)
);
serverLevel.addFreshEntity(entity);
serverLevel.setBlock(pos, state.setValue(BeehiveBlock.HONEY_LEVEL, currentValue - amount), Block.UPDATE_ALL_IMMEDIATE);
callbackInfoReturnable.setReturnValue(InteractionResult.SUCCESS);
}
callbackInfoReturnable.setReturnValue(InteractionResult.PASS);
}
CEActions.waxUseOn(context, callbackInfoReturnable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class CEItems {
);

public static final Supplier<Item> WAXED_OXIDIZED_COPPER_INGOT = ITEMS.register(
"waxed_oxidized_copper_ingot", () -> new Item(new Item.Properties())
"waxed_oxidized_copper_ingot", () -> new Item(new Item.Properties())
);

public static final Supplier<Item> EXPOSED_COPPER_INGOT = ITEMS.register(
Expand All @@ -38,13 +38,17 @@ public class CEItems {
);

public static final Supplier<Item> OXIDIZED_COPPER_INGOT = ITEMS.register(
"oxidized_copper_ingot", () -> new Item(new Item.Properties())
"oxidized_copper_ingot", () -> new Item(new Item.Properties())
);

public static void register(IEventBus bus) {
ITEMS.register(bus);
CEMaps.OXIDATION_MAP_ITEMS.put(Items.COPPER_INGOT, EXPOSED_COPPER_INGOT.get());
CEMaps.OXIDATION_MAP_ITEMS.put(EXPOSED_COPPER_INGOT.get(), WEATHERED_COPPER_INGOT.get());
CEMaps.OXIDATION_MAP_ITEMS.put(WEATHERED_COPPER_INGOT.get(), OXIDIZED_COPPER_INGOT.get());
CEMaps.WAXING_MAP_ITEMS.put(Items.COPPER_INGOT, WAXED_COPPER_INGOT.get());
CEMaps.WAXING_MAP_ITEMS.put(EXPOSED_COPPER_INGOT.get(), WAXED_EXPOSED_COPPER_INGOT.get());
CEMaps.WAXING_MAP_ITEMS.put(WEATHERED_COPPER_INGOT.get(), WAXED_WEATHERED_COPPER_INGOT.get());
CEMaps.WAXING_MAP_ITEMS.put(OXIDIZED_COPPER_INGOT.get(), OXIDIZED_COPPER_INGOT.get());
}
}
6 changes: 4 additions & 2 deletions neoforge/src/main/resources/copper_extension.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
"mixins": [],
"client": [
"ItemEntityMixin",
"ItemMixin"
"ItemMixin",
"BlockItemMixin"
],
"server": [
"ItemEntityMixin",
"ItemMixin"
"ItemMixin",
"BlockItemMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down

0 comments on commit b7db690

Please sign in to comment.