Skip to content

Commit

Permalink
add cocoa support
Browse files Browse the repository at this point in the history
closes #1
  • Loading branch information
MelanX committed Jul 29, 2023
1 parent b9a3392 commit 83f681f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
4 changes: 2 additions & 2 deletions mod.properties
Expand Up @@ -4,8 +4,8 @@ group=de.melanx
base_version=2.1

# dependencies
forge_version=1.20.1-47.0.1
#mappings=sugarcane_2022.11.06-1.19.2
forge_version=1.20.1-47.1.42
mappings=sugarcane_2023.07.23-1.20.1

# upload properties
upload_versions=1.20.1
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/de/melanx/simplyharvesting/EventListener.java
Expand Up @@ -10,9 +10,11 @@
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.CocoaBlock;
import net.minecraft.world.level.block.CropBlock;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.IntegerProperty;
import net.minecraft.world.level.storage.loot.LootParams;
import net.minecraft.world.level.storage.loot.LootTable;
import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
Expand All @@ -29,19 +31,23 @@ public void onRightclickBlock(PlayerInteractEvent.RightClickBlock event) {
BlockHitResult hitResult = event.getHitVec();
BlockPos pos = hitResult.getBlockPos();
BlockState state = event.getLevel().getBlockState(pos);
if (state.getBlock() instanceof CropBlock crop && state.getValue(crop.getAgeProperty()) == crop.getMaxAge()) {
Block block = state.getBlock();
Age age = block instanceof CropBlock crop ? new Age(crop.getAgeProperty(), crop.getMaxAge())
: block instanceof CocoaBlock ? new Age(CocoaBlock.AGE, CocoaBlock.MAX_AGE) : null;

if (age != null && state.getValue(age.property) == age.maxAge) {
Level level = event.getLevel();
if (!level.isClientSide) {
//noinspection ConstantConditions
LootTable lootTable = level.getServer().getLootData().getLootTable(crop.getLootTable());
LootTable lootTable = level.getServer().getLootData().getLootTable(block.getLootTable());
ObjectArrayList<ItemStack> drops = lootTable.getRandomItems(new LootParams.Builder((ServerLevel) level)
.withParameter(LootContextParams.THIS_ENTITY, event.getEntity())
.withParameter(LootContextParams.BLOCK_STATE, state)
.withParameter(LootContextParams.ORIGIN, Vec3.atCenterOf(pos))
.withParameter(LootContextParams.TOOL, ItemStack.EMPTY)
.create(LootContextParamSets.BLOCK));

level.setBlock(pos, state.setValue(crop.getAgeProperty(), 0), Block.UPDATE_ALL);
level.setBlock(pos, state.setValue(age.property, 0), Block.UPDATE_ALL);

for (ItemStack drop : drops) {
if (drop.getItem() instanceof BlockItem) {
Expand All @@ -52,7 +58,7 @@ public void onRightclickBlock(PlayerInteractEvent.RightClickBlock event) {
}
}

SoundType soundType = crop.getSoundType(state, level, pos, event.getEntity());
SoundType soundType = block.getSoundType(state, level, pos, event.getEntity());
level.playSound(event.getEntity(), pos, soundType.getBreakSound(), SoundSource.BLOCKS, 1.0f, 1.0f);
level.addDestroyBlockEffect(pos, state);

Expand All @@ -62,4 +68,6 @@ public void onRightclickBlock(PlayerInteractEvent.RightClickBlock event) {
event.setCanceled(true);
}
}

private record Age(IntegerProperty property, int maxAge) {}
}

0 comments on commit 83f681f

Please sign in to comment.