Skip to content

Commit

Permalink
[3.3.2] Allowed to redefine recievers when sending chunk changes to c…
Browse files Browse the repository at this point in the history
…lients (updateChunk)
  • Loading branch information
Qveshn committed Sep 3, 2019
1 parent e45ce06 commit 03afc52
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,13 @@ public class ChunkInfo {
private int x;
private int y;
private int z;

@SuppressWarnings("DeprecatedIsStillUsed")
@Deprecated // Bukkit.getOnlinePlayers() will be examined
private Collection<? extends Player> receivers;

@Deprecated
public ChunkInfo(World world, int chunkX, int chunkZ, Collection<? extends Player> players) {
this(world, chunkX, 256, chunkZ, players);
}

@SuppressWarnings("deprecation")
public ChunkInfo(World world, int chunkX, int chunk_y_height, int chunkZ, Collection<? extends Player> players) {
this.world = world;
this.x = chunkX;
Expand All @@ -70,16 +66,15 @@ public int getChunkYHeight() {
return y;
}

@SuppressWarnings("unused")
public void setChunkYHeight(int y) {
this.y = y;
}

@Deprecated
public Collection<? extends Player> getReceivers() {
return receivers;
}

@Deprecated
public void setReceivers(Collection<? extends Player> receivers) {
this.receivers = receivers;
}
Expand Down
2 changes: 1 addition & 1 deletion plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>ru.beykerykt</groupId>
<artifactId>LightAPI-fork</artifactId>
<version>3.3.1</version>
<version>3.3.2</version>

<properties>
<maven.compiler.source>1.6</maven.compiler.source>
Expand Down
7 changes: 6 additions & 1 deletion plugin/src/main/java/ru/beykerykt/lightapi/LightAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -354,11 +354,16 @@ public static boolean updateChunks(ChunkInfo info) {

@SuppressWarnings("WeakerAccess")
public static boolean updateChunk(ChunkInfo info) {
return updateChunk(info, null);
}

@SuppressWarnings("WeakerAccess")
public static boolean updateChunk(ChunkInfo info, Collection<? extends Player> players) {
if (getInstance().isEnabled()) {
UpdateChunkEvent event = new UpdateChunkEvent(info);
Bukkit.getPluginManager().callEvent(event);
if (!event.isCancelled()) {
machine.addChunkToUpdate(info);
machine.addChunkToUpdate(info, players);
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 Qveshn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package ru.beykerykt.lightapi.chunks;

import org.bukkit.entity.Player;

import java.util.Collection;
import java.util.HashSet;

public class ChunkUpdateInfo {

private int sectionMask = 0;
private Collection<Player> players = new HashSet<Player>();

public void add(int sectionMask, Collection<? extends Player> players) {
this.sectionMask |= sectionMask;
add(players);
}

private void add(Collection<? extends Player> players) {
this.players.addAll(players);
}

public Collection<Player> getPlayers() {
return players;
}

public int getSectionMask() {
return sectionMask;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* The MIT License (MIT)
*
* Copyright (c) 2016 Vladimir Mikhailov <[email protected]>
* Copyright (c) 2019 Qveshn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -23,10 +24,10 @@
*/
package ru.beykerykt.lightapi.request;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import ru.beykerykt.lightapi.chunks.ChunkInfo;
import ru.beykerykt.lightapi.chunks.ChunkLocation;
import ru.beykerykt.lightapi.chunks.ChunkUpdateInfo;
import ru.beykerykt.lightapi.server.ServerModManager;
import ru.beykerykt.lightapi.server.nms.INMSHandler;
import ru.beykerykt.lightapi.utils.Debug;
Expand All @@ -39,6 +40,7 @@ public class RequestSteamMachine implements Runnable {
private boolean isStarted;
private Queue<Runnable> REQUEST_QUEUE = new ConcurrentLinkedQueue<Runnable>();
private int maxIterationsPerTick;
private Map<ChunkLocation, ChunkUpdateInfo> chunksToUpdate = new HashMap<ChunkLocation, ChunkUpdateInfo>();

// THREADS
private ScheduledFuture<?> sch;
Expand Down Expand Up @@ -75,19 +77,23 @@ public boolean addToQueue(Runnable request) {
return false;
}

private Map<ChunkLocation, Integer> chunksToUpdate = new HashMap<ChunkLocation, Integer>();

public void addChunkToUpdate(ChunkInfo info) {
public void addChunkToUpdate(final ChunkInfo info, Collection<? extends Player> receivers) {
int SectionY = info.getChunkYHeight() >> 4;
INMSHandler nmsHandler = ServerModManager.getNMSHandler();
if (nmsHandler.isValidSectionY(SectionY)) {
final ChunkLocation chunk = new ChunkLocation(info.getWorld(), info.getChunkX(), info.getChunkZ());
final int sectionYMask = nmsHandler.asSectionMask(SectionY);
final Collection<Player> players = new ArrayList<Player>(
receivers != null ? receivers : info.getReceivers()
);
addToQueue(new Runnable() {
@Override
public void run() {
Integer sectionMask = chunksToUpdate.get(chunk);
chunksToUpdate.put(chunk, (sectionMask == null ? 0 : sectionMask) | sectionYMask);
ChunkUpdateInfo chunkUpdateInfo = chunksToUpdate.get(chunk);
if (chunkUpdateInfo == null) {
chunksToUpdate.put(chunk, chunkUpdateInfo = new ChunkUpdateInfo());
}
chunkUpdateInfo.add(sectionYMask, players);
}
});
}
Expand All @@ -112,18 +118,18 @@ public void run() {
}

INMSHandler nmsHandler = ServerModManager.getNMSHandler();
Collection<? extends Player> players = Bukkit.getOnlinePlayers();

for (Map.Entry<ChunkLocation, Integer> item : chunksToUpdate.entrySet()) {
for (Map.Entry<ChunkLocation, ChunkUpdateInfo> item : chunksToUpdate.entrySet()) {
ChunkLocation chunk = item.getKey();
int sectionMask = item.getValue();
Collection<? extends Player> p =
nmsHandler.filterVisiblePlayers(chunk.getWorld(), chunk.getX(), chunk.getZ(), players);
nmsHandler.sendChunkSectionsUpdate(chunk.getWorld(), chunk.getX(), chunk.getZ(), sectionMask, p);
ChunkUpdateInfo chunkUpdateInfo = item.getValue();
int sectionMask = chunkUpdateInfo.getSectionMask();
Collection<? extends Player> players = nmsHandler.filterVisiblePlayers(
chunk.getWorld(), chunk.getX(), chunk.getZ(), chunkUpdateInfo.getPlayers());
nmsHandler.sendChunkSectionsUpdate(chunk.getWorld(), chunk.getX(), chunk.getZ(), sectionMask, players);
if (debug) {
totalSends += p.size();
totalSends += players.size();
totalSections += Integer.bitCount(sectionMask);
usedPlayers.addAll(p);
usedPlayers.addAll(players);
}
}

Expand Down

0 comments on commit 03afc52

Please sign in to comment.