Add ObserverTracer
Add ObserverTracerListener Signed-off-by: yoyosource <yoyosource@nidido.de>
Dieser Commit ist enthalten in:
Ursprung
72c4366e5d
Commit
dc8bc30b88
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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<BlockFace> ALLOWED = EnumSet.of(BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST, BlockFace.UP, BlockFace.DOWN);
|
||||
|
||||
private Set<BiPredicate<Block, BlockFace>> 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<Location> seen = new HashSet<>();
|
||||
List<Block> 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<Block, BlockFace> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
In neuem Issue referenzieren
Einen Benutzer sperren