This repository has been archived on 2023-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
EnergonRelics/src/main/java/com/thebrokenrail/energonrelics/block/entity/infuser/InfuserEntry.java

58 lines
1.7 KiB
Java
Raw Normal View History

2020-07-30 02:51:29 +00:00
package com.thebrokenrail.energonrelics.block.entity.infuser;
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;
public class InfuserEntry {
2020-07-30 02:51:29 +00:00
public final long cost;
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
InfuserEntry(long cost, double successChance, InfuserAction[] success, InfuserAction[] fail) {
this.cost = cost;
this.successChance = successChance;
this.success = success;
this.fail = fail;
}
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);
}
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
}