Skip to content

Commit

Permalink
port work
Browse files Browse the repository at this point in the history
  • Loading branch information
ix0rai committed May 8, 2024
1 parent 253641a commit 4f57108
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 94 deletions.
35 changes: 18 additions & 17 deletions src/main/java/io/ix0rai/bodacious_berries/block/BasicBerryBush.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import net.minecraft.state.property.Properties;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.ItemInteractionResult;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
Expand All @@ -29,7 +30,6 @@
import net.minecraft.world.World;
import net.minecraft.world.WorldView;

@SuppressWarnings("deprecation")
public class BasicBerryBush extends PlantBlock implements BerryBush {
protected static final Vec3d BERRY_BUSH_SLOWING_VECTOR = new Vec3d(0.5D, 0.25D, 0.5D);
protected static final int GROW_CHANCE = 5;
Expand Down Expand Up @@ -63,7 +63,7 @@ public BasicBerryBush(Berry berry, int maxAge, VoxelShape smallShape, VoxelShape
* @return what kind of berries this block grows
*/
@Override
public ItemStack getPickStack(BlockView world, BlockPos pos, BlockState state) {
public ItemStack getPickStack(WorldView world, BlockPos pos, BlockState state) {
return this.getBerryItem().getDefaultStack();
}

Expand All @@ -76,14 +76,6 @@ public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos po
return state.get(getAge()) < sizeChangeAge ? smallShape : largeShape;
}

/**
* determines whether this block still needs to be random ticked - i.e. whether it can still grow or not
*/
@Override
public boolean hasRandomTicks(BlockState state) {
return state.get(getAge()) < maxAge;
}

/**
* runs when this bush is ticked
* grows the bush if it can, a random throw is met, and light level is high enough
Expand All @@ -108,24 +100,33 @@ public void onEntityCollision(BlockState state, World world, BlockPos pos, Entit
}
}

@Override
protected ItemInteractionResult onInteract(
ItemStack stack, BlockState state, World world, BlockPos pos, PlayerEntity entity, Hand hand, BlockHitResult hitResult
) {
int age = state.get(getAge());
boolean isMaxAge = age == getMaxAge();
return !isMaxAge && stack.isOf(Items.BONE_MEAL)
? ItemInteractionResult.SKIP_DEFAULT_BLOCK_INTERACTION
: super.onInteract(stack, state, world, pos, entity, hand, hitResult);
}

/**
* handles when our berry bush is right-clicked
* <br> if the player clicking has bone meal, grow the plant if possible or pick berries if fully grown
* <br> otherwise, pick berries if possible
* @return whether the action fails or passes
*/
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity entity, BlockHitResult hitResult) {
final int currentAge = state.get(getAge());
// if bone meal is allowed to be used, pass action
if (hasRandomTicks(state) && player.getStackInHand(hand).isOf(Items.BONE_MEAL)) {
return ActionResult.PASS;
} else if (currentAge == maxAge) {
if (currentAge == maxAge) {
// otherwise, give berries/unripe berries
return pickBerries(pos, world, state, this.getBerryItem());
} else {
// otherwise, do default use action from superclass
return super.onUse(state, world, pos, player, hand, hit);
return super.onUse(state, world, pos, entity, hitResult);
}
}

Expand Down Expand Up @@ -156,13 +157,13 @@ protected void appendProperties(StateManager.Builder<Block, BlockState> builder)
@Override
public boolean isFertilizable(WorldView world, BlockPos pos, BlockState state) {
// hasRandomTicks checks the same thing as this method
return hasRandomTicks(state);
return state.hasRandomTicks();
}

@Override
public boolean canFertilize(World world, RandomGenerator random, BlockPos pos, BlockState state) {
// hasRandomTicks checks the same thing as this method
return hasRandomTicks(state);
return state.hasRandomTicks();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.ix0rai.bodacious_berries.block;

import com.mojang.serialization.MapCodec;
import io.ix0rai.bodacious_berries.block.entity.BerryHarvesterBlockEntity;
import io.ix0rai.bodacious_berries.registry.BodaciousBlocks;
import net.minecraft.block.Block;
Expand Down Expand Up @@ -33,6 +34,12 @@ public BerryHarvesterBlock(Settings settings) {
this.setDefaultState(this.getStateManager().getDefaultState().with(FACING, Direction.NORTH));
}

@Override
protected MapCodec<? extends BlockWithEntity> getCodec() {
// todo
return null;
}

@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(FACING);
Expand Down Expand Up @@ -62,12 +69,12 @@ public BlockRenderType getRenderType(BlockState state) {
}

@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity entity, BlockHitResult hitResult) {
if (!world.isClient) {
// create screen
NamedScreenHandlerFactory screenHandlerFactory = state.createScreenHandlerFactory(world, pos);
if (screenHandlerFactory != null) {
player.openHandledScreen(screenHandlerFactory);
entity.openHandledScreen(screenHandlerFactory);
}
}

Expand Down
21 changes: 14 additions & 7 deletions src/main/java/io/ix0rai/bodacious_berries/block/BerryVine.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
import net.minecraft.state.property.IntProperty;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.ItemInteractionResult;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.random.RandomGenerator;
import net.minecraft.world.BlockView;
import net.minecraft.world.World;
import net.minecraft.world.WorldView;

@SuppressWarnings("deprecation")
public class BerryVine extends VineBlock implements BerryBush {
protected static final int MAX_AGE = 3;
protected static final int MAX_BERRY_AMOUNT = 3;
Expand All @@ -34,7 +34,7 @@ public BerryVine(Berry berry) {
}

@Override
public ItemStack getPickStack(BlockView world, BlockPos pos, BlockState state) {
public ItemStack getPickStack(WorldView world, BlockPos pos, BlockState state) {
return this.getBerryItem().getDefaultStack();
}

Expand All @@ -60,13 +60,20 @@ public void randomTick(BlockState state, ServerWorld world, BlockPos pos, Random
}

@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
if (hasRandomTicks(state) && player.getStackInHand(hand).isOf(Items.BONE_MEAL)) {
return ActionResult.PASS;
} else if (state.get(AGE) == MAX_AGE) {
protected ItemInteractionResult onInteract(ItemStack stack, BlockState state, World world, BlockPos pos, PlayerEntity entity, Hand hand, BlockHitResult hitResult) {
int age = state.get(AGE);
boolean isMaxAge = age == MAX_AGE;
return !isMaxAge && stack.isOf(Items.BONE_MEAL)
? ItemInteractionResult.SKIP_DEFAULT_BLOCK_INTERACTION
: super.onInteract(stack, state, world, pos, entity, hand, hitResult);
}

@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity entity, BlockHitResult hitResult) {
if (state.get(AGE) == MAX_AGE) {
return BasicBerryBush.pickBerries(pos, world, state, this.getBerryItem());
} else {
return super.onUse(state, world, pos, player, hand, hit);
return super.onUse(state, world, pos, entity, hitResult);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public void randomTick(BlockState state, ServerWorld world, BlockPos pos, Random
}

@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity entity, BlockHitResult hitResult) {
if (Boolean.FALSE.equals(state.get(DYING))) {
super.onUse(state, world, pos, player, hand, hit);
super.onUse(state, world, pos, entity, hitResult);
}

return ActionResult.FAIL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import net.minecraft.state.property.Properties;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.ItemInteractionResult;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
Expand All @@ -24,7 +25,6 @@
import net.minecraft.world.World;
import net.minecraft.world.WorldView;

@SuppressWarnings("deprecation")
public class DoubleBerryBush extends TallPlantBlock implements BerryBush {
public static final int MAX_AGE = 3;
public static final IntProperty AGE = Properties.AGE_3;
Expand All @@ -40,7 +40,7 @@ public DoubleBerryBush(Berry berry) {
}

@Override
public ItemStack getPickStack(BlockView world, BlockPos pos, BlockState state) {
public ItemStack getPickStack(WorldView world, BlockPos pos, BlockState state) {
return this.getBerryItem().getDefaultStack();
}

Expand Down Expand Up @@ -91,13 +91,22 @@ public void grow(ServerWorld world, BlockPos pos, BlockState state, int newAge)
}

@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
protected ItemInteractionResult onInteract(ItemStack stack, BlockState state, World world, BlockPos pos, PlayerEntity entity, Hand hand, BlockHitResult hitResult) {
int age = state.get(AGE);
boolean isMaxAge = age == MAX_AGE;
return !isMaxAge && stack.isOf(Items.BONE_MEAL)
? ItemInteractionResult.SKIP_DEFAULT_BLOCK_INTERACTION
: super.onInteract(stack, state, world, pos, entity, hand, hitResult);
}

@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hitResult) {
if (hasRandomTicks(state) && player.getStackInHand(hand).isOf(Items.BONE_MEAL)) {
return ActionResult.PASS;
} else if (state.get(getAge()) == MAX_AGE) {
return BasicBerryBush.pickBerries(pos, world, state, this.getBerryItem());
} else {
return super.onUse(state, world, pos, player, hand, hit);
return super.onUse(state, world, pos, player, hitResult);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import net.minecraft.block.BlockState;
import net.minecraft.block.TallPlantBlock;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.state.property.IntProperty;
import net.minecraft.state.property.Properties;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.ItemInteractionResult;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.random.RandomGenerator;
Expand All @@ -33,11 +35,6 @@ public void grow(ServerWorld world, BlockPos pos, BlockState state, int newAge)
}
}

@Override
public boolean hasRandomTicks(BlockState state) {
return state.get(getAge()) <= maxAge;
}

@Override
public void randomTick(BlockState state, ServerWorld world, BlockPos pos, RandomGenerator random) {
int age = state.get(getAge());
Expand All @@ -47,9 +44,20 @@ public void randomTick(BlockState state, ServerWorld world, BlockPos pos, Random
}

@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
protected ItemInteractionResult onInteract(
ItemStack stack, BlockState state, World world, BlockPos pos, PlayerEntity entity, Hand hand, BlockHitResult hitResult
) {
int i = state.get(getAge());
boolean isMaxAge = i == 3;
return !isMaxAge && stack.isOf(Items.BONE_MEAL)
? ItemInteractionResult.SKIP_DEFAULT_BLOCK_INTERACTION
: super.onInteract(stack, state, world, pos, entity, hand, hitResult);
}

@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity entity, BlockHitResult hitResult) {
// a GrowingBerryBush cannot produce berries until it grows to its double bush state
if (hasRandomTicks(state) && player.getStackInHand(hand).isOf(Items.BONE_MEAL)) {
if (state.hasRandomTicks() && player.getStackInHand(hand).isOf(Items.BONE_MEAL)) {
final int newAge = Math.min(maxAge, state.get(getAge()) + 1);
// grow to a double bush if new age exceeds maximum
if (newAge > maxAge) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public BlockRenderType getRenderType(BlockState state) {
}

@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity entity, BlockHitResult hitResult) {
if (!world.isClient) {
// create screen
NamedScreenHandlerFactory screenHandlerFactory = state.createScreenHandlerFactory(world, pos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.option.SimpleOptionsScreen;
import net.minecraft.client.option.Option;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.function.Consumer;
import java.util.function.Supplier;

import static io.ix0rai.bodacious_berries.BodaciousBerries.CONFIG;

Expand All @@ -19,55 +20,23 @@
*/
@Environment(EnvType.CLIENT)
public class BodaciousConfigScreen extends SimpleOptionsScreen {
private final SpruceOption resetOption;
private final SpruceOption[] generationOptions = new SpruceOption[Berry.values().length];

public BodaciousConfigScreen(@Nullable Screen parent) {
super(BodaciousBerries.translatableText("config.title"));
super(parent, MinecraftClient.getInstance().options, BodaciousBerries.translatableText("config.title"), createOptions());
}

private static Option<Boolean>[] createOptions() {
var options = new ArrayList<>();
for (Berry berry : Berry.values()) {
generationOptions[berry.ordinal()] = createGenOption(berry.toString(),
() -> CONFIG.isGenerating(berry),
options.add(createGenOption(berry.toString(),
value -> CONFIG.setGenerating(berry, value)
);
));
}

this.resetOption = SpruceSimpleActionOption.reset(btn -> {
CONFIG.reset();
MinecraftClient client = MinecraftClient.getInstance();
this.init(client, client.getWindow().getScaledWidth(), client.getWindow().getScaledHeight());
});
}

private SpruceBooleanOption createGenOption(String key, Supplier<Boolean> getter, Consumer<Boolean> setter) {
return new SpruceBooleanOption(BodaciousBerries.translatableTextKey("config." + "generate_" + key),
getter,
setter,
null
);
return options.stream().toArray(Option[]::new);
}

@Override
protected void init() {
super.init();

int buttonHeight = 20;

SpruceOptionListWidget options = new SpruceOptionListWidget(Position.of(0, 22), this.width, this.height - (35 + 22));
for (int i = 0; i < Berry.values().length; i += 2) {
SpruceOption secondToggle = null;
if (i + 1 < Berry.values().length) {
secondToggle = generationOptions[i + 1];
}
options.addOptionEntry(generationOptions[i], secondToggle);
}
this.addDrawableChild(options);

// reset button
this.addDrawableChild(this.resetOption.createWidget(Position.of(this, this.width / 2 - 155, this.height - 29), 150));
// done button
this.addDrawableChild(new SpruceButtonWidget(Position.of(this, this.width / 2 - 155 + 160, this.height - 29), 150,
buttonHeight, SpruceTexts.GUI_DONE,
buttonWidget -> this.closeScreen()));
private static Option<Boolean> createGenOption(String key, Consumer<Boolean> setter) {
return Option.ofBoolean(BodaciousBerries.translatableTextKey("config." + "generate_" + key), true, setter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public ItemStack finishUsing(ItemStack stack, World world, LivingEntity user) {
Iterator<StatusEffectInstance> iterator = user.getStatusEffects().iterator();
do {
StatusEffectInstance instance = iterator.next();
if (instance.getEffectType().getType().equals(StatusEffectType.HARMFUL)) {
if (instance.getEffectType().value().getType().equals(StatusEffectType.HARMFUL)) {
user.removeStatusEffect(instance.getEffectType());
}
} while (iterator.hasNext());
Expand Down
Loading

0 comments on commit 4f57108

Please sign in to comment.