-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
DispenserRecipes.java
128 lines (110 loc) · 5.02 KB
/
DispenserRecipes.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* 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 com.me4502.modularframework.module.Module;
import com.sk89q.craftbook.core.util.CraftBookException;
import com.sk89q.craftbook.core.util.documentation.DocumentationProvider;
import com.sk89q.craftbook.sponge.mechanics.types.SpongeBlockMechanic;
import org.spongepowered.api.block.BlockTypes;
import org.spongepowered.api.block.tileentity.carrier.Dispenser;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.block.ChangeBlockEvent;
import org.spongepowered.api.event.cause.EventContextKeys;
import org.spongepowered.api.event.cause.entity.spawn.SpawnTypes;
import org.spongepowered.api.event.entity.SpawnEntityEvent;
import org.spongepowered.api.event.filter.cause.First;
import org.spongepowered.api.event.item.inventory.DropItemEvent;
import org.spongepowered.api.item.inventory.Inventory;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.world.LocatableBlock;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.StreamSupport;
import javax.annotation.Nullable;
@Module(id = "dispenserrecipes", name = "DispenserRecipes", onEnable = "onInitialize", onDisable = "onDisable")
public class DispenserRecipes extends SpongeBlockMechanic implements DocumentationProvider {
private List<DispenserRecipe> recipes = new ArrayList<>();
@Override
public void onInitialize() throws CraftBookException {
super.onInitialize();
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
public String getPath() {
return "mechanics/dispenser_recipes";
}
@Override
public boolean isValid(Location<World> location) {
return location.getBlockType() == BlockTypes.DISPENSER;
}
@Listener
public void onDispense(DropItemEvent.Dispense event, @First LocatableBlock dispenser) {
if (dispenser.getBlockState().getType() != BlockTypes.DISPENSER) {
return;
}
Dispenser dispenserTile = (Dispenser) dispenser.getLocation().getTileEntity().get();
if (handleDispenserAction(dispenserTile, event.getEntities().get(0).getVelocity())) {
event.setCancelled(true);
}
}
@Listener
public void onCreateEntity(SpawnEntityEvent event, @First LocatableBlock dispenser) {
event.getContext().get(EventContextKeys.SPAWN_TYPE).ifPresent(spawnType -> {
if (spawnType != SpawnTypes.DISPENSE || dispenser.getBlockState().getType() != BlockTypes.DISPENSER) {
return;
}
Dispenser dispenserTile = (Dispenser) dispenser.getLocation().getTileEntity().get();
if (handleDispenserAction(dispenserTile, event.getEntities().get(0).getVelocity())) {
event.setCancelled(true);
}
});
}
@Listener
public void onChangeBlock(ChangeBlockEvent event, @First LocatableBlock dispenser) {
if (dispenser.getBlockState().getType() != BlockTypes.DISPENSER) {
return;
}
Dispenser dispenserTile = (Dispenser) dispenser.getLocation().getTileEntity().get();
if (handleDispenserAction(dispenserTile, null)) {
event.setCancelled(true);
}
}
public boolean handleDispenserAction(Dispenser dispenser, @Nullable Vector3d velocity) {
for (DispenserRecipe recipe : recipes) {
ItemStack[] items = StreamSupport.stream(dispenser.getInventory().slots().spliterator(), false)
.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.
return true;
}
break;
}
}
return false;
}
}