Skip to content

Commit

Permalink
Merge pull request #1 from Kooperlol/1.0.5
Browse files Browse the repository at this point in the history
1.0.5
  • Loading branch information
Kooperlol authored May 5, 2024
2 parents aa82926 + dd9c8a8 commit 57f20ff
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 37 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = 'codes.kooper'
version = '1.0.4-beta'
version = '1.0.5-beta'

dependencies {
implementation 'org.projectlombok:lombok:1.18.28'
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/codes/kooper/blockify/Blockify.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

@Getter
public final class Blockify extends JavaPlugin {
public static Blockify instance;
private StageManager stageManager;
private BlockChangeManager blockChangeManager;
private MiningUtils miningUtils;
Expand All @@ -33,7 +32,6 @@ public void onLoad() {

@Override
public void onEnable() {
instance = this;
new Metrics(this, 21782);
getLogger().info("Blockify has been enabled!");

Expand All @@ -53,4 +51,8 @@ public void onDisable() {
blockChangeManager.getBlockChangeTasks().values().forEach(BukkitTask::cancel);
PacketEvents.getAPI().terminate();
}

public static Blockify getInstance() {
return getPlugin(Blockify.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class StageBoundListener implements Listener {
@EventHandler
public void onPlayerMove(PlayerMoveEvent event) {
Player player = event.getPlayer();
List<Stage> stages = Blockify.instance.getStageManager().getStages(player.getUniqueId());
List<Stage> stages = Blockify.getInstance().getStageManager().getStages(player.getUniqueId());
for (Stage stage : stages) {
if (stage.isLocationWithin(event.getTo())) {
new PlayerEnterStageEvent(stage, player).callEvent();
Expand All @@ -53,7 +53,7 @@ public void onPlayerMove(PlayerMoveEvent event) {
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
List<Stage> stages = Blockify.instance.getStageManager().getStages(player.getUniqueId());
List<Stage> stages = Blockify.getInstance().getStageManager().getStages(player.getUniqueId());
for (Stage stage : stages) {
if (stage.isLocationWithin(player.getLocation())) {
new PlayerEnterStageEvent(stage, player).callEvent();
Expand All @@ -72,7 +72,7 @@ public void onPlayerJoin(PlayerJoinEvent event) {
@EventHandler
public void onPlayerQuit(PlayerQuitEvent event) {
Player player = event.getPlayer();
List<Stage> stages = Blockify.instance.getStageManager().getStages(player.getUniqueId());
List<Stage> stages = Blockify.getInstance().getStageManager().getStages(player.getUniqueId());
for (Stage stage : stages) {
new PlayerExitStageEvent(stage, player).callEvent();
}
Expand Down Expand Up @@ -120,11 +120,11 @@ public void onPlayerStageLeave(PlayerLeaveStageEvent event) {
public void onPlayerEnterStage(PlayerEnterStageEvent event) {
if (!event.getStage().getAudience().isArePlayersHidden()) return;
for (UUID uuid : event.getStage().getAudience().getPlayers()) {
Player player = Blockify.instance.getServer().getPlayer(uuid);
Player player = Blockify.getInstance().getServer().getPlayer(uuid);
if (player == null) continue;
player.hidePlayer(Blockify.instance, event.getPlayer());
player.hidePlayer(Blockify.getInstance(), event.getPlayer());
if (!event.getStage().isLocationWithin(player.getLocation())) continue;
event.getPlayer().hidePlayer(Blockify.instance, player);
event.getPlayer().hidePlayer(Blockify.getInstance(), player);
}
}

Expand All @@ -140,9 +140,9 @@ public void onPlayerEnterStage(PlayerEnterStageEvent event) {
public void onPlayerExitStage(PlayerExitStageEvent event) {
if (!event.getStage().getAudience().isArePlayersHidden()) return;
for (UUID uuid : event.getStage().getAudience().getPlayers()) {
Player player = Blockify.instance.getServer().getPlayer(uuid);
Player player = Blockify.getInstance().getServer().getPlayer(uuid);
if (player == null) continue;
player.showPlayer(Blockify.instance, event.getPlayer());
player.showPlayer(Blockify.getInstance(), event.getPlayer());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void sendBlockChanges(Stage stage, Audience audience, ConcurrentHashMap<B
if (blockChanges.isEmpty()) {
return;
}
Bukkit.getScheduler().runTask(Blockify.instance, () -> new OnBlockChangeSendEvent(stage, blockChanges).callEvent());
Bukkit.getScheduler().runTask(Blockify.getInstance(), () -> new OnBlockChangeSendEvent(stage, blockChanges).callEvent());

// If there is only one block change, send it to the player directly
if (blockChanges.size() == 1) {
Expand All @@ -87,7 +87,7 @@ public void sendBlockChanges(Stage stage, Audience audience, ConcurrentHashMap<B
// Create an array of chunks to send from the block changes map
BlockifyChunk[] chunksToSend = blockChanges.keySet().toArray(new BlockifyChunk[0]);
// Create a task to send a chunk to the player every tick
blockChangeTasks.put(uuid, Bukkit.getScheduler().runTaskTimer(Blockify.instance, () -> {
blockChangeTasks.put(uuid, Bukkit.getScheduler().runTaskTimer(Blockify.getInstance(), () -> {
// If the chunk index is greater than the length of the chunks to send array, cancel the task
if (chunkIndex.get() >= chunksToSend.length) {
blockChangeTasks.get(uuid).cancel();
Expand All @@ -100,7 +100,7 @@ public void sendBlockChanges(Stage stage, Audience audience, ConcurrentHashMap<B
// Check if the chunk is loaded, if not, return
if (!stage.getWorld().isChunkLoaded(chunk.x(), chunk.z())) return;
// Send the chunk packet to the player
Bukkit.getScheduler().runTaskAsynchronously(Blockify.instance, () -> sendChunkPacket(stage, onlinePlayer, chunk, blockChanges));
Bukkit.getScheduler().runTaskAsynchronously(Blockify.getInstance(), () -> sendChunkPacket(stage, onlinePlayer, chunk, blockChanges));
}, 0L, 1L));
}
}
Expand Down Expand Up @@ -152,7 +152,7 @@ public void sendChunkPacket(Stage stage, Player player, BlockifyChunk chunk, Con
// Send the packet to the player
WrapperPlayServerMultiBlockChange.EncodedBlock[] encodedBlocksArray = encodedBlocks.toArray(new WrapperPlayServerMultiBlockChange.EncodedBlock[0]);
WrapperPlayServerMultiBlockChange wrapperPlayServerMultiBlockChange = new WrapperPlayServerMultiBlockChange(new Vector3i(chunk.x(), chunkY, chunk.z()), true, encodedBlocksArray);
Bukkit.getScheduler().runTask(Blockify.instance, () -> user.sendPacket(wrapperPlayServerMultiBlockChange));
Bukkit.getScheduler().runTask(Blockify.getInstance(), () -> user.sendPacket(wrapperPlayServerMultiBlockChange));
}
// Remove the chunk from the chunks being sent list
chunksBeingSent.get(player.getUniqueId()).remove(chunk.getChunkKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class StageManager {
*/
public void createStage(Stage stage) {
if (stages.containsKey(stage.getName())) {
Blockify.instance.getLogger().warning("Stage with name " + stage.getName() + " already exists!");
Blockify.getInstance().getLogger().warning("Stage with name " + stage.getName() + " already exists!");
return;
}
new CreateStageEvent(stage).callEvent();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/codes/kooper/blockify/models/Audience.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public Set<UUID> removePlayer(UUID player) {
*/
public void setMiningSpeed(UUID player, float speed) {
if (speed <= 0 || speed == 1) {
Blockify.instance.getLogger().warning("Invalid mining speed for player " + player + ": " + speed);
Blockify.getInstance().getLogger().warning("Invalid mining speed for player " + player + ": " + speed);
return;
}
miningSpeeds.put(player, speed);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/codes/kooper/blockify/models/Stage.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void sendBlocksToAudience() {
for (View view : views) {
blocks.putAll(view.getBlocks());
}
Blockify.instance.getBlockChangeManager().sendBlockChanges(this, audience, blocks);
Blockify.getInstance().getBlockChangeManager().sendBlockChanges(this, audience, blocks);
}

/**
Expand All @@ -84,7 +84,7 @@ public void refreshBlocksToAudience(Set<BlockifyPosition> blocks) {
}
}
}
Blockify.instance.getBlockChangeManager().sendBlockChanges(this, audience, blockChanges);
Blockify.getInstance().getBlockChangeManager().sendBlockChanges(this, audience, blockChanges);
}

/**
Expand All @@ -94,7 +94,7 @@ public void refreshBlocksToAudience(Set<BlockifyPosition> blocks) {
*/
public void addView(View view) {
if (views.stream().anyMatch(v -> v.getName().equalsIgnoreCase(view.getName()))) {
Blockify.instance.getLogger().warning("View with name " + view.getName() + " already exists in stage " + name + "!");
Blockify.getInstance().getLogger().warning("View with name " + view.getName() + " already exists in stage " + name + "!");
return;
}
views.add(view);
Expand Down
48 changes: 47 additions & 1 deletion src/main/java/codes/kooper/blockify/models/View.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,50 @@ public void setBlock(BlockifyPosition position, BlockData blockData) {
blocks.get(position.toBlockifyChunk()).put(position, blockData);
}
}
}

/**
* Reset a block to a random block data from the pattern.
*
* @param position The position of the block.
*/
public void resetBlock(BlockifyPosition position) {
if (hasBlock(position)) {
blocks.get(position.toBlockifyChunk()).put(position, pattern.getRandomBlockData());
}
}

/**
* Reset a set of blocks to random block data from the pattern.
* Call this method asynchronously if you are resetting a large number of blocks.
*
* @param positions The set of blocks to reset.
*/
public void resetBlocks(Set<BlockifyPosition> positions) {
for (BlockifyPosition position : positions) {
resetBlock(position);
}
}

/**
* Reset all blocks in the view to random block data from the pattern.
* Call this method asynchronously.
*/
public void resetViewBlocks() {
for (BlockifyChunk chunk : blocks.keySet()) {
for (BlockifyPosition position : blocks.get(chunk).keySet()) {
blocks.get(chunk).put(position, pattern.getRandomBlockData());
}
}
}

/**
* Changes the pattern of the view.
* Call this method asynchronously.
*
* @param pattern The new pattern.
*/
public void changePattern(Pattern pattern) {
this.pattern = pattern;
resetViewBlocks();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void onPacketPlayReceive(PacketPlayReceiveEvent event) {
BlockifyPosition position = new BlockifyPosition(wrapper.getBlockPosition().getX(), wrapper.getBlockPosition().getY(), wrapper.getBlockPosition().getZ());

// Get stages the player is in. If the player is not in any stages, return.
List<Stage> stages = Blockify.instance.getStageManager().getStages(player.getUniqueId());
List<Stage> stages = Blockify.getInstance().getStageManager().getStages(player.getUniqueId());
if (stages == null || stages.isEmpty()) {
return;
}
Expand All @@ -47,7 +47,7 @@ public void onPacketPlayReceive(PacketPlayReceiveEvent event) {
BlockData blockData = view.getBlock(position);

// Call BlockifyInteractEvent to handle custom interaction
Bukkit.getScheduler().runTask(Blockify.instance, () -> new BlockifyInteractEvent(player, position.toPosition(), blockData, view, view.getStage()).callEvent());
Bukkit.getScheduler().runTask(Blockify.getInstance(), () -> new BlockifyInteractEvent(player, position.toPosition(), blockData, view, view.getStage()).callEvent());

// Check if block is breakable, if not, send block change packet to cancel the break
if (!view.isBreakable()) {
Expand All @@ -58,9 +58,9 @@ public void onPacketPlayReceive(PacketPlayReceiveEvent event) {

// Check if player has custom mining speed, if so, handle custom digging, else handle normal digging
if (view.getStage().getAudience().getMiningSpeed(player.getUniqueId()) != 1) {
Blockify.instance.getMiningUtils().handleCustomDigging(player, view, actionType, blockData, position);
Blockify.getInstance().getMiningUtils().handleCustomDigging(player, view, actionType, blockData, position);
} else {
Blockify.instance.getMiningUtils().handleNormalDigging(player, view, actionType, blockData, position);
Blockify.getInstance().getMiningUtils().handleNormalDigging(player, view, actionType, blockData, position);
}

return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void onPacketPlayReceive(PacketPlayReceiveEvent event) {
Player player = (Player) event.getPlayer();

// Get the stages the player is in. If the player is not in any stages, return.
List<Stage> stages = Blockify.instance.getStageManager().getStages(player.getUniqueId());
List<Stage> stages = Blockify.getInstance().getStageManager().getStages(player.getUniqueId());
if (stages == null || stages.isEmpty()) {
return;
}
Expand All @@ -35,7 +35,7 @@ public void onPacketPlayReceive(PacketPlayReceiveEvent event) {
for (View view : stage.getViews()) {
if (view.hasBlock(position)) {
// Call the event and cancel the placement
Bukkit.getScheduler().runTask(Blockify.instance, () -> new BlockifyPlaceEvent(player, position.toPosition(), view, stage).callEvent());
Bukkit.getScheduler().runTask(Blockify.getInstance(), () -> new BlockifyPlaceEvent(player, position.toPosition(), view, stage).callEvent());
event.setCancelled(true);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void onPacketPlaySend(PacketPlaySendEvent event) {
int chunkZ = chunkData.getColumn().getZ();

// Get the stages the player is in. If the player is not in any stages, return.
List<Stage> stages = Blockify.instance.getStageManager().getStages(player.getUniqueId());
List<Stage> stages = Blockify.getInstance().getStageManager().getStages(player.getUniqueId());
if (stages == null || stages.isEmpty()) {
return;
}
Expand All @@ -35,12 +35,12 @@ public void onPacketPlaySend(PacketPlaySendEvent event) {
for (View view : stage.getViews()) {
BlockifyChunk blockifyChunk = new BlockifyChunk(chunkX, chunkZ);
// If the chunk is being sent to the player, return.
if (Blockify.instance.getBlockChangeManager().getChunksBeingSent().get(player.getUniqueId()) != null && Blockify.instance.getBlockChangeManager().getChunksBeingSent().get(player.getUniqueId()).contains(blockifyChunk.getChunkKey())) {
if (Blockify.getInstance().getBlockChangeManager().getChunksBeingSent().get(player.getUniqueId()) != null && Blockify.getInstance().getBlockChangeManager().getChunksBeingSent().get(player.getUniqueId()).contains(blockifyChunk.getChunkKey())) {
return;
}
// If the view contains the chunk, send the chunk's blocks to the player.
if (view.getBlocks().containsKey(blockifyChunk)) {
Blockify.instance.getBlockChangeManager().sendChunkPacket(stage, player, blockifyChunk, view.getBlocks());
Blockify.getInstance().getBlockChangeManager().sendChunkPacket(stage, player, blockifyChunk, view.getBlocks());
}
}
}
Expand Down
Loading

0 comments on commit 57f20ff

Please sign in to comment.