Permalink
Browse files

Finished off Dispenser Recipes.

  • Loading branch information...
me4502 committed Jan 9, 2018
1 parent 1f84551 commit d04a2e94a7566304fa1d62a64d8f2e62427dfc17
@@ -38,7 +38,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.StreamSupport;
import javax.annotation.Nullable;
@@ -55,6 +54,9 @@ public void onInitialize() throws CraftBookException {
recipes.add(new Cannon());
recipes.add(new Fan());
recipes.add(new Vacuum());
recipes.add(new FireArrows());
recipes.add(new SnowShooter());
recipes.add(new XPShooter());
}
@Override
@@ -108,8 +110,9 @@ public void onChangeBlock(ChangeBlockEvent event, @First LocatableBlock dispense
public boolean handleDispenserAction(Dispenser dispenser, @Nullable Vector3d velocity) {
for (DispenserRecipe recipe : recipes) {
ItemStack[] items = StreamSupport.stream(dispenser.getInventory().slots().spliterator(), false)
.map(Inventory::peek).filter(Optional::isPresent)
.map(Optional::get).toArray(ItemStack[]::new);
.map(Inventory::peek)
.map(opt -> opt.orElse(ItemStack.empty()))
.toArray(ItemStack[]::new);
if (recipe.doesPass(items)) {
if (recipe.doAction(dispenser, items, velocity)) {
dispenser.getInventory().slots().forEach(inv -> inv.poll(1)); // Take one of every stack.
@@ -0,0 +1,62 @@
/*
* CraftBook Copyright (C) 2010-2018 sk89q <http://www.sk89q.com>
* CraftBook Copyright (C) 2011-2018 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.dispenser;
import com.flowpowered.math.vector.Vector3d;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.block.tileentity.carrier.Dispenser;
import org.spongepowered.api.data.key.Keys;
import org.spongepowered.api.entity.EntityTypes;
import org.spongepowered.api.entity.projectile.arrow.TippedArrow;
import org.spongepowered.api.event.CauseStackManager;
import org.spongepowered.api.event.cause.EventContextKeys;
import org.spongepowered.api.event.cause.entity.spawn.SpawnTypes;
import org.spongepowered.api.item.ItemTypes;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.util.Direction;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;
public class FireArrows extends SimpleDispenserRecipe {
public FireArrows() {
super(new ItemStack[] {
ItemStack.of(ItemTypes.AIR, 1), ItemStack.of(ItemTypes.FIRE_CHARGE, 1), ItemStack.of(ItemTypes.AIR, 1),
ItemStack.of(ItemTypes.FIRE_CHARGE, 1), ItemStack.of(ItemTypes.ARROW, 1), ItemStack.of(ItemTypes.FIRE_CHARGE, 1),
ItemStack.of(ItemTypes.AIR, 1), ItemStack.of(ItemTypes.FIRE_CHARGE, 1), ItemStack.of(ItemTypes.AIR, 1),
});
}
@Override
public boolean doAction(Dispenser dispenser, ItemStack[] recipe, Vector3d velocity) {
Direction face = dispenser.getLocation().get(Keys.DIRECTION).orElse(Direction.NONE);
if (face != Direction.NONE) {
Location<World> location = dispenser.getLocation().getRelative(face).add(0.5, 0.5, 0.5);
TippedArrow arrow = (TippedArrow) dispenser.getWorld().createEntityNaturally(EntityTypes.TIPPED_ARROW, location.getPosition());
arrow.offer(Keys.FIRE_TICKS, 5000);
Vector3d arrowVelocity = face.asOffset().add(0, 0.1f, 0).normalize().mul(1.5f);
arrow.setVelocity(arrowVelocity);
try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.CUSTOM);
dispenser.getWorld().spawnEntity(arrow);
}
return true;
}
return false;
}
}
@@ -16,9 +16,12 @@
*/
package com.sk89q.craftbook.sponge.mechanics.dispenser;
import org.spongepowered.api.item.ItemTypes;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.item.inventory.ItemStackComparators;
import java.util.Arrays;
public abstract class SimpleDispenserRecipe implements DispenserRecipe {
private ItemStack[] recipe;
@@ -30,7 +33,8 @@ public SimpleDispenserRecipe(ItemStack[] recipe) {
@Override
public boolean doesPass(ItemStack[] checkRecipe) {
for (int i = 0; i < checkRecipe.length; i++) {
if (ItemStackComparators.TYPE.compare(recipe[i], checkRecipe[i]) != 0) {
if (ItemStackComparators.TYPE.compare(recipe[i], checkRecipe[i]) != 0
&& !(checkRecipe[i].isEmpty() && recipe[i].getType() == ItemTypes.AIR)) {
return false;
}
}
@@ -0,0 +1,61 @@
/*
* CraftBook Copyright (C) 2010-2018 sk89q <http://www.sk89q.com>
* CraftBook Copyright (C) 2011-2018 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.dispenser;
import com.flowpowered.math.vector.Vector3d;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.block.tileentity.carrier.Dispenser;
import org.spongepowered.api.data.key.Keys;
import org.spongepowered.api.entity.EntityTypes;
import org.spongepowered.api.entity.projectile.Snowball;
import org.spongepowered.api.event.CauseStackManager;
import org.spongepowered.api.event.cause.EventContextKeys;
import org.spongepowered.api.event.cause.entity.spawn.SpawnTypes;
import org.spongepowered.api.item.ItemTypes;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.util.Direction;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;
public class SnowShooter extends SimpleDispenserRecipe {
public SnowShooter() {
super(new ItemStack[] {
ItemStack.of(ItemTypes.AIR, 1), ItemStack.of(ItemTypes.SNOW, 1), ItemStack.of(ItemTypes.AIR, 1),
ItemStack.of(ItemTypes.SNOW, 1), ItemStack.of(ItemTypes.POTION, 1), ItemStack.of(ItemTypes.SNOW, 1),
ItemStack.of(ItemTypes.AIR, 1), ItemStack.of(ItemTypes.SNOW, 1), ItemStack.of(ItemTypes.AIR, 1),
});
}
@Override
public boolean doAction(Dispenser dispenser, ItemStack[] recipe, Vector3d velocity) {
Direction face = dispenser.getLocation().get(Keys.DIRECTION).orElse(Direction.NONE);
if (face != Direction.NONE) {
Location<World> location = dispenser.getLocation().getRelative(face).add(0.5, 0.5, 0.5);
Snowball snowball = (Snowball) dispenser.getWorld().createEntityNaturally(EntityTypes.SNOWBALL, location.getPosition());
Vector3d arrowVelocity = face.asOffset().add(0, 0.1f, 0).normalize().mul(1.5f);
snowball.setVelocity(arrowVelocity);
try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.CUSTOM);
dispenser.getWorld().spawnEntity(snowball);
}
return true;
}
return false;
}
}
@@ -0,0 +1,60 @@
/*
* CraftBook Copyright (C) 2010-2018 sk89q <http://www.sk89q.com>
* CraftBook Copyright (C) 2011-2018 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.dispenser;
import com.flowpowered.math.vector.Vector3d;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.block.tileentity.carrier.Dispenser;
import org.spongepowered.api.data.key.Keys;
import org.spongepowered.api.entity.EntityTypes;
import org.spongepowered.api.entity.projectile.ThrownExpBottle;
import org.spongepowered.api.event.CauseStackManager;
import org.spongepowered.api.event.cause.EventContextKeys;
import org.spongepowered.api.event.cause.entity.spawn.SpawnTypes;
import org.spongepowered.api.item.ItemTypes;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.util.Direction;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;
public class XPShooter extends SimpleDispenserRecipe {
public XPShooter() {
super(new ItemStack[] {
ItemStack.of(ItemTypes.AIR, 1), ItemStack.of(ItemTypes.REDSTONE, 1), ItemStack.of(ItemTypes.AIR, 1),
ItemStack.of(ItemTypes.REDSTONE, 1), ItemStack.of(ItemTypes.GLASS_BOTTLE, 1), ItemStack.of(ItemTypes.REDSTONE, 1),
ItemStack.of(ItemTypes.AIR, 1), ItemStack.of(ItemTypes.REDSTONE, 1), ItemStack.of(ItemTypes.AIR, 1),
});
}
@Override
public boolean doAction(Dispenser dispenser, ItemStack[] recipe, Vector3d velocity) {
Direction face = dispenser.getLocation().get(Keys.DIRECTION).orElse(Direction.NONE);
if (face != Direction.NONE) {
Location<World> location = dispenser.getLocation().getRelative(face).add(0.5, 0.5, 0.5);
ThrownExpBottle bottle = (ThrownExpBottle) dispenser.getWorld().createEntity(EntityTypes.THROWN_EXP_BOTTLE, location.getPosition());
bottle.setVelocity(velocity);
try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.CUSTOM);
dispenser.getWorld().spawnEntity(bottle);
}
return true;
}
return false;
}
}

0 comments on commit d04a2e9

Please sign in to comment.