SteamWar/BauSystem2.0
Archiviert
12
0

Add SmartPlaceBehaviour
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Extract behaviours from SmartPlaceListener

Signed-off-by: yoyosource <yoyosource@nidido.de>
Dieser Commit ist enthalten in:
yoyosource 2021-12-10 11:36:15 +01:00
Ursprung 6172270a8d
Commit 1c47d89608
6 geänderte Dateien mit 276 neuen und 64 gelöschten Zeilen

Datei anzeigen

@ -0,0 +1,45 @@
/*
* 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.smartplace;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.player.PlayerInteractEvent;
public interface SmartPlaceBehaviour {
enum SmartPlaceResult {
APPLIED,
IGNORED
}
enum SmartPlaceType {
PLACE,
INTERACT_DIRECT, // Before checking if block is interacteable
INTERACT_INDIRECT, // After checking if block is interacteable
}
SmartPlaceType getType();
default SmartPlaceResult place(BlockPlaceEvent event) {
return SmartPlaceResult.IGNORED;
}
default SmartPlaceResult interact(PlayerInteractEvent event) {
return SmartPlaceResult.IGNORED;
}
}

Datei anzeigen

@ -23,41 +23,39 @@ import de.steamwar.bausystem.configplayer.Config;
import de.steamwar.bausystem.linkage.LinkageType;
import de.steamwar.bausystem.linkage.Linked;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.Directional;
import org.bukkit.block.data.Rotatable;
import org.bukkit.block.data.type.Repeater;
import org.bukkit.event.Event;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.InventoryHolder;
import java.util.ArrayList;
import java.util.List;
@Linked(LinkageType.LISTENER)
public class SmartPlaceListener implements Listener {
private static List<SmartPlaceBehaviour> smartPlaceBehaviours = new ArrayList<>();
public static void add(SmartPlaceBehaviour smartPlaceBehaviour) {
smartPlaceBehaviours.add(smartPlaceBehaviour);
}
@EventHandler
public void onBlockPlace(BlockPlaceEvent event) {
if (event.getPlayer().isSneaking() && Config.getInstance().get(event.getPlayer()).getPlainValueOrDefault("smartPlace", false)) {
Block block = event.getBlockPlaced();
BlockData blockData = block.getBlockData();
if (blockData instanceof Directional) {
Directional directional = (Directional) blockData;
BlockFace blockFace = directional.getFacing().getOppositeFace();
if (directional.getFaces().contains(blockFace)) {
directional.setFacing(blockFace);
SmartPlaceBehaviour.SmartPlaceResult smartPlaceResult = SmartPlaceBehaviour.SmartPlaceResult.IGNORED;
for (SmartPlaceBehaviour smartPlaceBehaviour : smartPlaceBehaviours) {
if (smartPlaceBehaviour.getType() == SmartPlaceBehaviour.SmartPlaceType.PLACE) {
smartPlaceBehaviour.place(event);
}
if (smartPlaceResult == SmartPlaceBehaviour.SmartPlaceResult.APPLIED) {
break;
}
} else if (blockData instanceof Rotatable) {
Rotatable rotatable = (Rotatable) blockData;
rotatable.setRotation(rotatable.getRotation());
}
block.setBlockData(blockData);
}
}
@ -67,20 +65,14 @@ public class SmartPlaceListener implements Listener {
if (event.getPlayer().getGameMode() == GameMode.SPECTATOR) return;
if (!Config.getInstance().get(event.getPlayer()).getPlainValueOrDefault("smartPlace", false)) return;
// Reverse repeater
if (event.getPlayer().isSneaking()) {
if (event.getClickedBlock().getType() == Material.REPEATER) {
if (event.getItem() != null && event.getMaterial() != Material.REPEATER) {
for (SmartPlaceBehaviour smartPlaceBehaviour : smartPlaceBehaviours) {
SmartPlaceBehaviour.SmartPlaceResult smartPlaceResult = SmartPlaceBehaviour.SmartPlaceResult.IGNORED;
if (smartPlaceBehaviour.getType() == SmartPlaceBehaviour.SmartPlaceType.INTERACT_DIRECT) {
smartPlaceResult = smartPlaceBehaviour.interact(event);
}
if (smartPlaceResult == SmartPlaceBehaviour.SmartPlaceResult.APPLIED) {
return;
}
Repeater repeater = (Repeater) event.getClickedBlock().getBlockData();
int i = repeater.getDelay() - 1;
if (i <= 0) i += 4;
repeater.setDelay(i);
event.getClickedBlock().setBlockData(repeater);
event.setCancelled(true);
}
return;
}
if (!event.getClickedBlock().getType().isInteractable()) return;
@ -95,38 +87,14 @@ public class SmartPlaceListener implements Listener {
return;
}
if (event.getClickedBlock().getType() == event.getMaterial() || event.getMaterial() == Material.HOPPER) {
if (!(event.getClickedBlock().getState() instanceof InventoryHolder)) {
return;
for (SmartPlaceBehaviour smartPlaceBehaviour : smartPlaceBehaviours) {
SmartPlaceBehaviour.SmartPlaceResult smartPlaceResult = SmartPlaceBehaviour.SmartPlaceResult.IGNORED;
if (smartPlaceBehaviour.getType() == SmartPlaceBehaviour.SmartPlaceType.INTERACT_INDIRECT) {
smartPlaceResult = smartPlaceBehaviour.interact(event);
}
} else {
BlockData blockData = event.getMaterial().createBlockData();
if (blockData instanceof Directional) {
return;
}
if (blockData instanceof Rotatable) {
if (smartPlaceResult == SmartPlaceBehaviour.SmartPlaceResult.APPLIED) {
return;
}
}
// TODO: Fix block setting
event.setUseInteractedBlock(Event.Result.DENY);
World world = event.getPlayer().getWorld();
Block block = world.getBlockAt(event.getClickedBlock().getX() + event.getBlockFace().getModX(), event.getClickedBlock().getY() + event.getBlockFace().getModY(), event.getClickedBlock().getZ() + event.getBlockFace().getModZ());
block.setType(event.getMaterial());
BlockData blockData = event.getItem().getType().createBlockData();
BlockFace blockFace = event.getBlockFace();
if (block.getType() == Material.HOPPER || block.getType() == Material.OBSERVER) {
blockFace = blockFace.getOppositeFace();
}
if (blockData instanceof Directional) {
Directional directional = (Directional) blockData;
if (directional.getFaces().contains(blockFace)) {
directional.setFacing(blockFace);
}
} else if (blockData instanceof Rotatable) {
((Rotatable) blockData).setRotation(blockFace);
}
block.setBlockData(blockData);
}
}

Datei anzeigen

@ -0,0 +1,60 @@
/*
* 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.smartplace.behaviour;
import de.steamwar.bausystem.features.smartplace.SmartPlaceBehaviour;
import de.steamwar.bausystem.linkage.LinkageType;
import de.steamwar.bausystem.linkage.Linked;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.Directional;
import org.bukkit.block.data.Rotatable;
import org.bukkit.event.block.BlockPlaceEvent;
@Linked(LinkageType.SMART_PLACE)
public class BlockPlaceBehaviour implements SmartPlaceBehaviour {
@Override
public SmartPlaceType getType() {
return SmartPlaceType.PLACE;
}
@Override
public SmartPlaceResult place(BlockPlaceEvent event) {
SmartPlaceResult smartPlaceResult = SmartPlaceResult.IGNORED;
Block block = event.getBlockPlaced();
BlockData blockData = block.getBlockData();
if (blockData instanceof Directional) {
Directional directional = (Directional) blockData;
BlockFace blockFace = directional.getFacing().getOppositeFace();
if (directional.getFaces().contains(blockFace)) {
directional.setFacing(blockFace);
}
smartPlaceResult = SmartPlaceResult.APPLIED;
} else if (blockData instanceof Rotatable) {
Rotatable rotatable = (Rotatable) blockData;
rotatable.setRotation(rotatable.getRotation());
smartPlaceResult = SmartPlaceResult.APPLIED;
}
block.setBlockData(blockData);
return smartPlaceResult;
}
}

Datei anzeigen

@ -0,0 +1,81 @@
/*
* 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.smartplace.behaviour;
import de.steamwar.bausystem.features.smartplace.SmartPlaceBehaviour;
import de.steamwar.bausystem.linkage.LinkageType;
import de.steamwar.bausystem.linkage.Linked;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.Directional;
import org.bukkit.block.data.Rotatable;
import org.bukkit.event.Event;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.InventoryHolder;
@Linked(LinkageType.SMART_PLACE)
public class BlockRotatingBehaviour implements SmartPlaceBehaviour {
@Override
public SmartPlaceType getType() {
return SmartPlaceType.INTERACT_INDIRECT;
}
@Override
public SmartPlaceResult interact(PlayerInteractEvent event) {
if (event.getClickedBlock().getType() == event.getMaterial() || event.getMaterial() == Material.HOPPER) {
if (!(event.getClickedBlock().getState() instanceof InventoryHolder)) {
return SmartPlaceResult.IGNORED;
}
} else {
BlockData blockData = event.getMaterial().createBlockData();
if (blockData instanceof Directional) {
return SmartPlaceResult.IGNORED;
}
if (blockData instanceof Rotatable) {
return SmartPlaceResult.IGNORED;
}
}
// TODO: Fix block setting
event.setUseInteractedBlock(Event.Result.DENY);
World world = event.getPlayer().getWorld();
Block block = world.getBlockAt(event.getClickedBlock().getX() + event.getBlockFace().getModX(), event.getClickedBlock().getY() + event.getBlockFace().getModY(), event.getClickedBlock().getZ() + event.getBlockFace().getModZ());
block.setType(event.getMaterial());
BlockData blockData = event.getItem().getType().createBlockData();
BlockFace blockFace = event.getBlockFace();
if (block.getType() == Material.HOPPER || block.getType() == Material.OBSERVER) {
blockFace = blockFace.getOppositeFace();
}
if (blockData instanceof Directional) {
Directional directional = (Directional) blockData;
if (directional.getFaces().contains(blockFace)) {
directional.setFacing(blockFace);
}
} else if (blockData instanceof Rotatable) {
((Rotatable) blockData).setRotation(blockFace);
}
block.setBlockData(blockData);
return SmartPlaceResult.APPLIED;
}
}

Datei anzeigen

@ -0,0 +1,55 @@
/*
* 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.smartplace.behaviour;
import de.steamwar.bausystem.features.smartplace.SmartPlaceBehaviour;
import de.steamwar.bausystem.linkage.LinkageType;
import de.steamwar.bausystem.linkage.Linked;
import org.bukkit.Material;
import org.bukkit.block.data.type.Repeater;
import org.bukkit.event.player.PlayerInteractEvent;
@Linked(LinkageType.SMART_PLACE)
public class ReverseRepeaterBehaviour implements SmartPlaceBehaviour {
@Override
public SmartPlaceType getType() {
return SmartPlaceType.INTERACT_DIRECT;
}
@Override
public SmartPlaceResult interact(PlayerInteractEvent event) {
if (event.getPlayer().isSneaking()) {
if (event.getClickedBlock().getType() == Material.REPEATER) {
if (event.getItem() != null && event.getMaterial() != Material.REPEATER) {
return SmartPlaceResult.APPLIED;
}
Repeater repeater = (Repeater) event.getClickedBlock().getBlockData();
int i = repeater.getDelay() - 1;
if (i <= 0) i += 4;
repeater.setDelay(i);
event.getClickedBlock().setBlockData(repeater);
event.setCancelled(true);
}
return SmartPlaceResult.APPLIED;
}
return SmartPlaceResult.IGNORED;
}
}

Datei anzeigen

@ -20,13 +20,15 @@
package de.steamwar.bausystem.linkage;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.configplayer.ConfigConverter;
import de.steamwar.bausystem.configplayer.Config;
import de.steamwar.bausystem.configplayer.ConfigConverter;
import de.steamwar.bausystem.features.gui.BauGUI;
import de.steamwar.bausystem.features.slaves.panzern.Panzern;
import de.steamwar.bausystem.features.slaves.panzern.PanzernAlgorithm;
import de.steamwar.bausystem.features.script.ScriptExecutor;
import de.steamwar.bausystem.features.script.SpecialCommand;
import de.steamwar.bausystem.features.slaves.panzern.Panzern;
import de.steamwar.bausystem.features.slaves.panzern.PanzernAlgorithm;
import de.steamwar.bausystem.features.smartplace.SmartPlaceBehaviour;
import de.steamwar.bausystem.features.smartplace.SmartPlaceListener;
import de.steamwar.bausystem.linkage.specific.BauGuiItem;
import de.steamwar.bausystem.linkage.specific.ScoreboardItem;
import de.steamwar.command.SWCommand;
@ -58,7 +60,8 @@ public enum LinkageType {
SCRIPT_COMMAND(4, false, SpecialCommand.class::isAssignableFrom, o -> ScriptExecutor.SPECIAL_COMMANDS.add((SpecialCommand) o)),
CONFIG_CONVERTER(5, false, ConfigConverter.class::isAssignableFrom, o -> Config.addConfigConverter((ConfigConverter) o)),
SCOREBOARD(6, false, ScoreboardItem.class::isAssignableFrom, o -> {}),
PANZERN(7, false, PanzernAlgorithm.class::isAssignableFrom, o -> Panzern.add((PanzernAlgorithm) o));
PANZERN(7, false, PanzernAlgorithm.class::isAssignableFrom, o -> Panzern.add((PanzernAlgorithm) o)),
SMART_PLACE(8, false, SmartPlaceBehaviour.class::isAssignableFrom, o -> SmartPlaceListener.add((SmartPlaceBehaviour) o));
private final int order;