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/api/energy/Action.java

105 lines
3.2 KiB
Java
Raw Normal View History

2020-08-04 17:06:11 +00:00
package com.thebrokenrail.energonrelics.api.energy;
2020-07-13 20:37:21 +00:00
import net.minecraft.block.BlockState;
import net.minecraft.state.property.Property;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
2020-08-04 17:06:11 +00:00
/**
* Action That Can Be Performed With Energy
*/
2020-07-13 20:37:21 +00:00
public class Action {
public interface ActionFunction {
void run(World world, BlockPos pos, BlockState state);
}
private final long cost;
private final ActionFunction success;
private final ActionFunction fail;
2020-08-04 17:06:11 +00:00
/**
* Create Action
* @param cost Cost In Energon
* @param success Success Function
* @param fail Failure Function
*/
2020-07-24 18:02:42 +00:00
public Action(long cost, ActionFunction success, ActionFunction fail) {
2020-07-13 20:37:21 +00:00
this.cost = cost;
this.success = success;
this.fail = fail;
}
2020-08-04 17:06:11 +00:00
/**
* Create Action That Changes Block State Property
* @param cost Cost In Energon
* @param property Block State Property
* @param successValue Value To Set On Success
* @param failureValue Value To Set On Failure
* @param <T> Block State Property Type
* @return New Action
*/
2020-07-24 18:02:42 +00:00
public static <T extends Comparable<T>> Action createBlockStatePropertyAction(long cost, Property<T> property, T successValue, T failureValue) {
2020-07-13 20:37:21 +00:00
return new Action(cost, (world, pos, state) -> {
2020-07-26 16:33:37 +00:00
if (state.contains(property) && !state.get(property).equals(successValue)) {
2020-07-13 20:37:21 +00:00
world.setBlockState(pos, state.with(property, successValue));
}
}, (world, pos, state) -> {
2020-07-26 16:33:37 +00:00
if (state.contains(property) && !state.get(property).equals(failureValue)) {
2020-07-13 20:37:21 +00:00
world.setBlockState(pos, state.with(property, failureValue));
}
2020-07-24 18:02:42 +00:00
});
2020-07-13 20:37:21 +00:00
}
2020-08-04 17:06:11 +00:00
/**
* Action That Has Been Propagated
*/
2020-07-13 20:37:21 +00:00
public interface PropagatedAction {
void expandPayments(int amount);
long amountOwed();
void pay(long amount);
}
2020-07-24 18:02:42 +00:00
public static class PropagatedActionImpl implements PropagatedAction {
2020-07-13 20:37:21 +00:00
private final Action action;
private final World world;
private final BlockPos pos;
2020-07-27 18:06:46 +00:00
private final BlockState state;
2020-07-13 20:37:21 +00:00
private int expectedPayments = 1;
private int payments = 0;
private long amountPaid = 0;
2020-07-27 18:06:46 +00:00
public PropagatedActionImpl(Action action, World world, BlockPos pos, BlockState state) {
2020-07-13 20:37:21 +00:00
this.action = action;
this.world = world;
this.pos = pos;
2020-07-27 18:06:46 +00:00
this.state = state;
2020-07-13 20:37:21 +00:00
}
@Override
public void expandPayments(int amount) {
if (amount < 1) {
throw new UnsupportedOperationException();
}
expectedPayments = expectedPayments - 1 + amount;
}
@Override
public long amountOwed() {
return action.cost - amountPaid;
}
@Override
public void pay(long amount) {
amountPaid = amountPaid + amount;
payments++;
2020-07-27 18:06:46 +00:00
if (state != null) {
2020-07-13 20:37:21 +00:00
if (amountOwed() <= 0) {
action.success.run(world, pos, state);
} else if (payments >= expectedPayments) {
action.fail.run(world, pos, state);
}
}
}
}
}