Skip to content

Commit

Permalink
v1.1.0 removeBlock()の修正(#3) + 軽量化 + 機能追加
Browse files Browse the repository at this point in the history
  • Loading branch information
bea4dev committed Sep 7, 2021
1 parent 71895b5 commit ae96800
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<dependency>
<groupId>com.github.Be4rJP</groupId>
<artifactId>Parallel</artifactId>
<version>v1.0.9</version>
<version>v1.1.0</version>
<scope>provided</scope>
</dependency>
```
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>be4rjp</groupId>
<artifactId>Parallel</artifactId>
<version>1.0.9</version>
<version>1.1.0</version>
<packaging>jar</packaging>

<name>Parallel</name>
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/be4rjp/parallel/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ public class Config {

private static boolean performanceMode = false;

private static boolean rewriteLightPacket = true;

public static WorkType getWorkType() {return workType;}

public static boolean isPerformanceMode() {return performanceMode;}

public static boolean isRewriteLightPacket() {return rewriteLightPacket;}

public static void load(){
File file = new File("plugins/Parallel", "config.yml");
file.getParentFile().mkdirs();
Expand All @@ -27,6 +31,7 @@ public static void load(){

if(yml.contains("work-type")) workType = WorkType.valueOf(yml.getString("work-type"));
if(yml.contains("performance-mode")) performanceMode = yml.getBoolean("performance-mode");
if(yml.contains("rewrite-light-packet")) rewriteLightPacket = yml.getBoolean("rewrite-light-packet");
}

public enum WorkType{
Expand Down
83 changes: 68 additions & 15 deletions src/main/java/be4rjp/parallel/ParallelWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,28 @@
*/
public class ParallelWorld {

private static Map<String, ParallelWorld> worldMap;
private static Map<UUID, ParallelWorld> worldMap;

private static final ParallelWorld onlyOneWorld;

static {
initialize();
onlyOneWorld = new ParallelWorld("");
onlyOneWorld = new ParallelWorld(UUID.randomUUID());
}

public static void initialize(){
worldMap = new ConcurrentHashMap<>();
}

public static synchronized ParallelWorld getParallelWorld(Player player){
return getParallelWorld(player.getUniqueId().toString());
return getParallelWorld(player.getUniqueId());
}

public static synchronized ParallelWorld getParallelWorld(String uuid){
public static synchronized ParallelWorld getParallelWorld(String uuidString){
return getParallelWorld(UUID.fromString(uuidString));
}

public static synchronized ParallelWorld getParallelWorld(UUID uuid){
if(Config.getWorkType() == Config.WorkType.NORMAL) {
if (worldMap.containsKey(uuid)) return worldMap.get(uuid);
return new ParallelWorld(uuid);
Expand All @@ -45,18 +49,22 @@ public static synchronized ParallelWorld getParallelWorld(String uuid){
}

public static synchronized void removeParallelWorld(String uuid){
worldMap.remove(UUID.fromString(uuid));
}

public static synchronized void removeParallelWorld(UUID uuid){
worldMap.remove(uuid);
}




private final String uuid;
private final UUID uuid;
private final Map<ChunkPosition, Map<BlockLocation, PBlockData>> chunkBlockMap;
private final Map<ChunkLocation, Object> editedPacketForChunkMap;
private final Map<ChunkLocation, Object> editedPacketForLightMap;

private ParallelWorld(String uuid){
private ParallelWorld(UUID uuid){
this.uuid = uuid;
this.chunkBlockMap = new ConcurrentHashMap<>();
this.editedPacketForChunkMap = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -107,12 +115,8 @@ public void setBlock(Block block, BlockData blockData, boolean blockUpdate){
pBlockData.setBlockData(blockData);

if(block.getChunk().isLoaded() && blockUpdate) {
for(Player player : Bukkit.getServer().getOnlinePlayers()) {
if (player.getUniqueId().toString().equals(uuid)) {
player.sendBlockChange(block.getLocation(), blockData);
break;
}
}
Player player = Bukkit.getPlayer(uuid);
if(player != null) player.sendBlockChange(block.getLocation(), blockData);
}
}

Expand Down Expand Up @@ -171,7 +175,7 @@ public void setBlocks(Map<Block, BlockData> blockDataMap, @Nullable UpdatePacket


if(Config.getWorkType() == Config.WorkType.NORMAL) {
Player player = Bukkit.getPlayer(UUID.fromString(uuid));
Player player = Bukkit.getPlayer(uuid);
if (player == null) return;
this.sendUpdatePacket(player, type, updateMap);
}else{
Expand Down Expand Up @@ -310,7 +314,7 @@ public void setBlocks(Map<Block, BlockData> blockDataMap, boolean chunkUpdate){


if(!chunkUpdate) return;
Player player = Bukkit.getPlayer(UUID.fromString(uuid));
Player player = Bukkit.getPlayer(uuid);
if(player == null) return;

for(Chunk chunk : chunkSet) {
Expand Down Expand Up @@ -358,7 +362,7 @@ public void setLightLevels(Map<Block, Integer> lightLevelMap){

/**
* 指定されたブロックに設定されているデータを削除します
* @param block
* @param block データを削除するブロック
*/
public void removeBlock(Block block){
BlockLocation location = BlockLocation.createBlockLocation(block);
Expand All @@ -371,6 +375,55 @@ public void removeBlock(Block block){
}


/**
* 指定されたブロックに設定されているデータを削除します
* @param block データを削除するブロック
* @param update ブロックの変更をプレイヤーに通知するかどうか
*/
public void removeBlock(Block block, boolean update){
removeBlock(block);
Player player = Bukkit.getPlayer(uuid);
if(player != null) player.sendBlockChange(block.getLocation(), block.getBlockData());
}

/**
* 一気に大量のブロックのデータを削除します。
* @param blocks データを削除するブロック
* @param type ブロックの変更をクライアントに適用させるためのパケットの種類。 MULTI_BLOCK_CHANGE推奨
*/
public void removeBlocks(Set<Block> blocks, @Nullable UpdatePacketType type){
if(type == null) type = UpdatePacketType.NO_UPDATE;

Map<Chunk, Set<Block>> updateMap = new HashMap<>();

for(Block block : blocks){
Location location = block.getLocation();
ChunkPosition chunkPosition = new ChunkPosition(location.getBlockX(), location.getBlockZ());
this.addEditedChunk(chunkPosition, block.getWorld());

Map<BlockLocation, PBlockData> blockMap = chunkBlockMap.get(chunkPosition);
if(blockMap != null){
blockMap.remove(BlockLocation.createBlockLocation(block));
}

Set<Block> updateBlocks = updateMap.computeIfAbsent(block.getChunk(), k -> new HashSet<>());
updateBlocks.add(block);
}


if(Config.getWorkType() == Config.WorkType.NORMAL) {
Player player = Bukkit.getPlayer(uuid);
if (player == null) return;
this.sendUpdatePacket(player, type, updateMap);
}else{
Set<Player> players = new HashSet<>(Bukkit.getOnlinePlayers());
for(Player player : players){
this.sendUpdatePacket(player, type, updateMap);
}
}
}


/**
* ブロックデータを取得します
* @param block 取得したいブロック
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/be4rjp/parallel/nms/PacketHandler.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package be4rjp.parallel.nms;
import be4rjp.parallel.Config;
import be4rjp.parallel.Parallel;
import be4rjp.parallel.nms.manager.*;
import io.netty.channel.*;
Expand Down Expand Up @@ -35,7 +36,7 @@ public void write(ChannelHandlerContext channelHandlerContext, Object packet, Ch
return;
}

if(packet.getClass().getSimpleName().equalsIgnoreCase("PacketPlayOutLightUpdate")){
if(packet.getClass().getSimpleName().equalsIgnoreCase("PacketPlayOutLightUpdate") && Config.isRewriteLightPacket()){
LightUpdatePacketManager manager = new LightUpdatePacketManager(channelHandlerContext, packet, channelPromise, this, player);
manager.runTaskAsynchronously(Parallel.getPlugin());
return;
Expand Down
6 changes: 4 additions & 2 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
# 動作モードの設定
# NORMAL or ONLY_ONE
# ONLY_ONEに設定した場合は全てのプレイヤーに対して同じParallelWorldが使われるようになります

work-type: NORMAL


# パフォーマンスモードを使用するかどうかの設定
# このプラグイン以外でブロックの設置が行われる可能性のある場合は false 推奨
performance-mode: false


performance-mode: false
## ライトパケットへの介入を行うかどうかの設定
rewrite-light-packet: true

0 comments on commit ae96800

Please sign in to comment.