mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-04-05 15:26:28 +00:00
Objectify objects
This commit is contained in:
124
src/main/java/com/volmit/iris/engine/data/cache/AtomicCache.java
vendored
Normal file
124
src/main/java/com/volmit/iris/engine/data/cache/AtomicCache.java
vendored
Normal file
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
* Iris is a World Generator for Minecraft Bukkit Servers
|
||||
* Copyright (c) 2021 Arcane Arts (Volmit Software)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.volmit.iris.engine.data.cache;
|
||||
|
||||
import com.volmit.iris.util.math.M;
|
||||
import com.volmit.iris.util.scheduling.IrisLock;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class AtomicCache<T> {
|
||||
private transient volatile T t;
|
||||
private transient volatile long a;
|
||||
private transient volatile int validations;
|
||||
private final IrisLock check;
|
||||
private final IrisLock time;
|
||||
private final IrisLock write;
|
||||
private final boolean nullSupport;
|
||||
|
||||
public AtomicCache() {
|
||||
this(false);
|
||||
}
|
||||
|
||||
public AtomicCache(boolean nullSupport) {
|
||||
this.nullSupport = nullSupport;
|
||||
check = new IrisLock("Check");
|
||||
write = new IrisLock("Write");
|
||||
time = new IrisLock("Time");
|
||||
validations = 0;
|
||||
a = -1;
|
||||
t = null;
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
check.lock();
|
||||
write.lock();
|
||||
time.lock();
|
||||
a = -1;
|
||||
t = null;
|
||||
time.unlock();
|
||||
write.unlock();
|
||||
check.unlock();
|
||||
}
|
||||
|
||||
public T aquire(Supplier<T> t) {
|
||||
if (nullSupport) {
|
||||
return aquireNull(t);
|
||||
}
|
||||
|
||||
if (this.t != null && validations > 1000) {
|
||||
return this.t;
|
||||
}
|
||||
|
||||
if (this.t != null && M.ms() - a > 1000) {
|
||||
if (this.t != null) {
|
||||
//noinspection NonAtomicOperationOnVolatileField
|
||||
validations++;
|
||||
}
|
||||
|
||||
return this.t;
|
||||
}
|
||||
|
||||
check.lock();
|
||||
|
||||
if (this.t == null) {
|
||||
write.lock();
|
||||
this.t = t.get();
|
||||
|
||||
time.lock();
|
||||
|
||||
if (a == -1) {
|
||||
a = M.ms();
|
||||
}
|
||||
|
||||
time.unlock();
|
||||
write.unlock();
|
||||
}
|
||||
|
||||
check.unlock();
|
||||
return this.t;
|
||||
}
|
||||
|
||||
public T aquireNull(Supplier<T> t) {
|
||||
if (validations > 1000) {
|
||||
return this.t;
|
||||
}
|
||||
|
||||
if (M.ms() - a > 1000) {
|
||||
//noinspection NonAtomicOperationOnVolatileField
|
||||
validations++;
|
||||
return this.t;
|
||||
}
|
||||
|
||||
check.lock();
|
||||
write.lock();
|
||||
this.t = t.get();
|
||||
|
||||
time.lock();
|
||||
|
||||
if (a == -1) {
|
||||
a = M.ms();
|
||||
}
|
||||
|
||||
time.unlock();
|
||||
write.unlock();
|
||||
check.unlock();
|
||||
return this.t;
|
||||
}
|
||||
}
|
||||
55
src/main/java/com/volmit/iris/engine/data/cache/Cache.java
vendored
Normal file
55
src/main/java/com/volmit/iris/engine/data/cache/Cache.java
vendored
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Iris is a World Generator for Minecraft Bukkit Servers
|
||||
* Copyright (c) 2021 Arcane Arts (Volmit Software)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.volmit.iris.engine.data.cache;
|
||||
|
||||
import org.bukkit.Chunk;
|
||||
|
||||
public interface Cache<V> {
|
||||
static long key(Chunk chunk) {
|
||||
return key(chunk.getX(), chunk.getZ());
|
||||
}
|
||||
|
||||
int getId();
|
||||
|
||||
V get(int x, int z);
|
||||
|
||||
static long key(int x, int z) {
|
||||
return (((long) x) << 32) | (z & 0xffffffffL);
|
||||
}
|
||||
|
||||
static int keyX(long key) {
|
||||
return (int) (key >> 32);
|
||||
}
|
||||
|
||||
static int keyZ(long key) {
|
||||
return (int) key;
|
||||
}
|
||||
|
||||
static int to1D(int x, int y, int z, int w, int h) {
|
||||
return (z * w * h) + (y * w) + x;
|
||||
}
|
||||
|
||||
static int[] to3D(int idx, int w, int h) {
|
||||
final int z = idx / (w * h);
|
||||
idx -= (z * w * h);
|
||||
final int y = idx / w;
|
||||
final int x = idx % w;
|
||||
return new int[]{x, y, z};
|
||||
}
|
||||
}
|
||||
26
src/main/java/com/volmit/iris/engine/data/cache/Multicache.java
vendored
Normal file
26
src/main/java/com/volmit/iris/engine/data/cache/Multicache.java
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Iris is a World Generator for Minecraft Bukkit Servers
|
||||
* Copyright (c) 2021 Arcane Arts (Volmit Software)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.volmit.iris.engine.data.cache;
|
||||
|
||||
public interface Multicache {
|
||||
<V> Cache<V> getCache(int id);
|
||||
|
||||
<V> Cache<V> createCache();
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ package com.volmit.iris.engine.data.mca;
|
||||
|
||||
import com.volmit.iris.Iris;
|
||||
import com.volmit.iris.core.nms.INMS;
|
||||
import com.volmit.iris.engine.cache.Cache;
|
||||
import com.volmit.iris.engine.data.cache.Cache;
|
||||
import com.volmit.iris.engine.data.B;
|
||||
import com.volmit.iris.engine.data.nbt.tag.CompoundTag;
|
||||
import com.volmit.iris.engine.data.nbt.tag.StringTag;
|
||||
|
||||
Reference in New Issue
Block a user