Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block interaction #111

Merged
merged 20 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@

package dev.huskuraft.effortless.api.core;

import dev.huskuraft.effortless.api.core.fluid.BucketCollectable;
import dev.huskuraft.effortless.api.core.fluid.LiquidPlaceable;
import dev.huskuraft.effortless.api.platform.PlatformReference;

public interface Block extends PlatformReference {

BlockState getDefaultBlockState();

BucketCollectable getBucketCollectable();

LiquidPlaceable getLiquidPlaceable();

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ public interface BlockItem extends Item {

Block getBlock();

InteractionResult place(Player player, BlockInteraction blockInteraction);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.stream.Collectors;

import dev.huskuraft.effortless.api.core.fluid.Fluid;
import dev.huskuraft.effortless.api.sound.SoundSet;

public interface BlockState extends StateHolder {
Expand Down Expand Up @@ -30,4 +31,10 @@ default String getPropertiesString() {
return "[" + getProperties().stream().map(PropertyHolder::getAsString).collect(Collectors.joining(",")) + "]";
}

Block getBlock();

boolean canReplace(Fluid fluid);

InteractionResult use(Player player, BlockInteraction blockInteraction);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package dev.huskuraft.effortless.api.core;

import dev.huskuraft.effortless.api.core.fluid.Fluid;
import dev.huskuraft.effortless.api.core.fluid.Fluids;

public interface BucketItem extends Item {

Fluid getContent();

boolean useContent(World world, Player player, BlockPosition blockPosition, BlockInteraction blockInteraction);

void useExtraContent(World world, Player player, BlockPosition blockPosition, ItemStack itemStack);

default boolean isEmpty() {
return getContent().referenceValue().equals(Fluids.EMPTY.referenceValue());
}

@Override
default InteractionResult use(Player player, BlockInteraction blockInteraction) {
var itemStack = player.getItemStack(blockInteraction.getHand());
var blockState = player.getWorld().getBlockState(blockInteraction.getBlockPosition());

if (itemStack.getItem() instanceof BucketItem bucketItem) {
if (bucketItem.isEmpty()) {
var bucketCollectable = blockState.getBlock().getBucketCollectable();
if (bucketCollectable != null) {
var collected = bucketCollectable.pickupBlock(player.getWorld(), player, blockInteraction.getBlockPosition(), blockState);
if (!collected.isEmpty()) {
player.awardStat(StatTypes.ITEM_USED.get(bucketItem));
var result = createFilledResult(player, itemStack, collected);
return InteractionResult.SUCCESS;
}
}
} else {
if (blockState.getBlock().getLiquidPlaceable() != null || blockState.isAir() || blockState.canReplace(bucketItem.getContent())) {
if (bucketItem.useContent(player.getWorld(), player, blockInteraction.getBlockPosition(), blockInteraction)) {
bucketItem.useExtraContent(player.getWorld(), player, blockInteraction.getBlockPosition(), itemStack);
player.awardStat(StatTypes.ITEM_USED.get(bucketItem));
return InteractionResult.SUCCESS;
}
}
}
return InteractionResult.FAIL;
}
return InteractionResult.PASS;
}

static ItemStack createFilledResult(Player player, ItemStack emptyItemStack, ItemStack filledItemStack, boolean preventDuplicates) {
var isCreative = player.getGameType().isCreative(); // player.getAbilities().instabuild;
if (preventDuplicates && isCreative) {
if (!player.getInventory().getItems().contains(filledItemStack)) {
player.getInventory().addItem(filledItemStack);
}
return emptyItemStack;
} else {
if (!isCreative) {
emptyItemStack.decrease(1);
}

if (emptyItemStack.isEmpty()) {
return filledItemStack;
} else {
if (!player.getInventory().addItem(filledItemStack)) {
player.drop(filledItemStack, false, false);
}
return emptyItemStack;
}
}
}

static ItemStack createFilledResult(Player pPlayer, ItemStack emptyItemStack, ItemStack filledItemStack) {
return createFilledResult(pPlayer, emptyItemStack, filledItemStack, true);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package dev.huskuraft.effortless.api.core;

public interface FluidState extends StateHolder {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package dev.huskuraft.effortless.api.core;

public enum InteractionResult {
SUCCESS,
CONSUME,
CONSUME_PARTIAL,
PASS,
FAIL;

public boolean consumesAction() {
return this == SUCCESS || this == CONSUME || this == CONSUME_PARTIAL;
}

public boolean shouldSwing() {
return this == SUCCESS;
}

public boolean shouldAwardStats() {
return this == SUCCESS || this == CONSUME;
}

public static InteractionResult sidedSuccess(boolean isClientSide) {
return isClientSide ? SUCCESS : CONSUME;
}
}
35 changes: 35 additions & 0 deletions api/src/main/java/dev/huskuraft/effortless/api/core/Inventory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package dev.huskuraft.effortless.api.core;

import java.util.List;

import dev.huskuraft.effortless.api.platform.PlatformReference;

public interface Inventory extends PlatformReference {

List<ItemStack> getItems();

List<ItemStack> getArmors();

List<ItemStack> getOffhand();

void setItem(int index, ItemStack itemStack);

ItemStack getItem(int index);

boolean addItem(ItemStack itemStack);

boolean addItem(int index, ItemStack itemStack);

default int getFreeSlotIndex() {
return getItems().stream().filter(ItemStack::isEmpty).findFirst().map(getItems()::indexOf).orElse(-1);
}

default int getSlotSize() {
return getItems().size() + getArmors().size() + getOffhand().size();
}

default boolean contains(ItemStack itemStack) {
return getItems().contains(itemStack);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ static Optional<Item> fromIdOptional(ResourceLocation id) {

ResourceLocation getId();

default boolean isBlockItem() {
return this instanceof BlockItem;
}
InteractionResult use(Player player, BlockInteraction blockInteraction);

}
Original file line number Diff line number Diff line change
Expand Up @@ -1361,6 +1361,11 @@ public ResourceLocation getId() {
return item().getId();
}

@Override
public InteractionResult use(Player player, BlockInteraction blockInteraction) {
return item().use(player, blockInteraction);
}

@Override
public Object referenceValue() {
return item().referenceValue();
Expand Down
27 changes: 19 additions & 8 deletions api/src/main/java/dev/huskuraft/effortless/api/core/Player.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
package dev.huskuraft.effortless.api.core;

import java.util.List;

import dev.huskuraft.effortless.api.text.Text;

public interface Player extends Entity {

PlayerProfile getProfile();

Inventory getInventory();

default boolean isOperator() {
return getServer().getPlayerList().isOperator(getProfile());
}

List<ItemStack> getItemStacks();

void setItemStack(int index, ItemStack itemStack);

ItemStack getItemStack(InteractionHand hand);

void setItemStack(InteractionHand hand, ItemStack itemStack);

void drop(ItemStack itemStack, boolean dropAround, boolean includeThrowerName);

void sendMessage(Text messages);

default void sendMessage(String message) {
Expand All @@ -38,8 +36,21 @@ default void sendClientMessage(String message, boolean actionBar) {

boolean canAttackBlock(BlockPosition blockPosition);

boolean tryPlaceBlock(BlockInteraction interaction);
boolean destroyBlock(BlockInteraction interaction);

default boolean useItem(BlockInteraction interaction) {
return getWorld().getBlockState(interaction.getBlockPosition()).use(this, interaction).consumesAction()
|| getItemStack(interaction.getHand()).getItem().use(this, interaction).consumesAction();
}

void awardStat(Stat<?> stat, int increment);

default void awardStat(Stat<?> stat) {
awardStat(stat, 1);
}

void resetStat(Stat<?> stat);


boolean tryBreakBlock(BlockInteraction interaction);

}
7 changes: 7 additions & 0 deletions api/src/main/java/dev/huskuraft/effortless/api/core/Stat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.huskuraft.effortless.api.core;

import dev.huskuraft.effortless.api.platform.PlatformReference;

public interface Stat<T> extends PlatformReference {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dev.huskuraft.effortless.api.core;

import dev.huskuraft.effortless.api.platform.PlatformReference;

public interface StatType<T extends PlatformReference> extends PlatformReference {

Stat<T> get(T value);

}
18 changes: 18 additions & 0 deletions api/src/main/java/dev/huskuraft/effortless/api/core/StatTypes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package dev.huskuraft.effortless.api.core;

import dev.huskuraft.effortless.api.platform.ContentFactory;
import dev.huskuraft.effortless.api.platform.PlatformReference;

public enum StatTypes {

ITEM_USED,
ITEM_BROKEN,
ITEM_PICKED_UP,
ITEM_DROPPED,
;

public Stat<?> get(PlatformReference value) {
return ContentFactory.getInstance().getStatType(this).get(value);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package dev.huskuraft.effortless.api.core.fluid;

import java.util.Optional;

import dev.huskuraft.effortless.api.core.BlockPosition;
import dev.huskuraft.effortless.api.core.BlockState;
import dev.huskuraft.effortless.api.core.ItemStack;
import dev.huskuraft.effortless.api.core.Player;
import dev.huskuraft.effortless.api.core.World;
import dev.huskuraft.effortless.api.sound.Sound;

public interface BucketCollectable {

ItemStack pickupBlock(World world, Player player, BlockPosition blockPosition, BlockState blockState);

Optional<Sound> getPickupSound();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package dev.huskuraft.effortless.api.core.fluid;

public interface FlowingFluid extends Fluid {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package dev.huskuraft.effortless.api.core.fluid;

import dev.huskuraft.effortless.api.platform.PlatformReference;

public interface Fluid extends PlatformReference {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package dev.huskuraft.effortless.api.core.fluid;

import dev.huskuraft.effortless.api.platform.ContentFactory;

public enum Fluids implements Fluid {
EMPTY,
FLOWING_WATER,
WATER,
FLOWING_LAVA,
LAVA,
;

@Override
public Object referenceValue() {
return ContentFactory.getInstance().getFluid(this).referenceValue();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package dev.huskuraft.effortless.api.core.fluid;

import dev.huskuraft.effortless.api.core.BlockPosition;
import dev.huskuraft.effortless.api.core.BlockState;
import dev.huskuraft.effortless.api.core.FluidState;
import dev.huskuraft.effortless.api.core.Player;
import dev.huskuraft.effortless.api.core.World;

public interface LiquidPlaceable {

boolean canPlaceLiquid(World world/*BlockGetter*/, Player player, BlockPosition blockPosition, BlockState blockState, Fluid fluid);

boolean placeLiquid(World world, BlockPosition blockPosition, BlockState blockState, FluidState fluidState);

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import dev.huskuraft.effortless.api.core.ItemStack;
import dev.huskuraft.effortless.api.core.Items;
import dev.huskuraft.effortless.api.core.ResourceLocation;
import dev.huskuraft.effortless.api.core.StatType;
import dev.huskuraft.effortless.api.core.StatTypes;
import dev.huskuraft.effortless.api.core.fluid.Fluid;
import dev.huskuraft.effortless.api.core.fluid.Fluids;
import dev.huskuraft.effortless.api.networking.Buffer;
import dev.huskuraft.effortless.api.sound.Sound;
import dev.huskuraft.effortless.api.sound.Sounds;
Expand Down Expand Up @@ -67,4 +71,8 @@ default Item getItem(Items items) {
return getOptionalItem(items).orElseThrow();
}

Fluid getFluid(Fluids fluids);

<T extends PlatformReference> StatType<T> getStatType(StatTypes statTypes);

}
Loading
Loading