mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-23 00:21:26 +01:00
Simplify PlayerInit
This commit is contained in:
parent
8c6d7ad1c3
commit
92ca5f6be6
@ -10,29 +10,27 @@ import net.minestom.server.entity.GameMode;
|
||||
import net.minestom.server.entity.ItemEntity;
|
||||
import net.minestom.server.entity.Player;
|
||||
import net.minestom.server.entity.damage.DamageType;
|
||||
import net.minestom.server.event.EventFilter;
|
||||
import net.minestom.server.event.Event;
|
||||
import net.minestom.server.event.EventNode;
|
||||
import net.minestom.server.event.GlobalEventHandler;
|
||||
import net.minestom.server.event.entity.EntityAttackEvent;
|
||||
import net.minestom.server.event.item.ItemDropEvent;
|
||||
import net.minestom.server.event.item.PickupItemEvent;
|
||||
import net.minestom.server.event.player.*;
|
||||
import net.minestom.server.instance.Chunk;
|
||||
import net.minestom.server.event.player.PlayerDeathEvent;
|
||||
import net.minestom.server.event.player.PlayerDisconnectEvent;
|
||||
import net.minestom.server.event.player.PlayerLoginEvent;
|
||||
import net.minestom.server.event.player.PlayerSpawnEvent;
|
||||
import net.minestom.server.instance.Instance;
|
||||
import net.minestom.server.instance.InstanceContainer;
|
||||
import net.minestom.server.instance.InstanceManager;
|
||||
import net.minestom.server.instance.block.Block;
|
||||
import net.minestom.server.instance.block.CustomBlock;
|
||||
import net.minestom.server.inventory.Inventory;
|
||||
import net.minestom.server.inventory.InventoryType;
|
||||
import net.minestom.server.inventory.PlayerInventory;
|
||||
import net.minestom.server.item.ItemStack;
|
||||
import net.minestom.server.item.ItemTag;
|
||||
import net.minestom.server.item.Material;
|
||||
import net.minestom.server.item.metadata.CompassMeta;
|
||||
import net.minestom.server.monitoring.BenchmarkManager;
|
||||
import net.minestom.server.monitoring.TickMonitor;
|
||||
import net.minestom.server.network.ConnectionManager;
|
||||
import net.minestom.server.utils.MathUtils;
|
||||
import net.minestom.server.utils.Position;
|
||||
import net.minestom.server.utils.Vector;
|
||||
@ -40,7 +38,6 @@ import net.minestom.server.utils.time.TimeUnit;
|
||||
import net.minestom.server.world.DimensionType;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Random;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
@ -50,6 +47,70 @@ public class PlayerInit {
|
||||
|
||||
private static final Inventory inventory;
|
||||
|
||||
private static final EventNode<Event> DEMO_NODE = EventNode.all("demo")
|
||||
.addListener(EntityAttackEvent.class, event -> {
|
||||
final Entity source = event.getEntity();
|
||||
final Entity entity = event.getTarget();
|
||||
if (entity instanceof Player) {
|
||||
Player target = (Player) entity;
|
||||
Vector velocity = source.getPosition().clone().getDirection().multiply(4);
|
||||
velocity.setY(3.5f);
|
||||
target.setVelocity(velocity);
|
||||
target.damage(DamageType.fromEntity(source), 5);
|
||||
} else {
|
||||
Vector velocity = source.getPosition().clone().getDirection().multiply(3);
|
||||
velocity.setY(3f);
|
||||
entity.setVelocity(velocity);
|
||||
}
|
||||
|
||||
if (source instanceof Player) {
|
||||
((Player) source).sendMessage("You attacked something!");
|
||||
}
|
||||
})
|
||||
.addListener(PlayerDeathEvent.class, event -> event.setChatMessage(Component.text("custom death message")))
|
||||
.addListener(PickupItemEvent.class, event -> {
|
||||
final Entity entity = event.getLivingEntity();
|
||||
if (entity instanceof Player) {
|
||||
// Cancel event if player does not have enough inventory space
|
||||
final ItemStack itemStack = event.getItemEntity().getItemStack();
|
||||
event.setCancelled(!((Player) entity).getInventory().addItemStack(itemStack));
|
||||
}
|
||||
})
|
||||
.addListener(ItemDropEvent.class, event -> {
|
||||
final Player player = event.getPlayer();
|
||||
ItemStack droppedItem = event.getItemStack();
|
||||
|
||||
Position position = player.getPosition().clone().add(0, 1.5f, 0);
|
||||
ItemEntity itemEntity = new ItemEntity(droppedItem, position);
|
||||
itemEntity.setPickupDelay(500, TimeUnit.MILLISECOND);
|
||||
itemEntity.setInstance(player.getInstance());
|
||||
Vector velocity = player.getPosition().clone().getDirection().multiply(6);
|
||||
itemEntity.setVelocity(velocity);
|
||||
})
|
||||
.addListener(PlayerDisconnectEvent.class, event -> System.out.println("DISCONNECTION " + event.getPlayer().getUsername()))
|
||||
.addListener(PlayerLoginEvent.class, event -> {
|
||||
final Player player = event.getPlayer();
|
||||
|
||||
var instances = MinecraftServer.getInstanceManager().getInstances();
|
||||
Instance instance = instances.stream().skip(new Random().nextInt(instances.size())).findFirst().orElse(null);
|
||||
event.setSpawningInstance(instance);
|
||||
int x = Math.abs(ThreadLocalRandom.current().nextInt()) % 500 - 250;
|
||||
int z = Math.abs(ThreadLocalRandom.current().nextInt()) % 500 - 250;
|
||||
player.setRespawnPoint(new Position(0, 42f, 0));
|
||||
})
|
||||
.addListener(PlayerSpawnEvent.class, event -> {
|
||||
final Player player = event.getPlayer();
|
||||
player.setGameMode(GameMode.CREATIVE);
|
||||
player.setPermissionLevel(4);
|
||||
ItemStack itemStack = ItemStack.builder(Material.STONE)
|
||||
.amount(64)
|
||||
.meta(itemMetaBuilder ->
|
||||
itemMetaBuilder.canPlaceOn(Set.of(Block.STONE))
|
||||
.canDestroy(Set.of(Block.DIAMOND_ORE)))
|
||||
.build();
|
||||
player.getInventory().addItemStack(itemStack);
|
||||
});
|
||||
|
||||
static {
|
||||
InstanceManager instanceManager = MinecraftServer.getInstanceManager();
|
||||
//StorageLocation storageLocation = MinecraftServer.getStorageManager().getLocation("instance_data", new StorageOptions().setCompression(true));
|
||||
@ -93,16 +154,13 @@ public class PlayerInit {
|
||||
private static AtomicReference<TickMonitor> LAST_TICK = new AtomicReference<>();
|
||||
|
||||
public static void init() {
|
||||
ConnectionManager connectionManager = MinecraftServer.getConnectionManager();
|
||||
BenchmarkManager benchmarkManager = MinecraftServer.getBenchmarkManager();
|
||||
|
||||
MinecraftServer.getGlobalEventHandler().addChild(DEMO_NODE);
|
||||
MinecraftServer.getUpdateManager().addTickMonitor(tickMonitor ->
|
||||
LAST_TICK.set(tickMonitor));
|
||||
|
||||
BenchmarkManager benchmarkManager = MinecraftServer.getBenchmarkManager();
|
||||
MinecraftServer.getSchedulerManager().buildTask(() -> {
|
||||
|
||||
Collection<Player> players = connectionManager.getOnlinePlayers();
|
||||
|
||||
Collection<Player> players = MinecraftServer.getConnectionManager().getOnlinePlayers();
|
||||
if (players.isEmpty())
|
||||
return;
|
||||
|
||||
@ -110,7 +168,6 @@ public class PlayerInit {
|
||||
ramUsage /= 1e6; // bytes to MB
|
||||
|
||||
TickMonitor tickMonitor = LAST_TICK.get();
|
||||
|
||||
final Component header = Component.text("RAM USAGE: " + ramUsage + " MB")
|
||||
.append(Component.newline())
|
||||
.append(Component.text("TICK TIME: " + MathUtils.round(tickMonitor.getTickTime(), 2) + "ms"))
|
||||
@ -118,198 +175,7 @@ public class PlayerInit {
|
||||
.append(Component.text("ACQ TIME: " + MathUtils.round(tickMonitor.getAcquisitionTime(), 2) + "ms"));
|
||||
final Component footer = benchmarkManager.getCpuMonitoringMessage();
|
||||
Audiences.players().sendPlayerListHeaderAndFooter(header, footer);
|
||||
|
||||
}).repeat(10, TimeUnit.TICK).schedule();
|
||||
|
||||
connectionManager.onPacketReceive((player, packetController, packet) -> {
|
||||
// Listen to all received packet
|
||||
//System.out.println("PACKET: "+packet.getClass().getSimpleName());
|
||||
packetController.setCancel(false);
|
||||
});
|
||||
|
||||
connectionManager.onPacketSend((players, packetController, packet) -> {
|
||||
// Listen to all sent packet
|
||||
//System.out.println("PACKET: " + packet.getClass().getSimpleName());
|
||||
packetController.setCancel(false);
|
||||
});
|
||||
|
||||
connectionManager.addPlayerInitialization(player -> {
|
||||
player.addEventCallback(PlayerTickEvent.class, event -> {});
|
||||
});
|
||||
|
||||
// EVENT REGISTERING
|
||||
|
||||
var globalNode = MinecraftServer.getGlobalEventHandler();
|
||||
var node = EventNode.value("test-builder", EventFilter.PLAYER, player -> player.getUsername().equals("TheMode911"))
|
||||
.addListener(PlayerMoveEvent.class, event -> System.out.println("move!"))
|
||||
.addListener(PlayerTickEvent.class, event -> System.out.println("tick!"));
|
||||
globalNode.addChild(node);
|
||||
globalNode.removeChild(node);
|
||||
|
||||
//var child = node.findChild("name");
|
||||
//child.addListener(PlayerTickEvent.class, (event) -> {
|
||||
//});
|
||||
//node.addChild(tagNode); -> Error: cannot add an item listener to a player node
|
||||
|
||||
GlobalEventHandler globalEventHandler = MinecraftServer.getGlobalEventHandler();
|
||||
globalEventHandler.addListener(EntityAttackEvent.class, event -> {
|
||||
final Entity source = event.getEntity();
|
||||
final Entity entity = event.getTarget();
|
||||
if (entity instanceof Player) {
|
||||
Player target = (Player) entity;
|
||||
Vector velocity = source.getPosition().clone().getDirection().multiply(4);
|
||||
velocity.setY(3.5f);
|
||||
target.setVelocity(velocity);
|
||||
target.damage(DamageType.fromEntity(source), 5);
|
||||
} else {
|
||||
Vector velocity = source.getPosition().clone().getDirection().multiply(3);
|
||||
velocity.setY(3f);
|
||||
entity.setVelocity(velocity);
|
||||
}
|
||||
|
||||
if (source instanceof Player) {
|
||||
((Player) source).sendMessage("You attacked something!");
|
||||
}
|
||||
});
|
||||
|
||||
globalEventHandler.addListener(PlayerDeathEvent.class, event -> {
|
||||
event.setChatMessage(Component.text("custom death message"));
|
||||
});
|
||||
|
||||
globalEventHandler.addListener(PlayerBlockPlaceEvent.class, event -> {
|
||||
if (event.getHand() != Player.Hand.MAIN)
|
||||
return;
|
||||
|
||||
final Block block = Block.fromStateId(event.getBlockStateId());
|
||||
|
||||
if (block == Block.STONE) {
|
||||
event.setCustomBlock("custom_block");
|
||||
System.out.println("custom stone");
|
||||
}
|
||||
if (block == Block.TORCH) {
|
||||
event.setCustomBlock((short) 3); // custom torch block
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
globalEventHandler.addListener(PlayerBlockInteractEvent.class, event -> {
|
||||
if (event.getHand() != Player.Hand.MAIN)
|
||||
return;
|
||||
final Player player = event.getPlayer();
|
||||
|
||||
final short blockStateId = player.getInstance().getBlockStateId(event.getBlockPosition());
|
||||
final CustomBlock customBlock = player.getInstance().getCustomBlock(event.getBlockPosition());
|
||||
final Block block = Block.fromStateId(blockStateId);
|
||||
player.sendMessage("You clicked at the block " + block + " " + customBlock);
|
||||
player.sendMessage("CHUNK COUNT " + player.getInstance().getChunks().size());
|
||||
});
|
||||
|
||||
globalEventHandler.addListener(PickupItemEvent.class, event -> {
|
||||
final Entity entity = event.getLivingEntity();
|
||||
if (entity instanceof Player) {
|
||||
// Cancel event if player does not have enough inventory space
|
||||
final ItemStack itemStack = event.getItemEntity().getItemStack();
|
||||
event.setCancelled(!((Player) entity).getInventory().addItemStack(itemStack));
|
||||
}
|
||||
});
|
||||
|
||||
globalEventHandler.addListener(ItemDropEvent.class, event -> {
|
||||
final Player player = event.getPlayer();
|
||||
ItemStack droppedItem = event.getItemStack();
|
||||
|
||||
Position position = player.getPosition().clone().add(0, 1.5f, 0);
|
||||
ItemEntity itemEntity = new ItemEntity(droppedItem, position);
|
||||
itemEntity.setPickupDelay(500, TimeUnit.MILLISECOND);
|
||||
itemEntity.setInstance(player.getInstance());
|
||||
Vector velocity = player.getPosition().clone().getDirection().multiply(6);
|
||||
itemEntity.setVelocity(velocity);
|
||||
});
|
||||
|
||||
globalEventHandler.addListener(PlayerDisconnectEvent.class, event -> {
|
||||
final Player player = event.getPlayer();
|
||||
System.out.println("DISCONNECTION " + player.getUsername());
|
||||
});
|
||||
|
||||
globalEventHandler.addListener(PlayerLoginEvent.class, event -> {
|
||||
final Player player = event.getPlayer();
|
||||
|
||||
var instances = MinecraftServer.getInstanceManager().getInstances();
|
||||
Instance instance = instances.stream().skip(new Random().nextInt(instances.size())).findFirst().orElse(null);
|
||||
event.setSpawningInstance(instance);
|
||||
int x = Math.abs(ThreadLocalRandom.current().nextInt()) % 500 - 250;
|
||||
int z = Math.abs(ThreadLocalRandom.current().nextInt()) % 500 - 250;
|
||||
player.setRespawnPoint(new Position(0, 42f, 0));
|
||||
|
||||
player.getInventory().addInventoryCondition((p, slot, clickType, inventoryConditionResult) -> {
|
||||
if (slot == -999)
|
||||
return;
|
||||
//ItemStack itemStack = p.getInventory().getItemStack(slot);
|
||||
//System.out.println("test " + itemStack.getIdentifier() + " " + itemStack.getData());
|
||||
});
|
||||
});
|
||||
|
||||
globalEventHandler.addListener(PlayerSpawnEvent.class, event -> {
|
||||
final Player player = event.getPlayer();
|
||||
player.setGameMode(GameMode.CREATIVE);
|
||||
|
||||
player.setPermissionLevel(4);
|
||||
|
||||
PlayerInventory inventory = player.getInventory();
|
||||
ItemStack itemStack = ItemStack.builder(Material.STONE)
|
||||
.amount(64)
|
||||
.meta(itemMetaBuilder ->
|
||||
itemMetaBuilder.canPlaceOn(Set.of(Block.STONE))
|
||||
.canDestroy(Set.of(Block.DIAMOND_ORE)))
|
||||
.build();
|
||||
|
||||
//itemStack = itemStack.withStore(storeBuilder -> storeBuilder.set("key2", 25, Integer::sum));
|
||||
|
||||
inventory.addItemStack(itemStack);
|
||||
|
||||
{
|
||||
ItemStack item = ItemStack.builder(Material.DIAMOND_CHESTPLATE)
|
||||
.displayName(Component.text("test"))
|
||||
.lore(Component.text("lore"))
|
||||
.build();
|
||||
|
||||
//inventory.setChestplate(item);
|
||||
|
||||
inventory.setChestplate(item.with(itemStackBuilder -> {
|
||||
itemStackBuilder.lore(Collections.emptyList());
|
||||
}));
|
||||
}
|
||||
});
|
||||
|
||||
globalEventHandler.addListener(PlayerBlockBreakEvent.class, event -> {
|
||||
final short blockStateId = event.getBlockStateId();
|
||||
System.out.println("broke " + blockStateId + " " + Block.fromStateId(blockStateId));
|
||||
});
|
||||
|
||||
globalEventHandler.addListener(PlayerUseItemEvent.class, useEvent -> {
|
||||
final Player player = useEvent.getPlayer();
|
||||
player.sendMessage("Using item in air: " + useEvent.getItemStack().getMaterial());
|
||||
});
|
||||
|
||||
globalEventHandler.addListener(PlayerUseItemOnBlockEvent.class, useEvent -> {
|
||||
final Player player = useEvent.getPlayer();
|
||||
player.sendMessage("Main item: " + player.getInventory().getItemInMainHand().getMaterial());
|
||||
player.sendMessage("Using item on block: " + useEvent.getItemStack().getMaterial() + " at " + useEvent.getPosition() + " on face " + useEvent.getBlockFace());
|
||||
});
|
||||
|
||||
globalEventHandler.addListener(PlayerChunkUnloadEvent.class, event -> {
|
||||
final Player player = event.getPlayer();
|
||||
final Instance instance = player.getInstance();
|
||||
|
||||
Chunk chunk = instance.getChunk(event.getChunkX(), event.getChunkZ());
|
||||
|
||||
if (chunk == null)
|
||||
return;
|
||||
|
||||
// Unload the chunk (save memory) if it has no remaining viewer
|
||||
if (chunk.getViewers().isEmpty()) {
|
||||
//player.getInstance().unloadChunk(chunk);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user