package com.hypixel.hytale.builtin.buildertools.scriptedbrushes.operations.sequential.flowcontrol.loops;
import com.hypixel.hytale.builtin.buildertools.scriptedbrushes.BrushConfig;
import com.hypixel.hytale.builtin.buildertools.scriptedbrushes.BrushConfigCommandExecutor;
import com.hypixel.hytale.builtin.buildertools.scriptedbrushes.operations.system.SequenceBrushOperation;
import com.hypixel.hytale.codec.Codec;
import com.hypixel.hytale.codec.KeyedCodec;
import com.hypixel.hytale.codec.builder.BuilderCodec;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.core.codec.PairCodec;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import it.unimi.dsi.fastutil.Pair;
import java.util.concurrent.ThreadLocalRandom;
import javax.annotation.Nonnull;
publicclassLoopRandomOperationextendsSequenceBrushOperation {
publicstaticfinalintMAX_REPETITIONS=100;
publicstaticfinalintIDLE_STATE= -1;
publicstaticfinal BuilderCodec<LoopRandomOperation> CODEC;
@NonnullpublicStringindexNameArg="Undefined";
@Nonnullpublic Pair<Integer, Integer> repetitionsArg = Pair.<Integer, Integer>of(1, 1);
privateintrepetitionsRemaining= -1;
publicLoopRandomOperation() {
super("Loop Operations Random Amount", "Loop the execution of instructions a random amount of times", false);
}
publicvoidresetInternalState() {
this.repetitionsRemaining = -1;
}
publicvoidmodifyBrushConfig(@Nonnull Ref<EntityStore> ref, @Nonnull BrushConfig brushConfig, @Nonnull BrushConfigCommandExecutor brushConfigCommandExecutor, @Nonnull ComponentAccessor<EntityStore> componentAccessor) {
if (this.repetitionsRemaining == -1) {
intrepetitions=this.randomlyChooseRepetitionsAmount();
if (repetitions > 100) {
brushConfig.setErrorFlag("Cannot have more than 100 repetitions");
return;
}
this.repetitionsRemaining = repetitions;
}
if (this.repetitionsRemaining == 0) {
this.repetitionsRemaining = -1;
} else {
--this.repetitionsRemaining;
brushConfigCommandExecutor.loadOperatingIndex(this.indexNameArg, false);
}
}
privateintrandomlyChooseRepetitionsAmount() {
return ((Integer)this.repetitionsArg.left()).equals(this.repetitionsArg.right()) ? (Integer)this.repetitionsArg.left() : ThreadLocalRandom.current().nextInt((Integer)this.repetitionsArg.left(), (Integer)this.repetitionsArg.right() + 1);
}
static {
CODEC = ((BuilderCodec.Builder)((BuilderCodec.Builder)((BuilderCodec.Builder)BuilderCodec.builder(LoopRandomOperation.class, LoopRandomOperation::new).append(newKeyedCodec("StoredIndexName", Codec.STRING), (op, val) -> op.indexNameArg = val, (op) -> op.indexNameArg).documentation("The name of the previously stored index to begin the loop at. Note: This can only be an index previous to the current.").add()).append(newKeyedCodec("RangeOfAdditionalRepetitions", PairCodec.IntegerPair.CODEC), (op, val) -> op.repetitionsArg = val.toPair(), (op) -> PairCodec.IntegerPair.fromPair(op.repetitionsArg)).documentation("The minimum and maximum of a range, randomly choosing the amount of additional times to repeat the loop after the initial, normal execution").add()).documentation("Loop the execution of instructions a random amount of times")).build();
}
}