Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,11 @@ public CMIAdvancement show(final Player... players) {
add();
grant(players);
final CMIAdvancement ad = this;
CMIScheduler.runTaskLater(() -> {
revoke(players);
CMILib.getInstance().getReflectionManager().removeAdvancement(ad);
}, 20L);
for (Player player : players) {
final Player one = player;
CMIScheduler.runAtEntityLater(one, () -> revoke(one), 20L);
}
CMIScheduler.runTaskLater(() -> CMILib.getInstance().getReflectionManager().removeAdvancement(ad), 40L);
return this;
}

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/net/Zrips/CMILib/BossBar/BossBarManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Map.Entry;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;

import org.bukkit.Bukkit;
import org.bukkit.boss.BarColor;
Expand All @@ -30,8 +31,10 @@ public BossBarManager(CMILib plugin) {

ConcurrentHashMap<UUID, ConcurrentHashMap<String, BossBarInfo>> barMap = new ConcurrentHashMap<UUID, ConcurrentHashMap<String, BossBarInfo>>();

private static final Pattern CLEAN = Pattern.compile("[^a-zA-Z0-9]");

private static String cleanName(String name) {
return name.replaceAll("[^a-zA-Z0-9]", "");
return CLEAN.matcher(name).replaceAll("");
}

public synchronized void addGlobalBar(BossBarInfo binfo) {
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/net/Zrips/CMILib/Chat/ChatFilterRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ public void setRuleName(String ruleName) {
public Matcher getMatcher(String msg) {
Matcher matcher = null;
for (Pattern one : pattern) {
if (one.matcher(msg).find()) {
matcher = one.matcher(msg);
Matcher m = one.matcher(msg);
if (m.find()) {
matcher = m.reset();
break;
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/net/Zrips/CMILib/Container/CMIBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ public int getDir() {
return dir;
}

private static final blockDirection[] VALUES = values();

public static blockDirection getByDir(int dir) {
for (blockDirection one : blockDirection.values()) {
for (blockDirection one : VALUES) {
if (one.getDir() == dir)
return one;
}
Expand Down Expand Up @@ -70,8 +72,10 @@ public static enum StairShape {
OUTER_RIGHT,
STRAIGHT;

private static final StairShape[] VALUES = values();

public static StairShape getByName(String name) {
for (StairShape one : StairShape.values()) {
for (StairShape one : VALUES) {
if (one.toString().equalsIgnoreCase(name))
return one;
}
Expand Down Expand Up @@ -2062,7 +2066,6 @@ public Inventory getInventory() {
if (block.getState() instanceof InventoryHolder) {
try {
block.getChunk().load(false);
block.getChunk().setForceLoaded(true);
} catch (Throwable e) {

}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/net/Zrips/CMILib/Container/CMICuboidArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ public Location getOutsideFreeLoc() {
boolean found = false;
int it = 0;
int maxIt = 30;
Random ran = new Random();
while (!found && it < maxIt) {
it++;

Random ran = new Random(System.currentTimeMillis());
if (randomLocList.isEmpty())
break;
int check = ran.nextInt(randomLocList.size());
Expand All @@ -125,8 +125,8 @@ public Location getOutsideFreeLoc() {
for (int i = max; i > getLowPoint().getY(); i--) {
loc.setY(i);
Block block = loc.getBlock();
Block block2 = loc.clone().add(0, 1, 0).getBlock();
Block block3 = loc.clone().add(0, -1, 0).getBlock();
Block block2 = loc.getWorld().getBlockAt(loc.getBlockX(), i + 1, loc.getBlockZ());
Block block3 = loc.getWorld().getBlockAt(loc.getBlockX(), i - 1, loc.getBlockZ());
if (block3.getType() != Material.AIR && block.getType() == Material.AIR && block2.getType() == Material.AIR) {
break;
}
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/net/Zrips/CMILib/Container/CMILocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,14 @@ public static Material getBlockTypeSafe(Location loc) {

ChunkSnapshot chunkSnapshot = null;

if (!world.getBlockAt(x, 0, z).getChunk().isLoaded()) {
world.getBlockAt(x, 0, z).getChunk().load();
chunkSnapshot = world.getBlockAt(x, 0, z).getChunk().getChunkSnapshot(false, false, false);
world.getBlockAt(x, 0, z).getChunk().unload();
Chunk chunk = world.getBlockAt(x, 0, z).getChunk();

if (!chunk.isLoaded()) {
chunk.load();
chunkSnapshot = chunk.getChunkSnapshot(false, false, false);
chunk.unload();
} else {
chunkSnapshot = world.getBlockAt(x, 0, z).getChunk().getChunkSnapshot();
chunkSnapshot = chunk.getChunkSnapshot();
}

if (chunkSnapshot == null)
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/net/Zrips/CMILib/Container/CuboidArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ public Location getOutsideFreeLoc() {
boolean found = false;
int it = 0;
int maxIt = 30;
Random ran = new Random();
while (!found && it < maxIt) {
it++;

Random ran = new Random(System.currentTimeMillis());
if (randomLocList.isEmpty())
break;
int check = ran.nextInt(randomLocList.size());
Expand All @@ -125,8 +125,8 @@ public Location getOutsideFreeLoc() {
for (int i = max; i > getLowPoint().getY(); i--) {
loc.setY(i);
Block block = loc.getBlock();
Block block2 = loc.clone().add(0, 1, 0).getBlock();
Block block3 = loc.clone().add(0, -1, 0).getBlock();
Block block2 = loc.getWorld().getBlockAt(loc.getBlockX(), i + 1, loc.getBlockZ());
Block block3 = loc.getWorld().getBlockAt(loc.getBlockX(), i - 1, loc.getBlockZ());
if (block3.getType() != Material.AIR && block.getType() == Material.AIR && block2.getType() == Material.AIR) {
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/Zrips/CMILib/FileDownloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void downloadUsingStream(@NotNull final String urlString, @NotNull final
CMIMessages.consoleMessage("You can do it manually, try again later or simply ignore it.");
}

FileDownloader.this.failedDownload();
CMIScheduler.runTask(FileDownloader.this::failedDownload);
} finally {
if (fileOutputStream != null)
try {
Expand Down
39 changes: 22 additions & 17 deletions src/main/java/net/Zrips/CMILib/Future/CMIFutureBatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import java.util.Queue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicBoolean;

public class CMIFutureBatcher {
private Queue<CompletableFuture<?>> queue = new ConcurrentLinkedQueue<>();
private boolean running = false;
private final AtomicBoolean running = new AtomicBoolean(false);
private int delayBetweenBatches = 0;

public CMIFutureBatcher() {
Expand All @@ -24,26 +25,30 @@ public CompletableFuture<?> addBatch(CompletableFuture<?> future) {
}

public void processQueue() {
if (running && !queue.isEmpty())
if (!running.compareAndSet(false, true))
return;
running = true;
CompletableFuture.runAsync(() -> {
while (!queue.isEmpty()) {
CompletableFuture<?> future = queue.poll();
while (true) {
while (!queue.isEmpty()) {
CompletableFuture<?> future = queue.poll();

if (future == null)
break;
if (future == null)
break;

if (delayBetweenBatches > 0)
try {
Thread.sleep(delayBetweenBatches);
} catch (InterruptedException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
}
future.complete(null);
if (delayBetweenBatches > 0)
try {
Thread.sleep(delayBetweenBatches);
} catch (InterruptedException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
}
future.complete(null);
}
running.set(false);
if (!queue.isEmpty() && running.compareAndSet(false, true))
continue;
break;
}
running = false;
});
}

Expand All @@ -60,6 +65,6 @@ public int getQueueSize() {
}

public boolean isRunning() {
return running;
return running.get();
}
}
4 changes: 2 additions & 2 deletions src/main/java/net/Zrips/CMILib/GUI/CMIGuiButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,13 @@ public void setGui(CMIGui sgui) {
}

public void startAutoUpdate(int intervalTicks) {
updateInterval = intervalTicks;
updateInterval = Math.max(1, intervalTicks);
tasker();
}

@Deprecated
public void startAutoUpdate(CMIGui sgui, int intervalTicks) {
updateInterval = intervalTicks;
updateInterval = Math.max(1, intervalTicks);
this.sgui = sgui;
tasker();
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/net/Zrips/CMILib/Images/CMIImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ private static BufferedImage getImage(URL url) {
BufferedImage res = null;
try {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
stream = connection.getInputStream();
res = ImageIO.read(stream);

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/Zrips/CMILib/Items/CMIMaterial.java
Original file line number Diff line number Diff line change
Expand Up @@ -2156,7 +2156,7 @@ public static CMIMaterial get(String id) {
}

idMap.put(id, mat);
return CMIMaterial.NONE;
return mat;
}

public static @NotNull CMIMaterial get(ItemStack item) {
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/net/Zrips/CMILib/Items/ItemManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class ItemManager {
private CMILib plugin;

static HashMap<Material, CMIMaterial> byRealMaterial = new HashMap<Material, CMIMaterial>();
private static final Pattern TAG_PATTERN = Pattern.compile("(\\{).+(\\})");

static HashMap<String, CMIMaterial> byName = new HashMap<String, CMIMaterial>();
private static HashMap<String, Material> byNameMaterial = new HashMap<String, Material>();

Expand Down Expand Up @@ -211,8 +213,7 @@ public CMIItemStack getItem(String name, CMIAsyncHead ahead) {

String tag = null;
if (name.contains("{") && name.contains("}")) {
Pattern ptr = Pattern.compile("(\\{).+(\\})");
Matcher match = ptr.matcher(name);
Matcher match = TAG_PATTERN.matcher(name);
if (match.find()) {
tag = match.group();
name = name.replace(match.group(), "");
Expand Down
Loading