Skip to content

Commit

Permalink
add addDiffs() method.
Browse files Browse the repository at this point in the history
  • Loading branch information
bea4dev committed Jan 30, 2022
1 parent 2d8593b commit 9c81deb
Show file tree
Hide file tree
Showing 10 changed files with 210 additions and 33 deletions.
2 changes: 1 addition & 1 deletion API/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>parallel_api</artifactId>
<packaging>jar</packaging>
<version>2.0.1</version>
<version>2.0.2</version>
<groupId>be4rjp</groupId>

<distributionManagement>
Expand Down
13 changes: 13 additions & 0 deletions API/src/main/java/be4rjp/parallel/ParallelUniverse.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import be4rjp.parallel.player.ParallelPlayer;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.Set;

/**
Expand Down Expand Up @@ -40,4 +41,16 @@ public interface ParallelUniverse {
* @return All players in this universe
*/
Set<ParallelPlayer> getResidents();

/**
* Get all world in this universe.
* @return All world.
*/
Collection<ParallelWorld> getAllWorld();

/**
* Add a diff for the specified universe.
* @param universe Universe
*/
void addDiffs(ParallelUniverse universe);
}
7 changes: 7 additions & 0 deletions API/src/main/java/be4rjp/parallel/ParallelWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.Set;

public interface ParallelWorld {
Expand Down Expand Up @@ -176,4 +177,10 @@ public interface ParallelWorld {
*/
void sendMultiBlockUpdate(Set<BlockPosition3i> blocks);

/**
* Get all chunks in this world.
* @return All chunks
*/
Collection<ParallelChunk> getAllChunk();

}
34 changes: 20 additions & 14 deletions Plugin/src/main/java/be4rjp/parallel/EventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@
import be4rjp.parallel.nms.NMSManager;
import be4rjp.parallel.nms.PacketHandler;
import be4rjp.parallel.player.ParallelPlayer;
import be4rjp.parallel.util.RegionBlocks;
import io.netty.channel.Channel;
import io.netty.channel.ChannelPipeline;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;

Expand All @@ -26,16 +21,9 @@ public void onjoin(PlayerJoinEvent event){
Player player = event.getPlayer();
ParallelPlayer parallelPlayer = ImplParallelPlayer.onPlayerJoin(player);

ParallelUniverse universe = ParallelAPI.getInstance().createUniverse(player.getName());
ParallelUniverse universe = ParallelAPI.getInstance().createUniverse(player.getUniqueId().toString());
universe.addPlayer(parallelPlayer);

ParallelWorld parallelWorld = universe.getWorld(player.getWorld().getName());
Location loc = player.getLocation();
RegionBlocks blocks = new RegionBlocks(loc.clone().add(20, 20, 20), loc.clone().add(-20, -20, -20));
for(Block block : blocks.getBlocks()){
parallelWorld.setType(block.getX(), block.getY(), block.getZ(), Material.AIR);
}

PacketHandler packetHandler = new PacketHandler(parallelPlayer);

try {
Expand All @@ -45,6 +33,24 @@ public void onjoin(PlayerJoinEvent event){
e.printStackTrace();
}
}

/*-----------------------TEST CODE--------------------------
@EventHandler
public void onClick(PlayerAnimationEvent event){
Player player = event.getPlayer();
if(!player.isSneaking()) return;
ParallelAPI api = ParallelAPI.getInstance();
ParallelPlayer parallelPlayer = api.getParallelPlayer(player);
if(parallelPlayer == null) return;
ParallelUniverse universe = parallelPlayer.getUniverse();
if(universe == null) return;
for(ParallelUniverse otherUniverse : api.getAllUniverse()){
if(otherUniverse != universe) universe.addDiffs(otherUniverse);
}
}
@EventHandler
Expand All @@ -71,7 +77,7 @@ public void onBlockBreak(BlockBreakEvent event){
}
event.setCancelled(true);
}
}*/


@EventHandler
Expand Down
40 changes: 37 additions & 3 deletions Plugin/src/main/java/be4rjp/parallel/impl/ImplParallelChunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public ImplParallelChunk(ParallelWorld parallelWorld, int chunkX, int chunkZ){
this.chunkX = chunkX;
this.chunkZ = chunkZ;

this.blockLightArrays = new SectionLevelArray[NMSManager.isHigher_v1_18_R1() ? 32 : 16];
this.skyLightArrays = new SectionLevelArray[NMSManager.isHigher_v1_18_R1() ? 32 : 16];
this.sectionTypeArrays = new SectionTypeArray[NMSManager.isHigher_v1_18_R1() ? 32 : 16];
this.blockLightArrays = new SectionLevelArray[NMSManager.isHigher_v1_18_R1() ? 24 : 16];
this.skyLightArrays = new SectionLevelArray[NMSManager.isHigher_v1_18_R1() ? 24 : 16];
this.sectionTypeArrays = new SectionTypeArray[NMSManager.isHigher_v1_18_R1() ? 24 : 16];
}

@Override
Expand All @@ -55,6 +55,40 @@ public ImplParallelChunk(ParallelWorld parallelWorld, int chunkX, int chunkZ){

@Override
public int getChunkZ() {return chunkZ;}


public SectionLevelArray createBlockLightSectionLevelArrayIfAbsent(int sectionY) {
int sectionIndex = getSectionIndex(sectionY << 4);
SectionLevelArray sectionLevelArray = blockLightArrays[sectionIndex];
if(sectionLevelArray == null) {
sectionLevelArray = new SectionLevelArray();
blockLightArrays[sectionIndex] = sectionLevelArray;
}

return sectionLevelArray;
}

public SectionLevelArray createSkyLightSectionLevelArrayIfAbsent(int sectionY) {
int sectionIndex = getSectionIndex(sectionY << 4);
SectionLevelArray sectionLevelArray = skyLightArrays[sectionIndex];
if(sectionLevelArray == null) {
sectionLevelArray = new SectionLevelArray();
skyLightArrays[sectionIndex] = sectionLevelArray;
}

return sectionLevelArray;
}

public SectionTypeArray createSectionTypeArrayIfAbsent(int sectionY) {
int sectionIndex = getSectionIndex(sectionY << 4);
SectionTypeArray sectionTypeArray = sectionTypeArrays[sectionIndex];
if(sectionTypeArray == null) {
sectionTypeArray = new SectionTypeArray();
sectionTypeArrays[sectionIndex] = sectionTypeArray;
}

return sectionTypeArray;
}


private int getSectionIndex(int blockY){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,7 @@ public synchronized void setUniverse(@Nullable ParallelUniverse parallelUniverse

this.currentUniverse = parallelUniverse;
}

public void setUniverseRaw(ParallelUniverse universe){this.currentUniverse = universe;}

}
72 changes: 60 additions & 12 deletions Plugin/src/main/java/be4rjp/parallel/impl/ImplParallelUniverse.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package be4rjp.parallel.impl;

import be4rjp.parallel.ParallelChunk;
import be4rjp.parallel.ParallelUniverse;
import be4rjp.parallel.ParallelWorld;
import be4rjp.parallel.nms.NMSManager;
import be4rjp.parallel.player.ParallelPlayer;
import be4rjp.parallel.util.SectionLevelArray;
import be4rjp.parallel.util.SectionTypeArray;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
Expand All @@ -21,9 +26,7 @@ public ImplParallelUniverse(String universeName){
}

@Override
public @NotNull String getName() {
return universeName;
}
public @NotNull String getName() {return universeName;}


private final Map<String, ParallelWorld> parallelWorldMap = new ConcurrentHashMap<>();
Expand All @@ -34,19 +37,64 @@ public ImplParallelUniverse(String universeName){
}

@Override
public void addPlayer(@NotNull ParallelPlayer player) {
player.setUniverse(this);
}
public void addPlayer(@NotNull ParallelPlayer player) {player.setUniverse(this);}

@Override
public void removePlayer(@NotNull ParallelPlayer player) {
player.setUniverse(null);
}
public void removePlayer(@NotNull ParallelPlayer player) {player.setUniverse(null);}

@Override
public Set<ParallelPlayer> getResidents() {
return new HashSet<>(players);
public Set<ParallelPlayer> getResidents() {return new HashSet<>(players);}

@Override
public Collection<ParallelWorld> getAllWorld() {return parallelWorldMap.values();}

@Override
public void addDiffs(ParallelUniverse universe) {
int indexStart = NMSManager.isHigher_v1_18_R1() ? -4 : 0;
int indexEnd = NMSManager.isHigher_v1_18_R1() ? 20 : 16;

for(ParallelWorld diffWorld : universe.getAllWorld()){
for(ParallelChunk diffChunk : diffWorld.getAllChunk()){
for(int i = indexStart; i < indexEnd; i++){
ParallelWorld thisWorld = null;
ParallelChunk thisChunk = null;

SectionTypeArray sectionTypeArray = diffChunk.getSectionTypeArray(i);
if(sectionTypeArray != null) {
thisWorld = this.getWorld(diffWorld.getName());
thisChunk = ((ImplParallelWorld) thisWorld).createChunkIfAbsent(diffChunk.getChunkX(), diffChunk.getChunkZ());
SectionTypeArray thisType = ((ImplParallelChunk) thisChunk).createSectionTypeArrayIfAbsent(i);

sectionTypeArray.threadsafeIteration(thisType::setType);
}

SectionLevelArray blockLightLevelArray = diffChunk.getBlockLightSectionLevelArray(i);
if(blockLightLevelArray != null){
if(thisWorld == null) thisWorld = this.getWorld(diffWorld.getName());
if(thisChunk == null) thisChunk = ((ImplParallelWorld) thisWorld).createChunkIfAbsent(diffChunk.getChunkX(), diffChunk.getChunkZ());
SectionLevelArray thisLevel = ((ImplParallelChunk) thisChunk).createBlockLightSectionLevelArrayIfAbsent(i);

blockLightLevelArray.threadsafeIteration(thisLevel::setLevel);
}

SectionLevelArray skyLightLevelArray = diffChunk.getSkyLightSectionLevelArray(i);
if(skyLightLevelArray != null){
if(thisWorld == null) thisWorld = this.getWorld(diffWorld.getName());
if(thisChunk == null) thisChunk = ((ImplParallelWorld) thisWorld).createChunkIfAbsent(diffChunk.getChunkX(), diffChunk.getChunkZ());
SectionLevelArray thisLevel = ((ImplParallelChunk) thisChunk).createSkyLightSectionLevelArrayIfAbsent(i);

skyLightLevelArray.threadsafeIteration(thisLevel::setLevel);
}
}
}
}


for(ParallelPlayer parallelPlayer : this.getResidents()){
((ImplParallelPlayer) parallelPlayer).setUniverseRaw(null);
parallelPlayer.setUniverse(this);
}
}

public Set<ParallelPlayer> getPlayers() {return players;}
}
12 changes: 10 additions & 2 deletions Plugin/src/main/java/be4rjp/parallel/impl/ImplParallelWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -31,7 +32,11 @@ public ImplParallelWorld(ParallelUniverse parallelUniverse, String worldName){



private final Map<Long, ImplParallelChunk> chunkMap = new ConcurrentHashMap<>();
private final Map<Long, ParallelChunk> chunkMap = new ConcurrentHashMap<>();

public ParallelChunk createChunkIfAbsent(int chunkX, int chunkZ){
return chunkMap.computeIfAbsent(ChunkUtil.getCoordinateKey(chunkX, chunkZ), key -> new ImplParallelChunk(this, chunkX, chunkZ));
}

@Override
public String getName() {
Expand Down Expand Up @@ -216,7 +221,7 @@ public boolean hasSkyLight(int blockX, int blockY, int blockZ) {
}

@Override
public ImplParallelChunk getChunk(int chunkX, int chunkZ){return chunkMap.get(ChunkUtil.getCoordinateKey(chunkX, chunkZ));}
public ParallelChunk getChunk(int chunkX, int chunkZ){return chunkMap.get(ChunkUtil.getCoordinateKey(chunkX, chunkZ));}

@Override
public void sendBlockUpdate(int blockX, int blockY, int blockZ) {
Expand All @@ -240,4 +245,7 @@ public void sendMultiBlockUpdate(Set<BlockPosition3i> blocks) {
}
}

@Override
public Collection<ParallelChunk> getAllChunk() {return chunkMap.values();}

}
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
## Parallel-v2
プレイヤーごとに表示するブロックを変更するためのプラグイン

このプラグインは他のプラグインからの呼び出しを前提としています


### Supported version
```
1.15.2, 1.16.5
```

### maven
```xml
<repositories>
<repository>
<id>github</id>
<url>https://raw.github.com/Be4rJP/Parallel/mvn-repo/</url>
</repository>
</repositories>
```

```xml
<dependency>
<groupId>be4rjp</groupId>
<artifactId>parallel_api</artifactId>
<version>2.0.2</version>
<scope>provided</scope>
</dependency>
```

### 使用例

* 別プラグインからブロックを設置
```java
Player player = ...;
Block block = ...;

//APIのインスタンスを取得
ParallelAPI api = ParallelAPI.getInstance();

//Universeを作成
ParallelUniverse universe = api.createUniverse("TestUniverse");

//ParallelPlayerを取得
ParallelPlayer parallelPlayer = api.getParallelPlayer(player);
if(parallelPlayer == null) return;

//作成したuniverseにプレイヤーを参加させる
universe.addPlayer(parallelPlayer);

//作成したuniverse内のParallelWorldを取得
ParallelWorld parallelWorld = universe.getWorld(player.getWorld().getName());

//指定された座標にブロックをセット
parallelWorld.setType(block.getX(), block.getY(), block.getZ(), Material.REDSTONE_BLOCK);

//ブロックの変更をプレイヤーに通知
parallelWorld.sendBlockChange(block.getX(), block.getY(), block.getZ());
```
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<revision>2.0.1</revision>
<revision>2.0.2</revision>
</properties>

<groupId>be4rjp</groupId>
Expand Down

0 comments on commit 9c81deb

Please sign in to comment.