Skip to content

Commit

Permalink
refactor: item tools (#520)
Browse files Browse the repository at this point in the history
  • Loading branch information
IWareQ authored Dec 20, 2024
1 parent 31eb1cf commit fec66d9
Show file tree
Hide file tree
Showing 39 changed files with 191 additions and 319 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import org.allaymc.api.block.type.BlockState;

/**
* @author daoge_cmd
* @author IWareQ
*/
public interface BlockWoodBaseComponent extends BlockBaseComponent {

public interface BlockStrippableComponent extends BlockComponent {
/**
* Get the stripped block state of the given block state
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
/**
* Represents the information about a player interacting with a block.
*
* @param player The player who is placing the block.
* @param clickBlockPos The block that the player clicked on.
* @param clickPos The precise pos where the player clicked.
* @param blockFace The face of the block that the player clicked on.
* @param player The player who is placing the block.
* @param clickedBlockPos The block that the player clicked on.
* @param clickedPos The precise pos where the player clicked.
* @param blockFace The face of the block that the player clicked on.
*
* @author daoge_cmd
*/
public record PlayerInteractInfo(
EntityPlayer player,
Vector3ic clickBlockPos,
Vector3fc clickPos,
Vector3ic clickedBlockPos,
Vector3fc clickedPos,
BlockFace blockFace
) {
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.allaymc.api.block.interfaces;

import org.allaymc.api.block.BlockBehavior;
import org.allaymc.api.block.component.BlockWoodBaseComponent;
import org.allaymc.api.block.component.BlockStrippableComponent;

public interface BlockCrimsonHyphaeBehavior extends BlockBehavior, BlockWoodBaseComponent {
public interface BlockCrimsonHyphaeBehavior extends BlockBehavior, BlockStrippableComponent {
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.allaymc.api.block.interfaces;

import org.allaymc.api.block.BlockBehavior;
import org.allaymc.api.block.component.BlockWoodBaseComponent;
import org.allaymc.api.block.component.BlockStrippableComponent;

public interface BlockCrimsonStemBehavior extends BlockBehavior, BlockWoodBaseComponent {
public interface BlockCrimsonStemBehavior extends BlockBehavior, BlockStrippableComponent {
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.allaymc.api.block.interfaces;

import org.allaymc.api.block.BlockBehavior;
import org.allaymc.api.block.component.BlockWoodBaseComponent;
import org.allaymc.api.block.component.BlockStrippableComponent;

public interface BlockStrippedCrimsonHyphaeBehavior extends BlockBehavior, BlockWoodBaseComponent {
public interface BlockStrippedCrimsonHyphaeBehavior extends BlockBehavior, BlockStrippableComponent {
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.allaymc.api.block.interfaces;

import org.allaymc.api.block.BlockBehavior;
import org.allaymc.api.block.component.BlockWoodBaseComponent;
import org.allaymc.api.block.component.BlockStrippableComponent;

public interface BlockStrippedCrimsonStemBehavior extends BlockBehavior, BlockWoodBaseComponent {
public interface BlockStrippedCrimsonStemBehavior extends BlockBehavior, BlockStrippableComponent {
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.allaymc.api.block.interfaces;

import org.allaymc.api.block.BlockBehavior;
import org.allaymc.api.block.component.BlockWoodBaseComponent;
import org.allaymc.api.block.component.BlockStrippableComponent;

public interface BlockStrippedWarpedHyphaeBehavior extends BlockBehavior, BlockWoodBaseComponent {
public interface BlockStrippedWarpedHyphaeBehavior extends BlockBehavior, BlockStrippableComponent {
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.allaymc.api.block.interfaces;

import org.allaymc.api.block.BlockBehavior;
import org.allaymc.api.block.component.BlockWoodBaseComponent;
import org.allaymc.api.block.component.BlockStrippableComponent;

public interface BlockStrippedWarpedStemBehavior extends BlockBehavior, BlockWoodBaseComponent {
public interface BlockStrippedWarpedStemBehavior extends BlockBehavior, BlockStrippableComponent {
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.allaymc.api.block.interfaces;

import org.allaymc.api.block.BlockBehavior;
import org.allaymc.api.block.component.BlockWoodBaseComponent;
import org.allaymc.api.block.component.BlockStrippableComponent;

public interface BlockWarpedHyphaeBehavior extends BlockBehavior, BlockWoodBaseComponent {
public interface BlockWarpedHyphaeBehavior extends BlockBehavior, BlockStrippableComponent {
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.allaymc.api.block.interfaces;

import org.allaymc.api.block.BlockBehavior;
import org.allaymc.api.block.component.BlockWoodBaseComponent;
import org.allaymc.api.block.component.BlockStrippableComponent;

public interface BlockWarpedStemBehavior extends BlockBehavior, BlockWoodBaseComponent {
public interface BlockWarpedStemBehavior extends BlockBehavior, BlockStrippableComponent {
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.allaymc.api.block.interfaces;

import org.allaymc.api.block.BlockBehavior;
import org.allaymc.api.block.component.BlockStrippableComponent;

public interface BlockWoodBehavior extends BlockBehavior {
public interface BlockWoodBehavior extends BlockBehavior, BlockStrippableComponent {
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public boolean onInteract(ItemStack itemStack, Dimension dimension, PlayerIntera
if (player.isSneaking()) return false;

var craftingTableContainer = player.getContainer(FullContainerType.CRAFTING_TABLE);
craftingTableContainer.setBlockPos(interactInfo.clickBlockPos());
craftingTableContainer.setBlockPos(interactInfo.clickedBlockPos());
craftingTableContainer.addViewer(player);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public boolean onInteract(ItemStack itemStack, Dimension dimension, PlayerIntera

var player = interactInfo.player();
var enchantTableContainer = player.getContainer(FullContainerType.ENCHANT_TABLE);
enchantTableContainer.setBlockPos(interactInfo.clickBlockPos());
enchantTableContainer.setBlockPos(interactInfo.clickedBlockPos());
enchantTableContainer.addViewer(player);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected void onNeighborChanged(CBlockOnNeighborUpdateEvent event) {

@EventHandler
protected void onInteract(CBlockOnInteractEvent event) {
var pos = event.getInteractInfo().clickBlockPos();
var pos = event.getInteractInfo().clickedBlockPos();
var blockEntity = getBlockEntity(pos.x(), pos.y(), pos.z(), event.getDimension());
((BlockEntityBaseComponentImpl) ((BlockEntityImpl) blockEntity).getBaseComponent()).onInteract(event);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public boolean onInteract(ItemStack itemStack, Dimension dimension, PlayerIntera
}

var player = interactInfo.player();
var blockEntity = blockEntityHolderComponent.getBlockEntity(new Position3i(interactInfo.clickBlockPos(), dimension));
var blockEntity = blockEntityHolderComponent.getBlockEntity(new Position3i(interactInfo.clickedBlockPos(), dimension));
if (blockEntity instanceof BlockEntityJukebox jukebox) {
var musicDisc = jukebox.getMusicDiscItem();
if (musicDisc != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public boolean place(Dimension dimension, BlockState blockState, Vector3ic place
face = face.opposite();
}

var clickedBlock = dimension.getBlockState(placementInfo.clickBlockPos());
var clickedBlock = dimension.getBlockState(placementInfo.clickedBlockPos());
if (clickedBlock.getBlockType() == getBlockType() && clickedBlock.getPropertyValue(FACING_DIRECTION) == face.ordinal()) {
face = face.opposite();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public boolean place(Dimension dimension, BlockState blockState, Vector3ic place
var blockFace = placementInfo.blockFace();
var stairFace = placementInfo.player().getHorizontalFace();
blockState = blockState.setProperty(BlockPropertyTypes.WEIRDO_DIRECTION, stairFace.toStairDirectionValue());
if ((placementInfo.clickPos().y() > 0.5 && blockFace != BlockFace.UP) || blockFace == BlockFace.DOWN) {
if ((placementInfo.clickedPos().y() > 0.5 && blockFace != BlockFace.UP) || blockFace == BlockFace.DOWN) {
blockState = blockState.setProperty(BlockPropertyTypes.UPSIDE_DOWN_BIT, true);
}
dimension.setBlockState(placeBlockPos.x(), placeBlockPos.y(), placeBlockPos.z(), blockState, placementInfo);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
package org.allaymc.server.block.component;

import org.allaymc.api.block.BlockBehavior;
import org.allaymc.api.block.component.BlockWoodBaseComponent;
import lombok.AllArgsConstructor;
import org.allaymc.api.block.component.BlockStrippableComponent;
import org.allaymc.api.block.data.BlockId;
import org.allaymc.api.block.type.BlockState;
import org.allaymc.api.block.type.BlockType;

/**
* @author Dhaiven
* @author IWareQ
*/
public class BlockWoodBaseComponentImpl extends BlockBaseComponentImpl implements BlockWoodBaseComponent {
@AllArgsConstructor
public class BlockStrippableComponentImpl implements BlockStrippableComponent {
protected final BlockId strippedType;

public BlockWoodBaseComponentImpl(BlockType<? extends BlockBehavior> blockType, BlockId strippedType) {
super(blockType);
this.strippedType = strippedType;
}

@Override
public BlockState getStrippedBlockState(BlockState unstrippedBlockState) {
return strippedType.getBlockType().copyPropertyValuesFrom(unstrippedBlockState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public boolean canKeepExisting(BlockStateWithPos current, BlockStateWithPos neig
public boolean onInteract(ItemStack itemStack, Dimension dimension, PlayerInteractInfo interactInfo) {
if (super.onInteract(itemStack, dimension, interactInfo)) return true;

var pos = interactInfo.clickBlockPos();
var pos = interactInfo.clickedBlockPos();
var blockState = dimension.getBlockState(pos);
if (!blockState.getPropertyValue(BUTTON_PRESSED_BIT)) {
dimension.updateBlockProperty(BUTTON_PRESSED_BIT, true, pos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public boolean onInteract(ItemStack itemStack, Dimension dimension, PlayerIntera
if (super.onInteract(itemStack, dimension, interactInfo)) return true;
if (itemStack instanceof ItemBoneMealStack) {
var blockState = dimension.getBlockState(interactInfo.clickBlockPos());
var blockState = dimension.getBlockState(interactInfo.clickedBlockPos());
if (blockState.getPropertyValue(GROWTH) < GROWTH.getMax()) {
int newAge = ThreadLocalRandom.current().nextInt(3) + 2; //Between 2 and 5
grow(dimension, interactInfo.clickBlockPos(), newAge);
grow(dimension, interactInfo.clickedBlockPos(), newAge);
//TODO: BoneMeal particle
interactInfo.player().tryConsumeItemInHand();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public boolean onInteract(ItemStack itemStack, Dimension dimension, PlayerIntera
if (super.onInteract(itemStack, dimension, interactInfo)) return true;
if (interactInfo == null) return false;

Vector3i pos = (Vector3i) interactInfo.clickBlockPos();
Vector3i pos = (Vector3i) interactInfo.clickedBlockPos();
var blockState = dimension.getBlockState(pos);

Vector3ic otherPos;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public boolean place(Dimension dimension, BlockState blockState, Vector3ic place
if (!oldBlock.getBlockType().hasBlockTag(BlockCustomTags.REPLACEABLE) || torchFace == TorchFacingDirection.UNKNOWN)
return false;

var targetBlock = dimension.getBlockState(placementInfo.clickBlockPos());
var targetBlock = dimension.getBlockState(placementInfo.clickedBlockPos());
if (targetBlock.getBlockStateData().isSolid()) {
blockState = blockState.setProperty(BlockPropertyTypes.TORCH_FACING_DIRECTION, torchFace);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
package org.allaymc.server.block.impl;

import lombok.experimental.Delegate;
import org.allaymc.api.block.component.BlockWoodBaseComponent;
import org.allaymc.api.block.component.BlockStrippableComponent;
import org.allaymc.api.block.interfaces.BlockWoodBehavior;
import org.allaymc.api.component.interfaces.Component;
import org.allaymc.server.component.interfaces.ComponentProvider;

import java.util.List;

public class BlockWoodBehaviorImpl extends BlockBehaviorImpl implements BlockWoodBehavior {
@Delegate
protected BlockStrippableComponent strippableComponent;

public BlockWoodBehaviorImpl(List<ComponentProvider<? extends Component>> componentProviders) {
super(componentProviders);
}

@Delegate
@Override
protected BlockWoodBaseComponent getBaseComponent() {
return (BlockWoodBaseComponent) super.getBaseComponent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,19 @@ public static void initWoods() {
BlockTypes.STRIPPED_PALE_OAK_WOOD = buildStrippedWood(BlockId.STRIPPED_PALE_OAK_WOOD);
}

private static <T extends BlockBehavior> BlockType<T> buildStrippedWood(BlockId blockId) {
return buildWood(blockId, blockId);
}

private static <T extends BlockBehavior> BlockType<T> buildWood(BlockId blockId, BlockId strippedBlockId) {
return AllayBlockType
.builder(BlockWoodBehaviorImpl.class)
.vanillaBlock(blockId)
.setProperties(BlockPropertyTypes.PILLAR_AXIS)
.addComponent(new BlockStrippableComponentImpl(strippedBlockId))
.build();
}

public static void initButtons() {
BlockTypes.ACACIA_BUTTON = buildWoodenButton(BlockId.ACACIA_BUTTON);
BlockTypes.BAMBOO_BUTTON = buildWoodenButton(BlockId.BAMBOO_BUTTON);
Expand Down Expand Up @@ -563,24 +576,6 @@ public static void initRods() {
.build();
}

private static <T extends BlockBehavior> BlockType<T> buildWood(BlockId blockId, BlockId strippedBlockId) {
return AllayBlockType
.builder(BlockWoodBehaviorImpl.class)
.vanillaBlock(blockId)
.setProperties(BlockPropertyTypes.PILLAR_AXIS)
.setBaseComponentSupplier(blockType -> new BlockWoodBaseComponentImpl(blockType, strippedBlockId))
.build();
}

private static <T extends BlockBehavior> BlockType<T> buildStrippedWood(BlockId blockId) {
return AllayBlockType
.builder(BlockWoodBehaviorImpl.class)
.vanillaBlock(blockId)
.setProperties(BlockPropertyTypes.PILLAR_AXIS)
.setBaseComponentSupplier(blockType -> new BlockWoodBaseComponentImpl(blockType, blockId))
.build();
}

private static <T extends BlockBehavior> BlockType<T> buildWoodenButton(BlockId blockId) {
return buildButton(blockId, BlockWoodenButtonBaseComponentImpl::new);
}
Expand Down
Loading

0 comments on commit fec66d9

Please sign in to comment.