Skip to content

Commit

Permalink
Random quest ID redux (#113)
Browse files Browse the repository at this point in the history
* Abstract database
* Newly created `questID` will be random
* Quest importers try to use the old quest id
* Use `RandomIndexDatabase.nextID()`
* Copy quest id button
* Remove quest id from chat
* Close popup on copy
* Require edit mode and advanced info to view quest id

---------

Co-authored-by: KatatsumuriPan <[email protected]>
  • Loading branch information
WaitingIdly and KatatsumuriPan authored May 23, 2024
1 parent c31d4d8 commit 5409ade
Show file tree
Hide file tree
Showing 16 changed files with 216 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private List<String> getQuestTooltip(IQuest quest, EntityPlayer player, int qID)
private List<String> getStandardTooltip(IQuest quest, EntityPlayer player, int qID) {
List<String> list = new ArrayList<>();

list.add(QuestTranslation.translate(quest.getProperty(NativeProps.NAME)) + (!Minecraft.getMinecraft().gameSettings.advancedItemTooltips ? "" : (" #" + qID)));
list.add(QuestTranslation.translate(quest.getProperty(NativeProps.NAME)) + (Minecraft.getMinecraft().gameSettings.advancedItemTooltips && QuestSettings.INSTANCE.getProperty(NativeProps.EDIT_MODE) ? (" #" + qID) : ""));

UUID playerID = QuestingAPI.getQuestingUUID(player);

Expand Down
127 changes: 127 additions & 0 deletions src/main/java/betterquesting/api2/storage/AbstractDatabase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package betterquesting.api2.storage;

import java.util.Collections;
import java.util.List;
import java.util.TreeMap;

public abstract class AbstractDatabase<T> implements IDatabase<T> {

/**
* If the cache size would somehow exceed 24MB (on 64bit machines) we stop.
*/
public static int CACHE_MAX_SIZE = 24 * 1024 * 1024 / 8;

/**
* If {@code mapDB.size < SPARSE_RATIO * (mapDB.lastKey() - mapDB.firstKey())} the database will be considered
* sparse and an cache array won't be built to save memory.
* <p>
* Under this sparsity a 10k element database will roughly result in a 0.5MB cache which is more than enough reasonable.
*/
public static double SPARSE_RATIO = 0.15d;

final TreeMap<Integer, T> mapDB = new TreeMap<>();

private LookupLogicType type = null;
private LookupLogic<T> logic = null;

private LookupLogic<T> getLookupLogic() {
if (type != null)
return logic;
LookupLogicType newType = LookupLogicType.determine(this);
type = newType;
logic = newType.get(this);
return logic;
}

private void updateLookupLogic() {
if (type == null)
return;
LookupLogicType newType = LookupLogicType.determine(this);
if (newType != type) {
type = null;
logic = null;
} else {
logic.onDataChange();
}
}

@Override
public synchronized DBEntry<T> add(int id, T value) {
if (value == null) {
throw new NullPointerException("Value cannot be null");
} else if (id < 0) {
throw new IllegalArgumentException("ID cannot be negative");
} else {
if (mapDB.putIfAbsent(id, value) == null) {
updateLookupLogic();
return new DBEntry<>(id, value);
} else {
throw new IllegalArgumentException("ID or value is already contained within database");
}
}
}

@Override
public synchronized boolean removeID(int key) {
if (key < 0)
return false;

if (mapDB.remove(key) != null) {
updateLookupLogic();
return true;
}
return false;
}

@Override
public synchronized boolean removeValue(T value) {
return value != null && removeID(getID(value));
}

@Override
public synchronized int getID(T value) {
if (value == null)
return -1;

for (DBEntry<T> entry : getEntries()) {
if (entry.getValue() == value)
return entry.getID();
}

return -1;
}

@Override
public synchronized T getValue(int id) {
if (id < 0 || mapDB.size() <= 0)
return null;
return mapDB.get(id);
}

@Override
public synchronized int size() {
return mapDB.size();
}

@Override
public synchronized void reset() {
mapDB.clear();
type = null;
logic = null;
}

@Override
public synchronized List<DBEntry<T>> getEntries() {
return mapDB.isEmpty() ? Collections.emptyList() : getLookupLogic().getRefCache();
}

/**
* First try to use array cache.
* If memory usage would be too high try use sort merge join if keys is large.
* Otherwise look up each key separately via {@link TreeMap#get(Object)}.
*/
@Override
public synchronized List<DBEntry<T>> bulkLookup(int... keys) {
return mapDB.isEmpty() || keys.length == 0 ? Collections.emptyList() : getLookupLogic().bulkLookup(keys);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class ArrayCacheLookupLogic<T> extends LookupLogic<T> {
private DBEntry<T>[] cache = null;
private int offset = -1;

public ArrayCacheLookupLogic(SimpleDatabase<T> simpleDatabase) {
super(simpleDatabase);
public ArrayCacheLookupLogic(AbstractDatabase<T> abstractDatabase) {
super(abstractDatabase);
}

@Override
Expand Down Expand Up @@ -48,10 +48,10 @@ public List<DBEntry<T>> bulkLookup(int[] keys) {
@SuppressWarnings("unchecked")
private void computeCache() {
if (cache != null) return;
cache = new DBEntry[simpleDatabase.mapDB.lastKey() - simpleDatabase.mapDB.firstKey() + 1];
offset = simpleDatabase.mapDB.firstKey();
cache = new DBEntry[abstractDatabase.mapDB.lastKey() - abstractDatabase.mapDB.firstKey() + 1];
offset = abstractDatabase.mapDB.firstKey();
if (refCache == null) {
for (Map.Entry<Integer, T> entry : simpleDatabase.mapDB.entrySet()) {
for (Map.Entry<Integer, T> entry : abstractDatabase.mapDB.entrySet()) {
cache[entry.getKey() - offset] = new DBEntry<>(entry.getKey(), entry.getValue());
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

public class EmptyLookupLogic<T> extends LookupLogic<T> {

public EmptyLookupLogic(SimpleDatabase<T> simpleDatabase) {
super(simpleDatabase);
public EmptyLookupLogic(AbstractDatabase<T> abstractDatabase) {
super(abstractDatabase);
}

@Override
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/betterquesting/api2/storage/LookupLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

abstract class LookupLogic<T> {

protected final SimpleDatabase<T> simpleDatabase;
protected final AbstractDatabase<T> abstractDatabase;
protected List<DBEntry<T>> refCache = null;

public LookupLogic(SimpleDatabase<T> simpleDatabase) {
this.simpleDatabase = simpleDatabase;
public LookupLogic(AbstractDatabase<T> abstractDatabase) {
this.abstractDatabase = abstractDatabase;
}

public void onDataChange() {
Expand All @@ -28,7 +28,7 @@ public List<DBEntry<T>> getRefCache() {

protected void computeRefCache() {
List<DBEntry<T>> temp = new ArrayList<>();
for (Map.Entry<Integer, T> entry : simpleDatabase.mapDB.entrySet()) {
for (Map.Entry<Integer, T> entry : abstractDatabase.mapDB.entrySet()) {
temp.add(new DBEntry<>(entry.getKey(), entry.getValue()));
}
refCache = Collections.unmodifiableList(temp);
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/betterquesting/api2/storage/LookupLogicType.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ enum LookupLogicType {
Empty(db -> db.mapDB.isEmpty(), EmptyLookupLogic::new),
ArrayCache(db -> db.mapDB.size() < CACHE_MAX_SIZE && db.mapDB.size() > SPARSE_RATIO * (db.mapDB.lastKey() - db.mapDB.firstKey()), ArrayCacheLookupLogic::new),
Naive(db -> true, NaiveLookupLogic::new);
private final Predicate<SimpleDatabase<?>> shouldUse;
private final Function<SimpleDatabase<?>, LookupLogic<?>> factory;
private final Predicate<AbstractDatabase<?>> shouldUse;
private final Function<AbstractDatabase<?>, LookupLogic<?>> factory;

LookupLogicType(Predicate<SimpleDatabase<?>> shouldUse, Function<SimpleDatabase<?>, LookupLogic<?>> factory) {
LookupLogicType(Predicate<AbstractDatabase<?>> shouldUse, Function<AbstractDatabase<?>, LookupLogic<?>> factory) {
this.shouldUse = shouldUse;
this.factory = factory;
}

static LookupLogicType determine(SimpleDatabase<?> db) {
static LookupLogicType determine(AbstractDatabase<?> db) {
for (LookupLogicType type : values()) {
if (type.shouldUse.test(db))
return type;
Expand All @@ -28,7 +28,7 @@ static LookupLogicType determine(SimpleDatabase<?> db) {
}

@SuppressWarnings("unchecked")
<T> LookupLogic<T> get(SimpleDatabase<T> db) {
<T> LookupLogic<T> get(AbstractDatabase<T> db) {
return (LookupLogic<T>) factory.apply(db);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class NaiveLookupLogic<T> extends LookupLogic<T> {

private TIntObjectMap<DBEntry<T>> backingMap;

public NaiveLookupLogic(SimpleDatabase<T> simpleDatabase) {
super(simpleDatabase);
public NaiveLookupLogic(AbstractDatabase<T> abstractDatabase) {
super(abstractDatabase);
}

@Override
Expand All @@ -23,7 +23,7 @@ public void onDataChange() {
@Override
public List<DBEntry<T>> bulkLookup(int[] keys) {
if (backingMap == null) {
backingMap = new TIntObjectHashMap<>(simpleDatabase.mapDB.size());
backingMap = new TIntObjectHashMap<>(abstractDatabase.mapDB.size());
for (DBEntry<T> entry : getRefCache()) {
backingMap.put(entry.getID(), entry);
}
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/betterquesting/api2/storage/RandomIndexDatabase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package betterquesting.api2.storage;

import java.util.Random;

public class RandomIndexDatabase<T> extends AbstractDatabase<T> {

private final Random random = new Random();

@Override
public synchronized int nextID() {
int id;
do {
// id >= 0
id = random.nextInt() & 0x7fff_ffff;
}
// The new id doesn't conflict with existing ones.
// However, new ids created by different players could conflict with each other.
while (mapDB.containsKey(id));
return id;
}

}
Loading

0 comments on commit 5409ade

Please sign in to comment.