forked from TheBrokenRail/EnergonRelics
79 lines
2.2 KiB
Java
79 lines
2.2 KiB
Java
package com.thebrokenrail.energonrelics.registry.infuser.data;
|
|
|
|
import com.thebrokenrail.energonrelics.util.WeightedList;
|
|
import net.fabricmc.api.EnvType;
|
|
import net.fabricmc.api.Environment;
|
|
import net.minecraft.util.math.BlockPos;
|
|
import net.minecraft.world.World;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* Infuser Registry Entry
|
|
*/
|
|
public class InfuserEntry {
|
|
/**
|
|
* Energy Cost
|
|
*/
|
|
public final long cost;
|
|
/**
|
|
* Success Chance
|
|
*/
|
|
public final double successChance;
|
|
private final InfuserAction[] success;
|
|
private final InfuserAction[] fail;
|
|
|
|
/**
|
|
* Create Infuser Registry Entry
|
|
* @param cost Energy Cost
|
|
* @param successChance Success Chance
|
|
* @param success Success Actions
|
|
* @param fail Failure Actions
|
|
*/
|
|
public InfuserEntry(long cost, double successChance, InfuserAction[] success, InfuserAction[] fail) {
|
|
this.cost = cost;
|
|
this.successChance = successChance;
|
|
this.success = success;
|
|
this.fail = fail;
|
|
}
|
|
|
|
/**
|
|
* Pick Action And Run It
|
|
* @param world World
|
|
* @param pos Infuser Position
|
|
*/
|
|
public void run(World world, BlockPos pos) {
|
|
boolean isSuccess = world.random.nextDouble() <= successChance;
|
|
|
|
InfuserAction[] list;
|
|
if (isSuccess) {
|
|
list = success;
|
|
} else {
|
|
list = fail;
|
|
}
|
|
|
|
WeightedList<InfuserAction> weightedList = new WeightedList<>();
|
|
for (InfuserAction action : list) {
|
|
weightedList.add(1, action);
|
|
}
|
|
|
|
weightedList.pick(world.random).run(world, pos);
|
|
}
|
|
|
|
@Environment(EnvType.CLIENT)
|
|
public List<InfuserOutputItem> getOutputItems(boolean isFail) {
|
|
InfuserAction[] actionList = isFail ? fail : success;
|
|
List<InfuserOutputItem> list = new ArrayList<>();
|
|
for (InfuserAction action : actionList) {
|
|
list.add(action.display());
|
|
}
|
|
return list;
|
|
}
|
|
|
|
@Environment(EnvType.CLIENT)
|
|
public double getSingleChance(boolean isFail) {
|
|
return (1d / (double) (isFail ? fail.length : success.length)) * (isFail ? 1d - successChance : successChance);
|
|
}
|
|
}
|