Skip to content

Commit

Permalink
Merge pull request #32 from Sefiraat/master
Browse files Browse the repository at this point in the history
[pull] master from Sefiraat:master
  • Loading branch information
weiii-joe authored Sep 5, 2023
2 parents 3205f47 + 3de3c9d commit d35d277
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.gmail.nossr50.mcMMO;
import dev.sefiraat.sefilib.misc.ParticleUtils;
import dev.sefiraat.sefilib.world.LocationUtils;
import io.github.bakedlibs.dough.blocks.BlockPosition;
import io.github.sefiraat.networks.NetworkStorage;
import io.github.sefiraat.networks.Networks;
import io.github.sefiraat.networks.managers.SupportedPluginManager;
Expand Down Expand Up @@ -31,6 +32,8 @@

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

public class NetworkControlV extends NetworkDirectional {
Expand All @@ -48,6 +51,8 @@ public class NetworkControlV extends NetworkDirectional {
private static final int DOWN_SLOT = 32;
private static final int REQUIRED_POWER = 100;

private final Set<BlockPosition> blockCache = new HashSet<>();

public static final CustomItemStack TEMPLATE_BACKGROUND_STACK = new CustomItemStack(
Material.BLUE_STAINED_GLASS_PANE, Theme.PASSIVE + "貼上符合範本的物品"
);
Expand All @@ -65,6 +70,11 @@ protected void onTick(@Nullable BlockMenu blockMenu, @Nonnull Block block) {
}
}

@Override
protected void onUniqueTick() {
blockCache.clear();
}

private void tryPasteBlock(@Nonnull BlockMenu blockMenu) {
final NodeDefinition definition = NetworkStorage.getAllNetworkObjects().get(blockMenu.getLocation());

Expand All @@ -83,6 +93,12 @@ private void tryPasteBlock(@Nonnull BlockMenu blockMenu) {
}

final Block targetBlock = blockMenu.getBlock().getRelative(direction);
final BlockPosition targetPosition = new BlockPosition(targetBlock);

if (this.blockCache.contains(targetPosition)) {
return;
}

final Material material = targetBlock.getType();

if (!material.isAir()) {
Expand Down Expand Up @@ -121,6 +137,7 @@ private void tryPasteBlock(@Nonnull BlockMenu blockMenu) {
return;
}

this.blockCache.add(targetPosition);
Bukkit.getScheduler().runTask(Networks.getInstance(), bukkitTask -> {
targetBlock.setType(fetchedStack.getType(), true);
if (SupportedPluginManager.getInstance().isMcMMO()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dev.sefiraat.sefilib.misc.ParticleUtils;
import dev.sefiraat.sefilib.world.LocationUtils;
import io.github.bakedlibs.dough.blocks.BlockPosition;
import io.github.sefiraat.networks.NetworkStorage;
import io.github.sefiraat.networks.Networks;
import io.github.sefiraat.networks.network.NodeDefinition;
Expand Down Expand Up @@ -30,6 +31,8 @@

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

public class NetworkControlX extends NetworkDirectional {
Expand All @@ -47,6 +50,8 @@ public class NetworkControlX extends NetworkDirectional {
private static final int DOWN_SLOT = 32;
private static final int REQUIRED_POWER = 100;

private final Set<BlockPosition> blockCache = new HashSet<>();

public static final CustomItemStack TEMPLATE_BACKGROUND_STACK = new CustomItemStack(
Material.BLUE_STAINED_GLASS_PANE,
Theme.PASSIVE + "剪下符合範本的物品。",
Expand All @@ -66,6 +71,11 @@ protected void onTick(@Nullable BlockMenu blockMenu, @Nonnull Block block) {
}
}

@Override
protected void onUniqueTick() {
this.blockCache.clear();
}

private void tryBreakBlock(@Nonnull BlockMenu blockMenu) {
final NodeDefinition definition = NetworkStorage.getAllNetworkObjects().get(blockMenu.getLocation());

Expand All @@ -84,6 +94,12 @@ private void tryBreakBlock(@Nonnull BlockMenu blockMenu) {
}

final Block targetBlock = blockMenu.getBlock().getRelative(direction);
final BlockPosition targetPosition = new BlockPosition(targetBlock);

if (this.blockCache.contains(targetPosition)) {
return;
}

final Material material = targetBlock.getType();

if (material.getHardness() < 0 || material.isAir()) {
Expand Down Expand Up @@ -117,6 +133,7 @@ private void tryBreakBlock(@Nonnull BlockMenu blockMenu) {
definition.getNode().getRoot().addItemStack(resultStack);

if (resultStack.getAmount() == 0) {
this.blockCache.add(targetPosition);
Bukkit.getScheduler().runTask(Networks.getInstance(), bukkitTask -> {
final BlockStateSnapshotResult blockState = PaperLib.getBlockState(targetBlock, true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ public void tick(Block block, SlimefunItem slimefunItem, Config config) {
@Override
public void uniqueTick() {
tick = tick <= 1 ? tickRate.getValue() : tick - 1;
if (tick <= 1) {
onUniqueTick();
}
}
}
);
Expand Down Expand Up @@ -163,6 +166,8 @@ protected void onTick(@Nullable BlockMenu blockMenu, @Nonnull Block block) {
updateGui(blockMenu);
}

protected void onUniqueTick() {}

@Override
public void postRegister() {
new BlockMenuPreset(this.getId(), this.getItemName()) {
Expand Down

0 comments on commit d35d277

Please sign in to comment.