SteamWar/BauSystem
Archiviert
13
0

Add CommandRedstoneTester

Add RedstoneListener
Dieser Commit ist enthalten in:
yoyosource 2021-03-07 12:31:36 +01:00
Ursprung d07f7d773b
Commit 0c17692fc3
4 geänderte Dateien mit 209 neuen und 1 gelöschten Zeilen

Datei anzeigen

@ -99,6 +99,7 @@ public class BauSystem extends JavaPlugin implements Listener {
getCommand("script").setExecutor(new CommandScript());
getCommand("simulator").setExecutor(new CommandSimulator());
getCommand("simulator").setTabCompleter(new CommandSimulatorTabCompleter());
getCommand("redstonetester").setExecutor(new CommandRedstoneTester());
getCommand("gui").setExecutor(new CommandGUI());
Bukkit.getPluginManager().registerEvents(this, this);
@ -109,6 +110,7 @@ public class BauSystem extends JavaPlugin implements Listener {
Bukkit.getPluginManager().registerEvents(new TNTSimulatorListener(), this);
Bukkit.getPluginManager().registerEvents(new CommandGUI(), this);
Bukkit.getPluginManager().registerEvents(new DetonatorListener(), this);
Bukkit.getPluginManager().registerEvents(new RedstoneListener(), this);
new AFKStopper();
autoShutdown = Bukkit.getScheduler().runTaskLater(this, Bukkit::shutdown, 1200);

Datei anzeigen

@ -0,0 +1,40 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 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.commands;
import de.steamwar.bausystem.PlayerUtils;
import de.steamwar.bausystem.world.RedstoneListener;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class CommandRedstoneTester implements CommandExecutor {
@Override
public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) {
if (!(commandSender instanceof Player))
return false;
Player player = (Player) commandSender;
PlayerUtils.giveItemToPlayer(player, RedstoneListener.WAND);
return false;
}
}

Datei anzeigen

@ -0,0 +1,165 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 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.world;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.inventory.SWItem;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.Powerable;
import org.bukkit.block.data.type.Piston;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.*;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class RedstoneListener implements Listener {
private static long currentTick = 0;
static {
Bukkit.getScheduler().runTaskTimer(BauSystem.getPlugin(), () -> {
currentTick++;
}, 1, 1);
}
private static class RedstoneTester {
private final Player player;
private Location loc1 = null;
private Location loc2 = null;
private Location activated = null;
private long tick = 0;
public RedstoneTester(Player player) {
this.player = player;
}
private void activate(Location location) {
if (loc1 != null && loc1.equals(location)) {
if (activated != null && !activated.equals(location)) {
player.sendMessage("§7Aktivierungsdifferenz§8: §e" + (currentTick - tick) + " §8(§7" + locationToString(activated) + " §8->§7 " + locationToString(location) + "§8)");
activated = null;
return;
}
activated = loc1;
tick = currentTick;
} else if (loc2 != null && loc2.equals(location)) {
if (activated != null && !activated.equals(location)) {
player.sendMessage("§7Aktivierungsdifferenz§8: §e" + (currentTick - tick) + " §8(§7" + locationToString(activated) + " §8->§7 " + locationToString(location) + "§8)");
activated = null;
return;
}
activated = loc2;
tick = currentTick;
}
}
}
private Map<Player, RedstoneTester> playerMap = new HashMap<>();
public static final ItemStack WAND = new SWItem(Material.BLAZE_ROD, "§eRedstonetester", Arrays.asList("§eRechtsklick Block §8- §7Setzt die 1. Position", "§eShift-Rechtsklick Luft §8- §7Zurücksetzten", "§eLinksklick Block §8- §7Setzt die 2. Position"), false, null).getItemStack();
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
if (!WAND.isSimilar(event.getItem())) return;
Player player = event.getPlayer();
Block block = event.getClickedBlock();
event.setCancelled(true);
switch (event.getAction()) {
case RIGHT_CLICK_AIR:
if (player.isSneaking()) {
playerMap.remove(event.getPlayer());
player.sendMessage("§7Positionen gelöscht§8.");
}
break;
case RIGHT_CLICK_BLOCK:
if (!validBlock(event.getPlayer(), block.getBlockData())) return;
playerMap.computeIfAbsent(event.getPlayer(), RedstoneTester::new).loc1 = block.getLocation();
sendLocation(event.getPlayer(), "§7POS1", block.getLocation());
break;
case LEFT_CLICK_BLOCK:
if (!validBlock(event.getPlayer(), block.getBlockData())) return;
playerMap.computeIfAbsent(event.getPlayer(), RedstoneTester::new).loc2 = block.getLocation();
sendLocation(event.getPlayer(), "§7POS2", block.getLocation());
break;
}
}
private void sendLocation(Player player, String prefix, Location location) {
player.sendMessage(prefix + "§8: §e" + locationToString(location));
}
private static String locationToString(Location location) {
return location.getBlockX() + " " + location.getBlockY() + " " + location.getBlockZ();
}
private boolean validBlock(Player player, BlockData block) {
if (block instanceof Powerable) return true;
if (block instanceof Piston) return true;
player.sendMessage("§7Unbekannte Position");
return false;
}
@EventHandler
public void onPlayerQuit(PlayerQuitEvent event) {
playerMap.remove(event.getPlayer());
}
@EventHandler
public void onPistonExtend(BlockPistonExtendEvent e) {
playerMap.forEach((player, redstoneTester) -> {
redstoneTester.activate(e.getBlock().getLocation());
});
}
@EventHandler
public void onPistonRetract(BlockPistonRetractEvent e) {
playerMap.forEach((player, redstoneTester) -> {
redstoneTester.activate(e.getBlock().getLocation());
});
}
@EventHandler
public void onRedstoneEvent(BlockRedstoneEvent e) {
playerMap.forEach((player, redstoneTester) -> {
redstoneTester.activate(e.getBlock().getLocation());
});
}
@EventHandler
public void onBlockDispense(BlockDispenseEvent e) {
playerMap.forEach((player, redstoneTester) -> {
redstoneTester.activate(e.getBlock().getLocation());
});
}
}

Datei anzeigen

@ -38,4 +38,5 @@ commands:
script:
simulator:
aliases: sim
gui:
gui:
redstonetester: