From 6be9ec0e3635b3f3457b6c6bd6bce4fd7e126010 Mon Sep 17 00:00:00 2001 From: Majrusz Date: Sun, 11 Feb 2024 19:18:12 +0100 Subject: [PATCH 1/3] Updated version name --- changelog.md | 2 -- gradle.properties | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/changelog.md b/changelog.md index b98fd46..e69de29 100644 --- a/changelog.md +++ b/changelog.md @@ -1,2 +0,0 @@ -- added config for enchantability of shields and horse armors -- updated Chinese translation (thanks to @Fiocher) \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index 02bac91..36fda6b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -8,7 +8,7 @@ minecraft_version_range=[1.20.1,) # Mod mod_id=majruszsenchantments mod_archives_name=majruszs-enchantments -mod_version=1.10.6 +mod_version=1.10.7-unfinished mod_display_name=Majrusz's Enchantments mod_description=Mod that adds plenty of unique and balanced enchantments to the game. mod_authors=Majrusz From 6bd75371cb96af0190ebc38f2058fcd40958433b Mon Sep 17 00:00:00 2001 From: Majrusz Date: Sun, 11 Feb 2024 19:18:38 +0100 Subject: [PATCH 2/3] Reworked Harvester enchantment --- changelog.md | 1 + .../enchantments/HarvesterEnchantment.java | 109 ++++++++---------- 2 files changed, 52 insertions(+), 58 deletions(-) diff --git a/changelog.md b/changelog.md index e69de29..503c3e9 100644 --- a/changelog.md +++ b/changelog.md @@ -0,0 +1 @@ +- reworked Harvester enchantment \ No newline at end of file diff --git a/common/src/main/java/com/majruszsenchantments/enchantments/HarvesterEnchantment.java b/common/src/main/java/com/majruszsenchantments/enchantments/HarvesterEnchantment.java index 1943b62..0f8cc81 100644 --- a/common/src/main/java/com/majruszsenchantments/enchantments/HarvesterEnchantment.java +++ b/common/src/main/java/com/majruszsenchantments/enchantments/HarvesterEnchantment.java @@ -1,21 +1,17 @@ package com.majruszsenchantments.enchantments; -import com.majruszsenchantments.MajruszsEnchantments; -import com.majruszsenchantments.common.Handler; import com.majruszlibrary.annotation.AutoInstance; import com.majruszlibrary.events.OnPlayerInteracted; -import com.majruszlibrary.emitter.ParticleEmitter; import com.majruszlibrary.item.CustomEnchantment; import com.majruszlibrary.item.EnchantmentHelper; import com.majruszlibrary.item.EquipmentSlots; import com.majruszlibrary.item.ItemHelper; import com.majruszlibrary.level.BlockHelper; import com.majruszlibrary.math.AnyPos; -import com.majruszlibrary.math.Random; import com.majruszlibrary.platform.Side; +import com.majruszsenchantments.MajruszsEnchantments; +import com.majruszsenchantments.common.Handler; import net.minecraft.core.BlockPos; -import net.minecraft.core.particles.ParticleTypes; -import net.minecraft.world.InteractionResult; import net.minecraft.world.inventory.Slot; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; @@ -23,18 +19,16 @@ import net.minecraft.world.level.Level; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Blocks; -import net.minecraft.world.level.block.CropBlock; -import net.minecraft.world.level.block.NetherWartBlock; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.storage.loot.LootParams; import net.minecraft.world.level.storage.loot.parameters.LootContextParams; +import java.util.ArrayList; import java.util.List; +import java.util.Optional; @AutoInstance public class HarvesterEnchantment extends Handler { - float growChance = 0.04f; - public static CustomEnchantment create() { return new CustomEnchantment() .rarity( Enchantment.Rarity.UNCOMMON ) @@ -57,42 +51,69 @@ public HarvesterEnchantment() { } private void apply( OnPlayerInteracted data ) { - this.collectCrop( data ); + this.collectNearbyCrops( data ); if( Side.isLogicalServer() ) { - this.spawnItems( data, this.tryToReplant( data ) ); - this.tickNearbyCrops( data ); - this.damageHoe( data ); + Result result = this.harvestNearbyCrops( data ); + this.spawnItems( data, result ); + this.damageHoe( data, result ); } data.finish(); } - private void collectCrop( OnPlayerInteracted data ) { - BlockPos blockPos = data.blockResult.getBlockPos(); - BlockState blockState = data.getLevel().getBlockState( blockPos ); - blockState.getBlock().playerWillDestroy( data.getLevel(), blockPos, blockState, data.player ); + private void collectNearbyCrops( OnPlayerInteracted data ) { + int range = EnchantmentHelper.getLevel( this.enchantment, data.itemStack ) - 1; + for( int z = -range; z <= range; ++z ) { + for( int x = -range; x <= range; ++x ) { + BlockPos blockPos = AnyPos.from( data.blockResult.getBlockPos() ).add( x, 0, z ).block(); + BlockState blockState = data.getLevel().getBlockState( blockPos ); + if( BlockHelper.isCropAtMaxAge( blockState ) ) { + blockState.getBlock().playerWillDestroy( data.getLevel(), blockPos, blockState, data.player ); + } + } + } + } + + private void spawnItems( OnPlayerInteracted data, Result result ) { + result.itemStacks.forEach( itemStack->Block.popResource( data.getLevel(), data.blockResult.getBlockPos(), itemStack ) ); } - private void spawnItems( OnPlayerInteracted data, List< ItemStack > itemStacks ) { - itemStacks.forEach( itemStack->Block.popResource( data.getLevel(), data.blockResult.getBlockPos(), itemStack ) ); + private Result harvestNearbyCrops( OnPlayerInteracted data ) { + List< ItemStack > itemStacks = new ArrayList<>(); + int count = 0; + int range = EnchantmentHelper.getLevel( this.enchantment, data.itemStack ) - 1; + for( int z = -range; z <= range; ++z ) { + for( int x = -range; x <= range; ++x ) { + Optional< List< ItemStack > > drops = this.tryToHarvest( data, x, z ); + if( drops.isPresent() ) { + ++count; + itemStacks.addAll( drops.get() ); + } + } + } + + return new Result( itemStacks, count ); } - private List< ItemStack > tryToReplant( OnPlayerInteracted data ) { + private Optional< List< ItemStack > > tryToHarvest( OnPlayerInteracted data, int x, int z ) { Level level = data.getLevel(); - BlockPos blockPos = data.blockResult.getBlockPos(); + BlockPos blockPos = AnyPos.from( data.blockResult.getBlockPos() ).add( x, 0, z ).block(); BlockState blockState = level.getBlockState( blockPos ); + if( !BlockHelper.isCropAtMaxAge( blockState ) ) { + return Optional.empty(); + } Block block = blockState.getBlock(); Item seed = block.getCloneItemStack( level, blockPos, blockState ).getItem(); - List< ItemStack > itemStacks = blockState.getDrops( new LootParams.Builder( data.getServerLevel() ) + List< ItemStack > drops = blockState.getDrops( new LootParams.Builder( data.getServerLevel() ) .withParameter( LootContextParams.ORIGIN, AnyPos.from( blockPos ).center().vec3() ) .withParameter( LootContextParams.TOOL, data.itemStack ) .withParameter( LootContextParams.BLOCK_STATE, blockState ) .withParameter( LootContextParams.THIS_ENTITY, data.player ) ); - for( ItemStack itemStack : itemStacks ) { + for( ItemStack itemStack : drops ) { if( itemStack.is( seed ) ) { itemStack.setCount( itemStack.getCount() - 1 ); level.setBlockAndUpdate( blockPos, block.defaultBlockState() ); - return itemStacks; + return Optional.of( drops ); } } for( Slot slot : data.player.inventoryMenu.slots ) { @@ -100,45 +121,17 @@ private List< ItemStack > tryToReplant( OnPlayerInteracted data ) { if( itemStack.is( seed ) ) { itemStack.setCount( itemStack.getCount() - 1 ); level.setBlockAndUpdate( blockPos, block.defaultBlockState() ); - return itemStacks; + return Optional.of( drops ); } } level.setBlockAndUpdate( blockPos, Blocks.AIR.defaultBlockState() ); - return itemStacks; + return Optional.of( drops ); } - private void tickNearbyCrops( OnPlayerInteracted data ) { - BlockPos blockPos = data.blockResult.getBlockPos(); - int range = EnchantmentHelper.getLevel( this.enchantment, data.itemStack ); - for( int z = -range; z <= range; ++z ) { - for( int x = -range; x <= range; ++x ) { - if( x == 0 && z == 0 ) { - continue; - } - - BlockPos neighbourPosition = blockPos.offset( x, 0, z ); - BlockState blockState = data.getLevel().getBlockState( blockPos.offset( x, 0, z ) ); - Block block = blockState.getBlock(); - if( !( block instanceof CropBlock ) && !( block instanceof NetherWartBlock ) ) { - continue; - } - - int particlesCount = 1; - if( Random.check( this.growChance ) ) { - BlockHelper.growCrop( data.getLevel(), neighbourPosition ); - particlesCount = 3; - } - - ParticleEmitter.of( ParticleTypes.HAPPY_VILLAGER ) - .position( AnyPos.from( blockPos ).center().vec3() ) - .count( particlesCount ) - .emit( data.getServerLevel() ); - } - } + private void damageHoe( OnPlayerInteracted data, Result result ) { + ItemHelper.damage( data.player, data.hand, result.cropsCount ); } - private void damageHoe( OnPlayerInteracted data ) { - ItemHelper.damage( data.player, data.hand, 1 ); - } + private record Result( List< ItemStack > itemStacks, int cropsCount ) {} } From dc49443da3d8d21d2389c3215f1ede7d2dde9e3e Mon Sep 17 00:00:00 2001 From: Majrusz Date: Sat, 17 Feb 2024 09:06:23 +0100 Subject: [PATCH 3/3] Removed unfinished tag --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 36fda6b..ee0f6d7 100644 --- a/gradle.properties +++ b/gradle.properties @@ -8,7 +8,7 @@ minecraft_version_range=[1.20.1,) # Mod mod_id=majruszsenchantments mod_archives_name=majruszs-enchantments -mod_version=1.10.7-unfinished +mod_version=1.10.7 mod_display_name=Majrusz's Enchantments mod_description=Mod that adds plenty of unique and balanced enchantments to the game. mod_authors=Majrusz