Skip to content

Commit

Permalink
refactor: merge block.function into block class
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Jun 14, 2024
1 parent a495c2d commit 1ce33a6
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 140 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.allaymc.api.block.component.common;

import com.google.common.base.Preconditions;
import org.allaymc.api.block.BlockBehavior;
import org.allaymc.api.block.component.BlockComponent;
import org.allaymc.api.block.function.*;
import org.allaymc.api.block.data.BlockFace;
import org.allaymc.api.block.data.BlockStateWithPos;
import org.allaymc.api.block.property.type.BlockPropertyType;
import org.allaymc.api.block.type.BlockState;
import org.allaymc.api.block.type.BlockType;
Expand All @@ -11,11 +13,14 @@
import org.allaymc.api.entity.component.common.EntityContainerHolderComponent;
import org.allaymc.api.entity.effect.type.EffectConduitPowerType;
import org.allaymc.api.entity.effect.type.EffectHasteType;
import org.allaymc.api.entity.interfaces.EntityPlayer;
import org.allaymc.api.item.ItemStack;
import org.allaymc.api.item.enchantment.type.EnchantmentAquaAffinityType;
import org.allaymc.api.item.enchantment.type.EnchantmentEfficiencyType;
import org.allaymc.api.item.type.ItemType;
import org.allaymc.api.utils.Utils;
import org.allaymc.api.world.Dimension;
import org.joml.Vector3fc;
import org.joml.Vector3ic;

import static org.allaymc.api.item.ItemHelper.isSword;

Expand All @@ -24,7 +29,7 @@
*
* @author daoge_cmd
*/
public interface BlockBaseComponent extends OnNeighborUpdate, OnRandomUpdate, Place, OnPlace, OnInteract, OnReplace, OnScheduledUpdate, BlockComponent {
public interface BlockBaseComponent extends BlockComponent {
BlockType<? extends BlockBehavior> getBlockType();

default <DATATYPE> void updateBlockProperty(BlockPropertyType<DATATYPE> propertyType, DATATYPE value, int x, int y, int z, Dimension dimension) {
Expand All @@ -46,7 +51,58 @@ default <DATATYPE> void updateBlockProperty(BlockPropertyType<DATATYPE> property
chunk.sendChunkPacket(Dimension.createBlockUpdatePacket(newBlockState, x, y, z, layer));
}

default boolean canDamageItem(ItemType<?> itemType) {
/**
* Call when a blockState causes another blockState to update.
*
* @param updated The vec of the updated block
* @param neighbor The vec of the block that triggered the update
* @param face The neighbor block is on the side of the current updated block
* @param dimension the dimension
*/
void onNeighborUpdate(Vector3ic updated, Vector3ic neighbor, BlockFace face, Dimension dimension);

void onRandomUpdate(BlockStateWithPos blockState);

static void checkParam(EntityPlayer player, Dimension dimension, BlockState blockState, Vector3ic targetBlockPos, Vector3ic placeBlockPos, Vector3fc clickPos, BlockFace blockFace) {
// player is nullable
Preconditions.checkNotNull(dimension);
Preconditions.checkNotNull(blockState);
Preconditions.checkNotNull(targetBlockPos);
Preconditions.checkNotNull(placeBlockPos);
// clickPos is nullable
Preconditions.checkNotNull(blockFace);
}

boolean place(EntityPlayer player, Dimension dimension, BlockState blockState, Vector3ic targetBlockPos, Vector3ic placeBlockPos, Vector3fc clickPos, BlockFace blockFace);

void onPlace(BlockStateWithPos currentBlockState, BlockState newBlockState);

/**
* @param player The player who interacted with the block, can be null
* @param itemStack The item in the player's hand
* @param blockPos The pos of the block that the player clicked on
* @param placeBlockPos Assuming the player is holding a block item in their hand, this parameter indicates where the block will be placed (if it can be placed)
* @param clickPos The precise pos where the player clicked
* @param blockFace The face of the block that the player clicked on
*
* @return Whether the operation is valid.
* For example, right-clicking on the crafting table is normally considered a valid operation, so this method will return true
* If false is returned, the useItemOn method of the player's item will continue to be called
*/
boolean onInteract(EntityPlayer player, ItemStack itemStack, Dimension dimension, Vector3ic blockPos, Vector3ic placeBlockPos, Vector3fc clickPos, BlockFace blockFace);

void onReplace(BlockStateWithPos currentBlockState, BlockState newBlockState);

void onScheduledUpdate(BlockStateWithPos blockState);

default ItemStack[] getDrops(ItemStack itemStack) {
if (getBlockType().getItemType() != null) {
return new ItemStack[] {getBlockType().getItemType().createItemStack()};
}
return Utils.EMPTY_ITEM_STACK_ARRAY;
}

default boolean canDamageItem(ItemStack itemStack) {
return true;
}

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

29 changes: 0 additions & 29 deletions Allay-API/src/main/java/org/allaymc/api/block/function/Place.java

This file was deleted.

2 changes: 2 additions & 0 deletions Allay-API/src/main/java/org/allaymc/api/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.netty.buffer.ByteBuf;
import lombok.experimental.UtilityClass;
import org.allaymc.api.block.type.BlockState;
import org.allaymc.api.item.ItemStack;

import java.io.*;
import java.nio.charset.StandardCharsets;
Expand All @@ -21,6 +22,7 @@
public class Utils {
public static final String[] EMPTY_STRING_ARRAY = new String[0];
public static final BlockState[][][] EMPTY_BLOCK_STATE_ARRAY_3D = new BlockState[0][0][0];
public static final ItemStack[] EMPTY_ITEM_STACK_ARRAY = new ItemStack[0];

public byte[] appendBytes(byte[] bytes1, byte[]... bytes2) {
int length = bytes1.length;
Expand Down
5 changes: 5 additions & 0 deletions Allay-API/src/main/java/org/allaymc/api/world/Dimension.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.allaymc.api.blockentity.BlockEntity;
import org.allaymc.api.entity.Entity;
import org.allaymc.api.entity.interfaces.EntityPlayer;
import org.allaymc.api.item.ItemStack;
import org.allaymc.api.math.position.Position3i;
import org.allaymc.api.utils.MathUtils;
import org.allaymc.api.utils.Utils;
Expand Down Expand Up @@ -409,4 +410,8 @@ default void addSound(float x, float y, float z, String sound, float volume, flo

getChunkService().getChunkByLevelPos((int) x, (int) z).addChunkPacket(packet);
}

default void dropItem(ItemStack itemStack, float x, float y, float z) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ protected void completeBreak(EntityPlayer player, int x, int y, int z) {
pk.setPosition(Vector3f.from(breakBlockX + 0.5f, breakBlockY + 0.5f, breakBlockZ + 0.5f));
pk.setData(oldState.blockStateHash());
player.getCurrentChunk().addChunkPacket(pk);
// TODO: on break
world.setBlockState(breakBlockX, breakBlockY, breakBlockZ, AIR_TYPE.getDefaultState());
// TODO: drop items
} else {
Expand Down

0 comments on commit 1ce33a6

Please sign in to comment.