Skip to content

Commit

Permalink
Refactor block placing
Browse files Browse the repository at this point in the history
  • Loading branch information
Creeperface01 committed Jun 13, 2022
1 parent 5b05a06 commit e65cae0
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 5 deletions.
10 changes: 7 additions & 3 deletions api/src/main/java/org/cloudburstmc/api/block/BlockBehaviors.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ private BlockBehaviors() {

public static final BehaviorKey<BooleanBlockBehavior, BooleanBlockBehavior.Executor> CAN_SPAWN_ON = DataKey.behavior(Identifier.fromString("can_spawn_on"), BooleanBlockBehavior.class, BooleanBlockBehavior.Executor.class);

public static final BehaviorKey<BooleanBlockBehavior, BooleanBlockBehavior.Executor> CAN_BE_USED = DataKey.behavior(Identifier.fromString("can_be_used"), BooleanBlockBehavior.class, BooleanBlockBehavior.Executor.class);

public static final BehaviorKey<FloatBlockBehavior, FloatBlockBehavior.Executor> GET_FRICTION = DataKey.behavior(Identifier.fromString("get_friction"), FloatBlockBehavior.class, FloatBlockBehavior.Executor.class);

public static final BehaviorKey<FloatBlockBehavior, FloatBlockBehavior.Executor> GET_HARDNESS = DataKey.behavior(Identifier.fromString("get_hardness"), FloatBlockBehavior.class, FloatBlockBehavior.Executor.class);
Expand Down Expand Up @@ -64,7 +66,7 @@ private BlockBehaviors() {

public static final BehaviorKey<ComplexBlockBehavior, ComplexBlockBehavior.Executor> ON_LIGHTNING_HIT = DataKey.behavior(Identifier.fromString("on_lightning_hit"), ComplexBlockBehavior.class, ComplexBlockBehavior.Executor.class);

public static final BehaviorKey<ComplexBlockBehavior, ComplexBlockBehavior.Executor> ON_PLACE = DataKey.behavior(Identifier.fromString("on_place"), ComplexBlockBehavior.class, ComplexBlockBehavior.Executor.class);
public static final BehaviorKey<PlaceBlockBehavior, PlaceBlockBehavior.Executor> ON_PLACE = DataKey.behavior(Identifier.fromString("on_place"), PlaceBlockBehavior.class, PlaceBlockBehavior.Executor.class);

public static final BehaviorKey<EntityBlockBehavior, EntityBlockBehavior.Executor> ON_PROJECTILE_HIT = DataKey.behavior(Identifier.fromString("on_projectile_hit"), EntityBlockBehavior.class, EntityBlockBehavior.Executor.class);

Expand All @@ -78,7 +80,7 @@ private BlockBehaviors() {

public static final BehaviorKey<TickBlockBehavior, TickBlockBehavior.Executor> ON_TICK = DataKey.behavior(Identifier.fromString("on_tick"), TickBlockBehavior.class, TickBlockBehavior.Executor.class);

public static final BehaviorKey<UseBlockBehavior, BooleanBlockBehavior.Executor> USE = DataKey.behavior(Identifier.fromString("use"), UseBlockBehavior.class, BooleanBlockBehavior.Executor.class);
public static final BehaviorKey<UseBlockBehavior, UseBlockBehavior.Executor> USE = DataKey.behavior(Identifier.fromString("use"), UseBlockBehavior.class, UseBlockBehavior.Executor.class);

public static final BehaviorKey<EntityBlockBehavior, EntityBlockBehavior.Executor> ON_STAND_ON = DataKey.behavior(Identifier.fromString("on_stand_on"), EntityBlockBehavior.class, EntityBlockBehavior.Executor.class);

Expand All @@ -100,6 +102,8 @@ private BlockBehaviors() {

public static final BehaviorKey<Boolean, Boolean> IS_LIQUID = DataKey.behavior(Identifier.fromString("is_liquid"), Boolean.class);

public static final BehaviorKey<Boolean, Boolean> USES_WATERLOGGING = DataKey.behavior(Identifier.fromString("uses_waterlogging"), Boolean.class);

public static final BehaviorKey<Boolean, Boolean> IS_TOP_SOLID = DataKey.behavior(Identifier.fromString("is_top_solid"), Boolean.class);

public static final BehaviorKey<Boolean, Boolean> IS_STAIRS = DataKey.behavior(Identifier.fromString("is_stairs"), Boolean.class);
Expand Down Expand Up @@ -146,7 +150,7 @@ private BlockBehaviors() {

public static final BehaviorKey<MapColorBehavior, MapColorBehavior.Executor> GET_MAP_COLOR = DataKey.behavior(Identifier.fromString("get_map_color"), MapColorBehavior.class, MapColorBehavior.Executor.class);

public static final BehaviorKey<BooleanBlockBehavior, BooleanBlockBehavior.Executor> CAN_PASS_THROUGH = DataKey.behavior(Identifier.fromString("can_pass_through"), BooleanBlockBehavior.class, BooleanBlockBehavior.Executor.class);
public static final BehaviorKey<BooleanBlockStateBehavior, BooleanBlockStateBehavior.Executor> CAN_PASS_THROUGH = DataKey.behavior(Identifier.fromString("can_pass_through"), BooleanBlockStateBehavior.class, BooleanBlockStateBehavior.Executor.class);

public static final BehaviorKey<CanBreakBlockBehavior, CanBreakBlockBehavior.Executor> IS_BREAKABLE = DataKey.behavior(Identifier.fromString("is_breakable"), CanBreakBlockBehavior.class, CanBreakBlockBehavior.Executor.class);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.cloudburstmc.api.block.behavior;

import org.cloudburstmc.api.block.BlockState;
import org.cloudburstmc.api.util.behavior.Behavior;

public interface BooleanBlockStateBehavior {

boolean test(Behavior<BooleanBlockStateBehavior.Executor> behavior, BlockState block);

@FunctionalInterface
interface Executor {
boolean execute(BlockState block);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.cloudburstmc.api.block.behavior;

import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.math.vector.Vector3i;
import org.cloudburstmc.api.block.BlockState;
import org.cloudburstmc.api.player.Player;
import org.cloudburstmc.api.util.Direction;
import org.cloudburstmc.api.util.behavior.Behavior;

public interface PlaceBlockBehavior {

boolean execute(
Behavior<PlaceBlockBehavior.Executor> behavior,
BlockState blockState,
Player player,
Vector3i blockPosition,
Direction face,
Vector3f clickPosition
);

@FunctionalInterface
interface Executor {

boolean execute(
BlockState blockState,
Player player,
Vector3i blockPosition,
Direction face,
Vector3f clickPosition
);
}
}
10 changes: 10 additions & 0 deletions api/src/main/java/org/cloudburstmc/api/item/ItemBehaviors.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.cloudburstmc.api.item;

import lombok.experimental.UtilityClass;
import org.cloudburstmc.api.block.BlockState;
import org.cloudburstmc.api.block.behavior.BooleanBlockBehavior;
import org.cloudburstmc.api.data.BehaviorKey;
import org.cloudburstmc.api.data.DataKey;
Expand All @@ -9,6 +10,8 @@
import org.cloudburstmc.api.util.behavior.FloatBehavior;
import org.cloudburstmc.api.util.behavior.IntBehavior;

import java.util.Optional;

@UtilityClass
public class ItemBehaviors {

Expand All @@ -30,6 +33,7 @@ public class ItemBehaviors {

public static final BehaviorKey<DamageChanceBehavior, DamageChanceBehavior.Executor> GET_DAMAGE_CHANCE = DataKey.behavior(Identifier.fromString("get_damage_chance"), DamageChanceBehavior.class, DamageChanceBehavior.Executor.class);

public static final BehaviorKey<BooleanItemBehavior, BooleanItemBehavior.Executor> CAN_BE_USED = DataKey.behavior(Identifier.fromString("can_be_used"), BooleanItemBehavior.class, BooleanItemBehavior.Executor.class);
public static final BehaviorKey<UseOnBehavior, UseOnBehavior.Executor> USE_ON = DataKey.behavior(Identifier.fromString("use_on"), UseOnBehavior.class, UseOnBehavior.Executor.class);

public static final BehaviorKey<Float, Float> GET_FUEL_DURATION = DataKey.behavior(Identifier.fromString("get_fuel_duration"), Float.class);
Expand All @@ -38,4 +42,10 @@ public class ItemBehaviors {

public static final BehaviorKey<BooleanItemBehavior, BooleanItemBehavior.Executor> IS_TOOL = DataKey.behavior(Identifier.fromString("is_tool"), BooleanItemBehavior.class, BooleanItemBehavior.Executor.class);

public static final BehaviorKey<BooleanItemBehavior, BooleanItemBehavior.Executor> CAN_BE_PLACED = DataKey.behavior(Identifier.fromString("can_be_placed"), BooleanItemBehavior.class, BooleanItemBehavior.Executor.class);

public static final BehaviorKey<CanBePlacedOnBehavior, CanBePlacedOnBehavior.Executor> CAN_BE_PLACED_ON = DataKey.behavior(Identifier.fromString("can_be_placed_on"), CanBePlacedOnBehavior.class, CanBePlacedOnBehavior.Executor.class);

public static final BehaviorKey<GetItemBehavior<Optional<BlockState>>, GetItemBehavior.Executor<Optional<BlockState>>> GET_BLOCK = DataKey.behavior(Identifier.fromString("get_block"), GetItemBehavior.class, GetItemBehavior.Executor.class);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.cloudburstmc.api.item.behavior;

import org.cloudburstmc.api.block.Block;
import org.cloudburstmc.api.item.ItemStack;
import org.cloudburstmc.api.util.behavior.Behavior;

public interface CanBePlacedOnBehavior {

boolean get(Behavior<CanBePlacedOnBehavior.Executor> behavior, ItemStack item, Block block);

@FunctionalInterface
interface Executor {

boolean execute(ItemStack itemStack, Block block);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.cloudburstmc.api.item.behavior;

import org.cloudburstmc.api.item.ItemStack;
import org.cloudburstmc.api.util.behavior.Behavior;

public interface GetItemBehavior<T> {

T get(Behavior<Executor<T>> behavior, ItemStack item);

@FunctionalInterface
interface Executor<T> {

T execute(ItemStack itemStack);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

public interface UseOnBehavior {

boolean useOn(Behavior<Executor> behavior, ItemStack itemStack, Entity entity, Vector3i blockPosition, Direction face, Vector3f clickPosition);
ItemStack useOn(Behavior<Executor> behavior, ItemStack itemStack, Entity entity, Vector3i blockPosition, Direction face, Vector3f clickPosition);

@FunctionalInterface
interface Executor {

boolean execute(ItemStack itemStack, Entity entity, Vector3i blockPosition, Direction face, Vector3f clickPosition);
ItemStack execute(ItemStack itemStack, Entity entity, Vector3i blockPosition, Direction face, Vector3f clickPosition);
}
}

0 comments on commit e65cae0

Please sign in to comment.