SteamWar/BauSystem2.0
Archiviert
12
0

Update SmartPlaceListener
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Fix PlaceItemUtils

Signed-off-by: yoyosource <yoyosource@nidido.de>
Dieser Commit ist enthalten in:
yoyosource 2023-09-26 09:27:30 +02:00
Ursprung 898d6269e5
Commit 9982804a20
8 geänderte Dateien mit 70 neuen und 356 gelöschten Zeilen

Datei anzeigen

@ -1,40 +0,0 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2022 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.linkage.types;
import de.steamwar.linkage.LinkageType;
import de.steamwar.linkage.plan.BuildPlan;
import de.steamwar.linkage.plan.MethodBuilder;
import javax.lang.model.element.TypeElement;
public class SmartPlaceBehaviour_GENERIC implements LinkageType {
@Override
public String method() {
return "linkSmartPlace";
}
@Override
public void generateCode(BuildPlan buildPlan, MethodBuilder methodBuilder, String s, TypeElement typeElement) {
buildPlan.addImport("de.steamwar.bausystem.features.smartplace.SmartPlaceListener");
methodBuilder.addLine("SmartPlaceListener.add(" + s + ");");
}
}

Datei anzeigen

@ -1,45 +0,0 @@
/*
* 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

@ -20,82 +20,81 @@
package de.steamwar.bausystem.features.smartplace;
import de.steamwar.bausystem.configplayer.Config;
import de.steamwar.bausystem.linkage.LinkageUtils;
import de.steamwar.bausystem.utils.PlaceItemUtils;
import de.steamwar.linkage.Linked;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.SoundGroup;
import org.bukkit.block.Block;
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.block.data.type.Sign;
import org.bukkit.block.data.type.Switch;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import java.util.ArrayList;
import java.util.List;
@Linked
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 (!Config.getInstance().get(event.getPlayer()).getPlainValueOrDefault("smartPlace", false)) return;
if (smartPlaceBehaviours.isEmpty()) LinkageUtils.linkSmartPlace();
SmartPlaceBehaviour.SmartPlaceResult smartPlaceResult = SmartPlaceBehaviour.SmartPlaceResult.IGNORED;
for (SmartPlaceBehaviour smartPlaceBehaviour : smartPlaceBehaviours) {
if (smartPlaceBehaviour.getType() == SmartPlaceBehaviour.SmartPlaceType.PLACE) {
smartPlaceResult = smartPlaceBehaviour.place(event);
}
if (smartPlaceResult == SmartPlaceBehaviour.SmartPlaceResult.APPLIED) {
break;
}
}
}
@EventHandler
@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerInteract(PlayerInteractEvent event) {
if (!Config.getInstance().get(event.getPlayer()).getPlainValueOrDefault("smartPlace", false)) return;
if (smartPlaceBehaviours.isEmpty()) LinkageUtils.linkSmartPlace();
if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return;
if (event.getPlayer().getGameMode() == GameMode.SPECTATOR) return;
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) {
boolean shouldRotate = event.getPlayer().isSneaking();
Material blockType = event.getClickedBlock().getType();
switch (blockType) {
case REPEATER:
if (shouldRotate && (event.getItem() == null || event.getItem().getType() == Material.REPEATER)) {
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;
}
BlockData blockData = event.getClickedBlock().getBlockData();
if (blockData instanceof Switch) {
if (!shouldRotate && (event.getItem() == null || event.getItem().getType() == event.getClickedBlock().getType())) {
return;
}
shouldRotate = false;
}
if (!event.getClickedBlock().getType().isInteractable()) return;
if (event.getItem() == null) return;
if (!event.getMaterial().isBlock()) return;
try {
BlockData blockData = event.getItem().getType().createBlockData();
if (!(blockData instanceof Directional) && !(blockData instanceof Rotatable)) {
return;
}
} catch (Exception e) {
return;
PlaceItemUtils.PlaceItemResult result = PlaceItemUtils.placeItem(event.getPlayer(), event.getItem(), event.getClickedBlock(), event.getBlockFace(), event.getHand(), true, false, shouldRotate, false);
if (result.isSuccess()) {
event.setCancelled(true);
Block block = event.getClickedBlock().getRelative(event.getBlockFace());
SoundGroup soundGroup = block.getBlockData().getSoundGroup();
Bukkit.getOnlinePlayers().forEach(player -> {
if (!(event.getClickedBlock().getBlockData() instanceof Sign) && player == event.getPlayer()) {
return;
}
player.playSound(block.getLocation(), soundGroup.getPlaceSound(), soundGroup.getVolume() * 0.8F, soundGroup.getPitch() * 0.8F);
});
}
// Fix Double sound for original player
}
for (SmartPlaceBehaviour smartPlaceBehaviour : smartPlaceBehaviours) {
SmartPlaceBehaviour.SmartPlaceResult smartPlaceResult = SmartPlaceBehaviour.SmartPlaceResult.IGNORED;
if (smartPlaceBehaviour.getType() == SmartPlaceBehaviour.SmartPlaceType.INTERACT_INDIRECT) {
smartPlaceResult = smartPlaceBehaviour.interact(event);
}
if (smartPlaceResult == SmartPlaceBehaviour.SmartPlaceResult.APPLIED) {
return;
}
}
@EventHandler
public void onBlockBreak(BlockBreakEvent event) {
if (!Config.getInstance().get(event.getPlayer()).getPlainValueOrDefault("smartPlace", false)) return;
if (!event.getPlayer().isSneaking()) return;
event.setCancelled(true);
event.getBlock().setType(Material.AIR, false);
SoundGroup soundGroup = event.getBlock().getBlockData().getSoundGroup();
Bukkit.getOnlinePlayers().forEach(player -> {
if (player == event.getPlayer()) return;
player.playSound(event.getBlock().getLocation(), soundGroup.getBreakSound(), soundGroup.getVolume() * 0.8F, soundGroup.getPitch() * 0.8F);
});
}
}

Datei anzeigen

@ -1,63 +0,0 @@
/*
* 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.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
public class BlockPlaceBehaviour implements SmartPlaceBehaviour {
@Override
public SmartPlaceType getType() {
return SmartPlaceType.PLACE;
}
@Override
public SmartPlaceResult place(BlockPlaceEvent event) {
if (!event.getPlayer().isSneaking()) {
return SmartPlaceResult.IGNORED;
}
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().getOppositeFace());
smartPlaceResult = SmartPlaceResult.APPLIED;
}
block.setBlockData(blockData);
return smartPlaceResult;
}
}

Datei anzeigen

@ -1,84 +0,0 @@
/*
* 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.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
public class BlockRotatingBehaviour implements SmartPlaceBehaviour {
@Override
public SmartPlaceType getType() {
return SmartPlaceType.INTERACT_INDIRECT;
}
@Override
public SmartPlaceResult interact(PlayerInteractEvent event) {
if (!(event.getClickedBlock().getState() instanceof InventoryHolder)) {
return SmartPlaceResult.IGNORED;
}
BlockData blockData = event.getMaterial().createBlockData();
if (!(blockData instanceof Directional) && !(blockData instanceof Rotatable)) {
return SmartPlaceResult.IGNORED;
}
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());
if (!block.getType().isAir()) {
return SmartPlaceResult.IGNORED;
}
block.setType(event.getMaterial());
blockData = event.getItem().getType().createBlockData();
BlockFace blockFace = event.getBlockFace();
if ((blockFace == BlockFace.UP || blockFace == BlockFace.DOWN) && blockData instanceof Directional) {
blockFace = event.getPlayer().getFacing().getOppositeFace();
}
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) {
try {
((Rotatable) blockData).setRotation(blockFace);
} catch (Exception e) {
// Ignore
}
}
block.setBlockData(blockData);
return SmartPlaceResult.APPLIED;
}
}

Datei anzeigen

@ -1,54 +0,0 @@
/*
* 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.linkage.Linked;
import org.bukkit.Material;
import org.bukkit.block.data.type.Repeater;
import org.bukkit.event.player.PlayerInteractEvent;
@Linked
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

@ -67,7 +67,7 @@ public class SignEditFrom20 implements Listener {
public void editSign(PlayerInteractEvent event) {
if (event.getClickedBlock() == null || !event.getClickedBlock().getType().name().contains("SIGN")) return;
if (event.getAction() == Action.RIGHT_CLICK_BLOCK && !event.getPlayer().isSneaking()) {
PlaceItemUtils.placeItem(event.getPlayer(), event.getItem(), event.getClickedBlock(), event.getBlockFace(), event.getHand(), false, true, false);
PlaceItemUtils.placeItem(event.getPlayer(), event.getItem(), event.getClickedBlock(), event.getBlockFace(), event.getHand(), false, true, false, true);
event.setCancelled(true);
}
if (!event.getPlayer().isSneaking()) return;

Datei anzeigen

@ -83,8 +83,10 @@ public class PlaceItemUtils {
* @param force allow illegal states to be created by placing the block
* @param applyPhysics apply physics while placing the block
* @param rotateAway rotate everything in the opposite direction, so a block facing the Player will face away, and the other way round
* @param playSound enables sound of placing
*/
public PlaceItemResult placeItem(Player player, ItemStack itemStack, Block against, BlockFace againstSide, EquipmentSlot hand, boolean force, boolean applyPhysics, boolean rotateAway) {
public PlaceItemResult placeItem(Player player, ItemStack itemStack, Block against, BlockFace againstSide, EquipmentSlot hand, boolean force, boolean applyPhysics, boolean rotateAway, boolean playSound) {
// TODO: Place inside of player
// If the ItemStack is null or air we cannot place it
if (itemStack == null) return PlaceItemResult.NO_ITEM_HELD;
if (itemStack.getType().isAir()) return PlaceItemResult.NO_ITEM_HELD;
@ -137,16 +139,6 @@ public class PlaceItemUtils {
return PlaceItemResult.NO_VALID_PLACEMENT;
}
}
if (blockData instanceof Switch) {
// Levers and Buttons are always Rotated the other way
Switch switchType = (Switch) blockData;
switch (switchType.getAttachedFace()) {
case FLOOR:
case CEILING:
switchType.setFacing(switchType.getFacing().getOppositeFace());
break;
}
}
}
if (blockData == null) return PlaceItemResult.NO_BLOCK_ITEM_HELD;
@ -202,6 +194,13 @@ public class PlaceItemUtils {
} else {
switchType.setFacing(againstSide);
}
// Levers and Buttons are always Rotated the other way
switch (switchType.getAttachedFace()) {
case FLOOR:
case CEILING:
switchType.setFacing(switchType.getFacing().getOppositeFace());
break;
}
}
if (blockData instanceof RedstoneWire) {
@ -299,9 +298,11 @@ public class PlaceItemUtils {
skull.update(true, false);
}
// Play the corresponding sound of placing the now placed Block
SoundGroup soundGroup = blockData.getSoundGroup();
block.getWorld().playSound(block.getLocation(), soundGroup.getPlaceSound(), soundGroup.getVolume() * 0.8F, soundGroup.getPitch() * 0.8F);
if (playSound) {
// Play the corresponding sound of placing the now placed Block
SoundGroup soundGroup = blockData.getSoundGroup();
block.getWorld().playSound(block.getLocation(), soundGroup.getPlaceSound(), soundGroup.getVolume() * 0.8F, soundGroup.getPitch() * 0.8F);
}
return PlaceItemResult.SUCCESS;
}