Convenience method to find horizontal Direction based on yaw, easier block properties usage and PlayerBlockPlaceEvent now references the player placing the block

This commit is contained in:
jglrxavpok 2020-04-28 21:52:06 +02:00
parent ca24ed36ee
commit 54111ece6a
6 changed files with 35 additions and 9 deletions

View File

@ -1,10 +1,10 @@
package fr.themode.demo.commands;
import net.minestom.server.MinecraftServer;
import net.minestom.server.command.CommandProcessor;
import net.minestom.server.entity.EntityCreature;
import net.minestom.server.entity.Player;
import net.minestom.server.instance.Instance;
import net.minestom.server.utils.MathUtils;
public class SimpleCommand implements CommandProcessor {
@Override
@ -27,7 +27,7 @@ public class SimpleCommand implements CommandProcessor {
creature.setPathTo(player.getPosition());
}
MinecraftServer.stopCleanly();
player.sendMessage("Direction: "+ MathUtils.getHorizontalDirection(player.getPosition().getYaw()));
return true;

View File

@ -5,6 +5,7 @@ import net.minestom.server.utils.BlockPosition;
public class PlayerBlockPlaceEvent extends CancellableEvent {
private final Player player;
private short blockId;
private short customBlockId;
private BlockPosition blockPosition;
@ -12,7 +13,8 @@ public class PlayerBlockPlaceEvent extends CancellableEvent {
private boolean consumeBlock;
public PlayerBlockPlaceEvent(short blockId, short customBlockId, BlockPosition blockPosition, Player.Hand hand) {
public PlayerBlockPlaceEvent(Player player, short blockId, short customBlockId, BlockPosition blockPosition, Player.Hand hand) {
this.player = player;
this.blockId = blockId;
this.customBlockId = customBlockId;
this.blockPosition = blockPosition;
@ -36,6 +38,10 @@ public class PlayerBlockPlaceEvent extends CancellableEvent {
return blockId;
}
public Player getPlayer() {
return player;
}
public BlockPosition getBlockPosition() {
return blockPosition;
}

View File

@ -85,7 +85,7 @@ public class BlockPlacementListener {
}
if (!intersect) {
PlayerBlockPlaceEvent playerBlockPlaceEvent = new PlayerBlockPlaceEvent(block.getBlockId(), (short) 0, blockPosition, packet.hand);
PlayerBlockPlaceEvent playerBlockPlaceEvent = new PlayerBlockPlaceEvent(player, block.getBlockId(), (short) 0, blockPosition, packet.hand);
playerBlockPlaceEvent.consumeBlock(player.getGameMode() != GameMode.CREATIVE);
// BlockPlacementRule check

View File

@ -14,10 +14,7 @@ import net.minestom.server.stat.StatisticType;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
public class RegistryMain {
@ -205,7 +202,7 @@ public class RegistryMain {
if (statePropertiesObject != null) {
Set<Map.Entry<String, JsonElement>> statePropertiesEntries = statePropertiesObject.entrySet();//will return members of your object
for (Map.Entry<String, JsonElement> propertyEntry : statePropertiesEntries) {
String propertyValue = propertyEntry.getValue().getAsString();
String propertyValue = propertyEntry.getKey()+"="+propertyEntry.getValue().getAsString();
blockState.propertiesValues.add(propertyValue);
}

View File

@ -0,0 +1,13 @@
package net.minestom.server.utils;
public enum Direction {
NORTH,
EAST,
SOUTH,
WEST,
UP,
DOWN;
public static final Direction[] HORIZONTAL = { SOUTH, WEST, NORTH, EAST };
}

View File

@ -28,4 +28,14 @@ public class MathUtils {
return (float) tmp / factor;
}
public static Direction getHorizontalDirection(float yawInDegrees) {
// +45f gives a 90° angle for the direction (-1° and 1° are towards the same direction)
int directionIndex = (int) ((yawInDegrees+45f) / 90f);
if(directionIndex < 0) {
directionIndex = Direction.HORIZONTAL.length-directionIndex;
}
directionIndex %= Direction.HORIZONTAL.length;
return Direction.HORIZONTAL[directionIndex];
}
}