-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
Bridge.java
276 lines (238 loc) · 13.2 KB
/
Bridge.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
* 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.area;
import com.google.common.collect.Lists;
import com.google.inject.Inject;
import com.me4502.modularframework.module.Module;
import com.me4502.modularframework.module.guice.ModuleConfiguration;
import com.sk89q.craftbook.core.util.ConfigValue;
import com.sk89q.craftbook.core.util.CraftBookException;
import com.sk89q.craftbook.core.util.PermissionNode;
import com.sk89q.craftbook.core.util.documentation.DocumentationProvider;
import com.sk89q.craftbook.sponge.mechanics.blockbags.BlockBag;
import com.sk89q.craftbook.sponge.mechanics.blockbags.EmbeddedBlockBag;
import com.sk89q.craftbook.sponge.mechanics.blockbags.MultiBlockBag;
import com.sk89q.craftbook.sponge.util.SpongeBlockFilter;
import com.sk89q.craftbook.sponge.util.BlockUtil;
import com.sk89q.craftbook.sponge.util.SignUtil;
import com.sk89q.craftbook.sponge.util.data.mutable.EmbeddedBlockBagData;
import com.sk89q.craftbook.sponge.util.locale.TranslationsManager;
import ninja.leaping.configurate.ConfigurationNode;
import org.spongepowered.api.block.BlockState;
import org.spongepowered.api.block.BlockTypes;
import org.spongepowered.api.block.tileentity.Sign;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.data.key.Keys;
import org.spongepowered.api.entity.EntityTypes;
import org.spongepowered.api.entity.Item;
import org.spongepowered.api.entity.living.Humanoid;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.TranslatableText;
import org.spongepowered.api.text.translation.ResourceBundleTranslation;
import org.spongepowered.api.util.Direction;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;
import java.util.List;
@Module(id = "bridge", name = "Bridge", onEnable="onInitialize", onDisable="onDisable")
public class Bridge extends SimpleArea implements DocumentationProvider {
@Inject
@ModuleConfiguration
public ConfigurationNode config;
private ConfigValue<Integer> maximumLength = new ConfigValue<>("maximum-length", "The maximum length the bridge can be.", 16);
private ConfigValue<Integer> maximumWidth = new ConfigValue<>("maximum-width", "The maximum width each side of the bridge can be. The overall max width is this*2 + 1.", 5);
private TranslatableText notAllowedMaterial = TranslatableText.of(new ResourceBundleTranslation("bridge.cant-use-material", TranslationsManager.getResourceBundleFunction()));
@Override
public void onInitialize() throws CraftBookException {
super.loadCommonConfig(config);
super.registerCommonPermissions();
maximumLength.load(config);
maximumWidth.load(config);
}
@Override
public boolean triggerMechanic(Location<World> block, Sign sign, Humanoid human, Boolean forceState) {
if (!"[Bridge End]".equals(SignUtil.getTextRaw(sign, 1))) {
Direction back = SignUtil.getBack(block);
Direction bridgeDirection = Direction.DOWN;
Location<World> baseBlock = block.getRelative(bridgeDirection);
if (block.getBlockType() == BlockTypes.WALL_SIGN) {
baseBlock = block.getRelative(SignUtil.getBack(block));
}
if(!BlockUtil.doesStatePassFilters(allowedBlocks.getValue(), baseBlock.getBlock())) {
if (block.getBlockType() != BlockTypes.WALL_SIGN) {
bridgeDirection = Direction.UP;
baseBlock = block.getRelative(bridgeDirection);
if(!BlockUtil.doesStatePassFilters(allowedBlocks.getValue(), baseBlock.getBlock())) {
if (human instanceof CommandSource) ((CommandSource) human).sendMessage(notAllowedMaterial);
return true;
}
} else {
if (human instanceof CommandSource) ((CommandSource) human).sendMessage(notAllowedMaterial);
return true;
}
}
Location<World> otherSide = BlockUtil.getNextMatchingSign(block, SignUtil.getBack(block), maximumLength.getValue(), this::isMechanicSign);
if (otherSide == null) {
if (human instanceof CommandSource) ((CommandSource) human).sendMessage(missingOtherEnd);
return true;
}
Location<World> otherBase = otherSide.getRelative(bridgeDirection);
if (otherSide.getBlockType() == BlockTypes.WALL_SIGN) {
otherBase = otherSide.getRelative(SignUtil.getBack(otherSide));
}
if(!baseBlock.getBlock().equals(otherBase.getBlock())) {
if (block.getBlockType() != BlockTypes.WALL_SIGN && bridgeDirection == Direction.DOWN) {
bridgeDirection = Direction.UP;
otherBase = otherSide.getRelative(bridgeDirection);
baseBlock = block.getRelative(bridgeDirection);
if(!baseBlock.getBlock().equals(otherBase.getBlock())) {
if (human instanceof CommandSource)
((CommandSource) human).sendMessage(Text.builder("Both ends must be the same material!").build());
return true;
}
} else {
if (human instanceof CommandSource)
((CommandSource) human).sendMessage(Text.builder("Both ends must be the same material!").build());
return true;
}
}
int leftBlocks, rightBlocks;
Location<World> left = baseBlock.getRelative(SignUtil.getLeft(block));
Location<World> right = baseBlock.getRelative(SignUtil.getRight(block));
//Calculate left distance
Location<World> otherLeft = otherBase.getRelative(SignUtil.getLeft(block));
leftBlocks = BlockUtil.getMinimumLength(left, otherLeft, baseBlock.getBlock(), SignUtil.getLeft(block), maximumWidth.getValue());
//Calculate right distance
Location<World> otherRight = otherBase.getRelative(SignUtil.getRight(block));
rightBlocks = BlockUtil.getMinimumLength(right, otherRight, baseBlock.getBlock(), SignUtil.getRight(block), maximumWidth.getValue());
baseBlock = baseBlock.getRelative(back);
BlockState type = block.getRelative(bridgeDirection).getBlock();
if (block.getBlockType() == BlockTypes.WALL_SIGN) {
type = block.getRelative(SignUtil.getBack(block)).getBlock();
}
if (baseBlock.getBlock().equals(type) || (forceState != null && !forceState)) type = BlockTypes.AIR.getDefaultState();
ItemStack blockBagItem = ItemStack.builder().fromBlockState(otherBase.getBlock()).quantity(1).build();
BlockBag blockBag = getBlockBag(sign.getLocation());
while (baseBlock.getBlockX() != otherBase.getBlockX() || baseBlock.getBlockZ() != otherBase.getBlockZ()) {
if (type.getType() == BlockTypes.AIR || blockBag.has(Lists.newArrayList(blockBagItem.copy()))) {
if (type.getType() == BlockTypes.AIR && baseBlock.getBlock().equals(otherBase.getBlock())) {
for (ItemStack leftover : blockBag.add(Lists.newArrayList(blockBagItem.copy()))) {
Item item = (Item) block.getExtent().createEntity(EntityTypes.ITEM, sign.getLocation().getPosition());
item.offer(Keys.REPRESENTED_ITEM, leftover.createSnapshot());
block.getExtent().spawnEntity(item);
}
} else if (type.getType() != BlockTypes.AIR && !baseBlock.getBlock().equals(otherBase.getBlock())) {
if (!blockBag.remove(Lists.newArrayList(blockBagItem.copy())).isEmpty()) {
continue;
}
}
baseBlock.setBlock(type);
left = baseBlock.getRelative(SignUtil.getLeft(block));
for(int i = 0; i < leftBlocks; i++) {
if (type.getType() == BlockTypes.AIR && left.getBlock().equals(otherBase.getBlock())) {
for (ItemStack leftover : blockBag.add(Lists.newArrayList(blockBagItem.copy()))) {
Item item = (Item) block.getExtent().createEntity(EntityTypes.ITEM, sign.getLocation().getPosition());
item.offer(Keys.REPRESENTED_ITEM, leftover.createSnapshot());
block.getExtent().spawnEntity(item);
}
} else if (type.getType() != BlockTypes.AIR && !left.getBlock().equals(otherBase.getBlock())) {
if (!blockBag.remove(Lists.newArrayList(blockBagItem.copy())).isEmpty()) {
continue;
}
}
left.setBlock(type);
left = left.getRelative(SignUtil.getLeft(block));
}
right = baseBlock.getRelative(SignUtil.getRight(block));
for(int i = 0; i < rightBlocks; i++) {
if (type.getType() == BlockTypes.AIR && right.getBlock().equals(otherBase.getBlock())) {
for (ItemStack leftover : blockBag.add(Lists.newArrayList(blockBagItem.copy()))) {
Item item = (Item) block.getExtent().createEntity(EntityTypes.ITEM, sign.getLocation().getPosition());
item.offer(Keys.REPRESENTED_ITEM, leftover.createSnapshot());
block.getExtent().spawnEntity(item);
}
} else if (type.getType() != BlockTypes.AIR && !right.getBlock().equals(otherBase.getBlock())) {
if (!blockBag.remove(Lists.newArrayList(blockBagItem.copy())).isEmpty()) {
continue;
}
}
right.setBlock(type);
right = right.getRelative(SignUtil.getRight(block));
}
baseBlock = baseBlock.getRelative(back);
} else {
if (human instanceof Player) {
((Player) human).sendMessage(Text.of("Out of blocks!"));
}
break;
}
}
if (blockBag instanceof EmbeddedBlockBag) {
sign.getLocation().offer(new EmbeddedBlockBagData((EmbeddedBlockBag) blockBag));
}
} else {
if (human instanceof CommandSource) ((CommandSource) human).sendMessage(Text.builder("Bridge not activatable from here!").build());
return false;
}
return true;
}
@Override
public BlockBag getBlockBag(Location<World> location) {
BlockBag mainBlockBag = super.getBlockBag(location);
Location<World> next = BlockUtil.getNextMatchingSign(location, SignUtil.getBack(location), maximumLength.getValue() + 2, this::isMechanicSign);
if (next != null) {
BlockBag nextBlockBag = super.getBlockBag(next);
if (nextBlockBag != null) {
return new MultiBlockBag(mainBlockBag, nextBlockBag);
}
}
return mainBlockBag;
}
@Override
public String[] getValidSigns() {
return new String[]{"[Bridge]", "[Bridge End]"};
}
@Override
public List<SpongeBlockFilter> getDefaultBlocks() {
List<SpongeBlockFilter> states = Lists.newArrayList();
states.add(new SpongeBlockFilter(BlockTypes.PLANKS));
states.add(new SpongeBlockFilter(BlockTypes.STONEBRICK));
states.add(new SpongeBlockFilter(BlockTypes.COBBLESTONE));
return states;
}
@Override
public String getPath() {
return "mechanics/bridge";
}
@Override
public ConfigValue<?>[] getConfigurationNodes() {
return new ConfigValue<?>[]{
allowedBlocks,
allowRedstone,
maximumLength,
maximumWidth
};
}
@Override
public PermissionNode[] getPermissionNodes() {
return new PermissionNode[]{
createPermissions,
usePermissions
};
}
}