parent
11f0a1e910
commit
64850aaf45
2 changed files with 144 additions and 0 deletions
@ -0,0 +1,142 @@ |
||||
package xyz.soukup.ecoCraftCore.sit; |
||||
|
||||
import org.bukkit.Location; |
||||
import org.bukkit.World; |
||||
import org.bukkit.block.Block; |
||||
import org.bukkit.block.data.Bisected; |
||||
import org.bukkit.block.data.type.Stairs; |
||||
import org.bukkit.entity.ArmorStand; |
||||
import org.bukkit.entity.Entity; |
||||
import org.bukkit.entity.Player; |
||||
import org.bukkit.event.EventHandler; |
||||
import org.bukkit.event.Listener; |
||||
import org.bukkit.event.block.Action; |
||||
import org.bukkit.event.player.PlayerInteractEvent; |
||||
import org.bukkit.event.player.PlayerQuitEvent; |
||||
import org.bukkit.event.player.PlayerTeleportEvent; |
||||
import org.bukkit.event.vehicle.VehicleExitEvent; |
||||
import org.bukkit.persistence.PersistentDataType; |
||||
import xyz.soukup.ecoCraftCore.utilities.PDC; |
||||
|
||||
|
||||
public class LetMeSit implements Listener { |
||||
|
||||
@EventHandler |
||||
public void onStairsClick(PlayerInteractEvent event){ |
||||
Player player = event.getPlayer(); |
||||
|
||||
if (player.isSneaking()){ |
||||
return; |
||||
} |
||||
|
||||
if (event.getAction() != Action.RIGHT_CLICK_BLOCK){ |
||||
return; |
||||
} |
||||
|
||||
Block block = event.getClickedBlock(); |
||||
|
||||
if (block == null){ |
||||
return; |
||||
} |
||||
|
||||
if (!(block.getBlockData() instanceof Stairs stairs)){ |
||||
return; |
||||
} |
||||
|
||||
if (stairs.getHalf() == Bisected.Half.TOP){ |
||||
return; |
||||
} |
||||
|
||||
switch (stairs.getShape()) { |
||||
case INNER_LEFT: |
||||
case INNER_RIGHT: |
||||
case OUTER_LEFT: |
||||
case OUTER_RIGHT: |
||||
return; |
||||
} |
||||
|
||||
float yaw = switch (stairs.getFacing()) { |
||||
case NORTH -> 0f; |
||||
case WEST -> -90f; |
||||
case EAST -> 90f; |
||||
default -> 180f; |
||||
}; |
||||
|
||||
Location location = block.getLocation(); |
||||
location.add(0.5, 0.5, 0.5); |
||||
location.setYaw(yaw); |
||||
|
||||
World world = location.getWorld(); |
||||
|
||||
|
||||
|
||||
for (Entity entity : world.getNearbyEntities(location, 0.5, 1,0.5)){ |
||||
if (!(entity instanceof ArmorStand armorStand)){ |
||||
continue; |
||||
} |
||||
|
||||
if (PDC.getUniversal(armorStand, "chair", PersistentDataType.BOOLEAN) == null){ |
||||
continue; |
||||
} |
||||
|
||||
if (!armorStand.getPassengers().isEmpty()){ |
||||
return; |
||||
} |
||||
|
||||
player.setRotation(armorStand.getYaw(), 0f); |
||||
armorStand.addPassenger(player); |
||||
event.setCancelled(true); |
||||
return; |
||||
} |
||||
|
||||
ArmorStand armorStand = world.spawn(location, ArmorStand.class, stand -> { |
||||
stand.setInvisible(true); |
||||
stand.setInvulnerable(true); |
||||
stand.setGravity(false); |
||||
stand.setSilent(true); |
||||
stand.setMarker(true); |
||||
}); |
||||
|
||||
PDC.setUniversal(armorStand, "chair", true, PersistentDataType.BOOLEAN); |
||||
player.setRotation(yaw, 0f); |
||||
armorStand.addPassenger(player); |
||||
event.setCancelled(true); |
||||
} |
||||
|
||||
@EventHandler |
||||
public void unmount(VehicleExitEvent event){ |
||||
Location location = event.getVehicle().getLocation(); |
||||
cleanArmorStand(location); |
||||
} |
||||
|
||||
@EventHandler |
||||
public void playerLeave(PlayerQuitEvent event){ |
||||
Location location = event.getPlayer().getLocation(); |
||||
cleanArmorStand(location); |
||||
} |
||||
|
||||
@EventHandler |
||||
public void teleport(PlayerTeleportEvent event){ |
||||
Location location = event.getFrom(); |
||||
cleanArmorStand(location); |
||||
} |
||||
|
||||
private void cleanArmorStand(Location location){ |
||||
World world = location.getWorld(); |
||||
for (Entity entity : world.getNearbyEntities(location, 0.5, 1,0.5)){ |
||||
if (!(entity instanceof ArmorStand armorStand)){ |
||||
continue; |
||||
} |
||||
|
||||
if (PDC.getUniversal(armorStand, "chair", PersistentDataType.BOOLEAN) == null){ |
||||
continue; |
||||
} |
||||
|
||||
if (!armorStand.getPassengers().isEmpty()){ |
||||
continue; |
||||
} |
||||
|
||||
armorStand.remove(); |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue