Skip to content
This repository has been archived by the owner on Jul 13, 2024. It is now read-only.

Commit

Permalink
More things
Browse files Browse the repository at this point in the history
  • Loading branch information
TonimatasDEV committed Mar 1, 2024
1 parent bbdcf30 commit 0a18852
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 9 deletions.
5 changes: 4 additions & 1 deletion src/main/java/dev/tonimatas/mossy/Mossy.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ public static void main(String[] args) {
public static void stop() {
long time = System.currentTimeMillis();
ConsoleThread.stopConsole();
MinecraftServer.getInstanceManager().getInstances().forEach(Instance::saveChunksToStorage);
MinecraftServer.getInstanceManager().getInstances().forEach(instance -> {
instance.saveChunksToStorage();
Logger.info("World " + instance.getDimensionName() + " saved.");
});
MinecraftServer.stopCleanly();
Logger.info("Finish! (" + (System.currentTimeMillis() - time) + "ms)");
}
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/dev/tonimatas/mossy/blocks/AnvilBlockHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package dev.tonimatas.mossy.blocks;

import net.kyori.adventure.text.Component;
import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockHandler;
import net.minestom.server.inventory.Inventory;
import net.minestom.server.inventory.InventoryType;
import net.minestom.server.inventory.type.AnvilInventory;
import net.minestom.server.utils.NamespaceID;
import org.jetbrains.annotations.NotNull;

public class AnvilBlockHandler implements BlockHandler {
@Override
public boolean onInteract(@NotNull BlockHandler.Interaction interaction) {
interaction.getPlayer().openInventory(new Inventory(InventoryType.ANVIL, Component.translatable("container.repair")));

// TODO: Add damage phases
return true;
}

@Override
public @NotNull NamespaceID getNamespaceId() {
return Block.ANVIL.namespace();
}
}
11 changes: 9 additions & 2 deletions src/main/java/dev/tonimatas/mossy/blocks/ChestBlockHandler.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
package dev.tonimatas.mossy.blocks;

import net.kyori.adventure.text.Component;
import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockHandler;
import net.minestom.server.inventory.Inventory;
import net.minestom.server.inventory.InventoryType;
import net.minestom.server.utils.NamespaceID;
import org.jetbrains.annotations.NotNull;

public class ChestBlockHandler implements BlockHandler {
private final Inventory inventory = new Inventory(InventoryType.CHEST_3_ROW, Component.translatable("container.chest"));

@Override
public boolean onInteract(@NotNull Interaction interaction) {
System.out.println("Chest opened.");
// TODO: Make the logic
interaction.getPlayer().openInventory(inventory);
return BlockHandler.super.onInteract(interaction);
}

@Override
public @NotNull NamespaceID getNamespaceId() {
return NamespaceID.from("minecraft:chest");
return Block.CHEST.namespace();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package dev.tonimatas.mossy.blocks;

import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockHandler;
import net.minestom.server.inventory.type.AnvilInventory;
import net.minestom.server.utils.NamespaceID;
import org.jetbrains.annotations.NotNull;

public class ChippedAnvilBlockHandler extends AnvilBlockHandler {
@Override
public @NotNull NamespaceID getNamespaceId() {
return Block.CHIPPED_ANVIL.namespace();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package dev.tonimatas.mossy.blocks;

import net.minestom.server.instance.block.Block;
import net.minestom.server.utils.NamespaceID;
import org.jetbrains.annotations.NotNull;

public class DamagedAnvilBlockHandler extends AnvilBlockHandler {
@Override
public @NotNull NamespaceID getNamespaceId() {
return Block.DAMAGED_ANVIL.namespace();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package dev.tonimatas.mossy.blocks.placement;

import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.rule.BlockPlacementRule;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Map;

public class AnvilBlockPlacementRule extends BlockPlacementRule {
private static final String HORIZONTAL_DIRECTION = "vertical_direction";

protected AnvilBlockPlacementRule() {
super(Block.ANVIL);
}

@Override
public @Nullable Block blockPlace(@NotNull BlockPlacementRule.PlacementState placementState) {
return null;
}
}
1 change: 0 additions & 1 deletion src/main/java/dev/tonimatas/mossy/events/BlockEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@

public class BlockEvents {
public static void init(GlobalEventHandler eventHandler) {

}
}
23 changes: 23 additions & 0 deletions src/main/java/dev/tonimatas/mossy/events/InventoryEvents.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package dev.tonimatas.mossy.events;

import net.minestom.server.event.GlobalEventHandler;
import net.minestom.server.event.inventory.InventoryCloseEvent;
import net.minestom.server.inventory.Inventory;
import net.minestom.server.inventory.InventoryType;
import net.minestom.server.item.ItemStack;

public class InventoryEvents {
public static void init(GlobalEventHandler eventHandler) {
eventHandler.addListener(InventoryCloseEvent.class, event -> {
Inventory inventory = event.getInventory();
if (inventory == null) return;
InventoryType inventoryType = inventory.getInventoryType();

if (inventoryType.equals(InventoryType.ANVIL)) {
for (ItemStack itemStack : inventory.getItemStacks()) {
event.getPlayer().getInventory().addItemStack(itemStack);
}
}
});
}
}
5 changes: 5 additions & 0 deletions src/main/java/dev/tonimatas/mossy/events/PlayerEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.minestom.server.entity.GameMode;
import net.minestom.server.event.GlobalEventHandler;
import net.minestom.server.event.player.AsyncPlayerConfigurationEvent;
import net.minestom.server.event.player.PlayerBlockPlaceEvent;
import net.minestom.server.instance.block.Block;
import dev.tonimatas.mossy.Mossy;

Expand All @@ -25,5 +26,9 @@ public static void init(GlobalEventHandler eventHandler) {
}
event.getPlayer().setRespawnPoint(new Pos(0, y, 0));
});

eventHandler.addListener(PlayerBlockPlaceEvent.class, event -> {
if (event.getPlayer().getOpenInventory() != null) event.setCancelled(true);
});
}
}
11 changes: 8 additions & 3 deletions src/main/java/dev/tonimatas/mossy/manager/MossyBlockManager.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package dev.tonimatas.mossy.manager;

import dev.tonimatas.mossy.blocks.AnvilBlockHandler;
import dev.tonimatas.mossy.blocks.ChestBlockHandler;
import dev.tonimatas.mossy.blocks.ChippedAnvilBlockHandler;
import dev.tonimatas.mossy.blocks.DamagedAnvilBlockHandler;
import dev.tonimatas.mossy.logger.Logger;
import net.minestom.server.MinecraftServer;
import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockHandler;
import net.minestom.server.instance.block.BlockManager;
import net.minestom.server.utils.NamespaceID;
import dev.tonimatas.mossy.logger.Logger;

import java.util.Objects;

Expand All @@ -15,10 +17,13 @@ public class MossyBlockManager {

public static void init() {
registerHandler(Block.CHEST, new ChestBlockHandler());
registerHandler(Block.ANVIL, new AnvilBlockHandler());
registerHandler(Block.DAMAGED_ANVIL, new DamagedAnvilBlockHandler());
registerHandler(Block.CHIPPED_ANVIL, new ChippedAnvilBlockHandler());
Logger.info("All block handlers registered.");
}

private static void registerHandler(Block block, BlockHandler blockHandler) {
blockManager.registerHandler(NamespaceID.from("minecraft:" + block.name().toLowerCase()), () -> Objects.requireNonNull(block.withHandler(blockHandler).handler()));
blockManager.registerHandler(block.namespace(), () -> Objects.requireNonNull(block.withHandler(blockHandler).handler()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import dev.tonimatas.mossy.logger.Logger;

public class MossyEventManager {
private static final GlobalEventHandler eventHandler = MinecraftServer.getGlobalEventHandler();

public static void init() {
GlobalEventHandler eventHandler = MinecraftServer.getGlobalEventHandler();
BlockEvents.init(eventHandler);
InstanceEvents.init(eventHandler);
InventoryEvents.init(eventHandler);
ItemEvents.init(eventHandler);
PlayerEvents.init(eventHandler);
ServerEvents.init(eventHandler);
Expand Down

0 comments on commit 0a18852

Please sign in to comment.