Skip to content

Commit

Permalink
v1.1.1 軽量化とスレッドセーフ化
Browse files Browse the repository at this point in the history
  • Loading branch information
BuildTools committed Nov 13, 2021
1 parent e9ba3e6 commit 1f3787b
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 8 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.1.0</version>
<version>v1.1.1</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.1.0</version>
<version>1.1.1</version>
<packaging>jar</packaging>

<name>Parallel</name>
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/be4rjp/parallel/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ public class Config {
private static boolean performanceMode = false;

private static boolean rewriteLightPacket = true;

private static boolean showChunkPacketWarning = true;

public static WorkType getWorkType() {return workType;}

public static boolean isPerformanceMode() {return performanceMode;}

public static boolean isRewriteLightPacket() {return rewriteLightPacket;}


public static boolean isShowChunkPacketWarning() {return showChunkPacketWarning;}

public static void load(){
File file = new File("plugins/Parallel", "config.yml");
file.getParentFile().mkdirs();
Expand All @@ -32,6 +36,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");
if(yml.contains("show-chunk-packet-warning")) showChunkPacketWarning = yml.getBoolean("show-chunk-packet-warning");
}

public enum WorkType{
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/be4rjp/parallel/EventListener.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package be4rjp.parallel;

import be4rjp.parallel.chunk.AsyncChunkCash;
import be4rjp.parallel.nms.NMSUtil;
import be4rjp.parallel.nms.PacketHandler;
import io.netty.channel.Channel;
import io.netty.channel.ChannelPipeline;
import org.bukkit.Chunk;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.world.ChunkLoadEvent;

public class EventListener implements Listener {

Expand Down Expand Up @@ -44,4 +48,13 @@ public void onleave(PlayerQuitEvent event){
e.printStackTrace();
}
}

@EventHandler
public void onChunkLoad(ChunkLoadEvent event){
World world = event.getWorld();
Chunk chunk = event.getChunk();

AsyncChunkCash asyncChunkCash = AsyncChunkCash.computeIfAbsentWorldAsyncChunkCash(world.getName());
asyncChunkCash.addLoadedChunk(chunk);
}
}
35 changes: 35 additions & 0 deletions src/main/java/be4rjp/parallel/chunk/AsyncChunkCash.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package be4rjp.parallel.chunk;

import be4rjp.parallel.nms.NMSUtil;
import org.bukkit.Chunk;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class AsyncChunkCash {

private static final Map<String, AsyncChunkCash> worldChunkCashMap = new ConcurrentHashMap<>();

public static AsyncChunkCash computeIfAbsentWorldAsyncChunkCash(String worldName){
return worldChunkCashMap.computeIfAbsent(worldName, k -> new AsyncChunkCash());
}

public static AsyncChunkCash getWorldAsyncChunkCash(String worldName){
return worldChunkCashMap.get(worldName);
}

private final Map<Long, Object> chunkCashMap = new ConcurrentHashMap<>();

public void addLoadedChunk(Chunk chunk){
try {
Object nmsChunk = NMSUtil.getNMSChunk(chunk);
long coordinate = ((long)chunk.getX() << 32) | (chunk.getZ() & 0xFFFFFFFFL);
chunkCashMap.put(coordinate, nmsChunk);
} catch (Exception e) {e.printStackTrace();}
}

public Object getCashedChunk(int chunkX, int chunkZ){
return chunkCashMap.get(((long)chunkX << 32) | (chunkZ & 0xFFFFFFFFL));
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package be4rjp.parallel.nms.manager;

import be4rjp.parallel.Config;
import be4rjp.parallel.Parallel;
import be4rjp.parallel.ParallelWorld;
import be4rjp.parallel.chunk.AsyncChunkCash;
import be4rjp.parallel.nms.NMSUtil;
import be4rjp.parallel.nms.PacketHandler;
import be4rjp.parallel.util.BlockLocation;
Expand Down Expand Up @@ -85,7 +87,20 @@ public void run() {
}

Object nmsWorld = NMSUtil.getNMSWorld(player.getWorld());
Object nmsChunk = NMSUtil.getNMSChunk(player.getWorld().getChunkAt(chunkX, chunkZ));

AsyncChunkCash asyncChunkCash = AsyncChunkCash.getWorldAsyncChunkCash(player.getWorld().getName());
if(asyncChunkCash == null){
packetHandler.doWrite(channelHandlerContext, packet, channelPromise);
sendChunkWarnMessage();
return;
}

Object nmsChunk = asyncChunkCash.getCashedChunk(chunkX, chunkZ);
if(nmsChunk == null){
packetHandler.doWrite(channelHandlerContext, packet, channelPromise);
sendChunkWarnMessage();
return;
}

Object newChunk = NMSUtil.createChunk(nmsWorld, loc.get(nmsChunk), d.get(nmsChunk));

Expand Down Expand Up @@ -149,8 +164,13 @@ public void run() {
parallelWorld.getEditedPacketForChunkMap().put(chunkLocation, newPacket);
packetHandler.doWrite(channelHandlerContext, newPacket, channelPromise);
}catch (ClosedChannelException e){
//None
}catch (Exception e){
e.printStackTrace();
}
}

public static void sendChunkWarnMessage(){
if(Config.isShowChunkPacketWarning()) Parallel.getPlugin().getLogger().warning("Attempted to send a packet with an unloaded chunk.");
}
}
8 changes: 4 additions & 4 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@

# 動作モードの設定
# NORMAL or ONLY_ONE
# ONLY_ONEに設定した場合は全てのプレイヤーに対して同じParallelWorldが使われるようになります
work-type: NORMAL


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


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

# 読み込まれていないチャンクのパケットを送信しようとしたときに警告を表示するかどうかの設定
show-chunk-packet-warning: true

0 comments on commit 1f3787b

Please sign in to comment.