From dc8bc30b88c9eb0d97b2972bb109d9fd258eb691 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 19 Jun 2021 23:49:49 +0200 Subject: [PATCH] Add ObserverTracer Add ObserverTracerListener Signed-off-by: yoyosource --- .../features/observer/ObserverTracer.java | 89 +++++++++++++++++++ .../observer/ObserverTracerListener.java | 42 +++++++++ 2 files changed, 131 insertions(+) create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/observer/ObserverTracer.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/observer/ObserverTracerListener.java diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/observer/ObserverTracer.java b/BauSystem_Main/src/de/steamwar/bausystem/features/observer/ObserverTracer.java new file mode 100644 index 00000000..bf4871bf --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/observer/ObserverTracer.java @@ -0,0 +1,89 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bausystem.features.observer; + +import lombok.experimental.UtilityClass; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.Particle; +import org.bukkit.block.*; +import org.bukkit.block.Dispenser; +import org.bukkit.block.Hopper; +import org.bukkit.block.data.Powerable; +import org.bukkit.block.data.type.*; +import org.bukkit.block.data.type.Observer; +import org.bukkit.entity.Player; + +import java.util.*; +import java.util.function.BiPredicate; + +@UtilityClass +public class ObserverTracer { + + private final Set ALLOWED = EnumSet.of(BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST, BlockFace.UP, BlockFace.DOWN); + + private Set> predicates = new HashSet<>(); + + static { + predicates.add((block, blockFace) -> { + if (block.getType() != Material.OBSERVER) { + return false; + } + Observer observer = (Observer) block.getBlockData(); + return observer.getFacing() == blockFace.getOppositeFace(); + }); + } + + public void trace(Player player, Block block) { + if (block.getType() != Material.OBSERVER) { + return; + } + + Set seen = new HashSet<>(); + List blockList = new ArrayList<>(); + blockList.add(block); + while (!blockList.isEmpty()) { + Block b = blockList.remove(0); + for (BlockFace blockFace : ALLOWED) { + Location location = b.getLocation().add(blockFace.getModX(), blockFace.getModY(), blockFace.getModZ()); + Block toCheck = location.getBlock(); + if (!seen.add(location)) { + continue; + } + + for (BiPredicate predicate : predicates) { + if (predicate.test(toCheck, blockFace)) { + blockList.add(toCheck); + player.spawnParticle(Particle.FLAME, location.clone().add(0, 0, 0), 1, 0, 0, 0, 0); + player.spawnParticle(Particle.FLAME, location.clone().add(1, 0, 0), 1, 0, 0, 0, 0); + player.spawnParticle(Particle.FLAME, location.clone().add(1, 0, 1), 1, 0, 0, 0, 0); + player.spawnParticle(Particle.FLAME, location.clone().add(0, 0, 1), 1, 0, 0, 0, 0); + player.spawnParticle(Particle.FLAME, location.clone().add(0, 1, 0), 1, 0, 0, 0, 0); + player.spawnParticle(Particle.FLAME, location.clone().add(1, 1, 0), 1, 0, 0, 0, 0); + player.spawnParticle(Particle.FLAME, location.clone().add(1, 1, 1), 1, 0, 0, 0, 0); + player.spawnParticle(Particle.FLAME, location.clone().add(0, 1, 1), 1, 0, 0, 0, 0); + break; + } + } + } + } + } + +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/observer/ObserverTracerListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/observer/ObserverTracerListener.java new file mode 100644 index 00000000..42428323 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/observer/ObserverTracerListener.java @@ -0,0 +1,42 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bausystem.features.observer; + +import de.steamwar.bausystem.linkage.LinkageType; +import de.steamwar.bausystem.linkage.Linked; +import org.bukkit.Material; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; +import org.bukkit.event.player.PlayerInteractEvent; + +@Linked(LinkageType.LISTENER) +public class ObserverTracerListener implements Listener { + + @EventHandler + public void onPlayerInteract(PlayerInteractEvent event) { + if (event.getAction() != Action.RIGHT_CLICK_BLOCK) { + return; + } + if (event.getClickedBlock().getType() == Material.OBSERVER) { + ObserverTracer.trace(event.getPlayer(), event.getClickedBlock()); + } + } +}