Skip to content

Commit

Permalink
Port to 1.21 (#317)
Browse files Browse the repository at this point in the history
* update(1.21-pre1): `ItemStack#damage` update

* update(1.21-pre1): `attachmentPos`->`attachedBlockPos`

* update(1.21-pre1): auto crafting update & fix dupe

it was possible to dupe when using a positioned recipe
not in the first slot of dropper

* update(1.21-pre1): `addToFirstFreeSlot` return type changed

* update(1.21-pre1): fix crash for `dispensersStripBlocks`

minecraft would invoke methods on `null` player.
also, reorganized carpet-extra.mixins.json, so that
it's alphabetically ordered.

* Update gradle.properties and fabric.mod.json to 1.21
This also Syncronizes the mod version to carpets current version (1.4.147)

---------

Co-authored-by: Benjamin Konrad Dawkins <[email protected]>
  • Loading branch information
voidpointer0x00 and BenCat07 authored Jun 25, 2024
1 parent 717a579 commit 80298cc
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 36 deletions.
10 changes: 5 additions & 5 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ org.gradle.jvmargs=-Xmx1G

# Fabric Properties
# check these on https://fabricmc.net/develop
minecraft_version=1.20.6
yarn_mappings=1.20.6+build.1
minecraft_version=1.21
yarn_mappings=1.21+build.2
loader_version=0.15.11
# check available versions on maven (https://masa.dy.fi/maven/carpet/fabric-carpet/) for the given minecraft version you are using
carpet_core_version=1.4.141+v240429
carpet_core_version=1.4.147+v240613

# Mod Properties
mod_version = 1.4.141
mod_version = 1.4.147
maven_group = carpet-extra
archives_base_name = carpet-extra

Expand All @@ -21,7 +21,7 @@ org.gradle.jvmargs=-Xmx1G
# The Curseforge versions "names" or ids for the main branch (comma separated: 1.16.4,1.16.5)
# This is needed because CF uses too vague names for prereleases and release candidates
# Can also be the version ID directly coming from https://minecraft.curseforge.com/api/game/versions?token=[API_TOKEN]
release-curse-versions = Minecraft 1.20:1.20.5,Minecraft 1.20:1.20.6
release-curse-versions = Minecraft 1.21:1.21
# Whether or not to build another branch on release
release-extra-branch = false
# The name of the second branch to release
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package carpetextra.dispenser;

import net.minecraft.block.dispenser.FallibleItemDispenserBehavior;
import net.minecraft.block.entity.DispenserBlockEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPointer;

Expand All @@ -15,7 +14,9 @@ protected ItemStack addOrDispense(BlockPointer pointer, ItemStack originalStack,
return newStack;
}
// try to add new stack to inventory, if it can't be added, dispense
else if (((DispenserBlockEntity) pointer.blockEntity()).addToFirstFreeSlot(newStack) < 0) {
if (!pointer.blockEntity().addToFirstFreeSlot(newStack).isEmpty()) {
// if the newStack still contains items, then the addToFirstFreeSlot()
// did not drain the stack fully -> there are items left to be dispensed
super.dispenseSilently(pointer, newStack);
}
return originalStack;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected ItemStack dispenseSilently(BlockPointer pointer, ItemStack stack) {
world.emitGameEvent(null, GameEvent.SHEAR, frontBlockPos);

// damage shears, remove if broken
stack.damage(1, world.random, null, () -> stack.setCount(0));
stack.damage(1, world, null, (item) -> stack.setCount(0));

return stack;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected ItemStack dispenseSilently(BlockPointer pointer, ItemStack stack) {
chicken.dropItem(Items.FEATHER);

// damage shears, remove if broken
stack.damage(1, world.random, null, () -> stack.setCount(0));
stack.damage(1, world, null, (item) -> stack.setCount(0));

// return shears
return stack;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ protected ItemStack dispenseSilently(BlockPointer pointer, ItemStack stack) {
// use on block, test if sucessful
if(stack.getItem().useOnBlock(context).isAccepted()) {
// damage axe, remove if broken
stack.damage(1, world.random, null, () -> stack.setCount(0));

stack.damage(1, world, null, (item) -> stack.setCount(0));
return stack;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected ItemStack dispenseSilently(BlockPointer pointer, ItemStack stack) {
// use on block, test if successful
if(stack.getItem().useOnBlock(context).isAccepted()) {
// damage hoe, remove if broken
stack.damage(1, world.random, null, () -> stack.setCount(0));
stack.damage(1, world, null, (item) -> stack.setCount(0));
return stack;
}
}
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/carpetextra/mixins/AxeItem_allowStripMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package carpetextra.mixins;

import net.minecraft.item.AxeItem;
import net.minecraft.item.ItemUsageContext;
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(AxeItem.class)
public class AxeItem_allowStripMixin {
/**
* Fixes crash when invoking {@code #shouldCancelStripAttempt(ItemUsageContext)}
* with {@code null} player in the context
*/
@Inject(method = "shouldCancelStripAttempt", at = @At(value = "HEAD"), cancellable=true)
private static void allowStripWithoutPlayer(ItemUsageContext context, CallbackInfoReturnable<Boolean> cir) {
if (context.getPlayer() == null) {
cir.setReturnValue(false);
}
}
}
45 changes: 27 additions & 18 deletions src/main/java/carpetextra/mixins/DropperBlock_craftingMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import net.minecraft.recipe.CraftingRecipe;
import net.minecraft.recipe.RecipeEntry;
import net.minecraft.recipe.RecipeType;
import net.minecraft.recipe.input.CraftingRecipeInput;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
Expand Down Expand Up @@ -82,31 +83,39 @@ private void tryCraft(ServerWorld world, BlockState state, BlockPos pos, Callbac
if (dispenser == null) return;
CraftingInventory craftingInventory = new CraftingInventory(new VoidContainer(), 3, 3);
for (int i=0; i < 9; i++) craftingInventory.setStack(i, dispenser.getStack(i));
RecipeEntry<CraftingRecipe> recipe = world.getRecipeManager().getFirstMatch(RecipeType.CRAFTING, craftingInventory, world).orElse(null);
CraftingRecipeInput recipeInput = craftingInventory.createRecipeInput();
RecipeEntry<CraftingRecipe> recipe = world.getRecipeManager().getFirstMatch(RecipeType.CRAFTING, recipeInput, world).orElse(null);
if (recipe == null) return;
// crafting it
Vec3d target = Vec3d.ofBottomCenter(front).add(0.0, 0.2, 0.0);
ItemStack result = recipe.value().craft(craftingInventory, world.getRegistryManager());
ItemStack result = recipe.value().craft(recipeInput, world.getRegistryManager());
spawn(world, target.x, target.y, target.z, result);

// copied from CraftingResultSlot.onTakeItem()
DefaultedList<ItemStack> defaultedList = world.getRecipeManager().getRemainingStacks(RecipeType.CRAFTING, craftingInventory, world);
for(int i = 0; i < defaultedList.size(); ++i) {
ItemStack itemStack = dispenser.getStack(i);
ItemStack itemStack2 = defaultedList.get(i);
if (!itemStack.isEmpty()) {
dispenser.removeStack(i, 1);
itemStack = dispenser.getStack(i);
}
CraftingRecipeInput.Positioned positioned = craftingInventory.createPositionedRecipeInput();
int left = positioned.left();
int top = positioned.top();
DefaultedList<ItemStack> defaultedList = world.getRecipeManager().getRemainingStacks(RecipeType.CRAFTING, recipeInput, world);

for (int row = 0; row < recipeInput.getHeight(); ++row) {
for (int column = 0; column < recipeInput.getWidth(); ++column) {
int index = column + left + (row + top) * craftingInventory.getWidth();
ItemStack itemStack = dispenser.getStack(index);
ItemStack itemStack2 = defaultedList.get(column + row * craftingInventory.getWidth());
if (!itemStack.isEmpty()) {
dispenser.removeStack(index, 1);
itemStack = dispenser.getStack(index);
}

if (!itemStack2.isEmpty()) {
if (itemStack.isEmpty()) {
dispenser.setStack(i, itemStack2);
} else if (ItemStack.areItemsAndComponentsEqual(itemStack, itemStack2)) {
itemStack2.increment(itemStack.getCount());
dispenser.setStack(i, itemStack2);
} else {
spawn(world, target.x, target.y, target.z, itemStack2);
if (!itemStack2.isEmpty()) {
if (itemStack.isEmpty()) {
dispenser.setStack(index, itemStack2);
} else if (ItemStack.areItemsAndComponentsEqual(itemStack, itemStack2)) {
itemStack2.increment(itemStack.getCount());
dispenser.setStack(index, itemStack2);
} else {
spawn(world, target.x, target.y, target.z, itemStack2);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ public ItemFrameEntity_comparatorBetterItemFramesMixin(EntityType<? extends Item
@Inject(method = "setHeldItemStack(Lnet/minecraft/item/ItemStack;Z)V", at = @At(value = "INVOKE", target="Lnet/minecraft/world/World;updateComparators(Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/Block;)V"), cancellable = true)
private void onStackChangeUpdateComparatorDownwards(ItemStack value, boolean update, CallbackInfo ci) {
if (CarpetExtraSettings.comparatorBetterItemFrames == ComparatorOptions.EXTENDED) {
this.getWorld().updateComparators(this.attachmentPos.offset(this.getHorizontalFacing().getOpposite()), Blocks.AIR);
this.getWorld().updateComparators(this.attachedBlockPos.offset(this.getHorizontalFacing().getOpposite()), Blocks.AIR);
}
}

@Inject(method = "setRotation(IZ)V", at = @At(value = "INVOKE", target="Lnet/minecraft/world/World;updateComparators(Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/Block;)V"), cancellable = true)
private void onRotationUpdateComparatorDownwards(int value, boolean bl, CallbackInfo ci) {
if (CarpetExtraSettings.comparatorBetterItemFrames == ComparatorOptions.EXTENDED) {
this.getWorld().updateComparators(this.attachmentPos.offset(this.getHorizontalFacing().getOpposite()), Blocks.AIR);
this.getWorld().updateComparators(this.attachedBlockPos.offset(this.getHorizontalFacing().getOpposite()), Blocks.AIR);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public void tick() {
//Every 1500 ticks, increase signal strength by one, so update comparators exactly then
if(this.getWorld().getTimeOfDay() % 1500 == 0 || firstTick) {
firstTick = false;
if(this.attachmentPos != null) {
this.getWorld().updateComparators(this.attachmentPos, Blocks.AIR);
if(this.attachedBlockPos != null) {
this.getWorld().updateComparators(this.attachedBlockPos, Blocks.AIR);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/carpet-extra.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"MooshroomEntity_StatusEffectAccessorMixin",
"HoeItem_TilledBlocksAccessorMixin",
"AxeItem_StrippedBlocksAccessorMixin",
"AxeItem_allowStripMixin",
"FlowerPotBlock_ContentAccessorMixin",
"BoatItemAccessorMixin",
"SculkCatalystMixin",
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@

"depends": {
"fabricloader": ">=0.15.11",
"carpet": ">=1.4.141",
"minecraft": ["1.20.5", "1.20.6"],
"carpet": ">=1.4.147",
"minecraft": ["1.21"],
"java": ">=21"
},
"custom": {
Expand Down

0 comments on commit 80298cc

Please sign in to comment.