EnergonRelics/src/main/java/com/thebrokenrail/energonrelics/registry/infuser/data/InfuserEntry.java

79 lines
2.2 KiB
Java
Raw Normal View History

2020-08-04 17:06:11 +00:00
package com.thebrokenrail.energonrelics.registry.infuser.data;
2020-07-30 02:51:29 +00:00
import com.thebrokenrail.energonrelics.util.WeightedList;
2020-08-03 17:00:08 +00:00
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
2020-07-30 02:51:29 +00:00
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
2020-07-30 19:49:18 +00:00
import java.util.ArrayList;
import java.util.List;
2020-08-04 17:06:11 +00:00
/**
* Infuser Registry Entry
*/
2020-07-30 19:49:18 +00:00
public class InfuserEntry {
2020-08-04 17:06:11 +00:00
/**
* Energy Cost
*/
2020-07-30 02:51:29 +00:00
public final long cost;
2020-08-04 17:06:11 +00:00
/**
* Success Chance
*/
2020-07-30 02:51:29 +00:00
public final double successChance;
2020-07-30 19:49:18 +00:00
private final InfuserAction[] success;
private final InfuserAction[] fail;
2020-07-30 02:51:29 +00:00
2020-08-04 17:06:11 +00:00
/**
* 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) {
2020-07-30 02:51:29 +00:00
this.cost = cost;
this.successChance = successChance;
this.success = success;
this.fail = fail;
}
2020-08-04 17:06:11 +00:00
/**
* Pick Action And Run It
* @param world World
* @param pos Infuser Position
*/
public void run(World world, BlockPos pos) {
2020-07-30 02:51:29 +00:00
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);
}
2020-07-30 19:49:18 +00:00
2020-08-03 17:00:08 +00:00
@Environment(EnvType.CLIENT)
public List<InfuserOutputItem> getOutputItems(boolean isFail) {
2020-07-30 19:49:18 +00:00
InfuserAction[] actionList = isFail ? fail : success;
2020-08-03 17:00:08 +00:00
List<InfuserOutputItem> list = new ArrayList<>();
2020-07-30 19:49:18 +00:00
for (InfuserAction action : actionList) {
2020-08-03 17:00:08 +00:00
list.add(action.display());
2020-07-30 19:49:18 +00:00
}
return list;
}
2020-08-03 17:00:08 +00:00
@Environment(EnvType.CLIENT)
2020-07-30 19:49:18 +00:00
public double getSingleChance(boolean isFail) {
2020-08-03 17:00:08 +00:00
return (1d / (double) (isFail ? fail.length : success.length)) * (isFail ? 1d - successChance : successChance);
2020-07-30 19:49:18 +00:00
}
2020-07-30 02:51:29 +00:00
}