102 lines
3.6 KiB
Java
102 lines
3.6 KiB
Java
package eu.mhsl.minecraft.pixelblocks;
|
|
|
|
import co.aikar.taskchain.BukkitTaskChainFactory;
|
|
import co.aikar.taskchain.TaskChain;
|
|
import co.aikar.taskchain.TaskChainFactory;
|
|
import eu.mhsl.minecraft.pixelblocks.commands.CreatePixelBlockCommand;
|
|
import eu.mhsl.minecraft.pixelblocks.commands.ExitWorldCommand;
|
|
import eu.mhsl.minecraft.pixelblocks.listeners.*;
|
|
import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.configuration.file.FileConfiguration;
|
|
import org.bukkit.event.Listener;
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
|
|
import java.io.File;
|
|
import java.sql.SQLException;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Objects;
|
|
|
|
public final class Main extends JavaPlugin {
|
|
public static Main plugin;
|
|
public static PixelBlockConfiguration configuration;
|
|
public static PixelBlockDatabase database;
|
|
|
|
public static TaskChainFactory taskFactory;
|
|
|
|
public static List<PixelBlock> pixelBlocks = new ArrayList<>();
|
|
|
|
public static <T> TaskChain<T> chain() {
|
|
return taskFactory.newChain();
|
|
}
|
|
public static <T> TaskChain<T> sharedChain(String name) {
|
|
return taskFactory.newSharedChain(name);
|
|
}
|
|
|
|
@Override
|
|
public void onLoad() {
|
|
Main.plugin = this;
|
|
|
|
FileConfiguration config = this.getConfig();
|
|
PixelBlockConfiguration.setDefaults(config);
|
|
this.saveConfig();
|
|
|
|
Main.configuration = new PixelBlockConfiguration(
|
|
config.getInt(PixelBlockConfiguration.Keys.PixelsPerBlock.getKey()),
|
|
config.getDouble(PixelBlockConfiguration.Keys.HitboxOffset.getKey()),
|
|
config.getBoolean(PixelBlockConfiguration.Keys.OnlyBreakableByOwners.getKey()),
|
|
config.getBoolean(PixelBlockConfiguration.Keys.OnlyEditableByOwners.getKey())
|
|
);
|
|
|
|
File databaseFile = new File(plugin.getDataFolder(), "blocks.db");
|
|
Main.database = new PixelBlockDatabase("jdbc:sqlite:" + databaseFile);
|
|
}
|
|
|
|
@Override
|
|
public void onEnable() {
|
|
Main.taskFactory = BukkitTaskChainFactory.create(this);
|
|
getLogger().info("Start constructing blocks from Database...");
|
|
database.loadPixelBlocks();
|
|
getLogger().info("Construction done!");
|
|
|
|
Listener[] listeners = {
|
|
new EnterPixelBlockListener(),
|
|
new FallOutOfPixelBlockListener(),
|
|
new BreakPixelListener(),
|
|
new PlacePixelBlockListener(),
|
|
new PreventInventorysListener(),
|
|
new ExitPixelWorldListener(),
|
|
new PreventIllegalBlocksListener(),
|
|
new BreakPixelBlockListener(),
|
|
new PlacePixelListener(),
|
|
new PreventHopperActionsListener(),
|
|
new PreventLiquidsFlowListener(),
|
|
new PreventPistonsListener(),
|
|
new PreventEntityPlacementListener(),
|
|
new DiscoverRecipesListener(),
|
|
new QuitWhileInPixelBlockListener(),
|
|
new PreventGrowthListener()
|
|
};
|
|
|
|
for (Listener listener : listeners) {
|
|
getServer().getPluginManager().registerEvents(listener, plugin);
|
|
}
|
|
|
|
Objects.requireNonNull(getCommand("createpixelblock")).setExecutor(new CreatePixelBlockCommand());
|
|
Objects.requireNonNull(getCommand("exitworld")).setExecutor(new ExitWorldCommand());
|
|
|
|
Bukkit.addRecipe(PixelBlockItem.getRecipe());
|
|
}
|
|
|
|
@Override
|
|
public void onDisable() {
|
|
Bukkit.getOnlinePlayers().forEach(QuitWhileInPixelBlockListener::kickPlayerOutOfWorld);
|
|
try {
|
|
database.close();
|
|
} catch (SQLException e) {
|
|
throw new RuntimeException("Failed disabling", e);
|
|
}
|
|
}
|
|
}
|