Skip to content

Commit

Permalink
Merge pull request #131 from huskcasaca/dev-undo-redo-revamp
Browse files Browse the repository at this point in the history
Undo/Redo revamp
  • Loading branch information
huskcasaca authored May 23, 2024
2 parents 65f5aad + 31a16c2 commit e48bf21
Show file tree
Hide file tree
Showing 29 changed files with 180 additions and 275 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void detachAll() {

@Override
public boolean isPauseGame() {
return true;
return false;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,13 @@ public boolean setPattern(Player player, Pattern pattern) {
if (!checkPermission(player)) {
return false;
}
updateContext(player, context -> context.withPattern(pattern).finalize(player, BuildStage.UPDATE_CONTEXT));
updateContext(player, context -> {
var nextContext = context.withPattern(pattern).finalize(player, BuildStage.UPDATE_CONTEXT);
if (!context.pattern().enabled() && nextContext.pattern().enabled()) {
nextContext = nextContext.finalize(player, BuildStage.ENABLE_PATTERN);
}
return nextContext;
});
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
public enum BuildStage {
TICK,
UPDATE_CONTEXT,
ENABLE_PATTERN,
INTERACT
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public Text getDisplayName() {
}

public Text getDisplayName(BuildMode buildMode) {
return Text.translate("effortless.state.%s.var1".formatted(
return Text.translate("effortless.state.%s.build_mode".formatted(
switch (this) {
case IDLE -> "idle";
case BREAK_BLOCK -> "breaking_block";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import dev.huskuraft.effortless.api.core.World;
import dev.huskuraft.effortless.api.math.BoundingBox3d;
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.ReplaceMode;
import dev.huskuraft.effortless.building.session.BatchBuildSession;
Expand Down Expand Up @@ -214,22 +215,37 @@ public Context withPattern(Pattern pattern) {
return new Context(id, buildState, buildType, buildInteractions, structureParams, patternParams.withPattern(pattern), customParams);
}

public Context withRandomPatternSeed() {
return new Context(id, buildState, buildType, buildInteractions, structureParams, patternParams.withRandomSeed(), customParams);
public Context withActiveState(EntityState activeState) {
return new Context(id, buildState, buildType, buildInteractions, structureParams, patternParams.withActiveState(activeState), customParams);
}

public Context withInteractState(EntityState interactState) {
return new Context(id, buildState, buildType, buildInteractions, structureParams, patternParams.withInteractState(interactState), customParams);
}

public Context withLimitedPatternProducer(boolean limitedProducer) {
return new Context(id, buildState, buildType, buildInteractions, structureParams, patternParams.withLimitProducer(limitedProducer), customParams);
}

public Context withRandomPatternSeed() {
return new Context(id, buildState, buildType, buildInteractions, structureParams, patternParams.withRandomSeed(), customParams);
}

public Context finalize(Player player, BuildStage stage) {
switch (stage) {
case TICK -> {
return withPattern(pattern().finalize(player, stage)).withRandomPatternSeed().withLimitedPatternProducer(player.getGameMode().isSurvival());
return withPattern(pattern().finalize(player, stage))
.withInteractState(EntityState.get(player))
.withLimitedPatternProducer(player.getGameMode().isSurvival())
.withRandomPatternSeed();
}
case UPDATE_CONTEXT -> {
}
case ENABLE_PATTERN -> {
return withActiveState(EntityState.get(player));
}
case INTERACT -> {

}
}
return withPattern(pattern().finalize(player, stage));
Expand Down Expand Up @@ -353,24 +369,34 @@ public StructureParams withReplaceMode(ReplaceMode replaceMode) {

public record PatternParams(
Pattern pattern,
EntityState activeState,
EntityState interactState,
boolean limitedProducer,
long seed
) {

public PatternParams(Pattern pattern) {
this(pattern, false, new Random().nextLong());
this(pattern, null, null, false, new Random().nextLong());
}

public PatternParams withPattern(Pattern pattern) {
return new PatternParams(pattern, limitedProducer, seed);
return new PatternParams(pattern, activeState, interactState, limitedProducer, seed);
}

public PatternParams withRandomSeed() {
return new PatternParams(pattern, limitedProducer, new Random().nextLong());
public PatternParams withActiveState(EntityState activeState) {
return new PatternParams(pattern, activeState, interactState, limitedProducer, seed);
}

public PatternParams withInteractState(EntityState interactState) {
return new PatternParams(pattern, activeState, interactState, limitedProducer, seed);
}

public PatternParams withLimitProducer(boolean limitedProducer) {
return new PatternParams(pattern, limitedProducer, new Random().nextLong());
return new PatternParams(pattern, activeState, interactState, limitedProducer, new Random().nextLong());
}

public PatternParams withRandomSeed() {
return new PatternParams(pattern, activeState, interactState, limitedProducer, new Random().nextLong());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

public enum PositionType implements BuildModifier {
ABSOLUTE("absolute"),
RELATIVE("relative"),
RELATIVE_ONCE("relative_once");
RELATIVE("relative");

private final String name;

Expand All @@ -23,15 +22,14 @@ public Text getDisplayName() {

@Override
public boolean isIntermediate() {
return this == RELATIVE_ONCE;
return this == RELATIVE;
}

@Override
public BuildStage getStage() {
return switch (this) {
case ABSOLUTE -> BuildStage.TICK;
case RELATIVE -> BuildStage.INTERACT;
case RELATIVE_ONCE -> BuildStage.UPDATE_CONTEXT;
case RELATIVE -> BuildStage.UPDATE_CONTEXT;
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public TagElement encode(PositionType[] positionTypeList) {

@Override
public PositionType[] validate(PositionType[] value) {
return value != null && value.length == 3 ? value : new PositionType[]{PositionType.RELATIVE_ONCE, PositionType.RELATIVE_ONCE, PositionType.RELATIVE_ONCE};
return value != null && value.length == 3 ? value : new PositionType[]{PositionType.RELATIVE, PositionType.RELATIVE, PositionType.RELATIVE};
}
}

Expand All @@ -131,7 +131,7 @@ public MirrorTransformer decode(TagElement tag) {
tag.asRecord().getUUID(TAG_ID),
tag.asRecord().getText(TAG_NAME),
tag.asRecord().getVector3d(TAG_POSITION),
tag.asRecord().getTag(TAG_POSITION_TYPE, new PositionTypeArrayTagSerializer()),
tag.asRecord().getEnum(TAG_POSITION_TYPE, PositionType.class),
tag.asRecord().getEnum(TAG_AXIS, Axis.class)
);
}
Expand All @@ -142,7 +142,7 @@ public TagElement encode(MirrorTransformer transformer) {
tag.asRecord().putUUID(TAG_ID, transformer.getId());
tag.asRecord().putText(TAG_NAME, transformer.getName());
tag.asRecord().putVector3d(TAG_POSITION, transformer.position());
tag.asRecord().putTag(TAG_POSITION_TYPE, transformer.getPositionType(), new PositionTypeArrayTagSerializer());
tag.asRecord().putEnum(TAG_POSITION_TYPE, transformer.getPositionType());
tag.asRecord().putEnum(TAG_AXIS, transformer.axis());
return tag;
}
Expand All @@ -155,7 +155,7 @@ public MirrorTransformer validate(MirrorTransformer value) {
value.getId() != null ? value.getId() : UUID.randomUUID(),
value.getName() != null ? value.getName() : MirrorTransformer.ZERO_Y.getName(),
value.position() != null ? value.position() : MirrorTransformer.ZERO_Y.position(),
new PositionTypeArrayTagSerializer().validate(value.getPositionType()),
value.positionType() != null ? value.positionType() : MirrorTransformer.ZERO_Y.positionType(),
value.axis() != null ? value.axis() : MirrorTransformer.ZERO_Y.axis()
);
}
Expand All @@ -173,7 +173,7 @@ public RadialTransformer decode(TagElement tag) {
tag.asRecord().getUUID(TAG_ID),
tag.asRecord().getText(TAG_NAME),
tag.asRecord().getVector3d(TAG_POSITION),
tag.asRecord().getTag(TAG_POSITION_TYPE, new PositionTypeArrayTagSerializer()),
tag.asRecord().getEnum(TAG_POSITION_TYPE, PositionType.class),
tag.asRecord().getInt(TAG_SLICE)
);
}
Expand All @@ -184,7 +184,7 @@ public TagElement encode(RadialTransformer transformer) {
tag.asRecord().putUUID(TAG_ID, transformer.getId());
tag.asRecord().putText(TAG_NAME, transformer.getName());
tag.asRecord().putVector3d(TAG_POSITION, transformer.position());
tag.asRecord().putTag(TAG_POSITION_TYPE, transformer.getPositionType(), new PositionTypeArrayTagSerializer());
tag.asRecord().putEnum(TAG_POSITION_TYPE, transformer.getPositionType());
tag.asRecord().putInt(TAG_SLICE, transformer.slices());
return tag;
}
Expand All @@ -197,7 +197,7 @@ public RadialTransformer validate(RadialTransformer value) {
value.getId() != null ? value.getId() : UUID.randomUUID(),
value.getName() != null ? value.getName() : RadialTransformer.ZERO.getName(),
value.position() != null ? value.position() : RadialTransformer.ZERO.position(),
new PositionTypeArrayTagSerializer().validate(value.getPositionType()),
value.positionType() != null ? value.positionType() : RadialTransformer.ZERO.positionType(),
value.slices()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import dev.huskuraft.effortless.api.math.BoundingBox3d;
import dev.huskuraft.effortless.api.math.Vector3d;
import dev.huskuraft.effortless.api.text.Text;
import dev.huskuraft.effortless.building.PositionType;
import dev.huskuraft.effortless.building.pattern.Transformer;
import dev.huskuraft.effortless.building.pattern.Transformers;
import dev.huskuraft.effortless.building.pattern.array.ArrayTransformer;
Expand Down Expand Up @@ -84,18 +83,6 @@ public static Vector3d getVector3d(Config config, String key) {
return new Vector3d(values[0], values[1], values[2]);
}

public static void definePositionType(ConfigSpec configSpec, String key, PositionType[] defaultValue) {
configSpec.define(key, () -> Arrays.stream(defaultValue).map(Enum::name).map(String::toLowerCase).toList(), value -> value instanceof List<?> list && list.size() == defaultValue.length && list.stream().allMatch(value1 -> value1 instanceof String name && isSuccess(() -> PositionType.valueOf(name.toUpperCase(Locale.ROOT)))));
}

public static void setPositionType(Config config, String key, PositionType[] value) {
config.set(key, Arrays.stream(value).map(Enum::name).map(String::toLowerCase).toList());
}

public static PositionType[] getPositionType(Config config, String key) {
return config.<List<String>>get(key).stream().map(String::toUpperCase).map(PositionType::valueOf).toArray(PositionType[]::new);
}

public static <T extends Enum<T>> void setEnum(Config config, String key, T value) {
config.set(key, value.name().toLowerCase(Locale.ROOT));
}
Expand Down Expand Up @@ -231,7 +218,7 @@ public ConfigSpec getSpec(Config config) {
// spec.define(KEY_NAME, () -> getDefault().getName().getString(), String.class::isInstance);
defineEnum(spec, KEY_TYPE, getDefault().getType());
defineVector3d(spec, KEY_POSITION, MirrorTransformer.ZERO_Y.position());
definePositionType(spec, KEY_POSITION_TYPE, getDefault().getPositionType());
defineEnum(spec, KEY_POSITION_TYPE, getDefault().getPositionType());
defineEnum(spec, KEY_AXIS, getDefault().axis());

return spec;
Expand All @@ -249,7 +236,7 @@ public MirrorTransformer deserialize(Config config) {
UUID.fromString(config.get(KEY_ID)),
Text.empty(),
getVector3d(config, KEY_POSITION),
getPositionType(config, KEY_POSITION_TYPE),
getEnum(config, KEY_POSITION_TYPE),
getEnum(config, KEY_AXIS)
);
}
Expand All @@ -261,7 +248,7 @@ public Config serialize(MirrorTransformer transformer) {
// config.set(KEY_NAME, transformer.getName().getString());
setEnum(config, KEY_TYPE, transformer.getType());
setVector3d(config, KEY_POSITION, transformer.position());
setPositionType(config, KEY_POSITION_TYPE, transformer.getPositionType());
setEnum(config, KEY_POSITION_TYPE, transformer.getPositionType());
config.set(KEY_AXIS, transformer.axis().name().toLowerCase(Locale.ROOT));
validate(config);
return config;
Expand All @@ -286,7 +273,7 @@ public ConfigSpec getSpec(Config config) {
// spec.define(KEY_NAME, () -> getDefault().getName().getString(), String.class::isInstance);
defineEnum(spec, KEY_TYPE, getDefault().getType());
defineVector3d(spec, KEY_POSITION, RadialTransformer.ZERO.position());
definePositionType(spec, KEY_POSITION_TYPE, getDefault().getPositionType());
defineEnum(spec, KEY_POSITION_TYPE, getDefault().getPositionType());
spec.defineInRange(KEY_SLICE, getDefault().slices(), RadialTransformer.SLICE_RANGE.min(), RadialTransformer.SLICE_RANGE.max());

return spec;
Expand All @@ -304,7 +291,7 @@ public RadialTransformer deserialize(Config config) {
UUID.fromString(config.get(KEY_ID)),
Text.empty(),
getVector3d(config, KEY_POSITION),
getPositionType(config, KEY_POSITION_TYPE),
getEnum(config, KEY_POSITION_TYPE),
config.get(KEY_SLICE)
);
}
Expand All @@ -316,7 +303,7 @@ public Config serialize(RadialTransformer transformer) {
// config.set(KEY_NAME, transformer.getName().getString());
setEnum(config, KEY_TYPE, transformer.getType());
setVector3d(config, KEY_POSITION, transformer.position());
setPositionType(config, KEY_POSITION_TYPE, transformer.getPositionType());
setEnum(config, KEY_POSITION_TYPE, transformer.getPositionType());
config.set(KEY_SLICE, transformer.slices());
validate(config);
return config;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,7 @@ public BlockBreakOperation(
Storage storage, // for preview
BlockInteraction interaction
) {
this(world, player, context, storage, interaction, EntityState.get(player));
}


public BlockBreakOperation(
World world,
Player player,
Context context,
Storage storage, // for preview
BlockInteraction interaction,
EntityState entityState
) {
super(world, player, context, storage, interaction, world.getBlockState(interaction.getBlockPosition()), entityState);
super(world, player, context, storage, interaction, world.getBlockState(interaction.getBlockPosition()));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ public TransformableOperation getReverseOperation() {
operation.getContext(),
operation.getStorage(),
operation.getInteraction(),
operation.getBlockState(),
operation.getEntityState()
operation.getBlockState()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,7 @@ public BlockInteractOperation(
Storage storage,
BlockInteraction interaction
) {
this(world, player, context, storage, interaction, EntityState.get(player));
}

public BlockInteractOperation(
World world,
Player player,
Context context,
Storage storage,
BlockInteraction interaction,
EntityState entityState
) {
super(world, player, context, storage, interaction, world.getBlockState(interaction.getBlockPosition()), entityState);
super(world, player, context, storage, interaction, world.getBlockState(interaction.getBlockPosition()));
}

@Override
Expand Down
Loading

0 comments on commit e48bf21

Please sign in to comment.