Skip to content

Commit

Permalink
Merge pull request #134 from huskcasaca/dev-new-replace-modes
Browse files Browse the repository at this point in the history
New replace modes
  • Loading branch information
huskcasaca authored May 27, 2024
2 parents 0aeae8f + d1797af commit ac31860
Show file tree
Hide file tree
Showing 110 changed files with 717 additions and 257 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ public interface Block extends PlatformReference {

void place(World world, Player player, BlockPosition blockPosition, BlockState blockState, ItemStack itemStack);

Item asItem();

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ public interface BlockState extends StateHolder {

boolean isAir();

Item getItem();

boolean canBeReplaced(Player player, BlockInteraction interaction);

boolean isReplaceable();
Expand All @@ -39,4 +37,8 @@ default String getPropertiesString() {

boolean requiresCorrectToolForDrops();

default Item getItem() {
return getBlock().asItem();
}

}
7 changes: 7 additions & 0 deletions api/src/main/java/dev/huskuraft/effortless/api/core/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

public interface Item extends PlatformReference {

Expand Down Expand Up @@ -31,4 +32,10 @@ static Optional<Item> fromIdOptional(ResourceLocation id) {

boolean mineBlock(World world, Player player, BlockPosition blockPosition, BlockState blockState, ItemStack itemStack);

Text getName(ItemStack itemStack);

default Text getName() {
return getName(getDefaultStack());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ default void mineBlock(World world, Player player, BlockPosition blockPosition,

}

default Text getName() {
return getItem().getName(this);
}

enum TooltipType {
NORMAL,
NORMAL_CREATIVE,
Expand Down
9 changes: 9 additions & 0 deletions api/src/main/java/dev/huskuraft/effortless/api/lang/Lang.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package dev.huskuraft.effortless.api.lang;

import dev.huskuraft.effortless.api.platform.Entrance;
import dev.huskuraft.effortless.api.platform.PlatformLoader;
import dev.huskuraft.effortless.api.text.Text;

public interface Lang {

static Lang getInstance() {
return PlatformLoader.getSingleton();
}

static Text translate(String key) {
return Text.translate(Entrance.getInstance().getId() + "." + key);
}
Expand All @@ -27,6 +32,10 @@ static String asKeyDesc(String key) {
return "key.%s.%s.desc".formatted(Entrance.getInstance().getId(), key);
}

static boolean hasKey(String key) {
return getInstance().has(key);
}

String getOrDefault(String id);

boolean has(String id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public void onKeyInput(InputKey key) {
}
if (EffortlessKeys.TOGGLE_REPLACE.getBinding().consumeClick()) {
getEntrance().getClient().getSoundManager().playButtonClickSound();
getEntrance().getStructureBuilder().setReplaceMode(getRunningClient().getPlayer(), getEntrance().getStructureBuilder().getContext(getRunningClient().getPlayer()).replaceMode().next());
getEntrance().getStructureBuilder().setReplace(getRunningClient().getPlayer(), getEntrance().getStructureBuilder().getContext(getRunningClient().getPlayer()).replace().next());
}

if (Platform.getInstance().isDevelopment()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
import dev.huskuraft.effortless.building.operation.block.BlockOperation;
import dev.huskuraft.effortless.building.operation.block.BlockOperationResult;
import dev.huskuraft.effortless.building.pattern.Pattern;
import dev.huskuraft.effortless.building.replace.ReplaceMode;
import dev.huskuraft.effortless.building.replace.Replace;
import dev.huskuraft.effortless.building.session.BatchBuildSession;
import dev.huskuraft.effortless.building.structure.BuildMode;
import dev.huskuraft.effortless.building.structure.builder.Structure;
Expand Down Expand Up @@ -286,11 +286,11 @@ public boolean setStructure(Player player, Structure structure) {
}

@Override
public boolean setReplaceMode(Player player, ReplaceMode replaceMode) {
public boolean setReplace(Player player, Replace replace) {
if (!checkPermission(player)) {
return false;
}
updateContext(player, context -> context.withReplaceMode(replaceMode));
updateContext(player, context -> context.withReplace(replace));
return true;
}

Expand Down Expand Up @@ -566,7 +566,7 @@ public void showBuildContextResult(UUID uuid, int priority, Player player, Conte
.lightMap(LightTexture.FULL_BLOCK)
.disableNormals()
.colored(Color.DARK_GRAY)
.stroke(1 / 64f);
.stroke(1 / 32f);
}

if (result instanceof BatchOperationResult batchOperationResult) {
Expand All @@ -585,7 +585,7 @@ public void showBuildContextResult(UUID uuid, int priority, Player player, Conte
.lightMap(LightTexture.FULL_BLOCK)
.disableNormals()
.colored(allColor)
.stroke(1 / 64f);
.stroke(1 / 32f);
}
} else {
getEntrance().getClientManager().getOperationsRenderer().remove(uuid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import dev.huskuraft.effortless.building.StructureBuilder;
import dev.huskuraft.effortless.building.history.OperationResultStack;
import dev.huskuraft.effortless.building.pattern.Pattern;
import dev.huskuraft.effortless.building.replace.ReplaceMode;
import dev.huskuraft.effortless.building.replace.Replace;
import dev.huskuraft.effortless.building.structure.builder.Structure;
import dev.huskuraft.effortless.networking.packets.player.PlayerBuildPreviewPacket;
import dev.huskuraft.effortless.networking.packets.player.PlayerBuildTooltipPacket;
Expand Down Expand Up @@ -85,7 +85,7 @@ public boolean setStructure(Player player, Structure structure) {
}

@Override
public boolean setReplaceMode(Player player, ReplaceMode replaceMode) {
public boolean setReplace(Player player, Replace replace) {
return false;
}

Expand Down
38 changes: 23 additions & 15 deletions common/src/main/java/dev/huskuraft/effortless/building/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import dev.huskuraft.effortless.api.math.Vector3i;
import dev.huskuraft.effortless.building.operation.block.EntityState;
import dev.huskuraft.effortless.building.pattern.Pattern;
import dev.huskuraft.effortless.building.replace.Replace;
import dev.huskuraft.effortless.building.replace.ReplaceMode;
import dev.huskuraft.effortless.building.session.BatchBuildSession;
import dev.huskuraft.effortless.building.session.BuildSession;
Expand All @@ -36,7 +37,7 @@ public record Context(

Structure structure,
Pattern pattern,
ReplaceMode replaceMode,
Replace replace,

Configs configs,
Extras extras
Expand All @@ -62,13 +63,17 @@ public static Context defaultSet() {
Interactions.EMPTY,
Structure.DISABLED,
Pattern.DISABLED,
ReplaceMode.DISABLED,
Replace.DISABLED,
new Configs(
ConstraintConfig.DEFAULT
), null
);
}

public ReplaceMode replaceMode() {
return replace.replaceMode();
}

private static BlockInteraction withPosition(BlockInteraction blockInteraction, BlockPosition blockPosition) {
return new BlockInteraction(blockInteraction.getPosition().add(blockPosition.sub(blockInteraction.getBlockPosition()).toVector3d()), blockInteraction.getDirection(), blockPosition, blockInteraction.isInside());
}
Expand Down Expand Up @@ -172,39 +177,39 @@ public TracingResult tracingResult() {
}

public Context withBuildState(BuildState state) {
return new Context(id, state, buildType, interactions, structure, pattern, replaceMode, configs, extras);
return new Context(id, state, buildType, interactions, structure, pattern, replace, configs, extras);
}

public Context withBuildType(BuildType type) {
return new Context(id, buildState, type, interactions, structure, pattern, replaceMode, configs, extras);
return new Context(id, buildState, type, interactions, structure, pattern, replace, configs, extras);
}

public Context withNextInteraction(BlockInteraction interaction) {
return new Context(id, buildState, buildType, interactions.put(interaction), structure, pattern, replaceMode, configs, extras);
return new Context(id, buildState, buildType, interactions.put(interaction), structure, pattern, replace, configs, extras);
}

public Context withNoInteraction() {
return new Context(id, buildState, buildType, Interactions.EMPTY, structure, pattern, replaceMode, configs, extras);
return new Context(id, buildState, buildType, Interactions.EMPTY, structure, pattern, replace, configs, extras);
}

public Context withStructure(Structure structure) {
return new Context(id, buildState, buildType, interactions, structure, pattern, replaceMode, configs, extras);
return new Context(id, buildState, buildType, interactions, structure, pattern, replace, configs, extras);
}

public Context withPattern(Pattern pattern) {
return new Context(id, buildState, buildType, interactions, structure, pattern, replaceMode, configs, extras);
return new Context(id, buildState, buildType, interactions, structure, pattern, replace, configs, extras);
}

public Context withReplaceMode(ReplaceMode replaceMode) {
return new Context(id, buildState, buildType, interactions, structure, pattern, replaceMode, configs, extras);
public Context withReplace(Replace replace) {
return new Context(id, buildState, buildType, interactions, structure, pattern, replace, configs, extras);
}

public Context withExtras(Extras extras) {
return new Context(id, buildState, buildType, interactions, structure, pattern, replaceMode, configs, extras);
return new Context(id, buildState, buildType, interactions, structure, pattern, replace, configs, extras);
}

public Context withPlayerExtras(Player player) {
return new Context(id, buildState, buildType, interactions, structure, pattern, replaceMode, configs, new Extras(player));
return new Context(id, buildState, buildType, interactions, structure, pattern, replace, configs, new Extras(player));
}

public Context finalize(Player player, BuildStage stage) {
Expand All @@ -223,8 +228,11 @@ public Context newInteraction() {
BuildState.IDLE,
buildType,
Interactions.EMPTY,
structure, pattern, replaceMode,
configs, extras
structure,
pattern,
replace,
configs,
extras
);
}

Expand All @@ -248,7 +256,7 @@ public Stream<BlockInteraction> collectInteractions() {
}

public Context withReachParams(Configs configs) {
return new Context(id, buildState, buildType, interactions, structure, pattern, replaceMode, configs, extras);
return new Context(id, buildState, buildType, interactions, structure, pattern, replace, configs, extras);
}

public Context withConstraintConfig(ConstraintConfig config) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ default Text getCategoryText() {
}

default Text getTooltipText() {
return Text.empty();
if (Lang.hasKey(getTooltipKey())) {
return Text.translate(getTooltipKey());
} else {
return Text.empty();
}
}

default ResourceLocation getIcon() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import dev.huskuraft.effortless.api.core.Player;
import dev.huskuraft.effortless.building.history.OperationResultStack;
import dev.huskuraft.effortless.building.pattern.Pattern;
import dev.huskuraft.effortless.building.replace.ReplaceMode;
import dev.huskuraft.effortless.building.replace.Replace;
import dev.huskuraft.effortless.building.structure.builder.Structure;

public abstract class StructureBuilder {
Expand All @@ -26,7 +26,7 @@ public abstract class StructureBuilder {

public abstract boolean setStructure(Player player, Structure structure);

public abstract boolean setReplaceMode(Player player, ReplaceMode replaceMode);
public abstract boolean setReplace(Player player, Replace replace);

public abstract boolean setPattern(Player player, Pattern pattern);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,9 @@ protected BlockOperationResult.Type placeBlock() {
return BlockOperationResult.Type.FAIL_ITEM_NOT_BLOCK;
}

if (!player.getWorld().getBlockState(getBlockPosition()).canBeReplaced(player, getInteraction())) {
if (context.replaceMode().isReplace()) {
var blockStateInWorld = getBlockStateInWorld();
if (!blockStateInWorld.canBeReplaced(player, getInteraction()) || !context.replace().shouldIgnore(blockStateInWorld)) {
if (context.replace().shouldReplace(context, blockStateInWorld)) {
var destroyCheck = destroyBlockCheckOnly();
if (destroyCheck.fail()) {
return destroyCheck;
Expand All @@ -280,8 +281,8 @@ protected BlockOperationResult.Type placeBlock() {
}

// build type
if (!player.getWorld().getBlockState(getBlockPosition()).canBeReplaced(player, getInteraction())) {
if (context.replaceMode().isReplace()) {
if (!getBlockStateInWorld().canBeReplaced(player, getInteraction()) || !context.replace().shouldIgnore(blockStateInWorld)) {
if (context.replace().shouldReplace(context, blockStateInWorld)) {
var destroyResult = destroyBlock();
if (!destroyResult.success()) {
return destroyResult;
Expand Down Expand Up @@ -409,6 +410,10 @@ protected BlockOperationResult.Type interactBlock() {
return BlockOperationResult.Type.SUCCESS;
}

public BlockState getBlockStateInWorld() {
return getWorld().getBlockState(getBlockPosition());
}


public boolean useItem(BlockInteraction interaction) {
return getWorld().getBlockState(interaction.getBlockPosition()).use(getPlayer(), interaction).consumesAction() || getPlayer().getItemStack(interaction.getHand()).getItem().useOnBlock(getPlayer(), interaction).consumesAction();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ default UUID getId() {
return id();
}

default Text getName() {
return name();
}
Text getName();

Operation transform(Operation operation);

Expand Down Expand Up @@ -94,4 +92,8 @@ default Transformer finalize(Player player, BuildStage stage) {
return this;
}

default List<Text> getDescriptions() {
return List.of();
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.huskuraft.effortless.building.pattern.array;

import java.util.List;
import java.util.UUID;
import java.util.stream.IntStream;
import java.util.stream.Stream;
Expand Down Expand Up @@ -27,7 +28,7 @@ public record ArrayTransformer(UUID id, Text name, Vector3i offset, int count) i
public static final Range1i COUNT_RANGE = new Range1i(1, Short.MAX_VALUE);

public ArrayTransformer(Vector3i offset, int count) {
this(UUID.randomUUID(), Text.translate("effortless.transformer.array"), offset, count);
this(UUID.randomUUID(), Text.empty(), offset, count);
}

@Override
Expand All @@ -37,6 +38,14 @@ public Operation transform(Operation operation) {
}));
}

@Override
public Text getName() {
if (!name().getString().isEmpty()) {
return name();
}
return Text.translate("effortless.transformer.array.no_name");
}

@Override
public Transformers getType() {
return Transformers.ARRAY;
Expand Down Expand Up @@ -98,4 +107,9 @@ public ArrayTransformer withCount(int count) {
public float volumeMultiplier() {
return count;
}

@Override
public List<Text> getDescriptions() {
return List.of(Text.text("Offset " + offset.x() + " " + offset.y() + " " + offset.z()), Text.text("Count " + count));
}
}
Loading

0 comments on commit ac31860

Please sign in to comment.