Skip to content

Commit

Permalink
Improve the greenhouse controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Nedelosk committed Apr 25, 2016
1 parent 080dd93 commit 7ca28d4
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 44 deletions.
15 changes: 15 additions & 0 deletions src/main/java/forestry/api/core/ICamouflageHandler.java
Expand Up @@ -10,14 +10,29 @@

public interface ICamouflageHandler{

/**
* @return The item of the block that is the camouflage of this handler for the EnumCamouflageType.
*/
ItemStack getCamouflageBlock(EnumCamouflageType type);

/**
* @return The default camouflage block item for the EnumCamouflageType.
*/
ItemStack getDefaultCamouflageBlock(EnumCamouflageType type);

/**
* Set the camouflage block item for the EnumCamouflageType.
*/
void setCamouflageBlock(EnumCamouflageType type, ItemStack camouflageBlock);

/**
* @return The coordinates of the handler as a BlockPos.
*/
BlockPos getCoordinates();

/**
* @return The world of the handler.
*/
World getWorld();

}
Expand Up @@ -21,7 +21,7 @@ public interface IGreenhouseHousing extends IErrorLogicSource {
float getExactHumidity();

/**
* @return The world in that the housing is
* @return The world in that the housing is.
*/
World getWorld();

Expand Down
Expand Up @@ -16,12 +16,12 @@
public interface IGreenhouseController extends IMultiblockController, IGreenhouseHousing, ICamouflageHandler, IClimateControlled {

/**
* Handle change events
* Handle change events.
*/
void onChange(EnumGreenhouseEventType type, Object event);

/**
* @return The logics of the greenhouse
* @return The logics of the greenhouse.
*/
List<IGreenhouseLogic> getLogics();

Expand Down
63 changes: 63 additions & 0 deletions src/main/java/forestry/greenhouse/FakeGreenhouseState.java
@@ -0,0 +1,63 @@
/*******************************************************************************
* Copyright (c) 2011-2014 SirSengir.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v3
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl-3.0.txt
*
* Various Contributors including, but not limited to:
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
******************************************************************************/
package forestry.greenhouse;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

import forestry.api.core.EnumHumidity;
import forestry.api.core.EnumTemperature;
import forestry.api.greenhouse.IGreenhouseState;
import forestry.api.greenhouse.IInternalBlock;
import forestry.api.multiblock.IMultiblockComponent;
import net.minecraft.world.World;

public class FakeGreenhouseState implements IGreenhouseState {

public static final FakeGreenhouseState instance = new FakeGreenhouseState();

@Override
public EnumTemperature getTemperature() {
return null;
}

@Override
public EnumHumidity getHumidity() {
return null;
}

@Override
public float getExactTemperature() {
return 0;
}

@Override
public float getExactHumidity() {
return 0;
}

@Override
public List<IInternalBlock> getInternalBlocks() {
return Collections.emptyList();
}

@Override
public Collection<IMultiblockComponent> getGreenhouseComponents() {
return Collections.emptyList();
}

@Override
public World getWorld() {
return null;
}

}
Expand Up @@ -24,6 +24,7 @@
import forestry.core.inventory.IInventoryAdapter;
import forestry.core.multiblock.FakeMultiblockController;
import forestry.energy.EnergyManager;
import forestry.greenhouse.FakeGreenhouseState;
import net.minecraft.item.ItemStack;
import net.minecraft.util.BlockPos;

Expand Down Expand Up @@ -87,12 +88,11 @@ public void addHumidityChange(float change, float boundaryDown, float boundaryUp

@Override
public IGreenhouseState createState() {
return null;
return FakeGreenhouseState.instance;
}

@Override
public List<IInternalBlock> getInternalBlocks() {
return Collections.emptyList();
}

}
Expand Up @@ -360,17 +360,22 @@ public void onChange(EnumGreenhouseEventType type, Object event) {
private void createLogics() {
logics.clear();
for(Class<? extends IGreenhouseLogic> logicClass : GreenhouseManager.greenhouseLogics){
try{
logics.add(createLogic(logicClass));
}catch(Exception e){
Log.error("Fail to create a greenhouse logic with the class: " + logicClass);
e.printStackTrace();
IGreenhouseLogic logic = createLogic(logicClass);
if(logic != null){
logics.add(logic);
}
}
}

private IGreenhouseLogic createLogic(Class<? extends IGreenhouseLogic> logicClass) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException{
return logicClass.getConstructor(IGreenhouseController.class).newInstance(this);
private IGreenhouseLogic createLogic(Class<? extends IGreenhouseLogic> logicClass) {
try {
return logicClass.getConstructor(IGreenhouseController.class).newInstance(this);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
| NoSuchMethodException | SecurityException e) {
e.printStackTrace();
Log.error("Fail to create a greenhouse logic with the class: " + logicClass);
return null;
}
}

@Override
Expand Down Expand Up @@ -547,7 +552,6 @@ protected void isMachineWhole() throws MultiblockValidationException {
boolean isNextRoof = false;
Class<? extends RectangularMultiblockControllerBase> myClass = this.getClass();

List<IInternalBlock> internalBlocks = Lists.newArrayList();
height: for (int y = minimumCoord.getY(); y <= maximumCoord.getY(); y++) {
for (int x = minimumCoord.getX(); x <= maximumCoord.getX(); x++) {
for (int z = minimumCoord.getZ(); z <= maximumCoord.getZ(); z++) {
Expand Down Expand Up @@ -624,37 +628,32 @@ protected void isMachineWhole() throws MultiblockValidationException {
}
}
}
if(isNextRoof){
CreateInternalBlockEvent event = new CreateInternalBlockEvent(createState(), new InternalBlock(worldObj, getMinimumCoord().add(1, 1, 1)));

MinecraftForge.EVENT_BUS.post(event);

checkInternalBlock(event.internalBlock, internalBlocks);
}else if(internalBlocks.isEmpty()){
internalBlocks.clear();

if(isNextRoof){
checkInternalBlock(createInternalBlock(new InternalBlock(worldObj, getMinimumCoord().add(1, 1, 1))));
}
if(internalBlocks.isEmpty()){
throw new MultiblockValidationException(StatCollector.translateToLocalFormatted("for.multiblock.error.space.closed"));
}
this.internalBlocks.addAll(internalBlocks);
}

@Override
protected void updateClient(int tickCount) {
}

@Override
protected void isGoodForExteriorLevel(IMultiblockComponent part, int level) throws MultiblockValidationException {

int hatches = 0;
for(IMultiblockComponent comp : connectedParts){
if(comp instanceof IGreenhouseComponent.ButterflyHatch){
hatches++;
}
}
if(hatches > 1){
throw new MultiblockValidationException(StatCollector.translateToLocalFormatted("for.multiblock.error.butterflyhatch.tomany"));
}
}

@Override
protected void isGoodForInterior(IMultiblockComponent part) throws MultiblockValidationException {

}

@Override
protected void isBlockGoodForInterior(World world, BlockPos pos) throws MultiblockValidationException {
protected void updateClient(int tickCount) {
}

private void checkInternalBlock(IInternalBlock blockToCheck, List<IInternalBlock> internalBlocks) throws MultiblockValidationException{
private void checkInternalBlock(IInternalBlock blockToCheck) throws MultiblockValidationException{
internalBlocks.add(blockToCheck);
BlockPos posRoot = blockToCheck.getPos();

Expand Down Expand Up @@ -684,22 +683,28 @@ else if(!(tileFace instanceof TileGreenhouseSprinkler)){
faceToCheck.setTested(true);
}
}else{
CreateInternalBlockEvent createEvent = new CreateInternalBlockEvent(createState(), new InternalBlock(worldObj, posFacing, face.getOpposite(), blockToCheck));
MinecraftForge.EVENT_BUS.post(createEvent);

IInternalBlock internalBlock = createEvent.internalBlock;
IInternalBlock internalBlock = createInternalBlock(new InternalBlock(worldObj, posFacing, face.getOpposite(), blockToCheck));

// Check is the internal block in the list
if(internalBlocks.contains(internalBlock)){
faceToCheck.setTested(true);
}else{
checkInternalBlock(internalBlock, internalBlocks);
checkInternalBlock(internalBlock);
}
}
}
}
}

private IInternalBlock createInternalBlock(IInternalBlock internalBlock){
CreateInternalBlockEvent createEvent = new CreateInternalBlockEvent(createState(), internalBlock);

MinecraftForge.EVENT_BUS.post(createEvent);

return createEvent.internalBlock;

}

@Override
public List<IInternalBlock> getInternalBlocks() {
return internalBlocks;
Expand All @@ -717,5 +722,19 @@ public static ButterflyHatch getGreenhouseButterflyHatch(World world, BlockPos p
}
return null;
}

@Override
protected void isGoodForExteriorLevel(IMultiblockComponent part, int level) throws MultiblockValidationException {

}

@Override
protected void isGoodForInterior(IMultiblockComponent part) throws MultiblockValidationException {

}

@Override
protected void isBlockGoodForInterior(World world, BlockPos pos) throws MultiblockValidationException {
}

}
Expand Up @@ -27,19 +27,34 @@

public interface IGreenhouseControllerInternal extends IGreenhouseController, IMultiblockControllerInternal, IRestrictedAccess, IStreamableGui {

/**
* @return The inventory of the controller.
*/
@Nonnull
IInventoryAdapter getInternalInventory();

/**
* @return The tank manager of the controller.
*/
@Nonnull
ITankManager getTankManager();


/**
* @return The energy manager of the controller.
*/
@Nullable
EnergyManager getEnergyManager();

@Nullable
IGreenhouseState createState();

/**
* @return The current state of the greenhouse controller.
*/
@Nonnull
List<IInternalBlock> getInternalBlocks();
IGreenhouseState createState();

/**
* @return A list with all internal block's of the greenhouse.
*/
List<IInternalBlock> getInternalBlocks();
}

0 comments on commit 7ca28d4

Please sign in to comment.