Skip to content

Commit

Permalink
Merge pull request Sefiraat#214 from Sefiraat/fix/network-fixes
Browse files Browse the repository at this point in the history
Fix Dual controllers, ghost block sync and distinctive blueprints
  • Loading branch information
Sefiraat authored Sep 8, 2024
2 parents bb71d96 + eeb327a commit b2e783e
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 14 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@
<url>https://jitpack.io</url>
</repository>
<repository>
<id>jeff-media-public</id>
<url>https://hub.jeff-media.com/nexus/repository/jeff-media-public/</url>
<id>jeffMediaPublic</id>
<url>https://repo.jeff-media.com/public</url>
</repository>
<repository>
<id>mcmmo-repo</id>
Expand All @@ -148,7 +148,7 @@
<dependency>
<groupId>com.github.Slimefun</groupId>
<artifactId>Slimefun4</artifactId>
<version>03e5b9a</version>
<version>f1363ceadf</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.github.sefiraat.networks.listeners;

import javax.annotation.Nonnull;

import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;

import io.github.sefiraat.networks.utils.NetworkUtils;

public class SyncListener {

@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onBlockBreak(@Nonnull BlockBreakEvent event) {
NetworkUtils.clearNetwork(event.getBlock().getLocation());
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBlockPlace(@Nonnull BlockPlaceEvent event) {
NetworkUtils.clearNetwork(event.getBlock().getLocation());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

// Check if it's in the network already and, if not, create a child node and propagate further.
Expand Down Expand Up @@ -132,7 +134,7 @@ public void run() {
}
};
runnable.runTask(Networks.getInstance());
NetworkStorage.getAllNetworkObjects().remove(location);
NetworkController.wipeNetwork(location);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class NetworkRoot extends NetworkNode {
private final int maxNodes;
private boolean isOverburdened = false;

private Location controller = null;
private final Set<Location> bridges = ConcurrentHashMap.newKeySet();
private final Set<Location> monitors = ConcurrentHashMap.newKeySet();
private final Set<Location> importers = ConcurrentHashMap.newKeySet();
Expand Down Expand Up @@ -69,14 +70,13 @@ public NetworkRoot(@Nonnull Location location, @Nonnull NodeType type, int maxNo
super(location, type);
this.maxNodes = maxNodes;
this.root = this;
registerNode(location, type);
}

public void registerNode(@Nonnull Location location, @Nonnull NodeType type) {
nodeLocations.add(location);
switch (type) {
case CONTROLLER -> {
// Nothing here guvnor
}
case CONTROLLER -> this.controller = location;
case BRIDGE -> bridges.add(location);
case STORAGE_MONITOR -> monitors.add(location);
case IMPORT -> importers.add(location);
Expand Down Expand Up @@ -131,6 +131,11 @@ public void setOverburdened(boolean overburdened) {
this.isOverburdened = overburdened;
}

@Nullable
public Location getController() {
return controller;
}

public Set<Location> getBridges() {
return this.bridges;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import io.github.sefiraat.networks.NetworkStorage;
import io.github.sefiraat.networks.network.NetworkNode;
import io.github.sefiraat.networks.network.NetworkRoot;
import io.github.sefiraat.networks.network.NodeDefinition;
import io.github.sefiraat.networks.network.NodeType;
import io.github.sefiraat.networks.utils.Theme;
import io.github.thebusybiscuit.slimefun4.api.events.PlayerRightClickEvent;
import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup;
import io.github.thebusybiscuit.slimefun4.api.items.ItemSetting;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem;
Expand All @@ -15,12 +18,14 @@
import me.mrCookieSlime.Slimefun.api.BlockStorage;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.inventory.ItemStack;

import javax.annotation.Nonnull;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

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

NodeDefinition definition = NetworkStorage.getAllNetworkObjects().get(block.getLocation());
if (definition != null) {
definition.setNode(networkRoot);
}

boolean crayon = CRAYONS.contains(block.getLocation());
if (crayon) {
networkRoot.setDisplayParticles(true);
Expand All @@ -68,6 +78,45 @@ public void tick(Block block, SlimefunItem item, Config data) {
);
}

@Override
protected void prePlace(@Nonnull PlayerRightClickEvent event) {
Optional<Block> blockOptional = event.getClickedBlock();

if (blockOptional.isPresent()) {
Block block = blockOptional.get();
Block target = block.getRelative(event.getClickedFace());

for (BlockFace checkFace : CHECK_FACES) {
Block checkBlock = target.getRelative(checkFace);
SlimefunItem slimefunItem = BlockStorage.check(checkBlock);

// For directly adjacent controllers
if (slimefunItem instanceof NetworkController) {
cancelPlace(event);
return;
}

// Check for node definitions. If there isn't one, we don't care
NodeDefinition definition = NetworkStorage.getAllNetworkObjects().get(checkBlock.getLocation());
if (definition == null) {
continue;
}

// There is a definition, if it has a node, then it's part of an active network.
if (definition.getNode() != null) {
cancelPlace(event);
return;
}
}
}
}

@Override
protected void cancelPlace(PlayerRightClickEvent event) {
event.getPlayer().sendMessage(Theme.ERROR.getColor() + "This network already has a controller!");
event.cancel();
}

private void onFirstTick(@Nonnull Block block, @Nonnull Config data) {
final String crayon = data.getString(CRAYON);
if (Boolean.parseBoolean(crayon)) {
Expand Down Expand Up @@ -98,8 +147,11 @@ public static boolean hasCrayon(@Nonnull Location location) {
}

public static void wipeNetwork(@Nonnull Location location) {
for (NetworkNode node : NETWORKS.remove(location).getChildrenNodes()) {
NetworkStorage.removeNode(node.getNodePosition());
NetworkRoot networkRoot = NETWORKS.remove(location);
if (networkRoot != null) {
for (NetworkNode node : networkRoot.getChildrenNodes()) {
NetworkStorage.removeNode(node.getNodePosition());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,38 @@
package io.github.sefiraat.networks.slimefun.network;

import io.github.sefiraat.networks.NetworkStorage;
import io.github.sefiraat.networks.network.NetworkRoot;
import io.github.sefiraat.networks.network.NodeDefinition;
import io.github.sefiraat.networks.network.NodeType;
import io.github.sefiraat.networks.utils.Theme;
import io.github.thebusybiscuit.slimefun4.api.events.PlayerRightClickEvent;
import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack;
import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockBreakHandler;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockPlaceHandler;
import io.github.thebusybiscuit.slimefun4.core.handlers.ItemUseHandler;
import io.github.thebusybiscuit.slimefun4.implementation.items.blocks.UnplaceableBlock;

import lombok.Getter;
import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config;
import me.mrCookieSlime.Slimefun.Objects.handlers.BlockTicker;
import me.mrCookieSlime.Slimefun.api.BlockStorage;
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.inventory.ItemStack;

import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;

public abstract class NetworkObject extends SlimefunItem implements AdminDebuggable {

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

protected static final Set<BlockFace> CHECK_FACES = Set.of(
BlockFace.UP,
BlockFace.DOWN,
BlockFace.NORTH,
BlockFace.SOUTH,
BlockFace.EAST,
BlockFace.WEST
);


protected NetworkObject(ItemGroup itemGroup, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, NodeType type) {
this(itemGroup, item, recipeType, recipe, null, type);
}
Expand Down Expand Up @@ -57,6 +78,18 @@ public void onPlayerBreak(BlockBreakEvent event, ItemStack item, List<ItemStack>
preBreak(event);
onBreak(event);
}
},
new BlockPlaceHandler(false) {
@Override
public void onPlayerPlace(@Nonnull BlockPlaceEvent blockPlaceEvent) {
onPlace(blockPlaceEvent);
}
},
new ItemUseHandler() {
@Override
public void onRightClick(PlayerRightClickEvent playerRightClickEvent) {
prePlace(playerRightClickEvent);
}
}
);
}
Expand All @@ -81,13 +114,55 @@ protected void onBreak(@Nonnull BlockBreakEvent event) {
blockMenu.dropItems(location, i);
}
}
NetworkStorage.removeNode(location);
// NetworkStorage.removeNode(location);
//
// if (this.nodeType == NodeType.CONTROLLER) {
// NetworkController.wipeNetwork(location);
// }

BlockStorage.clearBlockInfo(location);
}

if (this.nodeType == NodeType.CONTROLLER) {
NetworkController.wipeNetwork(location);
protected void prePlace(@Nonnull PlayerRightClickEvent event) {
Optional<Block> blockOptional = event.getClickedBlock();
Location controllerLocation = null;

if (blockOptional.isPresent()) {
Block block = blockOptional.get();
Block target = block.getRelative(event.getClickedFace());

addToRegistry(block);
for (BlockFace checkFace : CHECK_FACES) {
Block checkBlock = target.getRelative(checkFace);

// Check for node definitions. If there isn't one, we don't care
NodeDefinition definition = NetworkStorage.getAllNetworkObjects().get(checkBlock.getLocation());
if (definition == null) {
continue;
}

// There is a definition, if it has a node, then it's part of an active network.
if (definition.getNode() != null) {
NetworkRoot networkRoot = definition.getNode().getRoot();
if (controllerLocation == null) {
// First network found, store root location
controllerLocation = networkRoot.getController();
} else if (!controllerLocation.equals(networkRoot.getController())) {
// Location differs from that previously recorded, would result in two controllers
cancelPlace(event);
}
}
}
}
}

protected void cancelPlace(PlayerRightClickEvent event) {
event.getPlayer().sendMessage(Theme.ERROR.getColor() + "This placement would connect two controllers!");
event.cancel();
}

protected void onPlace(@Nonnull BlockPlaceEvent event) {

BlockStorage.clearBlockInfo(location);
}

public boolean runSync() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,28 @@
import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack;
import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType;
import io.github.thebusybiscuit.slimefun4.core.attributes.DistinctiveItem;
import io.github.thebusybiscuit.slimefun4.implementation.items.blocks.UnplaceableBlock;
import org.bukkit.ChatColor;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.ArrayList;
import java.util.List;

public class CraftingBlueprint extends UnplaceableBlock {
public class CraftingBlueprint extends UnplaceableBlock implements DistinctiveItem {

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

@Override
public boolean canStack(@Nonnull ItemMeta itemMetaOne, @Nonnull ItemMeta itemMetaTwo) {
return itemMetaOne.getPersistentDataContainer().equals(itemMetaTwo.getPersistentDataContainer());
}

@ParametersAreNonnullByDefault
public static void setBlueprint(ItemStack blueprint, ItemStack[] recipe, ItemStack output) {
final ItemMeta itemMeta = blueprint.getItemMeta();
Expand Down
Loading

0 comments on commit b2e783e

Please sign in to comment.