Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	pom.xml
#	src/main/java/io/github/sefiraat/networks/network/NetworkRoot.java
#	src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkController.java
#	src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkObject.java
  • Loading branch information
ybw0014 committed Sep 8, 2024
2 parents 672c505 + b2e783e commit 1da39f0
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 11 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,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 @@ -136,7 +136,7 @@
<dependency>
<groupId>com.github.SlimefunGuguProject</groupId>
<artifactId>Slimefun4</artifactId>
<version>38953fe13a</version>
<version>b39097e015</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 @@ -103,6 +103,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 @@ -134,7 +135,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,16 +70,13 @@ public NetworkRoot(@Nonnull Location location, @Nonnull NodeType type, int maxNo
super(location, type);
this.maxNodes = maxNodes;
this.root = this;
NetworkNode node = new NetworkNode(location, NodeType.CONTROLLER);
io.github.sefiraat.networks.NetworkStorage.getAllNetworkObjects().get(location).setNode(node);
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 @@ -133,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 @@ -5,7 +5,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.Objects.handlers.BlockTicker;
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, SlimefunBlockData 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, SlimefunBlockData 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 SlimefunBlockData data) {
final String crayon = data.getData(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
Expand Up @@ -3,26 +3,37 @@
import com.xzavier0722.mc.plugin.slimefun4.storage.controller.SlimefunBlockData;
import com.xzavier0722.mc.plugin.slimefun4.storage.util.StorageCacheUtils;
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.implementation.Slimefun;
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.Slimefun.Objects.handlers.BlockTicker;
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 @@ -31,6 +42,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 @@ -58,6 +79,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 Down Expand Up @@ -86,6 +119,48 @@ protected void onBreak(@Nonnull BlockBreakEvent event) {
Slimefun.getDatabaseManager().getBlockDataController().removeBlock(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) {

}

public boolean runSync() {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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;
Expand All @@ -25,6 +26,11 @@ public CraftingBlueprint(ItemGroup itemGroup, SlimefunItemStack item, RecipeType
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
24 changes: 24 additions & 0 deletions src/main/java/io/github/sefiraat/networks/utils/NetworkUtils.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package io.github.sefiraat.networks.utils;

import de.jeff_media.morepersistentdatatypes.DataType;

import io.github.sefiraat.networks.NetworkStorage;
import io.github.sefiraat.networks.network.NetworkNode;
import io.github.sefiraat.networks.network.NodeDefinition;
import io.github.sefiraat.networks.network.NodeType;
import io.github.sefiraat.networks.slimefun.network.NetworkController;
import io.github.sefiraat.networks.slimefun.network.NetworkDirectional;
import io.github.sefiraat.networks.slimefun.network.NetworkPusher;
import io.github.sefiraat.networks.slimefun.tools.NetworkConfigurator;
import io.github.sefiraat.networks.utils.datatypes.DataTypeMethods;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem;
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -76,4 +84,20 @@ public static void applyConfig(@Nonnull NetworkDirectional directional, @Nonnull
player.sendMessage(Theme.WARNING + "Items: " + Theme.PASSIVE + "No items in stored config");
}
}

public static void clearNetwork(Location location) {
NodeDefinition definition = NetworkStorage.getAllNetworkObjects().get(location);

if (definition == null || definition.getNode() == null) {
return;
}

NetworkNode node = definition.getNode();

if (node != null && node.getNodeType() == NodeType.CONTROLLER) {
NetworkController.wipeNetwork(location);
}

NetworkStorage.removeNode(location);
}
}

0 comments on commit 1da39f0

Please sign in to comment.