Skip to content

Commit

Permalink
Change CauldronBehaviorMap And BucketManager
Browse files Browse the repository at this point in the history
  • Loading branch information
FirstMegaGame4 committed Sep 16, 2023
1 parent d38ea09 commit 903bfa4
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,25 @@ public ItemStack getFilledItem(ItemStack stack) {
}
};

ItemStack getFilledItem(ItemStack stack);

ItemStack getEmptiedItem(ItemStack stack);

ItemStack getFilledItem(ItemStack stack);
default ItemStack getFilledItemOrElse(ItemStack stack, ItemStack or) {
ItemStack check = this.getFilledItem(stack);
return !check.isEmpty() ? check : or;
}

default ItemStack getEmptiedItemOrElse(ItemStack stack, ItemStack or) {
ItemStack check = this.getEmptiedItem(stack);
return !check.isEmpty() ? check : or;
}

default ItemStack getFilledItemOrDefault(ItemStack stack) {
return this.getFilledItemOrElse(stack, stack);
}

default ItemStack getEmptiedItemOrDefault(ItemStack stack) {
return this.getEmptiedItemOrElse(stack, stack);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mmodding.mmodding_lib.library.fluids.cauldrons;

import com.mmodding.mmodding_lib.library.base.MModdingBootstrapInitializer;
import com.mmodding.mmodding_lib.library.fluids.buckets.CustomBucketItem;
import com.mmodding.mmodding_lib.library.utils.BiArrayList;
import com.mmodding.mmodding_lib.library.utils.BiList;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
Expand Down Expand Up @@ -40,17 +41,23 @@ public static CauldronBehaviorMap of(Map<? extends Item, ? extends CauldronBehav

public void addFillCauldronBehavior(BlockState cauldronState, SoundEvent soundEvent, Item... bucketItems) {
for (Item bucketItem : bucketItems) {
CauldronBehaviorMap.FILL_BEHAVIORS.add(bucketItem, (state, world, pos, player, hand, stack) -> CauldronBehavior.fillCauldron(
world, pos, player, hand, stack, cauldronState, soundEvent
));
CauldronBehaviorMap.FILL_BEHAVIORS.add(
bucketItem instanceof CustomBucketItem bucket ? bucket.getManager().getFilledItemOrDefault(new ItemStack(bucketItem)).getItem() : bucketItem,
(state, world, pos, player, hand, stack) -> CauldronBehavior.fillCauldron(
world, pos, player, hand, stack, cauldronState, soundEvent
)
);
}
}

public void addEmptyCauldronBehavior(Predicate<BlockState> emptyCondition, SoundEvent soundEvent, Item... bucketItems) {
for (Item bucketItem : bucketItems) {
this.put(Items.BUCKET, (state, world, pos, player, hand, stack) -> CauldronBehavior.emptyCauldron(
state, world, pos, player, hand, stack, new ItemStack(bucketItem), emptyCondition, soundEvent
));
this.put(
bucketItem instanceof CustomBucketItem bucket ? bucket.getManager().getEmptiedItemOrDefault(new ItemStack(Items.BUCKET)).getItem() : Items.BUCKET,
(state, world, pos, player, hand, stack) -> CauldronBehavior.emptyCauldron(
state, world, pos, player, hand, stack, bucketItem instanceof CustomBucketItem bucket ? bucket.getManager().getFilledItemOrDefault(new ItemStack(bucketItem)) : new ItemStack(bucketItem), emptyCondition, soundEvent
)
);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
package com.mmodding.mmodding_lib.mixin.injectors;

import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
import com.mmodding.mmodding_lib.library.fluids.buckets.CustomBucketItem;
import com.mmodding.mmodding_lib.library.fluids.cauldrons.CauldronBehaviorMap;
import net.minecraft.block.BlockState;
import net.minecraft.block.cauldron.CauldronBehavior;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.sound.SoundEvent;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.objectweb.asm.Opcodes;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand All @@ -17,4 +27,9 @@ public interface CauldronBehaviorMixin {
private static void registerBucketBehavior(Map<Item, CauldronBehavior> behavior, CallbackInfo ci) {
CauldronBehaviorMap.FILL_BEHAVIORS.forEach(behavior::put);
}

@ModifyExpressionValue(method = "fillCauldron", at = @At(value = "FIELD", target = "Lnet/minecraft/item/Items;BUCKET:Lnet/minecraft/item/Item;", opcode = Opcodes.GETSTATIC))
private static Item fillCauldron(Item original, World world, BlockPos pos, PlayerEntity player, Hand hand, ItemStack stack, BlockState state, SoundEvent soundEvent) {
return stack.getItem() instanceof CustomBucketItem bucket ? bucket.getManager().getEmptiedItemOrDefault(stack).getItem() : stack.getItem();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ public abstract class CowEntityMixin extends AnimalEntityMixin {
private void interactMob(PlayerEntity player, Hand hand, CallbackInfoReturnable<ActionResult> cir) {
ItemStack stack = player.getStackInHand(hand);
if (stack.getItem() instanceof CustomMilkBucketItem bucket) {
player.playSound(SoundEvents.ENTITY_COW_MILK, 1.0F, 1.0F);
player.playSound(SoundEvents.ENTITY_COW_MILK, 1.0f, 1.0f);
ItemStack check = bucket.getManager().getFilledItem(new ItemStack(Items.MILK_BUCKET));
if (!check.isEmpty()) {
ItemStack result = ItemUsage.exchangeStack(stack, player, check);
player.setStackInHand(hand, result);
cir.setReturnValue(ActionResult.success(this.world.isClient));
}
else {
super.interactMob(player, hand);
cir.setReturnValue(super.interactMob(player, hand));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public abstract class GoatEntityMixin extends AnimalEntityMixin implements Self<
private void interactMob(PlayerEntity player, Hand hand, CallbackInfoReturnable<ActionResult> cir) {
ItemStack stack = player.getStackInHand(hand);
if (stack.getItem() instanceof CustomMilkBucketItem bucket) {
player.playSound(SoundEvents.ENTITY_COW_MILK, 1.0F, 1.0F);
player.playSound(SoundEvents.ENTITY_COW_MILK, 1.0f, 1.0f);
ItemStack check = bucket.getManager().getFilledItem(new ItemStack(Items.MILK_BUCKET));
if (!check.isEmpty()) {
ItemStack result = ItemUsage.exchangeStack(stack, player, check);
Expand All @@ -45,7 +45,7 @@ private void interactMob(PlayerEntity player, Hand hand, CallbackInfoReturnable<
this.getEatSound(stack),
SoundCategory.NEUTRAL,
1.0F,
MathHelper.nextBetween(this.world.random, 0.8F, 1.2F)
MathHelper.nextBetween(this.world.random, 0.8f, 1.2f)
);
}
cir.setReturnValue(actionResult);
Expand Down

0 comments on commit 903bfa4

Please sign in to comment.