Advertisement
xeRicker

Untitled

Jan 17th, 2025
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.44 KB | None | 0 0
  1. package com.example.tempblocks;
  2.  
  3. import org.bukkit.Bukkit;
  4. import org.bukkit.Location;
  5. import org.bukkit.Material;
  6. import org.bukkit.NamespacedKey;
  7. import org.bukkit.block.Block;
  8. import org.bukkit.persistence.PersistentDataType;
  9. import org.bukkit.persistence.PersistentDataContainer;
  10. import org.bukkit.plugin.java.JavaPlugin;
  11. import org.bukkit.scheduler.BukkitTask;
  12.  
  13. import java.util.*;
  14. import java.util.concurrent.ConcurrentHashMap;
  15.  
  16. public class TempBlocksPlugin extends JavaPlugin {
  17. private final Map<Location, TempBlock> activeBlocks = new ConcurrentHashMap<>();
  18. private final NamespacedKey blockKey;
  19. private final NamespacedKey timeKey;
  20. private BukkitTask saveTask;
  21.  
  22. public TempBlocksPlugin() {
  23. blockKey = new NamespacedKey(this, "temp_block");
  24. timeKey = new NamespacedKey(this, "expiry_time");
  25. }
  26.  
  27. @Override
  28. public void onEnable() {
  29. // Register events
  30. getServer().getPluginManager().registerEvents(new BlockListener(this), this);
  31.  
  32. // Load saved blocks from persistent storage
  33. loadTempBlocks();
  34.  
  35. // Start periodic saving
  36. startSaveTask();
  37. }
  38.  
  39. @Override
  40. public void onDisable() {
  41. // Save all active blocks
  42. saveTempBlocks();
  43. }
  44.  
  45. private void startSaveTask() {
  46. saveTask = Bukkit.getScheduler().runTaskTimerAsynchronously(this, this::saveTempBlocks, 300L, 300L); // Save every 15 seconds
  47. }
  48.  
  49. public void createTempBlock(Location location, Material initialMaterial, List<Material> stages, int duration) {
  50. TempBlock tempBlock = new TempBlock(location, initialMaterial, stages, duration);
  51. activeBlocks.put(location, tempBlock);
  52.  
  53. // Save block data to chunk
  54. saveTempBlockData(location, tempBlock);
  55.  
  56. // Schedule block changes
  57. scheduleBlockChanges(tempBlock);
  58. }
  59.  
  60. private void scheduleBlockChanges(TempBlock tempBlock) {
  61. long interval = tempBlock.getDuration() / (tempBlock.getStages().size() + 1);
  62.  
  63. for (int i = 0; i < tempBlock.getStages().size(); i++) {
  64. final int stage = i;
  65. Bukkit.getScheduler().runTaskLater(this, () -> {
  66. if (activeBlocks.containsKey(tempBlock.getLocation())) {
  67. Block block = tempBlock.getLocation().getBlock();
  68. block.setType(tempBlock.getStages().get(stage));
  69. }
  70. }, (i + 1) * interval);
  71. }
  72.  
  73. // Final stage - remove block
  74. Bukkit.getScheduler().runTaskLater(this, () -> {
  75. if (activeBlocks.containsKey(tempBlock.getLocation())) {
  76. Block block = tempBlock.getLocation().getBlock();
  77. block.setType(Material.AIR);
  78. activeBlocks.remove(tempBlock.getLocation());
  79. removeTempBlockData(tempBlock.getLocation());
  80. }
  81. }, tempBlock.getDuration());
  82. }
  83.  
  84. private void saveTempBlockData(Location location, TempBlock tempBlock) {
  85. PersistentDataContainer chunkContainer = location.getChunk().getPersistentDataContainer();
  86.  
  87. // Store block data as JSON string
  88. String blockData = tempBlock.serialize();
  89. chunkContainer.set(blockKey, PersistentDataType.STRING, blockData);
  90. chunkContainer.set(timeKey, PersistentDataType.LONG, System.currentTimeMillis() + (tempBlock.getDuration() * 50)); // Convert ticks to milliseconds
  91. }
  92.  
  93. private void removeTempBlockData(Location location) {
  94. PersistentDataContainer chunkContainer = location.getChunk().getPersistentDataContainer();
  95. chunkContainer.remove(blockKey);
  96. chunkContainer.remove(timeKey);
  97. }
  98.  
  99. private void loadTempBlocks() {
  100. for (org.bukkit.World world : Bukkit.getWorlds()) {
  101. for (org.bukkit.Chunk chunk : world.getLoadedChunks()) {
  102. PersistentDataContainer chunkContainer = chunk.getPersistentDataContainer();
  103.  
  104. if (chunkContainer.has(blockKey, PersistentDataType.STRING)) {
  105. String blockData = chunkContainer.get(blockKey, PersistentDataType.STRING);
  106. long expiryTime = chunkContainer.get(timeKey, PersistentDataType.LONG);
  107.  
  108. TempBlock tempBlock = TempBlock.deserialize(blockData);
  109.  
  110. if (tempBlock != null) {
  111. // Calculate remaining duration
  112. long remainingTime = expiryTime - System.currentTimeMillis();
  113.  
  114. if (remainingTime > 0) {
  115. tempBlock.setDuration((int) (remainingTime / 50)); // Convert milliseconds to ticks
  116. activeBlocks.put(tempBlock.getLocation(), tempBlock);
  117. scheduleBlockChanges(tempBlock);
  118. } else {
  119. // Block should have expired - clean up
  120. tempBlock.getLocation().getBlock().setType(Material.AIR);
  121. removeTempBlockData(tempBlock.getLocation());
  122. }
  123. }
  124. }
  125. }
  126. }
  127. }
  128.  
  129. private void saveTempBlocks() {
  130. for (TempBlock tempBlock : activeBlocks.values()) {
  131. saveTempBlockData(tempBlock.getLocation(), tempBlock);
  132. }
  133. }
  134. }
  135.  
  136. class TempBlock {
  137. private final Location location;
  138. private final Material initialMaterial;
  139. private final List<Material> stages;
  140. private int duration;
  141.  
  142. public TempBlock(Location location, Material initialMaterial, List<Material> stages, int duration) {
  143. this.location = location;
  144. this.initialMaterial = initialMaterial;
  145. this.stages = stages;
  146. this.duration = duration;
  147. }
  148.  
  149. // Getters
  150. public Location getLocation() { return location; }
  151. public Material getInitialMaterial() { return initialMaterial; }
  152. public List<Material> getStages() { return stages; }
  153. public int getDuration() { return duration; }
  154.  
  155. // Setter for duration
  156. public void setDuration(int duration) { this.duration = duration; }
  157.  
  158. // Serialization methods
  159. public String serialize() {
  160. // Implement JSON serialization using your preferred method (e.g., Gson)
  161. // Should include location, initialMaterial, stages, and duration
  162. return ""; // Implement actual serialization
  163. }
  164.  
  165. public static TempBlock deserialize(String data) {
  166. // Implement JSON deserialization
  167. return null; // Implement actual deserialization
  168. }
  169. }
  170.  
  171. class BlockListener implements org.bukkit.event.Listener {
  172. private final TempBlocksPlugin plugin;
  173.  
  174. public BlockListener(TempBlocksPlugin plugin) {
  175. this.plugin = plugin;
  176. }
  177.  
  178. @org.bukkit.event.EventHandler
  179. public void onBlockPlace(org.bukkit.event.block.BlockPlaceEvent event) {
  180. // Implement your block placement logic here
  181. // Example:
  182. // if (event.getItemInHand().getType() == Material.YOUR_SPECIAL_ITEM) {
  183. // plugin.createTempBlock(event.getBlock().getLocation(),
  184. // Material.DIAMOND_BLOCK,
  185. // Arrays.asList(Material.GOLD_BLOCK, Material.IRON_BLOCK),
  186. // 200L); // 10 seconds (20 ticks per second)
  187. // }
  188. }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement