Improve the greenhouse controller

This commit is contained in:
Nedelosk committed 2016-04-25 20:40:55 +02:00
1 parent 080dd93dc2
commit 7ca28d4d10
7 files changed
+156 -44

No files matched your search

@@ -10,14 +10,29 @@ import net.minecraft.world.World;
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();
}
@@ -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();
@@ -16,12 +16,12 @@ import forestry.api.greenhouse.IGreenhouseLogic;
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();
@@ -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;
}
}
@@ -24,6 +24,7 @@ import forestry.core.inventory.FakeInventoryAdapter;
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;
@@ -87,12 +88,11 @@ public class FakeGreenhouseController extends FakeMultiblockController implement
@Override
public IGreenhouseState createState() {
return null;
return FakeGreenhouseState.instance;
}
@Override
public List<IInternalBlock> getInternalBlocks() {
return Collections.emptyList();
}
}
@@ -360,17 +360,22 @@ public class GreenhouseController extends RectangularMultiblockControllerBase im
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
@@ -547,7 +552,6 @@ public class GreenhouseController extends RectangularMultiblockControllerBase im
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++) {
@@ -624,37 +628,32 @@ public class GreenhouseController extends RectangularMultiblockControllerBase im
}
}
}
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);
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 updateClient(int tickCount) {
}
@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 {
}
private void checkInternalBlock(IInternalBlock blockToCheck, List<IInternalBlock> internalBlocks) throws MultiblockValidationException{
private void checkInternalBlock(IInternalBlock blockToCheck) throws MultiblockValidationException{
internalBlocks.add(blockToCheck);
BlockPos posRoot = blockToCheck.getPos();
@@ -684,22 +683,28 @@ public class GreenhouseController extends RectangularMultiblockControllerBase im
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;
@@ -717,5 +722,19 @@ public class GreenhouseController extends RectangularMultiblockControllerBase im
}
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 {
}
}
@@ -27,19 +27,34 @@ import forestry.energy.EnergyManager;
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
/**
* @return The current state of the greenhouse controller.
*/
@Nonnull
IGreenhouseState createState();
@Nonnull
/**
* @return A list with all internal block's of the greenhouse.
*/
List<IInternalBlock> getInternalBlocks();
}