Skip to content

Commit 4db3185

Browse files
Merge branch 'master' into chore/prevent-storages-in-storages
2 parents e37e2c5 + afd71ce commit 4db3185

File tree

9 files changed

+205
-13
lines changed

9 files changed

+205
-13
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@
124124
<url>https://jitpack.io</url>
125125
</repository>
126126
<repository>
127-
<id>jeff-media-public</id>
128-
<url>https://hub.jeff-media.com/nexus/repository/jeff-media-public/</url>
127+
<id>jeffMediaPublic</id>
128+
<url>https://repo.jeff-media.com/public</url>
129129
</repository>
130130
<repository>
131131
<id>mcmmo-repo</id>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.github.sefiraat.networks.listeners;
2+
3+
import javax.annotation.Nonnull;
4+
5+
import org.bukkit.event.EventHandler;
6+
import org.bukkit.event.EventPriority;
7+
import org.bukkit.event.Listener;
8+
import org.bukkit.event.block.BlockBreakEvent;
9+
import org.bukkit.event.block.BlockPlaceEvent;
10+
11+
import io.github.sefiraat.networks.utils.NetworkUtils;
12+
13+
public class SyncListener implements Listener {
14+
15+
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
16+
public void onBlockBreak(@Nonnull BlockBreakEvent event) {
17+
NetworkUtils.clearNetwork(event.getBlock().getLocation());
18+
}
19+
20+
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
21+
public void onBlockPlace(@Nonnull BlockPlaceEvent event) {
22+
NetworkUtils.clearNetwork(event.getBlock().getLocation());
23+
}
24+
}

src/main/java/io/github/sefiraat/networks/managers/ListenerManager.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
import io.github.sefiraat.networks.Networks;
44
import io.github.sefiraat.networks.listeners.ExplosiveToolListener;
5+
import io.github.sefiraat.networks.listeners.SyncListener;
6+
57
import org.bukkit.event.Listener;
68

79
public class ListenerManager {
810

911
public ListenerManager() {
1012
addListener(new ExplosiveToolListener());
13+
addListener(new SyncListener());
1114
}
1215

1316
private void addListener(Listener listener) {

src/main/java/io/github/sefiraat/networks/network/NetworkNode.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.github.sefiraat.networks.NetworkStorage;
44
import io.github.sefiraat.networks.Networks;
5+
import io.github.sefiraat.networks.slimefun.network.NetworkController;
56
import io.github.sefiraat.networks.slimefun.network.NetworkPowerNode;
67
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem;
78
import me.mrCookieSlime.Slimefun.api.BlockStorage;
@@ -103,6 +104,7 @@ public void addAllChildren() {
103104
// Kill additional controllers if it isn't the root
104105
if (testType == NodeType.CONTROLLER && !testLocation.equals(getRoot().nodePosition)) {
105106
killAdditionalController(testLocation);
107+
continue;
106108
}
107109

108110
// Check if it's in the network already and, if not, create a child node and propagate further.
@@ -132,7 +134,7 @@ public void run() {
132134
}
133135
};
134136
runnable.runTask(Networks.getInstance());
135-
NetworkStorage.getAllNetworkObjects().remove(location);
137+
NetworkController.wipeNetwork(location);
136138
}
137139
}
138140

src/main/java/io/github/sefiraat/networks/network/NetworkRoot.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public class NetworkRoot extends NetworkNode {
3737
private final int maxNodes;
3838
private boolean isOverburdened = false;
3939

40+
private Location controller = null;
4041
private final Set<Location> bridges = ConcurrentHashMap.newKeySet();
4142
private final Set<Location> monitors = ConcurrentHashMap.newKeySet();
4243
private final Set<Location> importers = ConcurrentHashMap.newKeySet();
@@ -69,14 +70,13 @@ public NetworkRoot(@Nonnull Location location, @Nonnull NodeType type, int maxNo
6970
super(location, type);
7071
this.maxNodes = maxNodes;
7172
this.root = this;
73+
registerNode(location, type);
7274
}
7375

7476
public void registerNode(@Nonnull Location location, @Nonnull NodeType type) {
7577
nodeLocations.add(location);
7678
switch (type) {
77-
case CONTROLLER -> {
78-
// Nothing here guvnor
79-
}
79+
case CONTROLLER -> this.controller = location;
8080
case BRIDGE -> bridges.add(location);
8181
case STORAGE_MONITOR -> monitors.add(location);
8282
case IMPORT -> importers.add(location);
@@ -131,6 +131,11 @@ public void setOverburdened(boolean overburdened) {
131131
this.isOverburdened = overburdened;
132132
}
133133

134+
@Nullable
135+
public Location getController() {
136+
return controller;
137+
}
138+
134139
public Set<Location> getBridges() {
135140
return this.bridges;
136141
}

src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkController.java

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
import io.github.sefiraat.networks.NetworkStorage;
44
import io.github.sefiraat.networks.network.NetworkNode;
55
import io.github.sefiraat.networks.network.NetworkRoot;
6+
import io.github.sefiraat.networks.network.NodeDefinition;
67
import io.github.sefiraat.networks.network.NodeType;
8+
import io.github.sefiraat.networks.utils.Theme;
9+
import io.github.thebusybiscuit.slimefun4.api.events.PlayerRightClickEvent;
710
import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup;
811
import io.github.thebusybiscuit.slimefun4.api.items.ItemSetting;
912
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem;
@@ -15,12 +18,14 @@
1518
import me.mrCookieSlime.Slimefun.api.BlockStorage;
1619
import org.bukkit.Location;
1720
import org.bukkit.block.Block;
21+
import org.bukkit.block.BlockFace;
1822
import org.bukkit.inventory.ItemStack;
1923

2024
import javax.annotation.Nonnull;
2125
import java.util.HashMap;
2226
import java.util.HashSet;
2327
import java.util.Map;
28+
import java.util.Optional;
2429
import java.util.Set;
2530

2631
public class NetworkController extends NetworkObject {
@@ -57,6 +62,11 @@ public void tick(Block block, SlimefunItem item, Config data) {
5762
NetworkRoot networkRoot = new NetworkRoot(block.getLocation(), NodeType.CONTROLLER, maxNodes.getValue());
5863
networkRoot.addAllChildren();
5964

65+
NodeDefinition definition = NetworkStorage.getAllNetworkObjects().get(block.getLocation());
66+
if (definition != null) {
67+
definition.setNode(networkRoot);
68+
}
69+
6070
boolean crayon = CRAYONS.contains(block.getLocation());
6171
if (crayon) {
6272
networkRoot.setDisplayParticles(true);
@@ -68,6 +78,45 @@ public void tick(Block block, SlimefunItem item, Config data) {
6878
);
6979
}
7080

81+
@Override
82+
protected void prePlace(@Nonnull PlayerRightClickEvent event) {
83+
Optional<Block> blockOptional = event.getClickedBlock();
84+
85+
if (blockOptional.isPresent()) {
86+
Block block = blockOptional.get();
87+
Block target = block.getRelative(event.getClickedFace());
88+
89+
for (BlockFace checkFace : CHECK_FACES) {
90+
Block checkBlock = target.getRelative(checkFace);
91+
SlimefunItem slimefunItem = BlockStorage.check(checkBlock);
92+
93+
// For directly adjacent controllers
94+
if (slimefunItem instanceof NetworkController) {
95+
cancelPlace(event);
96+
return;
97+
}
98+
99+
// Check for node definitions. If there isn't one, we don't care
100+
NodeDefinition definition = NetworkStorage.getAllNetworkObjects().get(checkBlock.getLocation());
101+
if (definition == null) {
102+
continue;
103+
}
104+
105+
// There is a definition, if it has a node, then it's part of an active network.
106+
if (definition.getNode() != null) {
107+
cancelPlace(event);
108+
return;
109+
}
110+
}
111+
}
112+
}
113+
114+
@Override
115+
protected void cancelPlace(PlayerRightClickEvent event) {
116+
event.getPlayer().sendMessage(Theme.ERROR.getColor() + "This network already has a controller!");
117+
event.cancel();
118+
}
119+
71120
private void onFirstTick(@Nonnull Block block, @Nonnull Config data) {
72121
final String crayon = data.getString(CRAYON);
73122
if (Boolean.parseBoolean(crayon)) {
@@ -98,8 +147,11 @@ public static boolean hasCrayon(@Nonnull Location location) {
98147
}
99148

100149
public static void wipeNetwork(@Nonnull Location location) {
101-
for (NetworkNode node : NETWORKS.remove(location).getChildrenNodes()) {
102-
NetworkStorage.removeNode(node.getNodePosition());
150+
NetworkRoot networkRoot = NETWORKS.remove(location);
151+
if (networkRoot != null) {
152+
for (NetworkNode node : networkRoot.getChildrenNodes()) {
153+
NetworkStorage.removeNode(node.getNodePosition());
154+
}
103155
}
104156
}
105157
}

src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkObject.java

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,38 @@
11
package io.github.sefiraat.networks.slimefun.network;
22

33
import io.github.sefiraat.networks.NetworkStorage;
4+
import io.github.sefiraat.networks.network.NetworkRoot;
45
import io.github.sefiraat.networks.network.NodeDefinition;
56
import io.github.sefiraat.networks.network.NodeType;
7+
import io.github.sefiraat.networks.utils.Theme;
8+
import io.github.thebusybiscuit.slimefun4.api.events.PlayerRightClickEvent;
69
import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup;
710
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem;
811
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack;
912
import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType;
1013
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockBreakHandler;
14+
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockPlaceHandler;
15+
import io.github.thebusybiscuit.slimefun4.core.handlers.ItemUseHandler;
16+
import io.github.thebusybiscuit.slimefun4.implementation.items.blocks.UnplaceableBlock;
17+
1118
import lombok.Getter;
1219
import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config;
1320
import me.mrCookieSlime.Slimefun.Objects.handlers.BlockTicker;
1421
import me.mrCookieSlime.Slimefun.api.BlockStorage;
1522
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu;
1623
import org.bukkit.Location;
1724
import org.bukkit.block.Block;
25+
import org.bukkit.block.BlockFace;
1826
import org.bukkit.event.block.BlockBreakEvent;
27+
import org.bukkit.event.block.BlockPlaceEvent;
1928
import org.bukkit.inventory.ItemStack;
2029

2130
import javax.annotation.Nonnull;
2231
import javax.annotation.ParametersAreNonnullByDefault;
2332
import java.util.ArrayList;
2433
import java.util.List;
34+
import java.util.Optional;
35+
import java.util.Set;
2536

2637
public abstract class NetworkObject extends SlimefunItem implements AdminDebuggable {
2738

@@ -30,6 +41,16 @@ public abstract class NetworkObject extends SlimefunItem implements AdminDebugga
3041
@Getter
3142
private final List<Integer> slotsToDrop = new ArrayList<>();
3243

44+
protected static final Set<BlockFace> CHECK_FACES = Set.of(
45+
BlockFace.UP,
46+
BlockFace.DOWN,
47+
BlockFace.NORTH,
48+
BlockFace.SOUTH,
49+
BlockFace.EAST,
50+
BlockFace.WEST
51+
);
52+
53+
3354
protected NetworkObject(ItemGroup itemGroup, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, NodeType type) {
3455
this(itemGroup, item, recipeType, recipe, null, type);
3556
}
@@ -57,6 +78,18 @@ public void onPlayerBreak(BlockBreakEvent event, ItemStack item, List<ItemStack>
5778
preBreak(event);
5879
onBreak(event);
5980
}
81+
},
82+
new BlockPlaceHandler(false) {
83+
@Override
84+
public void onPlayerPlace(@Nonnull BlockPlaceEvent blockPlaceEvent) {
85+
onPlace(blockPlaceEvent);
86+
}
87+
},
88+
new ItemUseHandler() {
89+
@Override
90+
public void onRightClick(PlayerRightClickEvent playerRightClickEvent) {
91+
prePlace(playerRightClickEvent);
92+
}
6093
}
6194
);
6295
}
@@ -81,13 +114,55 @@ protected void onBreak(@Nonnull BlockBreakEvent event) {
81114
blockMenu.dropItems(location, i);
82115
}
83116
}
84-
NetworkStorage.removeNode(location);
117+
// NetworkStorage.removeNode(location);
118+
//
119+
// if (this.nodeType == NodeType.CONTROLLER) {
120+
// NetworkController.wipeNetwork(location);
121+
// }
122+
123+
BlockStorage.clearBlockInfo(location);
124+
}
85125

86-
if (this.nodeType == NodeType.CONTROLLER) {
87-
NetworkController.wipeNetwork(location);
126+
protected void prePlace(@Nonnull PlayerRightClickEvent event) {
127+
Optional<Block> blockOptional = event.getClickedBlock();
128+
Location controllerLocation = null;
129+
130+
if (blockOptional.isPresent()) {
131+
Block block = blockOptional.get();
132+
Block target = block.getRelative(event.getClickedFace());
133+
134+
addToRegistry(block);
135+
for (BlockFace checkFace : CHECK_FACES) {
136+
Block checkBlock = target.getRelative(checkFace);
137+
138+
// Check for node definitions. If there isn't one, we don't care
139+
NodeDefinition definition = NetworkStorage.getAllNetworkObjects().get(checkBlock.getLocation());
140+
if (definition == null) {
141+
continue;
142+
}
143+
144+
// There is a definition, if it has a node, then it's part of an active network.
145+
if (definition.getNode() != null) {
146+
NetworkRoot networkRoot = definition.getNode().getRoot();
147+
if (controllerLocation == null) {
148+
// First network found, store root location
149+
controllerLocation = networkRoot.getController();
150+
} else if (!controllerLocation.equals(networkRoot.getController())) {
151+
// Location differs from that previously recorded, would result in two controllers
152+
cancelPlace(event);
153+
}
154+
}
155+
}
88156
}
157+
}
158+
159+
protected void cancelPlace(PlayerRightClickEvent event) {
160+
event.getPlayer().sendMessage(Theme.ERROR.getColor() + "This placement would connect two controllers!");
161+
event.cancel();
162+
}
163+
164+
protected void onPlace(@Nonnull BlockPlaceEvent event) {
89165

90-
BlockStorage.clearBlockInfo(location);
91166
}
92167

93168
public boolean runSync() {

src/main/java/io/github/sefiraat/networks/slimefun/tools/CraftingBlueprint.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,28 @@
99
import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup;
1010
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack;
1111
import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType;
12+
import io.github.thebusybiscuit.slimefun4.core.attributes.DistinctiveItem;
1213
import io.github.thebusybiscuit.slimefun4.implementation.items.blocks.UnplaceableBlock;
1314
import org.bukkit.ChatColor;
1415
import org.bukkit.inventory.ItemStack;
1516
import org.bukkit.inventory.meta.ItemMeta;
1617

18+
import javax.annotation.Nonnull;
1719
import javax.annotation.ParametersAreNonnullByDefault;
1820
import java.util.ArrayList;
1921
import java.util.List;
2022

21-
public class CraftingBlueprint extends UnplaceableBlock {
23+
public class CraftingBlueprint extends UnplaceableBlock implements DistinctiveItem {
2224

2325
public CraftingBlueprint(ItemGroup itemGroup, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) {
2426
super(itemGroup, item, recipeType, recipe);
2527
}
2628

29+
@Override
30+
public boolean canStack(@Nonnull ItemMeta itemMetaOne, @Nonnull ItemMeta itemMetaTwo) {
31+
return itemMetaOne.getPersistentDataContainer().equals(itemMetaTwo.getPersistentDataContainer());
32+
}
33+
2734
@ParametersAreNonnullByDefault
2835
public static void setBlueprint(ItemStack blueprint, ItemStack[] recipe, ItemStack output) {
2936
final ItemMeta itemMeta = blueprint.getItemMeta();

0 commit comments

Comments
 (0)