Move capability registry
This commit is contained in:
parent
7e57fefcaa
commit
9e537f464a
@ -30,6 +30,7 @@ import ru.dbotthepony.mc.otm.block.BlockAndroidStation;
|
||||
import ru.dbotthepony.mc.otm.block.entity.BlockEntityAndroidStation;
|
||||
import ru.dbotthepony.mc.otm.capability.AndroidCapability;
|
||||
import ru.dbotthepony.mc.otm.capability.IAndroidCapability;
|
||||
import ru.dbotthepony.mc.otm.capability.MatteryCapability;
|
||||
import ru.dbotthepony.mc.otm.menu.AndroidStationMenu;
|
||||
import ru.dbotthepony.mc.otm.screen.AndroidStationScreen;
|
||||
|
||||
@ -104,7 +105,7 @@ public class OverdriveThatMatters {
|
||||
@SubscribeEvent
|
||||
public void onLivingTick(LivingEvent.LivingUpdateEvent event) {
|
||||
if (event.getEntity() instanceof ServerPlayer ply) {
|
||||
LazyOptional<IAndroidCapability> capability = ply.getCapability(AndroidCapability.CAPABILITY);
|
||||
LazyOptional<IAndroidCapability> capability = ply.getCapability(MatteryCapability.ANDROID);
|
||||
|
||||
if (!capability.isPresent()) {
|
||||
return;
|
||||
|
@ -18,9 +18,8 @@ import net.minecraftforge.common.capabilities.Capability;
|
||||
import net.minecraftforge.common.util.LazyOptional;
|
||||
import net.minecraftforge.energy.CapabilityEnergy;
|
||||
import ru.dbotthepony.mc.otm.OverdriveThatMatters;
|
||||
import ru.dbotthepony.mc.otm.capability.AndroidCapability;
|
||||
import ru.dbotthepony.mc.otm.capability.IAndroidCapability;
|
||||
import ru.dbotthepony.mc.otm.capability.MatteryEnergyCapability;
|
||||
import ru.dbotthepony.mc.otm.capability.MatteryCapability;
|
||||
import ru.dbotthepony.mc.otm.capability.MatteryMachineEnergyStorage;
|
||||
import ru.dbotthepony.mc.otm.menu.AndroidStationMenu;
|
||||
|
||||
@ -50,7 +49,7 @@ public class BlockEntityAndroidStation extends BlockEntity implements MenuProvid
|
||||
|
||||
@Nonnull
|
||||
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
|
||||
if (cap == MatteryEnergyCapability.CAPABILITY || cap == CapabilityEnergy.ENERGY) {
|
||||
if (cap == MatteryCapability.ENERGY || cap == CapabilityEnergy.ENERGY) {
|
||||
return LazyOptional.of(() -> energy).cast();
|
||||
}
|
||||
|
||||
@ -81,7 +80,7 @@ public class BlockEntityAndroidStation extends BlockEntity implements MenuProvid
|
||||
List<LivingEntity> entities = getLevel().getEntitiesOfClass(LivingEntity.class, new AABB(pos.getX(), pos.getY(), pos.getZ(), pos.getX() + 1, pos.getY() + 2, pos.getZ() + 1));
|
||||
|
||||
for (LivingEntity ent : entities) {
|
||||
LazyOptional<IAndroidCapability> resolver = ent.getCapability(AndroidCapability.CAPABILITY);
|
||||
LazyOptional<IAndroidCapability> resolver = ent.getCapability(MatteryCapability.ANDROID);
|
||||
|
||||
if (resolver.isPresent()) {
|
||||
IAndroidCapability capability = resolver.resolve().get();
|
||||
|
@ -1,17 +1,11 @@
|
||||
package ru.dbotthepony.mc.otm.capability;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import net.minecraft.core.Direction;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.LongTag;
|
||||
import net.minecraft.nbt.Tag;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.world.entity.LivingEntity;
|
||||
import net.minecraft.world.food.FoodData;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraft.world.phys.shapes.VoxelShape;
|
||||
import net.minecraftforge.common.capabilities.Capability;
|
||||
import net.minecraftforge.common.capabilities.CapabilityInject;
|
||||
import net.minecraftforge.common.capabilities.ICapabilityProvider;
|
||||
import net.minecraftforge.common.util.INBTSerializable;
|
||||
import net.minecraftforge.common.util.LazyOptional;
|
||||
@ -20,7 +14,6 @@ import ru.dbotthepony.mc.otm.OverdriveThatMatters;
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class AndroidCapability implements ICapabilityProvider, IAndroidCapability, INBTSerializable<CompoundTag> {
|
||||
private final LivingEntity ent;
|
||||
@ -33,9 +26,6 @@ public class AndroidCapability implements ICapabilityProvider, IAndroidCapabilit
|
||||
this.ent = ent;
|
||||
}
|
||||
|
||||
@CapabilityInject(IAndroidCapability.class)
|
||||
public static Capability<IAndroidCapability> CAPABILITY = null;
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public CompoundTag serializeNBT() {
|
||||
@ -107,7 +97,7 @@ public class AndroidCapability implements ICapabilityProvider, IAndroidCapabilit
|
||||
@Nonnull
|
||||
@Override
|
||||
public BigDecimal extractEnergyInner(BigDecimal howMuch, boolean simulate) {
|
||||
BigDecimal new_energy = energy_stored.subtract(howMuch, MatteryEnergyCapability.ROUND_RULES).max(BigDecimal.ZERO);
|
||||
BigDecimal new_energy = energy_stored.subtract(howMuch, MatteryCapability.ROUND_RULES).max(BigDecimal.ZERO);
|
||||
BigDecimal diff = energy_stored.subtract(new_energy);
|
||||
|
||||
if (!simulate) {
|
||||
@ -120,7 +110,7 @@ public class AndroidCapability implements ICapabilityProvider, IAndroidCapabilit
|
||||
@Nonnull
|
||||
@Override
|
||||
public BigDecimal receiveEnergyOuter(BigDecimal howMuch, boolean simulate) {
|
||||
BigDecimal new_energy = energy_stored.add(howMuch, MatteryEnergyCapability.ROUND_RULES).min(energy_stored_max);
|
||||
BigDecimal new_energy = energy_stored.add(howMuch, MatteryCapability.ROUND_RULES).min(energy_stored_max);
|
||||
BigDecimal diff = new_energy.subtract(energy_stored);
|
||||
|
||||
if (!simulate) {
|
||||
@ -156,7 +146,7 @@ public class AndroidCapability implements ICapabilityProvider, IAndroidCapabilit
|
||||
@Nonnull
|
||||
@Override
|
||||
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
|
||||
if (cap == AndroidCapability.CAPABILITY) {
|
||||
if (cap == MatteryCapability.ANDROID) {
|
||||
return LazyOptional.of(() -> this).cast();
|
||||
}
|
||||
|
||||
|
@ -1,14 +1,9 @@
|
||||
package ru.dbotthepony.mc.otm.capability;
|
||||
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraftforge.common.util.LazyOptional;
|
||||
import net.minecraftforge.energy.CapabilityEnergy;
|
||||
import net.minecraftforge.energy.IEnergyStorage;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
|
||||
// IEnergyStorage for direct compat with Forge Energy
|
||||
public interface IMatteryEnergyStorage extends IEnergyStorage {
|
||||
@ -77,7 +72,7 @@ public interface IMatteryEnergyStorage extends IEnergyStorage {
|
||||
BigDecimal getMaxBatteryLevel();
|
||||
|
||||
default BigDecimal getMissingPower() {
|
||||
return getMaxBatteryLevel().subtract(getBatteryLevel(), MatteryEnergyCapability.ROUND_RULES);
|
||||
return getMaxBatteryLevel().subtract(getBatteryLevel(), MatteryCapability.ROUND_RULES);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -90,7 +85,7 @@ public interface IMatteryEnergyStorage extends IEnergyStorage {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return receiveEnergyOuter(received.round(MatteryEnergyCapability.ROUND_RULES), simulate).intValue();
|
||||
return receiveEnergyOuter(received.round(MatteryCapability.ROUND_RULES), simulate).intValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -103,14 +98,14 @@ public interface IMatteryEnergyStorage extends IEnergyStorage {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return extractEnergyOuter(extracted.round(MatteryEnergyCapability.ROUND_RULES), simulate).intValue();
|
||||
return extractEnergyOuter(extracted.round(MatteryCapability.ROUND_RULES), simulate).intValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
default int getEnergyStored() {
|
||||
BigDecimal level = getBatteryLevel();
|
||||
|
||||
if (level.compareTo(MatteryEnergyCapability.INT_MAX_VALUE) == -1) {
|
||||
if (level.compareTo(MatteryCapability.INT_MAX_VALUE) == -1) {
|
||||
return level.intValue();
|
||||
}
|
||||
|
||||
@ -121,7 +116,7 @@ public interface IMatteryEnergyStorage extends IEnergyStorage {
|
||||
default int getMaxEnergyStored() {
|
||||
BigDecimal level = getMaxBatteryLevel();
|
||||
|
||||
if (level.compareTo(MatteryEnergyCapability.INT_MAX_VALUE) == -1) {
|
||||
if (level.compareTo(MatteryCapability.INT_MAX_VALUE) == -1) {
|
||||
return level.intValue();
|
||||
}
|
||||
|
||||
|
@ -7,9 +7,12 @@ import java.math.BigDecimal;
|
||||
import java.math.MathContext;
|
||||
import java.math.RoundingMode;
|
||||
|
||||
public class MatteryEnergyCapability {
|
||||
public class MatteryCapability {
|
||||
@CapabilityInject(IMatteryEnergyStorage.class)
|
||||
public static Capability<IMatteryEnergyStorage> CAPABILITY = null;
|
||||
public static Capability<IMatteryEnergyStorage> ENERGY = null;
|
||||
|
||||
@CapabilityInject(IAndroidCapability.class)
|
||||
public static Capability<IAndroidCapability> ANDROID = null;
|
||||
|
||||
public static final MathContext ROUND_RULES = new MathContext(0, RoundingMode.HALF_UP);
|
||||
|
@ -2,7 +2,6 @@ package ru.dbotthepony.mc.otm.capability;
|
||||
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraftforge.common.util.INBTSerializable;
|
||||
import ru.dbotthepony.mc.otm.OverdriveThatMatters;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.math.BigDecimal;
|
||||
@ -52,7 +51,7 @@ public class MatteryMachineEnergyStorage implements IMatteryEnergyStorage, INBTS
|
||||
@Nonnull
|
||||
@Override
|
||||
public BigDecimal extractEnergyInner(BigDecimal howMuch, boolean simulate) {
|
||||
BigDecimal new_energy = energy_stored.subtract(howMuch.min(max_output), MatteryEnergyCapability.ROUND_RULES).max(BigDecimal.ZERO);
|
||||
BigDecimal new_energy = energy_stored.subtract(howMuch.min(max_output), MatteryCapability.ROUND_RULES).max(BigDecimal.ZERO);
|
||||
BigDecimal diff = energy_stored.subtract(new_energy);
|
||||
|
||||
if (!simulate) {
|
||||
@ -75,7 +74,7 @@ public class MatteryMachineEnergyStorage implements IMatteryEnergyStorage, INBTS
|
||||
@Nonnull
|
||||
@Override
|
||||
public BigDecimal receiveEnergyInner(BigDecimal howMuch, boolean simulate) {
|
||||
BigDecimal new_energy = energy_stored.add(howMuch.min(max_input), MatteryEnergyCapability.ROUND_RULES).min(energy_stored_max);
|
||||
BigDecimal new_energy = energy_stored.add(howMuch.min(max_input), MatteryCapability.ROUND_RULES).min(energy_stored_max);
|
||||
BigDecimal diff = new_energy.subtract(energy_stored);
|
||||
|
||||
if (!simulate) {
|
||||
|
Loading…
Reference in New Issue
Block a user