Permalink
Browse files

Fixed pipes and added colour conditional pipes.

  • Loading branch information...
me4502 committed Jul 18, 2017
1 parent 5676cf4 commit 7fd272c1eea970fb4c102379395d232d72144e98
@@ -17,7 +17,11 @@
package com.sk89q.craftbook.sponge.mechanics.pipe;
import com.me4502.modularframework.module.Module;
import com.sk89q.craftbook.core.util.ConfigValue;
import com.sk89q.craftbook.core.util.PermissionNode;
import com.sk89q.craftbook.core.util.documentation.DocumentationProvider;
import com.sk89q.craftbook.sponge.CraftBookPlugin;
import com.sk89q.craftbook.sponge.mechanics.pipe.parts.ColorConditionalPipePart;
import com.sk89q.craftbook.sponge.mechanics.pipe.parts.InputPipePart;
import com.sk89q.craftbook.sponge.mechanics.pipe.parts.OutputPipePart;
import com.sk89q.craftbook.sponge.mechanics.pipe.parts.PassthroughPipePart;
@@ -51,14 +55,15 @@
import java.util.Set;
@Module(id = "pipes", name = "Pipes", onEnable="onInitialize", onDisable="onDisable")
public class Pipes extends SpongeBlockMechanic {
public class Pipes extends SpongeBlockMechanic implements DocumentationProvider {
private PipePart[] pipeParts;
@Override
public void onInitialize() {
List<PipePart> pipePartList = new ArrayList<>();
pipePartList.add(new PassthroughPipePart());
pipePartList.add(new ColorConditionalPipePart());
pipePartList.add(new InputPipePart());
pipePartList.add(new OutputPipePart());
@@ -117,22 +122,24 @@ private ItemStack doPipeIteration(Location<World> location, ItemStack itemStack,
if(pipePart == null)
return itemStack;
for(Location<World> location1 : pipePart.findValidOutputs(location, itemStack, fromDirection)) {
if(pipePart instanceof OutputPipePart) {
Optional<Inventory> inventory = LocationUtil.getInventoryForLocation(location1);
if(inventory.isPresent()) {
InventoryTransactionResult result = inventory.get().offer(itemStack);
if(!result.getRejectedItems().isEmpty()) {
for (ItemStackSnapshot snapshot : result.getRejectedItems()) {
itemStack = snapshot.createStack();
for (Location<World> location1 : pipePart.findPotentialOutputs(location, itemStack, fromDirection)) {
if (pipePart.validateOutput(location, location1, itemStack)) {
if (pipePart instanceof OutputPipePart) {
Optional<Inventory> inventory = LocationUtil.getInventoryForLocation(location1);
if (inventory.isPresent()) {
InventoryTransactionResult result = inventory.get().offer(itemStack);
if (!result.getRejectedItems().isEmpty()) {
for (ItemStackSnapshot snapshot : result.getRejectedItems()) {
itemStack = snapshot.createStack();
}
} else {
itemStack.setQuantity(0);
}
} else {
itemStack.setQuantity(0);
}
}
} else
itemStack = doPipeIteration(location1, itemStack, BlockUtil.getFacing(location, location1), traversed);
} else
itemStack = doPipeIteration(location1, itemStack, BlockUtil.getFacing(location1, location), traversed);
}
}
return itemStack;
@@ -149,4 +156,23 @@ private PipePart getPipePart(Location location) {
public boolean isValid(Location<World> location) {
return location.getBlockType() == BlockTypes.STICKY_PISTON;
}
@Override
public String getPath() {
return "mechanics/pipes";
}
@Override
public ConfigValue<?>[] getConfigurationNodes() {
return new ConfigValue<?>[] {
};
}
@Override
public PermissionNode[] getPermissionNodes() {
return new PermissionNode[] {
};
}
}
@@ -0,0 +1,41 @@
/*
* CraftBook Copyright (C) 2010-2017 sk89q <http://www.sk89q.com>
* CraftBook Copyright (C) 2011-2017 me4502 <http://www.me4502.com>
* CraftBook Copyright (C) Contributors
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not,
* see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.craftbook.sponge.mechanics.pipe.parts;
import org.spongepowered.api.block.BlockState;
import org.spongepowered.api.block.BlockTypes;
import org.spongepowered.api.data.key.Keys;
import org.spongepowered.api.data.type.DyeColors;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;
public class ColorConditionalPipePart extends PassthroughPipePart {
@Override
public boolean isValid(BlockState blockState) {
return blockState.getType() == BlockTypes.STAINED_GLASS;
}
@Override
public boolean validateOutput(Location<World> location, Location<World> output, ItemStack itemStack) {
if (isValid(output.getBlock())) {
return location.get(Keys.DYE_COLOR).orElse(DyeColors.WHITE).equals(output.get(Keys.DYE_COLOR).orElse(DyeColors.WHITE));
}
return super.validateOutput(location, output, itemStack);
}
}
@@ -35,7 +35,7 @@ public boolean isValid(BlockState blockState) {
}
@Override
public List<Location<World>> findValidOutputs(Location<World> location, ItemStack itemStack, Direction inputSide) {
public List<Location<World>> findPotentialOutputs(Location<World> location, ItemStack itemStack, Direction inputSide) {
List<Location<World>> locations = new ArrayList<>();
@@ -34,7 +34,7 @@ public boolean isValid(BlockState blockState) {
}
@Override
public List<Location<World>> findValidOutputs(Location<World> location, ItemStack itemStack, Direction inputSide) {
return BlockUtil.getAdjacentExcept(location, inputSide.getOpposite());
public List<Location<World>> findPotentialOutputs(Location<World> location, ItemStack itemStack, Direction inputSide) {
return BlockUtil.getAdjacentExcept(location, inputSide);
}
}
@@ -38,5 +38,21 @@
* @param inputSide The side that the input has come from.
* @return A list of possible output locations.
*/
List<Location<World>> findValidOutputs(Location<World> location, ItemStack itemStack, Direction inputSide);
List<Location<World>> findPotentialOutputs(Location<World> location, ItemStack itemStack, Direction inputSide);
/**
* Determines if an output is valid.
*
* <p>
* This only needs to check for special behaviour.
* </p>
*
* @param location The location of this part.
* @param output The location of the output.
* @param itemStack The itemstack passed through.
* @return If the output is valid.
*/
default boolean validateOutput(Location<World> location, Location<World> output, ItemStack itemStack) {
return true;
}
}
2 docs

0 comments on commit 7fd272c

Please sign in to comment.