Search in sources :

Example 1 with EnvironmentModule

use of mcjty.rftools.blocks.environmental.modules.EnvironmentModule in project RFTools by McJty.

the class NoTeleportAreaManager method isTeleportPrevented.

public static boolean isTeleportPrevented(Entity entity, GlobalCoordinate coordinate) {
    if (areas.isEmpty()) {
        return false;
    }
    List<GlobalCoordinate> toRemove = new ArrayList<>();
    boolean noTeleport = false;
    long curtime = System.currentTimeMillis() - 10000;
    for (Map.Entry<GlobalCoordinate, NoTeleportArea> entry : areas.entrySet()) {
        NoTeleportArea area = entry.getValue();
        GlobalCoordinate entryCoordinate = entry.getKey();
        if (area.in(coordinate, entryCoordinate)) {
            World world = mcjty.lib.varia.TeleportationTools.getWorldForDimension(entryCoordinate.getDimension());
            TileEntity te = world.getTileEntity(entryCoordinate.getCoordinate());
            if (te instanceof EnvironmentalControllerTileEntity) {
                EnvironmentalControllerTileEntity controllerTileEntity = (EnvironmentalControllerTileEntity) te;
                noTeleport = controllerTileEntity.isEntityAffected(entity);
            }
        }
        if (area.getLastTouched() < curtime) {
            // Hasn't been touched for at least 10 seconds. Probably no longer valid.
            // To be sure we will first check this by testing if the environmental controller is still active and running.
            WorldServer world = DimensionManager.getWorld(entryCoordinate.getDimension());
            if (world != null) {
                BlockPos c = entryCoordinate.getCoordinate();
                // If the world is not loaded we don't do anything and we also don't remove the area since we have no information about it.
                if (RFToolsTools.chunkLoaded(world, c)) {
                    boolean removeArea = true;
                    TileEntity te = world.getTileEntity(c);
                    if (te instanceof EnvironmentalControllerTileEntity) {
                        EnvironmentalControllerTileEntity controllerTileEntity = (EnvironmentalControllerTileEntity) te;
                        for (EnvironmentModule module : controllerTileEntity.getEnvironmentModules()) {
                            if (module instanceof NoTeleportEModule) {
                                if (((NoTeleportEModule) module).isActive()) {
                                    removeArea = false;
                                    break;
                                }
                            }
                        }
                    }
                    if (removeArea) {
                        toRemove.add(entryCoordinate);
                    }
                }
            }
        }
    }
    for (GlobalCoordinate globalCoordinate : toRemove) {
        areas.remove(globalCoordinate);
    }
    return noTeleport;
}
Also used : EnvironmentModule(mcjty.rftools.blocks.environmental.modules.EnvironmentModule) NoTeleportEModule(mcjty.rftools.blocks.environmental.modules.NoTeleportEModule) ArrayList(java.util.ArrayList) WorldServer(net.minecraft.world.WorldServer) GlobalCoordinate(mcjty.lib.varia.GlobalCoordinate) World(net.minecraft.world.World) TileEntity(net.minecraft.tileentity.TileEntity) BlockPos(net.minecraft.util.math.BlockPos) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with EnvironmentModule

use of mcjty.rftools.blocks.environmental.modules.EnvironmentModule in project RFTools by McJty.

the class PeacefulAreaManager method isPeaceful.

public static boolean isPeaceful(GlobalCoordinate coordinate) {
    if (areas.isEmpty()) {
        return false;
    }
    List<GlobalCoordinate> toRemove = new ArrayList<>();
    boolean peaceful = false;
    long curtime = System.currentTimeMillis() - 10000;
    for (Map.Entry<GlobalCoordinate, PeacefulArea> entry : areas.entrySet()) {
        PeacefulArea area = entry.getValue();
        GlobalCoordinate entryCoordinate = entry.getKey();
        if (area.in(coordinate, entryCoordinate)) {
            peaceful = true;
        }
        if (area.getLastTouched() < curtime) {
            // Hasn't been touched for at least 10 seconds. Probably no longer valid.
            // To be sure we will first check this by testing if the environmental controller is still active and running.
            WorldServer world = DimensionManager.getWorld(entryCoordinate.getDimension());
            if (world != null) {
                BlockPos c = entryCoordinate.getCoordinate();
                // If the world is not loaded we don't do anything and we also don't remove the area since we have no information about it.
                if (RFToolsTools.chunkLoaded(world, c)) {
                    boolean removeArea = true;
                    TileEntity te = world.getTileEntity(c);
                    if (te instanceof EnvironmentalControllerTileEntity) {
                        EnvironmentalControllerTileEntity controllerTileEntity = (EnvironmentalControllerTileEntity) te;
                        for (EnvironmentModule module : controllerTileEntity.getEnvironmentModules()) {
                            if (module instanceof PeacefulEModule) {
                                if (((PeacefulEModule) module).isActive()) {
                                    removeArea = false;
                                    break;
                                }
                            }
                        }
                    }
                    if (removeArea) {
                        toRemove.add(entryCoordinate);
                    }
                }
            }
        }
    }
    for (GlobalCoordinate globalCoordinate : toRemove) {
        areas.remove(globalCoordinate);
    }
    return peaceful;
}
Also used : EnvironmentModule(mcjty.rftools.blocks.environmental.modules.EnvironmentModule) ArrayList(java.util.ArrayList) PeacefulEModule(mcjty.rftools.blocks.environmental.modules.PeacefulEModule) WorldServer(net.minecraft.world.WorldServer) GlobalCoordinate(mcjty.lib.varia.GlobalCoordinate) TileEntity(net.minecraft.tileentity.TileEntity) BlockPos(net.minecraft.util.math.BlockPos) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with EnvironmentModule

use of mcjty.rftools.blocks.environmental.modules.EnvironmentModule in project RFTools by McJty.

the class EnvironmentalControllerTileEntity method deactivate.

public void deactivate() {
    for (EnvironmentModule module : environmentModules) {
        module.activate(false);
    }
    if (active) {
        active = false;
        markDirtyClient();
    }
}
Also used : EnvironmentModule(mcjty.rftools.blocks.environmental.modules.EnvironmentModule)

Example 4 with EnvironmentModule

use of mcjty.rftools.blocks.environmental.modules.EnvironmentModule in project RFTools by McJty.

the class EnvironmentalControllerTileEntity method getEnvironmentModules.

// This is called server side.
public List<EnvironmentModule> getEnvironmentModules() {
    if (environmentModules == null) {
        int volume = getVolume();
        totalRfPerTick = 0;
        environmentModules = new ArrayList<>();
        for (int i = 0; i < inventoryHelper.getCount(); i++) {
            ItemStack itemStack = inventoryHelper.getStackInSlot(i);
            if (!itemStack.isEmpty() && itemStack.getItem() instanceof EnvModuleProvider) {
                EnvModuleProvider moduleProvider = (EnvModuleProvider) itemStack.getItem();
                Class<? extends EnvironmentModule> moduleClass = moduleProvider.getServerEnvironmentModule();
                EnvironmentModule environmentModule;
                try {
                    environmentModule = moduleClass.newInstance();
                } catch (InstantiationException e) {
                    Logging.log("Failed to instantiate controller module!");
                    continue;
                } catch (IllegalAccessException e) {
                    Logging.log("Failed to instantiate controller module!");
                    continue;
                }
                environmentModules.add(environmentModule);
                totalRfPerTick += (int) (environmentModule.getRfPerTick() * volume);
            }
        }
    }
    return environmentModules;
}
Also used : EnvironmentModule(mcjty.rftools.blocks.environmental.modules.EnvironmentModule) ItemStack(net.minecraft.item.ItemStack)

Example 5 with EnvironmentModule

use of mcjty.rftools.blocks.environmental.modules.EnvironmentModule in project RFTools by McJty.

the class EnvironmentalControllerTileEntity method checkStateServer.

private void checkStateServer() {
    if (powerTimeout > 0) {
        powerTimeout--;
        return;
    }
    int rf = getEnergyStored();
    if (!isMachineEnabled()) {
        rf = 0;
    }
    getEnvironmentModules();
    int rfNeeded = getTotalRfPerTick();
    if (rfNeeded > rf || environmentModules.isEmpty()) {
        deactivate();
        powerTimeout = 20;
    } else {
        consumeEnergy(rfNeeded);
        for (EnvironmentModule module : environmentModules) {
            module.activate(true);
            module.tick(getWorld(), getPos(), radius, miny, maxy, this);
        }
        if (!active) {
            active = true;
            markDirtyClient();
        }
    }
}
Also used : EnvironmentModule(mcjty.rftools.blocks.environmental.modules.EnvironmentModule)

Aggregations

EnvironmentModule (mcjty.rftools.blocks.environmental.modules.EnvironmentModule)5 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 GlobalCoordinate (mcjty.lib.varia.GlobalCoordinate)2 TileEntity (net.minecraft.tileentity.TileEntity)2 BlockPos (net.minecraft.util.math.BlockPos)2 WorldServer (net.minecraft.world.WorldServer)2 NoTeleportEModule (mcjty.rftools.blocks.environmental.modules.NoTeleportEModule)1 PeacefulEModule (mcjty.rftools.blocks.environmental.modules.PeacefulEModule)1 ItemStack (net.minecraft.item.ItemStack)1 World (net.minecraft.world.World)1