cd4017be.lib.jvm_utils.FieldWrapper

Here are the examples of the java api cd4017be.lib.jvm_utils.FieldWrapper taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

1 Examples 7

16 Source : DraconicFusionSensor.java
with MIT License
from CD4017BE

/**
 * @author CD4017BE
 */
public clreplaced DraconicFusionSensor implements IBlockSensor, INBTSerializable<NBTBase> {

    private static final Clreplaced<?> C_REACTOR_COMP = getClreplacedOrNull("com.brandon3055.draconicevolution.blocks.reactor.tileenreplacedy.TileReactorComponent"), C_REACTOR_CORE = getClreplacedOrNull("com.brandon3055.draconicevolution.blocks.reactor.tileenreplacedy.TileReactorCore"), C_MANAGED_ENUM = getClreplacedOrNull("com.brandon3055.brandonscore.lib.datamanager.ManagedEnum"), C_MANAGED_DOUBLE = getClreplacedOrNull("com.brandon3055.brandonscore.lib.datamanager.ManagedDouble"), C_MANAGED_INT = getClreplacedOrNull("com.brandon3055.brandonscore.lib.datamanager.ManagedInt");

    private static final Method M_GET_CORE = getMethodOrNull(C_REACTOR_COMP, "tryGetCore");

    private static final Field F_RS_MODE = getFieldOrNull(C_REACTOR_COMP, "rsMode"), F_REACTABLE_FUEL = getFieldOrNull(C_REACTOR_CORE, "reactableFuel"), F_CONVERTED_FUEL = getFieldOrNull(C_REACTOR_CORE, "convertedFuel"), F_TEMPERATURE = getFieldOrNull(C_REACTOR_CORE, "temperature"), F_SHIELD = getFieldOrNull(C_REACTOR_CORE, "shieldCharge"), F_MAX_SHIELD = getFieldOrNull(C_REACTOR_CORE, "maxShieldCharge"), F_SATURATION = getFieldOrNull(C_REACTOR_CORE, "saturation"), F_MAX_SATURATION = getFieldOrNull(C_REACTOR_CORE, "maxSaturation"), F_ENUM_VALUE = getFieldOrNull(C_MANAGED_ENUM, "value"), F_DOUBLE_VALUE = getFieldOrNull(C_MANAGED_DOUBLE, "value"), F_INT_VALUE = getFieldOrNull(C_MANAGED_INT, "value");

    public static final ResourceLocation MODEL = new ResourceLocation(Main.ID, "block/_sensor.draconic()");

    private static final double PRECISION = 1000D;

    public static boolean INVALID_API;

    static {
        if (F_RS_MODE == null || F_REACTABLE_FUEL == null || F_CONVERTED_FUEL == null || F_TEMPERATURE == null || F_SHIELD == null || F_MAX_SHIELD == null || F_SATURATION == null || F_MAX_SATURATION == null || F_ENUM_VALUE == null || F_DOUBLE_VALUE == null || F_INT_VALUE == null || M_GET_CORE == null) {
            INVALID_API = true;
            Main.LOG.warn("API to Draconic Fusion Reactor is probably outdated: some clreplacedes, methods or fields not found!");
        }
    }

    IHost host;

    /**
     * cached references to Reactor Core state fields.
     * Conveniently DE wrapped them in isolated objects so there is no reference to the TileEnreplacedy itself kept in memory.
     */
    FieldWrapper<?> satur, maxSatur, fuel, chaos, temp, shield, maxShield;

    FieldWrapper<Enum<?>> mode;

    IBlockState lastBlock;

    byte lastMode = -2;

    @Override
    public int readValue(BlockReference block) {
        if (block.getState() != lastBlock)
            updateState(block);
        if (lastMode < 0)
            return 0;
        int m = mode.get().ordinal();
        if (m != lastMode && host != null) {
            lastMode = (byte) m;
            host.syncSensorState();
        }
        switch(m) {
            case // TEMP -> T [°C]
            0:
                return (int) temp.getAsDouble();
            case 1:
                {
                    // TEMP_INV -> reactor status
                    int t = (int) temp.getAsDouble();
                    return t > 2500 ? (t > 8000 ? 3 : 2) : t > 2000 ? 1 : t > 100 ? 0 : -1;
                }
            case // FIELD -> E_shield [RF]
            2:
                return (int) shield.getAsDouble();
            case // FIELD_INV -> E_shield [0/00]
            3:
                return (int) (shield.getAsDouble() / maxShield.getAsDouble() * PRECISION);
            case // SAT -> E_core [RF]
            4:
                return satur.getAsInt();
            case // SAT_INV -> E_core [0/00]
            5:
                return (int) (satur.getAsDouble() / maxSatur.getAsDouble() * PRECISION);
            case // FUEL
            6:
                return (int) (fuel.getAsDouble() + chaos.getAsDouble());
            case 7:
                {
                    // FUEL_INV
                    double c = chaos.getAsDouble();
                    return (int) (c / (c + fuel.getAsDouble()) * PRECISION);
                }
            default:
                return 0;
        }
    }

    @Override
    public void onRefChange(BlockReference block, IHost host) {
        if ((this.host = host) == null) {
            fuel = chaos = temp = shield = maxShield = satur = maxSatur = mode = null;
        }
        lastBlock = null;
        lastMode = -2;
    }

    private void updateState(BlockReference block) {
        lastBlock = block.getState();
        if (INVALID_API)
            return;
        TileEnreplacedy te = block.getTileEnreplacedy();
        if (!C_REACTOR_COMP.isInstance(te)) {
            fuel = chaos = temp = shield = maxShield = satur = maxSatur = mode = null;
            if (lastMode != -1) {
                lastMode = -1;
                host.syncSensorState();
            }
            return;
        }
        try {
            mode = new FieldWrapper<>(F_ENUM_VALUE, F_RS_MODE.get(te));
            Object core = M_GET_CORE.invoke(te);
            if (core != null) {
                fuel = new FieldWrapper<>(F_DOUBLE_VALUE, F_REACTABLE_FUEL.get(core));
                chaos = new FieldWrapper<>(F_DOUBLE_VALUE, F_CONVERTED_FUEL.get(core));
                temp = new FieldWrapper<>(F_DOUBLE_VALUE, F_TEMPERATURE.get(core));
                shield = new FieldWrapper<>(F_DOUBLE_VALUE, F_SHIELD.get(core));
                maxShield = new FieldWrapper<>(F_DOUBLE_VALUE, F_MAX_SHIELD.get(core));
                satur = new FieldWrapper<>(F_INT_VALUE, F_SATURATION.get(core));
                maxSatur = new FieldWrapper<>(F_INT_VALUE, F_MAX_SATURATION.get(core));
                if (lastMode < 0) {
                    lastMode = (byte) mode.get().ordinal();
                    host.syncSensorState();
                }
                return;
            } else
                fuel = chaos = temp = shield = maxShield = satur = maxSatur = null;
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            Main.LOG.error("API to Draconic Fusion Reactor is probably outdated: ", e);
            INVALID_API = true;
        }
        if (lastMode != -1) {
            lastMode = -1;
            host.syncSensorState();
        }
    }

    @Override
    public String getTooltipString() {
        return TooltipUtil.translate("sensor.rs_ctr.dfr" + lastMode);
    }

    @Override
    public ResourceLocation getModel() {
        return MODEL;
    }

    @Override
    public NBTBase serializeNBT() {
        return new NBTTagByte(lastMode);
    }

    @Override
    public void deserializeNBT(NBTBase nbt) {
        lastMode = ((NBTPrimitive) nbt).getByte();
    }
}