Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.tempblocks;
- import org.bukkit.Bukkit;
- import org.bukkit.Location;
- import org.bukkit.Material;
- import org.bukkit.NamespacedKey;
- import org.bukkit.block.Block;
- import org.bukkit.persistence.PersistentDataType;
- import org.bukkit.persistence.PersistentDataContainer;
- import org.bukkit.plugin.java.JavaPlugin;
- import org.bukkit.scheduler.BukkitTask;
- import java.util.*;
- import java.util.concurrent.ConcurrentHashMap;
- public class TempBlocksPlugin extends JavaPlugin {
- private final Map<Location, TempBlock> activeBlocks = new ConcurrentHashMap<>();
- private final NamespacedKey blockKey;
- private final NamespacedKey timeKey;
- private BukkitTask saveTask;
- public TempBlocksPlugin() {
- blockKey = new NamespacedKey(this, "temp_block");
- timeKey = new NamespacedKey(this, "expiry_time");
- }
- @Override
- public void onEnable() {
- // Register events
- getServer().getPluginManager().registerEvents(new BlockListener(this), this);
- // Load saved blocks from persistent storage
- loadTempBlocks();
- // Start periodic saving
- startSaveTask();
- }
- @Override
- public void onDisable() {
- // Save all active blocks
- saveTempBlocks();
- }
- private void startSaveTask() {
- saveTask = Bukkit.getScheduler().runTaskTimerAsynchronously(this, this::saveTempBlocks, 300L, 300L); // Save every 15 seconds
- }
- public void createTempBlock(Location location, Material initialMaterial, List<Material> stages, int duration) {
- TempBlock tempBlock = new TempBlock(location, initialMaterial, stages, duration);
- activeBlocks.put(location, tempBlock);
- // Save block data to chunk
- saveTempBlockData(location, tempBlock);
- // Schedule block changes
- scheduleBlockChanges(tempBlock);
- }
- private void scheduleBlockChanges(TempBlock tempBlock) {
- long interval = tempBlock.getDuration() / (tempBlock.getStages().size() + 1);
- for (int i = 0; i < tempBlock.getStages().size(); i++) {
- final int stage = i;
- Bukkit.getScheduler().runTaskLater(this, () -> {
- if (activeBlocks.containsKey(tempBlock.getLocation())) {
- Block block = tempBlock.getLocation().getBlock();
- block.setType(tempBlock.getStages().get(stage));
- }
- }, (i + 1) * interval);
- }
- // Final stage - remove block
- Bukkit.getScheduler().runTaskLater(this, () -> {
- if (activeBlocks.containsKey(tempBlock.getLocation())) {
- Block block = tempBlock.getLocation().getBlock();
- block.setType(Material.AIR);
- activeBlocks.remove(tempBlock.getLocation());
- removeTempBlockData(tempBlock.getLocation());
- }
- }, tempBlock.getDuration());
- }
- private void saveTempBlockData(Location location, TempBlock tempBlock) {
- PersistentDataContainer chunkContainer = location.getChunk().getPersistentDataContainer();
- // Store block data as JSON string
- String blockData = tempBlock.serialize();
- chunkContainer.set(blockKey, PersistentDataType.STRING, blockData);
- chunkContainer.set(timeKey, PersistentDataType.LONG, System.currentTimeMillis() + (tempBlock.getDuration() * 50)); // Convert ticks to milliseconds
- }
- private void removeTempBlockData(Location location) {
- PersistentDataContainer chunkContainer = location.getChunk().getPersistentDataContainer();
- chunkContainer.remove(blockKey);
- chunkContainer.remove(timeKey);
- }
- private void loadTempBlocks() {
- for (org.bukkit.World world : Bukkit.getWorlds()) {
- for (org.bukkit.Chunk chunk : world.getLoadedChunks()) {
- PersistentDataContainer chunkContainer = chunk.getPersistentDataContainer();
- if (chunkContainer.has(blockKey, PersistentDataType.STRING)) {
- String blockData = chunkContainer.get(blockKey, PersistentDataType.STRING);
- long expiryTime = chunkContainer.get(timeKey, PersistentDataType.LONG);
- TempBlock tempBlock = TempBlock.deserialize(blockData);
- if (tempBlock != null) {
- // Calculate remaining duration
- long remainingTime = expiryTime - System.currentTimeMillis();
- if (remainingTime > 0) {
- tempBlock.setDuration((int) (remainingTime / 50)); // Convert milliseconds to ticks
- activeBlocks.put(tempBlock.getLocation(), tempBlock);
- scheduleBlockChanges(tempBlock);
- } else {
- // Block should have expired - clean up
- tempBlock.getLocation().getBlock().setType(Material.AIR);
- removeTempBlockData(tempBlock.getLocation());
- }
- }
- }
- }
- }
- }
- private void saveTempBlocks() {
- for (TempBlock tempBlock : activeBlocks.values()) {
- saveTempBlockData(tempBlock.getLocation(), tempBlock);
- }
- }
- }
- class TempBlock {
- private final Location location;
- private final Material initialMaterial;
- private final List<Material> stages;
- private int duration;
- public TempBlock(Location location, Material initialMaterial, List<Material> stages, int duration) {
- this.location = location;
- this.initialMaterial = initialMaterial;
- this.stages = stages;
- this.duration = duration;
- }
- // Getters
- public Location getLocation() { return location; }
- public Material getInitialMaterial() { return initialMaterial; }
- public List<Material> getStages() { return stages; }
- public int getDuration() { return duration; }
- // Setter for duration
- public void setDuration(int duration) { this.duration = duration; }
- // Serialization methods
- public String serialize() {
- // Implement JSON serialization using your preferred method (e.g., Gson)
- // Should include location, initialMaterial, stages, and duration
- return ""; // Implement actual serialization
- }
- public static TempBlock deserialize(String data) {
- // Implement JSON deserialization
- return null; // Implement actual deserialization
- }
- }
- class BlockListener implements org.bukkit.event.Listener {
- private final TempBlocksPlugin plugin;
- public BlockListener(TempBlocksPlugin plugin) {
- this.plugin = plugin;
- }
- @org.bukkit.event.EventHandler
- public void onBlockPlace(org.bukkit.event.block.BlockPlaceEvent event) {
- // Implement your block placement logic here
- // Example:
- // if (event.getItemInHand().getType() == Material.YOUR_SPECIAL_ITEM) {
- // plugin.createTempBlock(event.getBlock().getLocation(),
- // Material.DIAMOND_BLOCK,
- // Arrays.asList(Material.GOLD_BLOCK, Material.IRON_BLOCK),
- // 200L); // 10 seconds (20 ticks per second)
- // }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement