How can I get a player's location and stock it in tree differents ints [Spigot 1.15.2] - plugins

I want to create a command that gets the coordinates of the player who executed the command and stock it in three int (X Y Z) and to be able to use them in another command to teleport a player to those coordinates (it's a /spawn command).

You need to create a CommandExecutor, explained over here. You can get the Location fragments (X Y Z) from the Location of the Player. Then you need to save it somewhere to access it when the other command is executed, for example in a Hashmap to link the location to the player.
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
// get location and location fragments
Location loc = player.getLocation();
double x = loc.getX();
double y = loc.getY();
double z = loc.getZ();
// convert to int (information loss)
int xInt = (int)x;
int yInt = (int)y;
int zInt = (int)z;
}
}

Related

Get normalized click position on a RectTransform

Ive made a ui touch/click controller by using an UI image with collider. The ui is rendered with a stacked camera.
Im using IPointerDownHandler.OnPointerDown to get the click event.
The controller is supposed to give a value from 0-1 depending on how far up you click it.
Im using Canvas Scaler on the UI to make the controllers resize depending on device. But that messes up my calculations since the click position wont be the same. How is this supposed to be handled? Now the calculation is only correct when i disable Canvas Scaler or run it on a display with the default dimensions.
public void OnPointerDown(PointerEventData pointerEventData)
{
SetAccelerationValue(pointerEventData.position.y);
}
private void SetAccelerationValue(float posY)
{
float percentagePosition;
var positionOnAccelerator = posY - minY;
var acceleratorHeight = maxY - minY;
percentagePosition = positionOnAccelerator / acceleratorHeight;
Debug.Log(percentagePosition);
}
I would use RectTransformUtility.ScreenPointToLocalPointInRectangle to get a position in the local space of the given RectTransform.
Then combine it with Rect.PointToNormalized
Returns the normalized coordinates cooresponding the the point.
The returned Vector2 is in the range 0 to 1 with values more 1 or less than zero clamped.
to get a normalized position within that RectTransform.rect (0,0) being bottom-left corner, (1,1) being the top-right corner
[SerializeField] private RectTransform _rectTransform;
private void Awake ()
{
if(!_rectTransform) _rectTransform = GetComponent<RectTransform>();
}
private bool GetNormalizedPosition(PointerEventData pointerEventData, out Vector2 normalizedPosition)
{
normalizedPosition = default;
// get the pointer position in the local space of the UI element
// NOTE: For click vents use "pointerEventData.pressEventCamera"
// For hover events you would rather use "pointerEventData.enterEventCamera"
if(!RectTransformUtility.ScreenPointToLocalPointInRectangle(_rectTransform, pointerEventData.position, pointerEventData.pressEventCamera, out var localPosition)) return false;
normalizedPosition = Rect.PointToNormalized(_rectTransform.rect, localPosition);
// I think this kind of equals doing something like
//var rect = _rectTransform.rect;
//var normalizedPosition = new Vector2 (
// (localPosition.x - rect.x) / rect.width,
// (localPosition.y - rect.y) / rect.height);
Debug.Log(normalizedPosition);
return true;
}
Since the normalized position returns values like
(0|1)-----(1|1)
| |
| (0|0) |
| |
(0|0)-----(1|0)
but you sounds like what you want to get is
(-1|1)----(1|1)
| |
| 0|0 |
| |
(-1|-1)----(1|-1)
So you can simply shift the returned value using e.g.
// Shift the normalized Rect position from [0,0] (bottom-left), [1,1] (top-right)
// into [-1, -1] (bottom-left), [1,1] (top-right)
private static readonly Vector2 _multiplcator = Vector2.one * 2f;
private static readonly Vector2 _shifter = Vector2.one * 0.5f;
private static Vector2 GetShiftedNormalizedPosition(Vector2 normalizedPosition)
{
return Vector2.Scale((normalizedPosition - _shifter), _multiplcator);
}
So finally you would use e.g.
public void OnPointerDown(PointerEventData pointerEventData)
{
if(!GetNormalizedPosition(pointerEventData, out var normalizedPosition)) return;
var shiftedNormalizedPosition = GetShiftedNormalizedPosition(normalizedPosition);
SetAccelerationValue(shiftedNormalizedPosition.y);
// And probably for your other question also
SetSteeringValue(shiftedNormalizedPosition.x);
}
And of course within SetAccelerationValue you don't calculate anything but just set the value ;)
This uses always the current rect so you don't have to store any min/max values and it also applies to any dynamic re-scaling of the rect.
This would then probably also apply to your other almost duplicate question ;)

How correctly get object by screenshot point coordinates in Unity3d?

I want to make next:
get screenshot from camera
process it with python script and find some points
for each point - find object that projected to this point
Screenshot getting code works well. Python script output has next view:
x=X1 y=Y1 ... \n
x=X2 y=Y2 ... \n
Where 0 <= X1 < screenshot width, 0 <= Y1 < screenshot height.
E.g. in debug mode I can see next image :
As you can see - part of points located on chairs/table projections.
And I need to get this chair/table objects (each object have attached BoxCollider).
I trying to get ray with camera.ScreenPointToRay and Physics.Raycast, but I have wrong output:
private void ApplyClassTextures()
{
var screenshot = GetComponent<Screenshoter>().MakeScreenshot();
Debug.Log(screenshot);
var map = GetComponent<Scenemap>().MakeMap(screenshot);
var objectContours = new Dictionary<GameObject, List<Scenemap.MapContour>>();
var camera = GetComponent<Camera>();
foreach (var item in map)
{
var ray = camera.ScreenPointToRay(new Vector3(item.X, item.Y, 0.0f));
var rayHit = new RaycastHit();
if (Physics.Raycast(ray, out rayHit, raycastHitDistance))
{
var obj = rayHit.collider.gameObject;
if (!objectContours.ContainsKey(obj))
{
Debug.Log(obj);
objectContours.Add(obj, new List<Scenemap.MapContour>());
}
objectContours[obj].Add(item);
}
}
}
And there I can see few floor/wall plane and one chair, but not other and table.

Is there a way to know when the stage is moved in Java FX?

In my application, I have the need to open a stage in the last place that it existed.
Currently I have implemented the saving of the location like so:
stage.setOnHidden(event -> {
PositionDTO dto = new PositionDTO();
dto.setHeight(stage.getHeight());
dto.setWidth(stage.getWidth());
dto.setX(stage.getX());
dto.setY(stage.getY());
//save the position to either a file or database...
});
However, I am wondering if there is a way to set that value when the user drags the window(stage) to a new location since they can open this stage more than one at a time, and opening in the same spot is what the users want. They may not have closed the first one that was opened?
I can't seem to find an event I can listen for.
Thanks!
This is one option for detecting whether or not the Stage has stopped moving so that you can record the location of its X and Y position:
double thisX = 0;
double thisY = 0;
private boolean windowStoppedMoving() {
boolean windowStoppedMoving = false;
try {
double x = thisX;
double y = thisY;
TimeUnit.SECONDS.sleep(2);
boolean xNotChanged = x == thisX;
boolean yNotChanged = y == thisY;
windowStoppedMoving = yNotChanged && xNotChanged;
}
catch (InterruptedException e) {e.printStackTrace();}
return windowStoppedMoving;
}
Then you register a ChangeListener on the Scene Window xProperty and yProperty:
scene.getWindow().xProperty().addListener((observable, oldValue, newValue) -> {
thisX = (double) newValue;
new Thread(() -> {
if (windowStoppedMoving()) {
double finalXValue = thisX;
double finalYValue = thisY;
}
}).start();
});
scene.getWindow().yProperty().addListener((observable, oldValue, newValue) -> {
thisY = (double) newValue;
});
As the user is dragging the window around the screen, the xProperty ChangeListener sets thisX to the newValue each time it is called, then it fires off a thread, while the yProperty is simply setting the value of thisY each time it is called. The thread starts by recording the value of thisX and thisY, then it waits for 2 seconds and if those values have not changed, it returns true.
It works quite well and only returns one true result after the window stops moving.

Unity box to Mouse issue

I am having an issue with my code, i'm trying to move a 3D box to the variable of the position of the mouse, I need to know how to change the box's x,y,z with my mouse position script.
All im asking really, is how do I change my boxes x,y,z with a variable in another script. Thanks!
Code:
#pragma strict
public var distance : float = 4.5;
var box = Transform;
private var firstObject : cube;
function Start () {
}
function Update () {
CastRayToWorld();
}
function CastRayToWorld() {
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var point : Vector3 = ray.origin + (ray.direction * distance);
Debug.Log( "World point " + point );
firstObject = GameObject.Find("pos").GetComponent("cube").pos = point;
firstObject.pos = point;
}
Make sure that the other object is aware of your box gameObject (lets say under the name 'adjustable'), then its simply a case of:
adjustable.transform.position = new Vector3(x, y, z)
To make sure that the object is aware of the boxes gameObject, you could make adjustable a public variable, and then manual drag the box from your scene into the field that would be created in the component on the object in question.

Mutil monitor mouse tracking

I have a need to track mouse position. While I have attempted several ways of doing this, I never am able to follow/capture the position if the mouse is on another monitor.
[DllImport("user32.dll")]
public static extern bool GetCursorPos(ref Point pt);
[DllImport("user32.dll")]
public static extern bool GetCursorInfo(out CURSORINFO pci);
public void GetPosition(out int X, out int Y)
{
Point pt = new Point(0, 0);
X = Y = 0;
if (MouseMonitor.GetCursorPos(ref pt))
{
X = pt.X;
Y = pt.Y;
}
This works but only on one screen. I also read that I might try GetCursorInfo. I have attempted this but it always comes back false.
[DllImport("user32.dll")]
public static extern bool GetCursorInfo(out CURSORINFO pci);
Any suggestions? My goal is to track mouse position (outside of my own app) regardless of what screen it is on.
Your sample code works for me on my dual-monitor system...
You can actually simplify things quite a bit by using the .NET Framework: the System.Windows.Forms.Cursor class has a static Position property.
For example, I created a new Windows Forms project and then dragged a System.Windows.Forms.Timer onto the form. I set the Enabled property to true, and added this code to the Tick event:
this.Text = string.Format("{0}, {1}", Cursor.Position.X, Cursor.Position.Y);
Ran the project and it worked as expected on both my monitors...
Use DWORD GetMessagePos() - it gives you the last windows message mouse position. But be careful, it returns DWORD, but inside there are two SHORTS (16-bit signed ints) packed. So LOWORD/HIWORD macros (or C# respective) won't work.
http://msdn.microsoft.com/en-us/library/ms644938(VS.85).aspx