Skip to content

Commit

Permalink
Merge pull request #28 from CivClassic/civ14
Browse files Browse the repository at this point in the history
Civ14 p2
  • Loading branch information
ProgrammerDan authored Oct 2, 2019
2 parents deed7e3 + 4520d76 commit aad89a1
Show file tree
Hide file tree
Showing 21 changed files with 374 additions and 76 deletions.
5 changes: 3 additions & 2 deletions src/main/java/vg/civcraft/mc/civmodcore/CivModCorePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
import vg.civcraft.mc.civmodcore.locations.chunkmeta.api.ChunkMetaAPI;
import vg.civcraft.mc.civmodcore.playersettings.PlayerSettingAPI;
import vg.civcraft.mc.civmodcore.playersettings.gui.ConfigCommand;
import vg.civcraft.mc.civmodcore.scoreboard.ScoreBoardListener;
import vg.civcraft.mc.civmodcore.scoreboard.bottom.BottomLineAPI;
import vg.civcraft.mc.civmodcore.scoreboard.side.ScoreBoardListener;

import java.sql.SQLException;

Expand Down Expand Up @@ -48,7 +49,7 @@ public void onEnable() {
// Load APIs
ItemNames.loadItemNames();
EnchantmentNames.loadEnchantmentNames();
new DialogManager();
BottomLineAPI.init();
if (database != null) {
ChunkDAO dao = new ChunkDAO(database, this);
if (dao.updateDatabase()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
public class DialogManager {

private static Map<UUID, Dialog> dialogs = new TreeMap<>();

private DialogManager() {}

public static Dialog getDialog(Player p) {
return getDialog(p.getUniqueId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,10 @@ public class ClickableInventory {
* name of the inventory which is shown at the top when a player has it open
*/
public ClickableInventory(InventoryType type, String name) {
if (name == null) {
throw new IllegalArgumentException("Inventory name may not be null");
}
this(name);
if (type == null) {
throw new IllegalArgumentException("Inventory type may not be null");
}
if (name.length() > 32) {
log.warning("ClickableInventory title exceeds Bukkit limits: " + name);
name = name.substring(0, 32);
}
this.runnables = new LinkedList<>();
inventory = Bukkit.createInventory(null, type, name);
this.clickables = new IClickable[inventory.getSize() + 1];
}
Expand All @@ -70,15 +63,20 @@ public ClickableInventory(InventoryType type, String name) {
* name of the inventory which is shown at the top when a player has it open
*/
public ClickableInventory(int size, String name) {
this(name);
inventory = Bukkit.createInventory(null, size, name);
this.clickables = new IClickable[size + 1];
}

private ClickableInventory(String name) {
if (name == null) {
throw new IllegalArgumentException("Inventory name may not be null");
}
if (name.length() > 32) {
log.warning("ClickableInventory title exceeds Bukkit limits: " + name);
name = name.substring(0, 32);
}
inventory = Bukkit.createInventory(null, size, name);
this.clickables = new IClickable[size + 1];
this.runnables = new LinkedList<>();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@ public enum CacheState {
NEW, NORMAL, MODIFIED, DELETED;

public CacheState progress(CacheState next) {
if (next == MODIFIED && this == NEW) {
return NEW;
}
if (this == DELETED) {
return DELETED;
return this;
}
if (this == NEW) {
if (next == MODIFIED) {
return NEW;
}
if (next == DELETED) {
//if the data was new and deleted before being persisted, we don't
//need to do anything
return NORMAL;
}
}
return next;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
import java.util.Objects;
import java.util.TreeMap;
import java.util.function.Supplier;
import java.util.logging.Level;

import org.bukkit.World;

public class ChunkCoord {
import vg.civcraft.mc.civmodcore.CivModCorePlugin;

public class ChunkCoord implements Comparable<ChunkCoord> {

/**
* When was this chunk last loaded in Minecraft as UNIX timestamp
Expand Down Expand Up @@ -37,10 +40,10 @@ public class ChunkCoord {
*/
private int z;

private int worldID;
private short worldID;
private World world;

ChunkCoord(int x, int z, int worldID, World world) {
ChunkCoord(int x, int z, short worldID, World world) {
this.x = x;
this.z = z;
this.worldID = worldID;
Expand All @@ -50,7 +53,7 @@ public class ChunkCoord {
this.lastLoadingTime = -1;
this.lastUnloadingTime = -1;
}

/**
* @return World this instance is in
*/
Expand Down Expand Up @@ -135,7 +138,7 @@ ChunkMeta<?> getMeta(int pluginID) {
/**
* @return Internal ID of the world this chunk is in
*/
public int getWorldID() {
public short getWorldID() {
return worldID;
}

Expand Down Expand Up @@ -166,7 +169,14 @@ void loadAll() {
ChunkMeta<?> chunk = generator.getValue().get();
chunk.setChunkCoord(this);
chunk.setPluginID(generator.getKey());
chunk.populate();
try {
chunk.populate();
} catch (Exception e) {
// need to catch everything here, otherwise we block the main thread forever
// once it tries to read this
CivModCorePlugin.getInstance().getLogger().log(Level.SEVERE,
"Failed to load chunk data", e);
}
addChunkMeta(chunk);
}
this.notifyAll();
Expand All @@ -189,4 +199,17 @@ void minecraftChunkLoaded() {
void minecraftChunkUnloaded() {
this.lastUnloadingTime = System.currentTimeMillis();
}

@Override
public int compareTo(ChunkCoord o) {
int worldComp = Short.compare(this.worldID, o.getWorldID());
if (worldComp != 0) {
return worldComp;
}
int xComp = Integer.compare(this.x, o.getX());
if (xComp != 0) {
return worldComp;
}
return Integer.compare(this.z, o.getZ());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public int getOrCreatePluginID(JavaPlugin plugin) {
}
}

int getOrCreateWorldID(World world) {
short getOrCreateWorldID(World world) {
try (Connection insertConn = db.getConnection();
PreparedStatement insertWorld = insertConn
.prepareStatement("select id from cmc_worlds where uuid = ?;")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

public class GlobalChunkMetaManager {

private Map<UUID, Integer> uuidToInternalID;
private Map<UUID, Short> uuidToInternalID;
private Map<UUID, WorldChunkMetaManager> worldToManager;
private ChunkDAO chunkDao;

Expand Down Expand Up @@ -115,7 +115,7 @@ boolean registerWorld(World world) {
if (uuidToInternalID.containsKey(world.getUID())) {
return true;
}
int id = chunkDao.getOrCreateWorldID(world);
short id = chunkDao.getOrCreateWorldID(world);
if (id == -1) {
// very bad
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class WorldChunkMetaManager {
private static final long UNLOAD_DELAY = 5L * 60L * 1000L;
private static final long UNLOAD_CHECK_INTERVAL = 5L * 60L * 1000L;

private final int worldID;
private final short worldID;
private final Map<ChunkCoord, ChunkCoord> metas;
/**
* A synchronized TreeSet holding all chunk metadata belonging to unloaded
Expand All @@ -45,7 +45,7 @@ public class WorldChunkMetaManager {
private Queue<ChunkCoord> chunkLoadingQueue;
private World world;

public WorldChunkMetaManager(World world, int worldID) {
public WorldChunkMetaManager(World world, short worldID) {
this.worldID = worldID;
this.world = world;
this.metas = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,6 @@ protected D remove(int x, int y, int z) {
@SuppressWarnings("unchecked")
D oldData = (D) l4ZSection[z];
if (oldData != null) {
oldData.delete();
l4ZSection[z] = null;
setCacheState(CacheState.MODIFIED);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ public BlockDataObject(Location location) {
this.location = location;
}

public abstract void delete();

public Location getLocation() {
return location;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@ public JsonableDataObject(Location location) {

public abstract void concreteSerialize(JsonObject base);

@Override
public void delete() {
// dont need to do anything here, because the chunk will reserialize everything
// anyway and simply deleting this instance from the chunks tracking is good
// enough
}

protected JsonObject serialize() {
JsonObject json = new JsonObject();
JsonObject inner = new JsonObject();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package vg.civcraft.mc.civmodcore.locations.chunkmeta.block.table;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

import org.bukkit.Location;

import vg.civcraft.mc.civmodcore.locations.chunkmeta.CacheState;
import vg.civcraft.mc.civmodcore.locations.chunkmeta.block.BlockBasedChunkMeta;
import vg.civcraft.mc.civmodcore.locations.chunkmeta.block.BlockDataObject;

public abstract class TableBasedBlockChunkMeta<D extends TableBasedDataObject>
extends BlockBasedChunkMeta<TableBasedDataObject, TableStorageEngine<D>> {

private List<D> modifiedEntries;

public TableBasedBlockChunkMeta(boolean isNew, TableStorageEngine<D> storage) {
super(isNew, storage);
this.modifiedEntries = new ArrayList<>();
}

@SuppressWarnings("rawtypes")
Expand Down Expand Up @@ -43,22 +49,61 @@ private void iterateAll(Consumer<D> functionToApply) {
}
}

public void reportChange(D data) {
modifiedEntries.add(data);
}

@SuppressWarnings("unchecked")
@Override
public void put(int x, int y, int z, TableBasedDataObject blockData, boolean isNew) {
super.put(x, y, z, blockData, isNew);
if (isNew) {
modifiedEntries.add((D) blockData);
}
}

@SuppressWarnings("unchecked")
@Override
public void remove(TableBasedDataObject blockData) {
super.remove(blockData);
blockData.setCacheState(CacheState.DELETED);
//this may look weird, but is what happens if the data was NEW previously, never written to the
//db and doesn't need to be deleted from there either
if (blockData.getCacheState() != CacheState.NORMAL) {
modifiedEntries.add((D) blockData);
}
}

@SuppressWarnings("unchecked")
@Override
protected TableBasedDataObject remove(int x, int y, int z) {
TableBasedDataObject data = super.remove(x, y, z);
if (data != null) {
data.setCacheState(CacheState.DELETED);
if (data.getCacheState() != CacheState.NORMAL) {
modifiedEntries.add((D) data);
}
}
return data;
}

@Override
public void insert() {
iterateAll(d -> {
switch (d.getCacheState()) {
for (D data : modifiedEntries) {
switch (data.getCacheState()) {
case NORMAL:
return;
continue;
case MODIFIED:
storage.update(d, chunkCoord);
return;
storage.update(data, chunkCoord);
break;
case NEW:
storage.insert(d, chunkCoord);
return;
storage.insert(data, chunkCoord);
break;
case DELETED:
storage.delete(d, chunkCoord);
storage.delete(data, chunkCoord);
}
});
data.setCacheState(CacheState.NORMAL);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ public TableBasedDataObject(Location location, boolean isNew) {
this.cacheState = isNew ? CacheState.NEW : CacheState.NORMAL;
}

@Override
public void delete() {
this.cacheState = CacheState.DELETED;
}

public CacheState getCacheState() {
return cacheState;
}
Expand All @@ -27,11 +22,14 @@ public void setDirty() {
setCacheState(CacheState.MODIFIED);
}

@SuppressWarnings("unchecked")
@Override
public void setCacheState(CacheState state) {
CacheState oldState = this.cacheState;
this.cacheState = this.cacheState.progress(state);
if (state == CacheState.MODIFIED) {
getOwningCache().setCacheState(state);
if (cacheState != CacheState.NORMAL && cacheState != oldState) {
getOwningCache().setCacheState(CacheState.MODIFIED);
((TableBasedBlockChunkMeta<TableBasedDataObject>) getOwningCache()).reportChange(this);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

public class BooleanSetting extends PlayerSetting<Boolean> {

public BooleanSetting(JavaPlugin owningPlugin, Boolean defaultValue, String name, String identifier, ItemStack gui,
public BooleanSetting(JavaPlugin owningPlugin, Boolean defaultValue, String name, String identifier,
String description) {
super(owningPlugin, defaultValue, name, identifier, gui, description);
super(owningPlugin, defaultValue, name, identifier, new ItemStack(Material.STONE), description);
}

@Override
Expand Down
Loading

0 comments on commit aad89a1

Please sign in to comment.