Search in sources :

Example 1 with SyncedField

use of pneumaticCraft.common.inventory.SyncedField in project PneumaticCraft by MineMaarten.

the class NetworkUtils method getSyncedFieldsForField.

private static List<SyncedField> getSyncedFieldsForField(Field field, Object te, Class searchedAnnotation) {
    boolean isLazy = field.getAnnotation(LazySynced.class) != null;
    List<SyncedField> syncedFields = new ArrayList<SyncedField>();
    SyncedField syncedField = getSyncedFieldForField(field, te);
    if (syncedField != null) {
        syncedFields.add(syncedField.setLazy(isLazy));
        return syncedFields;
    } else {
        Object o;
        try {
            int filteredIndex = field.getAnnotation(FilteredSynced.class) != null ? field.getAnnotation(FilteredSynced.class).index() : -1;
            field.setAccessible(true);
            o = field.get(te);
            if (o instanceof int[]) {
                int[] array = (int[]) o;
                if (filteredIndex >= 0) {
                    syncedFields.add(new SyncedInt(te, field).setArrayIndex(filteredIndex).setLazy(isLazy));
                } else {
                    for (int i = 0; i < array.length; i++) {
                        syncedFields.add(new SyncedInt(te, field).setArrayIndex(i).setLazy(isLazy));
                    }
                }
                return syncedFields;
            }
            if (o instanceof float[]) {
                float[] array = (float[]) o;
                if (filteredIndex >= 0) {
                    syncedFields.add(new SyncedFloat(te, field).setArrayIndex(filteredIndex).setLazy(isLazy));
                } else {
                    for (int i = 0; i < array.length; i++) {
                        syncedFields.add(new SyncedFloat(te, field).setArrayIndex(i).setLazy(isLazy));
                    }
                }
                return syncedFields;
            }
            if (o instanceof double[]) {
                double[] array = (double[]) o;
                if (filteredIndex >= 0) {
                    syncedFields.add(new SyncedDouble(te, field).setArrayIndex(filteredIndex).setLazy(isLazy));
                } else {
                    for (int i = 0; i < array.length; i++) {
                        syncedFields.add(new SyncedDouble(te, field).setArrayIndex(i).setLazy(isLazy));
                    }
                }
                return syncedFields;
            }
            if (o instanceof boolean[]) {
                boolean[] array = (boolean[]) o;
                if (filteredIndex >= 0) {
                    syncedFields.add(new SyncedBoolean(te, field).setArrayIndex(filteredIndex).setLazy(isLazy));
                } else {
                    for (int i = 0; i < array.length; i++) {
                        syncedFields.add(new SyncedBoolean(te, field).setArrayIndex(i).setLazy(isLazy));
                    }
                }
                return syncedFields;
            }
            if (o instanceof String[]) {
                String[] array = (String[]) o;
                if (filteredIndex >= 0) {
                    syncedFields.add(new SyncedString(te, field).setArrayIndex(filteredIndex).setLazy(isLazy));
                } else {
                    for (int i = 0; i < array.length; i++) {
                        syncedFields.add(new SyncedString(te, field).setArrayIndex(i).setLazy(isLazy));
                    }
                }
                return syncedFields;
            }
            if (o.getClass().isArray() && o.getClass().getComponentType().isEnum()) {
                Object[] enumArray = (Object[]) o;
                if (filteredIndex >= 0) {
                    syncedFields.add(new SyncedEnum(te, field).setArrayIndex(filteredIndex).setLazy(isLazy));
                } else {
                    for (int i = 0; i < enumArray.length; i++) {
                        syncedFields.add(new SyncedEnum(te, field).setArrayIndex(i).setLazy(isLazy));
                    }
                }
                return syncedFields;
            }
            if (o instanceof ItemStack[]) {
                ItemStack[] array = (ItemStack[]) o;
                if (filteredIndex >= 0) {
                    syncedFields.add(new SyncedItemStack(te, field).setArrayIndex(filteredIndex).setLazy(isLazy));
                } else {
                    for (int i = 0; i < array.length; i++) {
                        syncedFields.add(new SyncedItemStack(te, field).setArrayIndex(i).setLazy(isLazy));
                    }
                }
                return syncedFields;
            }
            if (o instanceof FluidTank[]) {
                FluidTank[] array = (FluidTank[]) o;
                if (filteredIndex >= 0) {
                    syncedFields.add(new SyncedFluidTank(te, field).setArrayIndex(filteredIndex).setLazy(isLazy));
                } else {
                    for (int i = 0; i < array.length; i++) {
                        syncedFields.add(new SyncedFluidTank(te, field).setArrayIndex(i).setLazy(isLazy));
                    }
                }
                return syncedFields;
            }
            if (field.getType().isArray()) {
                Object[] array = (Object[]) o;
                for (Object obj : array) {
                    syncedFields.addAll(getSyncedFields(obj, searchedAnnotation));
                }
            } else {
                syncedFields.addAll(getSyncedFields(o, searchedAnnotation));
            }
            if (syncedFields.size() > 0)
                return syncedFields;
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.warning("Field " + field + " didn't produce any syncable fields!");
        return syncedFields;
    }
}
Also used : ArrayList(java.util.ArrayList) SyncedBoolean(pneumaticCraft.common.inventory.SyncedField.SyncedBoolean) SyncedString(pneumaticCraft.common.inventory.SyncedField.SyncedString) SyncedInt(pneumaticCraft.common.inventory.SyncedField.SyncedInt) SyncedDouble(pneumaticCraft.common.inventory.SyncedField.SyncedDouble) SyncedEnum(pneumaticCraft.common.inventory.SyncedField.SyncedEnum) SyncedField(pneumaticCraft.common.inventory.SyncedField) SyncedItemStack(pneumaticCraft.common.inventory.SyncedField.SyncedItemStack) SyncedFluidTank(pneumaticCraft.common.inventory.SyncedField.SyncedFluidTank) FluidTank(net.minecraftforge.fluids.FluidTank) SyncedItemStack(pneumaticCraft.common.inventory.SyncedField.SyncedItemStack) ItemStack(net.minecraft.item.ItemStack) SyncedFluidTank(pneumaticCraft.common.inventory.SyncedField.SyncedFluidTank) SyncedFloat(pneumaticCraft.common.inventory.SyncedField.SyncedFloat) SyncedString(pneumaticCraft.common.inventory.SyncedField.SyncedString)

Example 2 with SyncedField

use of pneumaticCraft.common.inventory.SyncedField in project PneumaticCraft by MineMaarten.

the class NetworkUtils method getSyncedFields.

/*private static Map<Class, List<Field>> extraMarkedFields = new HashMap<Class, List<Field>>();

    public static void addExtraMarkedField(Class clazz, Field field){
        List<Field> markedFields = extraMarkedFields.get(clazz);
        if(markedFields == null) {
            markedFields = new ArrayList<Field>();
            extraMarkedFields.put(clazz, markedFields);
        }
        markedFields.add(field);
    }*/
public static List<SyncedField> getSyncedFields(Object te, Class searchedAnnotation) {
    List<SyncedField> syncedFields = new ArrayList<SyncedField>();
    Class examinedClass = te.getClass();
    while (examinedClass != null) {
        for (Field field : examinedClass.getDeclaredFields()) {
            if (field.getAnnotation(searchedAnnotation) != null) {
                syncedFields.addAll(getSyncedFieldsForField(field, te, searchedAnnotation));
            }
        }
        examinedClass = examinedClass.getSuperclass();
    }
    return syncedFields;
}
Also used : SyncedField(pneumaticCraft.common.inventory.SyncedField) Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) SyncedField(pneumaticCraft.common.inventory.SyncedField)

Example 3 with SyncedField

use of pneumaticCraft.common.inventory.SyncedField in project PneumaticCraft by MineMaarten.

the class SemiBlockBasic method update.

@Override
public void update() {
    if (!world.isRemote && !canStay())
        drop();
    if (!world.isRemote) {
        if (descriptionFields == null)
            descriptionPacketScheduled = true;
        for (SyncedField field : getDescriptionFields()) {
            if (field.update()) {
                descriptionPacketScheduled = true;
            }
        }
        if (descriptionPacketScheduled) {
            descriptionPacketScheduled = false;
            sendDescriptionPacket();
        }
    }
}
Also used : SyncedField(pneumaticCraft.common.inventory.SyncedField)

Example 4 with SyncedField

use of pneumaticCraft.common.inventory.SyncedField in project PneumaticCraft by MineMaarten.

the class TileEntityBase method updateEntity.

@Override
public void updateEntity() {
    if (firstRun && !worldObj.isRemote) {
        //firstRun = false;
        onFirstServerUpdate();
        onNeighborTileUpdate();
        onNeighborBlockUpdate();
    }
    firstRun = false;
    if (!worldObj.isRemote) {
        if (this instanceof IHeatExchanger) {
            ((IHeatExchanger) this).getHeatExchangerLogic(ForgeDirection.UNKNOWN).update();
        }
        if (descriptionFields == null)
            descriptionPacketScheduled = true;
        for (SyncedField field : getDescriptionFields()) {
            if (field.update()) {
                descriptionPacketScheduled = true;
            }
        }
        if (descriptionPacketScheduled) {
            descriptionPacketScheduled = false;
            sendDescriptionPacket();
        }
    }
}
Also used : IHeatExchanger(pneumaticCraft.api.tileentity.IHeatExchanger) SyncedField(pneumaticCraft.common.inventory.SyncedField)

Aggregations

SyncedField (pneumaticCraft.common.inventory.SyncedField)4 ArrayList (java.util.ArrayList)2 Field (java.lang.reflect.Field)1 ItemStack (net.minecraft.item.ItemStack)1 FluidTank (net.minecraftforge.fluids.FluidTank)1 IHeatExchanger (pneumaticCraft.api.tileentity.IHeatExchanger)1 SyncedBoolean (pneumaticCraft.common.inventory.SyncedField.SyncedBoolean)1 SyncedDouble (pneumaticCraft.common.inventory.SyncedField.SyncedDouble)1 SyncedEnum (pneumaticCraft.common.inventory.SyncedField.SyncedEnum)1 SyncedFloat (pneumaticCraft.common.inventory.SyncedField.SyncedFloat)1 SyncedFluidTank (pneumaticCraft.common.inventory.SyncedField.SyncedFluidTank)1 SyncedInt (pneumaticCraft.common.inventory.SyncedField.SyncedInt)1 SyncedItemStack (pneumaticCraft.common.inventory.SyncedField.SyncedItemStack)1 SyncedString (pneumaticCraft.common.inventory.SyncedField.SyncedString)1