implement trapdoors / doors
All checks were successful
/ build (push) Successful in 38s

This commit is contained in:
Soph :3 2024-05-06 19:20:43 +03:00
parent f55ae746be
commit 27fbb70dd3
Signed by: sophie
GPG key ID: EDA5D222A0C270F2

View file

@ -6,30 +6,32 @@ import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import net.minestom.server.MinecraftServer;
import net.minestom.server.coordinate.Point;
import net.minestom.server.coordinate.Pos;
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.PlayerBlockInteractEvent;
import net.minestom.server.event.player.PlayerSpawnEvent;
import net.minestom.server.extras.velocity.VelocityProxy;
import net.minestom.server.instance.AnvilLoader;
import net.minestom.server.instance.InstanceContainer;
import net.minestom.server.instance.InstanceManager;
import net.minestom.server.instance.LightingChunk;
import net.minestom.server.instance.block.Block;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
public class Main {
public static void main( String[] args )
{
public static void main(String[] args) {
File file = new File("config.json");
if(!file.exists()) {
if (!file.exists()) {
System.err.println("config.json missing!");
return;
}
if(file.isDirectory()) {
if (file.isDirectory()) {
System.err.println("config.json has to be a file!");
return;
}
@ -49,7 +51,7 @@ public class Main {
instance.setChunkLoader(new AnvilLoader("./world"));
instance.setChunkSupplier(LightingChunk::new);
if(json.has("velocity")) {
if (json.has("velocity")) {
MinecraftServer.LOGGER.info("Enabling velocity proxy support!");
VelocityProxy.enable(json.get("velocity").getAsString());
} else {
@ -62,6 +64,61 @@ public class Main {
event.getPlayer().setRespawnPoint(new Pos(-342, 57, -342));
event.getEntity().setGameMode(GameMode.ADVENTURE);
});
globalEventHandler.addListener(PlayerBlockInteractEvent.class, event -> {
Block[] trapdoors = {
Block.IRON_TRAPDOOR,
Block.OAK_TRAPDOOR,
Block.SPRUCE_TRAPDOOR,
Block.BIRCH_TRAPDOOR,
Block.JUNGLE_TRAPDOOR,
Block.ACACIA_TRAPDOOR,
Block.DARK_OAK_TRAPDOOR,
Block.MANGROVE_TRAPDOOR,
Block.CHERRY_TRAPDOOR,
Block.BAMBOO_TRAPDOOR,
Block.CRIMSON_TRAPDOOR,
Block.WARPED_TRAPDOOR
};
Block[] doors = {
Block.IRON_DOOR,
Block.OAK_DOOR,
Block.SPRUCE_DOOR,
Block.BIRCH_DOOR,
Block.JUNGLE_DOOR,
Block.ACACIA_DOOR,
Block.DARK_OAK_DOOR,
Block.MANGROVE_DOOR,
Block.CHERRY_DOOR,
Block.BAMBOO_DOOR,
Block.CRIMSON_DOOR,
Block.WARPED_DOOR
};
boolean isTrapdoor = false;
boolean isDoor = false;
for (int i = 0; i < trapdoors.length; i++) {
if (trapdoors[i].compare(event.getBlock())) {
isTrapdoor = true;
}
}
for (int i = 0; i < doors.length; i++) {
if (doors[i].compare(event.getBlock())) {
isDoor = true;
}
}
if (isTrapdoor || isDoor) {
boolean isOpen = event.getBlock().getProperty("open").equals("true");
Block newBlock = event.getBlock().withProperty("open", isOpen ? "false" : "true");
event.getInstance().setBlock(event.getBlockPosition(), newBlock);
}
if (isDoor) {
Point pos = event.getBlockPosition().add(0, event.getBlock().getProperty("half").equals("upper") ? -1 : 1,
0);
Block newBlock = event.getInstance().getBlock(pos).withProperty("open",
event.getBlock().getProperty("open").equals("true") ? "false" : "true");
event.getInstance().setBlock(pos, newBlock);
}
});
globalEventHandler.addListener(PlayerSpawnEvent.class, event -> {
event.getEntity().sendMessage("Welcome to The Alliance!");
event.getEntity().sendMessage("");
@ -72,6 +129,7 @@ public class Main {
// Start the server
minecraftServer.start(json.get("host").getAsString(), json.get("port").getAsInt());
System.out.println("Server started on host " + json.get("host").getAsString() + " and port " + json.get("port").getAsInt());
System.out.println("Server started on host " + json.get("host").getAsString() + " and port "
+ json.get("port").getAsInt());
}
}