Cycle & DynamicCycle, recipes
This commit is contained in:
@@ -14,7 +14,7 @@ parchment_version = 2024.11.17
|
|||||||
mod_id=mechanicals
|
mod_id=mechanicals
|
||||||
mod_name=Mechanicals Lib
|
mod_name=Mechanicals Lib
|
||||||
mod_license=LGPL3
|
mod_license=LGPL3
|
||||||
mod_version=0.1.4
|
mod_version=0.1.14
|
||||||
mod_group_id=com.oierbravo
|
mod_group_id=com.oierbravo
|
||||||
mod_author=oierbravo
|
mod_author=oierbravo
|
||||||
mod_description=Utility Library for Create Addons.
|
mod_description=Utility Library for Create Addons.
|
||||||
|
|||||||
@@ -179,4 +179,5 @@ public class CycleBehavior extends BlockEntityBehaviour {
|
|||||||
public int getRunningTicks() {
|
public int getRunningTicks() {
|
||||||
return runningTicks;
|
return runningTicks;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,65 +10,53 @@ import net.minecraft.world.level.Level;
|
|||||||
|
|
||||||
public class DynamicCycleBehavior extends BlockEntityBehaviour {
|
public class DynamicCycleBehavior extends BlockEntityBehaviour {
|
||||||
|
|
||||||
|
private int cycleTime;
|
||||||
public static final BehaviourType<DynamicCycleBehavior> TYPE = new BehaviourType<>();
|
public static final BehaviourType<DynamicCycleBehavior> TYPE = new BehaviourType<>();
|
||||||
public DynamicCycleBehaviorSpecifics specifics;
|
public DynamicCycleBehaviorSpecifics specifics;
|
||||||
private int prevRunningTicks;
|
private int prevRunningTicks;
|
||||||
private int runningTicks;
|
private int runningTicks;
|
||||||
private int processingTime;
|
|
||||||
private int currentTime;
|
|
||||||
private boolean running;
|
private boolean running;
|
||||||
private boolean finished;
|
private boolean finished;
|
||||||
|
|
||||||
public interface DynamicCycleBehaviorSpecifics {
|
public interface DynamicCycleBehaviorSpecifics {
|
||||||
|
|
||||||
void onCycleCompleted();
|
void onOperationCompleted();
|
||||||
float getKineticSpeed();
|
float getKineticSpeed();
|
||||||
int getProcessingTime();
|
|
||||||
boolean tryProcess(boolean simulate);
|
boolean tryProcess(boolean simulate);
|
||||||
void playSound();
|
void playCompletionSound();
|
||||||
void setWorking(boolean value);
|
int getProcessingTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T extends SmartBlockEntity & DynamicCycleBehaviorSpecifics> DynamicCycleBehavior(T te) {
|
public <T extends SmartBlockEntity & DynamicCycleBehaviorSpecifics> DynamicCycleBehavior(T te) {
|
||||||
super(te);
|
super(te);
|
||||||
this.specifics = te;
|
this.specifics = te;
|
||||||
processingTime = 0;
|
|
||||||
currentTime = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void read(CompoundTag compound, HolderLookup.Provider registries, boolean clientPacket) {
|
public void read(CompoundTag compound, HolderLookup.Provider registries, boolean clientPacket) {
|
||||||
currentTime = compound.getInt("CurrentTime");
|
|
||||||
processingTime = compound.getInt("ProcessingTime");
|
|
||||||
prevRunningTicks = runningTicks = compound.getInt("Ticks");
|
|
||||||
running = compound.getBoolean("Running");
|
running = compound.getBoolean("Running");
|
||||||
finished = compound.getBoolean("Finished");
|
finished = compound.getBoolean("Finished");
|
||||||
super.read(compound, registries, clientPacket);
|
prevRunningTicks = runningTicks = compound.getInt("Ticks");
|
||||||
|
cycleTime = compound.getInt("CycleTime");
|
||||||
|
super.read(compound,registries, clientPacket);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(CompoundTag compound, HolderLookup.Provider registries, boolean clientPacket) {
|
public void write(CompoundTag compound, HolderLookup.Provider registries, boolean clientPacket) {
|
||||||
compound.putInt("CurrentTime", currentTime);
|
|
||||||
compound.putInt("ProcessingTime", processingTime);
|
|
||||||
compound.putBoolean("Running", running);
|
compound.putBoolean("Running", running);
|
||||||
compound.putBoolean("Finished", finished);
|
compound.putBoolean("Finished", finished);
|
||||||
|
compound.putInt("Ticks", runningTicks);
|
||||||
|
compound.putInt("CycleTime", cycleTime);
|
||||||
super.write(compound, registries, clientPacket);
|
super.write(compound, registries, clientPacket);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void start() {
|
public void start() {
|
||||||
running = true;
|
running = true;
|
||||||
currentTime = 0;
|
prevRunningTicks = 0;
|
||||||
processingTime = specifics.getProcessingTime();
|
runningTicks = 0;
|
||||||
specifics.setWorking(true);
|
cycleTime = specifics.getProcessingTime();
|
||||||
blockEntity.sendData();
|
|
||||||
}
|
|
||||||
public void stop(){
|
|
||||||
running = false;
|
|
||||||
finished = true;
|
|
||||||
currentTime = 0;
|
|
||||||
processingTime = 0;
|
|
||||||
specifics.setWorking(false);
|
|
||||||
blockEntity.sendData();
|
blockEntity.sendData();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -92,57 +80,47 @@ public class DynamicCycleBehavior extends BlockEntityBehaviour {
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(!specifics.tryProcess(true)){
|
|
||||||
running = false;
|
|
||||||
blockEntity.sendData();
|
if (level.isClientSide && runningTicks == -cycleTime) {
|
||||||
return;
|
prevRunningTicks = cycleTime;
|
||||||
}
|
|
||||||
if (level.isClientSide && runningTicks == -processingTime) {
|
|
||||||
prevRunningTicks = currentTime;
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (runningTicks == processingTime && specifics.getKineticSpeed() != 0) {
|
if (runningTicks >= cycleTime && specifics.getKineticSpeed() != 0) {
|
||||||
apply();
|
apply();
|
||||||
specifics.playSound();
|
specifics.playCompletionSound();
|
||||||
if (!level.isClientSide)
|
if (!level.isClientSide)
|
||||||
blockEntity.sendData();
|
blockEntity.sendData();
|
||||||
}
|
}
|
||||||
|
|
||||||
specifics.setWorking(true);
|
if (!level.isClientSide && runningTicks > cycleTime) {
|
||||||
running = true;
|
finished = true;
|
||||||
currentTime += getRunningTickSpeed();
|
running = false;
|
||||||
|
specifics.onOperationCompleted();
|
||||||
|
|
||||||
if (!level.isClientSide && runningTicks > processingTime) {
|
|
||||||
specifics.onCycleCompleted();
|
|
||||||
stop();
|
|
||||||
|
|
||||||
blockEntity.sendData();
|
blockEntity.sendData();
|
||||||
return;
|
return;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
prevRunningTicks = runningTicks;
|
prevRunningTicks = runningTicks;
|
||||||
runningTicks += getRunningTickSpeed();
|
runningTicks += getRunningTickSpeed();
|
||||||
if (prevRunningTicks < processingTime && runningTicks >= processingTime) {
|
if (prevRunningTicks < cycleTime && runningTicks >= cycleTime) {
|
||||||
runningTicks = processingTime / 2;
|
runningTicks = cycleTime;
|
||||||
// Pause the ticks until a packet is received
|
// Pause the ticks until a packet is received
|
||||||
if (level.isClientSide && !blockEntity.isVirtual())
|
if (level.isClientSide && !blockEntity.isVirtual())
|
||||||
runningTicks = -(processingTime / 2);
|
runningTicks = -(cycleTime);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public float getProgress(float partialTicks){
|
public float getProgress(float partialTicks){
|
||||||
|
if (!running)
|
||||||
|
return 0;
|
||||||
int runningTicks = Math.abs(this.runningTicks);
|
int runningTicks = Math.abs(this.runningTicks);
|
||||||
float ticks = Mth.lerp(partialTicks, prevRunningTicks, runningTicks);
|
float ticks = Mth.lerp(partialTicks, prevRunningTicks, runningTicks);
|
||||||
return ticks/ getProccessingTime() * 100;
|
return ticks/ cycleTime * 100;
|
||||||
}
|
|
||||||
public int getProccessingTime(){
|
|
||||||
return processingTime;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected void apply() {
|
protected void apply() {
|
||||||
Level level = getWorld();
|
Level level = getWorld();
|
||||||
|
|
||||||
@@ -157,32 +135,39 @@ public class DynamicCycleBehavior extends BlockEntityBehaviour {
|
|||||||
float speed = specifics.getKineticSpeed();
|
float speed = specifics.getKineticSpeed();
|
||||||
if (speed == 0)
|
if (speed == 0)
|
||||||
return 0;
|
return 0;
|
||||||
return (int) Mth.lerp(Mth.clamp(Math.abs(speed) / 512f, 0, 1), 1, 30);
|
return (int) Mth.lerp(Mth.clamp(Math.abs(speed) / 512f, 0, 1), 1, 60);
|
||||||
}
|
}
|
||||||
|
public boolean isRunning(){
|
||||||
|
return running;
|
||||||
|
}
|
||||||
|
public int getTotalProgressPercent() {
|
||||||
|
return Mth.clamp(runningTicks * 100 / cycleTime, 0,100);
|
||||||
|
}
|
||||||
|
public int getCycleTime(){
|
||||||
|
return cycleTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPrevRunningTicks() {
|
||||||
|
return prevRunningTicks;
|
||||||
|
}
|
||||||
|
public int getRunningTicks() {
|
||||||
|
return runningTicks;
|
||||||
|
}
|
||||||
|
|
||||||
public int getProgressPercent() {
|
public int getProgressPercent() {
|
||||||
if(!running)
|
if(!running)
|
||||||
return 0;
|
return 0;
|
||||||
return Mth.clamp(runningTicks * 100 / (getProccessingTime()), 0,100);
|
return Mth.clamp(runningTicks * 100 / (getCycleTime()), 0,100);
|
||||||
}
|
}
|
||||||
public float getProgressPercentFloat() {
|
public float getProgressPercentFloat() {
|
||||||
if(!running)
|
if(!running)
|
||||||
return 0;
|
return 0;
|
||||||
return (float) runningTicks / getProccessingTime();
|
return (float) runningTicks / getCycleTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
public float getProcessingRemainingPercentFloat() {
|
public float getProcessingRemainingPercentFloat() {
|
||||||
if(!running)
|
if(!running)
|
||||||
return 1;
|
return 1;
|
||||||
return 1 - (float) (processingTime - runningTicks) / processingTime;
|
return 1 - (float) (getCycleTime() - runningTicks) / getCycleTime();
|
||||||
}
|
|
||||||
public int getCurrentTime(){
|
|
||||||
return runningTicks;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isRunning(){
|
|
||||||
return running;
|
|
||||||
}
|
|
||||||
public boolean isFinished(){
|
|
||||||
return finished;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ public class RecipeRequirementsBehaviour<R extends IRecipeWithRequirements> exte
|
|||||||
return !missingRequirements.isEmpty();
|
return !missingRequirements.isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> boolean checkRequirements(R pRecipe) {
|
public boolean checkRequirements(R pRecipe) {
|
||||||
missingRequirements = new ArrayList<>();
|
missingRequirements = new ArrayList<>();
|
||||||
|
|
||||||
if(!specifics.matchesIngredients(pRecipe)){
|
if(!specifics.matchesIngredients(pRecipe)){
|
||||||
|
|||||||
@@ -21,9 +21,9 @@ public abstract class AbstractMechanicalRecipe<T extends RecipeInput, P extends
|
|||||||
protected ArrayList<ICondition> conditions;
|
protected ArrayList<ICondition> conditions;
|
||||||
|
|
||||||
public AbstractMechanicalRecipe(P params){
|
public AbstractMechanicalRecipe(P params){
|
||||||
this.id = params.id;
|
id = params.id;
|
||||||
recipeRequirements = params.recipeRequirements;
|
recipeRequirements = params.recipeRequirements;
|
||||||
this.conditions = params.conditions;
|
conditions = params.conditions;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<ICondition> getConditions(){
|
public List<ICondition> getConditions(){
|
||||||
|
|||||||
@@ -19,14 +19,9 @@ import java.util.function.Consumer;
|
|||||||
|
|
||||||
public abstract class AbstractMechanicalRecipeBuilder<R extends AbstractMechanicalRecipe<?,P>, P extends AbstractMechanicalRecipeParams, BRB extends AbstractMechanicalRecipeBuilder<R,P,?>> {
|
public abstract class AbstractMechanicalRecipeBuilder<R extends AbstractMechanicalRecipe<?,P>, P extends AbstractMechanicalRecipeParams, BRB extends AbstractMechanicalRecipeBuilder<R,P,?>> {
|
||||||
protected final Map<String, Criterion<?>> criteria = new LinkedHashMap<>();
|
protected final Map<String, Criterion<?>> criteria = new LinkedHashMap<>();
|
||||||
|
|
||||||
protected P params;
|
protected P params;
|
||||||
protected ArrayList<IRecipeRequirement> recipeRequirements;
|
|
||||||
protected ArrayList<ICondition> recipeConditions;
|
|
||||||
|
|
||||||
public AbstractMechanicalRecipeBuilder(){
|
public AbstractMechanicalRecipeBuilder(){
|
||||||
recipeRequirements = new ArrayList<>();
|
|
||||||
recipeConditions = new ArrayList<>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract R build();
|
public abstract R build();
|
||||||
@@ -44,15 +39,19 @@ public abstract class AbstractMechanicalRecipeBuilder<R extends AbstractMechanic
|
|||||||
public BRB whenModLoaded(String modid) {
|
public BRB whenModLoaded(String modid) {
|
||||||
return withCondition(new ModLoadedCondition(modid));
|
return withCondition(new ModLoadedCondition(modid));
|
||||||
}
|
}
|
||||||
|
|
||||||
public BRB whenModMissing(String modid) {
|
public BRB whenModMissing(String modid) {
|
||||||
return withCondition(new NotCondition(new ModLoadedCondition(modid)));
|
return withCondition(new NotCondition(new ModLoadedCondition(modid)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public BRB withCondition(ICondition condition) {
|
public BRB withCondition(ICondition condition) {
|
||||||
recipeConditions.add(condition);
|
params.conditions.add(condition);
|
||||||
return (BRB) this;
|
return (BRB) this;
|
||||||
}
|
}
|
||||||
|
public BRB withConditions(List<ICondition> conditions) {
|
||||||
|
params.conditions.addAll(conditions);
|
||||||
|
return (BRB) this;
|
||||||
|
}
|
||||||
|
|
||||||
public void save(RecipeOutput recipeOutput, ResourceLocation resourceLocation) {
|
public void save(RecipeOutput recipeOutput, ResourceLocation resourceLocation) {
|
||||||
Advancement.Builder advancement = recipeOutput.advancement()
|
Advancement.Builder advancement = recipeOutput.advancement()
|
||||||
.addCriterion("has_the_recipe", RecipeUnlockedTrigger.unlocked(resourceLocation))
|
.addCriterion("has_the_recipe", RecipeUnlockedTrigger.unlocked(resourceLocation))
|
||||||
@@ -63,10 +62,25 @@ public abstract class AbstractMechanicalRecipeBuilder<R extends AbstractMechanic
|
|||||||
recipeOutput.accept(resourceLocation, build(), advancement.build(params.id.withPrefix("recipes/")));
|
recipeOutput.accept(resourceLocation, build(), advancement.build(params.id.withPrefix("recipes/")));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void saveCompat(RecipeOutput recipeOutput, ResourceLocation resourceLocation) {
|
||||||
|
Advancement.Builder advancement = recipeOutput.advancement()
|
||||||
|
.addCriterion("has_the_recipe", RecipeUnlockedTrigger.unlocked(resourceLocation))
|
||||||
|
.rewards(AdvancementRewards.Builder.recipe(resourceLocation))
|
||||||
|
.requirements(AdvancementRequirements.Strategy.OR);
|
||||||
|
this.criteria.forEach(advancement::addCriterion);
|
||||||
|
|
||||||
|
recipeOutput.accept(resourceLocation, build(), advancement.build(params.id.withPrefix("recipes/")));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public void save(RecipeOutput recipeOutput) {
|
public void save(RecipeOutput recipeOutput) {
|
||||||
save(recipeOutput, params.id);
|
save(recipeOutput, params.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void saveCompat(RecipeOutput recipeOutput) {
|
||||||
|
saveCompat(recipeOutput, params.id);
|
||||||
|
}
|
||||||
|
|
||||||
public BRB with(Consumer<BRB> consummer){
|
public BRB with(Consumer<BRB> consummer){
|
||||||
consummer.accept((BRB) this);
|
consummer.accept((BRB) this);
|
||||||
return (BRB) this;
|
return (BRB) this;
|
||||||
|
|||||||
@@ -8,11 +8,11 @@ import java.util.ArrayList;
|
|||||||
public abstract class AbstractMechanicalRecipeParams {
|
public abstract class AbstractMechanicalRecipeParams {
|
||||||
public ResourceLocation id;
|
public ResourceLocation id;
|
||||||
public ArrayList<IRecipeRequirement> recipeRequirements;
|
public ArrayList<IRecipeRequirement> recipeRequirements;
|
||||||
protected ArrayList<ICondition> conditions;
|
public ArrayList<ICondition> conditions;
|
||||||
|
|
||||||
protected AbstractMechanicalRecipeParams(ResourceLocation id) {
|
protected AbstractMechanicalRecipeParams(ResourceLocation id) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
recipeRequirements = new ArrayList<>();
|
this.recipeRequirements = new ArrayList<>();
|
||||||
conditions = new ArrayList<>();
|
this.conditions = new ArrayList<>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user