This commit is contained in:
Felix Cravic 2020-03-30 19:48:25 +02:00
parent 994494c5de
commit 5c2771c059
10 changed files with 88 additions and 8 deletions

View File

@ -69,6 +69,10 @@ public class PlayerInit {
});
player.setEventCallback(PlayerUseItemEvent.class, event -> {
System.out.println("CALLBACK EVENT");
});
player.setEventCallback(PickupItemEvent.class, event -> {
event.setCancelled(!player.getInventory().addItemStack(event.getItemStack())); // Cancel event if player does not have enough inventory space
});

View File

@ -1,7 +1,9 @@
package fr.themode.demo.blocks;
import fr.themode.minestom.data.Data;
import fr.themode.minestom.entity.Player;
import fr.themode.minestom.instance.block.CustomBlock;
import fr.themode.minestom.utils.BlockPosition;
import fr.themode.minestom.utils.time.UpdateOption;
public class StoneBlock extends CustomBlock {
@ -10,6 +12,11 @@ public class StoneBlock extends CustomBlock {
super((short) 1, "custom_block");
}
@Override
public void interact(Player player, Player.Hand hand, BlockPosition blockPosition, Data data) {
player.sendMessage("INTERACT STONE");
}
@Override
public UpdateOption getUpdateOption() {
return null;

View File

@ -21,6 +21,11 @@ public class UpdatableBlockDemo extends CustomBlock {
System.out.println("BLOCK UPDATE");
}
@Override
public void interact(Player player, Player.Hand hand, BlockPosition blockPosition, Data data) {
player.sendMessage("INTERACT UPDATABLE");
}
@Override
public UpdateOption getUpdateOption() {
return UPDATE_OPTION;

View File

@ -180,9 +180,6 @@ public class MinecraftServer {
long sleepTime = (tickDistance - (System.nanoTime() - currentTime)) / 1000000;
sleepTime = Math.max(1, sleepTime);
//String perfMessage = "Online: " + getConnectionManager().getOnlinePlayers().size() + " Tick time: " + (TICK_MS - sleepTime) + " ms";
//getConnectionManager().getOnlinePlayers().forEach(player -> player.sendMessage(perfMessage));
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {

View File

@ -0,0 +1,23 @@
package fr.themode.minestom.event;
import fr.themode.minestom.entity.Player;
import fr.themode.minestom.utils.BlockPosition;
public class PlayerBlockInteractEvent extends CancellableEvent {
private BlockPosition blockPosition;
private Player.Hand hand;
public PlayerBlockInteractEvent(BlockPosition blockPosition, Player.Hand hand) {
this.blockPosition = blockPosition;
this.hand = hand;
}
public BlockPosition getBlockPosition() {
return blockPosition;
}
public Player.Hand getHand() {
return hand;
}
}

View File

@ -153,11 +153,19 @@ public abstract class Instance implements BlockModifier, DataContainer {
return chunk.getCustomBlock((byte) (x % 16), (byte) y, (byte) (z % 16));
}
public CustomBlock getCustomBlock(BlockPosition blockPosition) {
return getCustomBlock(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ());
}
public Data getBlockData(int x, int y, int z) {
Chunk chunk = getChunkAt(x, z);
return chunk.getData((byte) (x % 16), (byte) y, (byte) (z % 16));
}
public Data getBlockData(BlockPosition blockPosition) {
return getBlockData(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ());
}
public Chunk getChunkAt(double x, double z) {
int chunkX = ChunkUtils.getChunkCoordinate((int) x);
int chunkZ = ChunkUtils.getChunkCoordinate((int) z);

View File

@ -44,15 +44,17 @@ public class InstanceManager {
return;
long time = System.currentTimeMillis();
blocksPool.execute(() -> {
for (Instance instance : instances) {
if (instance instanceof InstanceContainer) { // SharedInstance should be updated at the same time (verify?)
for (Instance instance : instances) {
if (instance instanceof InstanceContainer) { // SharedInstance should be updated at the same time (verify?)
blocksPool.execute(() -> {
for (Chunk chunk : instance.getChunks()) {
chunk.updateBlocks(time, instance);
}
}
});
}
});
}
}
public Set<Instance> getInstances() {

View File

@ -31,6 +31,8 @@ public abstract class CustomBlock {
throw new UnsupportedOperationException("Update method not overridden");
}
public abstract void interact(Player player, Player.Hand hand, BlockPosition blockPosition, Data data);
public abstract UpdateOption getUpdateOption();
/*

View File

@ -1,10 +1,13 @@
package fr.themode.minestom.listener;
import fr.themode.minestom.data.Data;
import fr.themode.minestom.entity.GameMode;
import fr.themode.minestom.entity.Player;
import fr.themode.minestom.event.PlayerBlockInteractEvent;
import fr.themode.minestom.event.PlayerBlockPlaceEvent;
import fr.themode.minestom.instance.Chunk;
import fr.themode.minestom.instance.Instance;
import fr.themode.minestom.instance.block.CustomBlock;
import fr.themode.minestom.inventory.PlayerInventory;
import fr.themode.minestom.item.ItemStack;
import fr.themode.minestom.item.Material;
@ -16,6 +19,8 @@ import fr.themode.minestom.utils.ChunkUtils;
public class BlockPlacementListener {
private Instance instance;
public static void listener(ClientPlayerBlockPlacementPacket packet, Player player) {
PlayerInventory playerInventory = player.getInventory();
Player.Hand hand = packet.hand;
@ -26,6 +31,16 @@ public class BlockPlacementListener {
if (instance == null)
return;
PlayerBlockInteractEvent playerBlockInteractEvent = new PlayerBlockInteractEvent(blockPosition, hand);
player.callCancellableEvent(PlayerBlockInteractEvent.class, playerBlockInteractEvent, () -> {
CustomBlock customBlock = instance.getCustomBlock(blockPosition);
if (customBlock != null) {
Data data = instance.getBlockData(blockPosition);
customBlock.interact(player, hand, blockPosition, data);
}
});
ItemStack usedItem = hand == Player.Hand.MAIN ? playerInventory.getItemInMainHand() : playerInventory.getItemInOffHand();
Material material = Material.fromId(usedItem.getMaterialId());
if (material != null && !material.isBlock()) {

View File

@ -6,6 +6,7 @@ import fr.themode.minestom.net.player.PlayerConnection;
import java.util.*;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.function.Consumer;
import java.util.function.Function;
public class ConnectionManager {
@ -30,6 +31,22 @@ public class ConnectionManager {
return null;
}
public void broadcastMessage(String message, Function<Player, Boolean> condition) {
if (condition == null) {
getOnlinePlayers().forEach(player -> player.sendMessage(message));
} else {
getOnlinePlayers().forEach(player -> {
boolean result = condition.apply(player);
if (result)
player.sendMessage(message);
});
}
}
public void broadcastMessage(String message) {
broadcastMessage(message, null);
}
public Consumer<Player> getPlayerInitialization() {
return playerInitialization;
}