Skip to content

Commit

Permalink
Refactor scheduling for stargate
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorinwasher committed Feb 21, 2024
1 parent eb15df7 commit 69426c7
Show file tree
Hide file tree
Showing 18 changed files with 444 additions and 243 deletions.
26 changes: 14 additions & 12 deletions src/main/java/org/sgrewritten/stargate/database/SQLDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.sgrewritten.stargate.config.TableNameConfiguration;
import org.sgrewritten.stargate.database.property.StoredPropertiesAPI;
import org.sgrewritten.stargate.database.property.StoredProperty;
import org.sgrewritten.stargate.exception.GateConflictException;
import org.sgrewritten.stargate.exception.InvalidStructureException;
import org.sgrewritten.stargate.exception.PortalLoadException;
import org.sgrewritten.stargate.exception.StargateInitializationException;
Expand Down Expand Up @@ -289,18 +288,21 @@ private void loadPortal(PortalData portalData, StargateAPI stargateAPI) throws S
final List<PortalPosition> portalPositions = getPortalPositions(portalData, network.getId());

//Actually register the gate and its positions
new StargateRegionTask(portalData.gateData().topLeft(), () -> {
try {
registerPortalGate(portalData, network, stargateAPI, portalPositions);
} catch (TranslatableException e) {
Stargate.log(e);
} catch (InvalidStructureException e) {
Stargate.log(Level.WARNING, String.format(
"The portal %s in %snetwork %s located at %s is in an invalid state, and could therefore not be recreated",
portalData.name(), (portalData.portalType() == StorageType.INTER_SERVER ? "inter-server-" : ""), portalData.networkName(),
portalData.gateData().topLeft()));
new StargateRegionTask(portalData.gateData().topLeft()) {
@Override
public void run() {
try {
registerPortalGate(portalData, network, stargateAPI, portalPositions);
} catch (TranslatableException e) {
Stargate.log(e);
} catch (InvalidStructureException e) {
Stargate.log(Level.WARNING, String.format(
"The portal %s in %snetwork %s located at %s is in an invalid state, and could therefore not be recreated",
portalData.name(), (portalData.portalType() == StorageType.INTER_SERVER ? "inter-server-" : ""), portalData.networkName(),
portalData.gateData().topLeft()));
}
}
}).run();
}.runNow();
}

/**
Expand Down
79 changes: 44 additions & 35 deletions src/main/java/org/sgrewritten/stargate/gate/Gate.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,19 +145,22 @@ private void drawSigns(final SignLine[] signLines) {
List<PortalPosition> activePortalPositions = getActivePortalPositions(PositionType.SIGN);
for (PortalPosition portalPosition : activePortalPositions) {
Location signLocation = getLocation(portalPosition.getRelativePositionLocation());
new StargateRegionTask(signLocation, () -> {
Stargate.log(Level.FINER, "Drawing sign at location " + signLocation);
BlockState signState = signLocation.getBlock().getState();
if (!(signState instanceof Sign sign)) {
Stargate.log(Level.FINE, "Could not find sign at position " + signLocation);
return;
new StargateRegionTask(signLocation){
@Override
public void run() {
Stargate.log(Level.FINER, "Drawing sign at location " + signLocation);
BlockState signState = signLocation.getBlock().getState();
if (!(signState instanceof Sign sign)) {
Stargate.log(Level.FINE, "Could not find sign at position " + signLocation);
return;
}
StargateSignFormatPortalEvent event = new StargateSignFormatPortalEvent(portal, signLines, portalPosition, signLocation);
Bukkit.getPluginManager().callEvent(event);
SignLine[] newSignLines = event.getLines();
setSignLines(sign, newSignLines);
sign.update();
}
StargateSignFormatPortalEvent event = new StargateSignFormatPortalEvent(portal, signLines, portalPosition, signLocation);
Bukkit.getPluginManager().callEvent(event);
SignLine[] newSignLines = event.getLines();
setSignLines(sign, newSignLines);
sign.update();
}).run();
}.runNow();
}
}

Expand All @@ -179,19 +182,22 @@ private void setSignLines(Sign sign, SignLine[] signLines) {
private void drawButtons() {
for (PortalPosition portalPosition : getActivePortalPositions(PositionType.BUTTON)) {
Location buttonLocation = getLocation(portalPosition.getRelativePositionLocation());
new StargateRegionTask(buttonLocation, () -> {
Material blockType = buttonLocation.getBlock().getType();
if (ButtonHelper.isButton(blockType)) {
return;
}
Material buttonMaterial = ButtonHelper.getButtonMaterial(buttonLocation);
Stargate.log(Level.FINEST, "buttonMaterial: " + buttonMaterial);
Directional buttonData = (Directional) Bukkit.createBlockData(buttonMaterial);
buttonData.setFacing(facing);
new StargateRegionTask(buttonLocation) {
@Override
public void run() {
Material blockType = buttonLocation.getBlock().getType();
if (ButtonHelper.isButton(blockType)) {
return;
}
Material buttonMaterial = ButtonHelper.getButtonMaterial(buttonLocation);
Stargate.log(Level.FINEST, "buttonMaterial: " + buttonMaterial);
Directional buttonData = (Directional) Bukkit.createBlockData(buttonMaterial);
buttonData.setFacing(facing);

buttonLocation.getBlock().setBlockData(buttonData);
BlockDropManager.disableBlockDrops(buttonLocation.getBlock());
}).run();
buttonLocation.getBlock().setBlockData(buttonData);
BlockDropManager.disableBlockDrops(buttonLocation.getBlock());
}
}.runNow();

}
}
Expand Down Expand Up @@ -295,19 +301,22 @@ private void setIrisMaterial(Material material) {

for (BlockLocation blockLocation : locations) {
Block block = blockLocation.getLocation().getBlock();
new StargateRegionTask(block.getLocation(), ()->{
block.setBlockData(blockData);
if (material == Material.END_GATEWAY) {// force a location to prevent exit gateway generation
EndGateway gateway = (EndGateway) block.getState();
// https://github.com/stargate-bukkit/Stargate-Bukkit/issues/36
gateway.setAge(-9223372036854775808L);
if (block.getWorld().getEnvironment() == World.Environment.THE_END) {
gateway.setExitLocation(block.getLocation());
gateway.setExactTeleport(true);
new StargateRegionTask(block.getLocation()) {
@Override
public void run() {
block.setBlockData(blockData);
if (material == Material.END_GATEWAY) {// force a location to prevent exit gateway generation
EndGateway gateway = (EndGateway) block.getState();
// https://github.com/stargate-bukkit/Stargate-Bukkit/issues/36
gateway.setAge(-9223372036854775808L);
if (block.getWorld().getEnvironment() == World.Environment.THE_END) {
gateway.setExitLocation(block.getLocation());
gateway.setExactTeleport(true);
}
gateway.update(false, false);
}
gateway.update(false, false);
}
}).run();
}.runNow();
}
}

Expand Down
31 changes: 17 additions & 14 deletions src/main/java/org/sgrewritten/stargate/gate/structure/GateIris.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,26 @@ protected boolean isValidBlock(BlockVector blockVector, Material material) {
public void generateStructure(VectorOperation converter, Location topLeft) {
// (Clear all blocks that are in the portal iris)
Material[] irisClosedList = irisClosed.toArray(new Material[0]);
for(BlockVector blockVector : this.blocks){
for (BlockVector blockVector : this.blocks) {
int target = RANDOM.nextInt(irisClosedList.length);
Material chosenType = irisClosedList[target];
Location location = VectorUtils.getLocation(topLeft,converter,blockVector);
new StargateRegionTask(location, () -> {
Block block = location.getBlock();
if(chosenType == block.getType()){
return;
Location location = VectorUtils.getLocation(topLeft, converter, blockVector);
new StargateRegionTask(location) {
@Override
public void run() {
Block block = location.getBlock();
if (chosenType == block.getType()) {
return;
}
BlockData blockData = chosenType.createBlockData();
// Over-engineering :)
if (blockData instanceof Orientable orientable) {
orientable.setAxis(converter.getIrisNormal());
}
block.setBlockData(blockData);
BlockDropManager.disableBlockDrops(block);
}
BlockData blockData = chosenType.createBlockData();
// Over-engineering :)
if(blockData instanceof Orientable orientable){
orientable.setAxis(converter.getIrisNormal());
}
block.setBlockData(blockData);
BlockDropManager.disableBlockDrops(block);
}).run();
}.runNow();

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,18 +197,23 @@ public void onPlayerJoin(PlayerJoinEvent event) {
*/
private void getBungeeServerName() {
//Action for loading bungee server id
Runnable action = (() -> {
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
dataOutputStream.writeUTF(PluginChannel.GET_SERVER.getChannel());
Bukkit.getServer().sendPluginMessage(Stargate.getInstance(), PluginChannel.BUNGEE.getChannel(),
byteArrayOutputStream.toByteArray());
} catch (IOException e) {
Stargate.log(e);
new StargateGlobalTask() {
@Override
public void run() {
if(Bukkit.getServer().getOnlinePlayers().isEmpty()){
return;
}
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
dataOutputStream.writeUTF(PluginChannel.GET_SERVER.getChannel());
Bukkit.getServer().sendPluginMessage(Stargate.getInstance(), PluginChannel.BUNGEE.getChannel(),
byteArrayOutputStream.toByteArray());
} catch (IOException e) {
Stargate.log(e);
}
this.cancel();
}
});
new StargateGlobalTask(action).run(true);
}.runTaskTimer(0,20);


//Update the server name in the database once it's known
Expand All @@ -219,13 +224,16 @@ private void getBungeeServerName() {
* Updates this server's name in the database if necessary
*/
private void updateServerName() {
new StargateGlobalTask(() -> {
try {
storageAPI.startInterServerConnection();
} catch (StorageWriteException e) {
Stargate.log(e);
new StargateGlobalTask(true) {
@Override
public void run() {
try {
storageAPI.startInterServerConnection();
} catch (StorageWriteException e) {
Stargate.log(e);
}
}
}).run(true);
}.runNow();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,16 @@ public Network createNetwork(String name, NetworkType type, StorageType storageT
if (network != null && network.getType() != type) {
String newId = registry.getValidNewName(network);
registry.renameNetwork(newId, network.getId(), network.getStorageType());
new StargateQueuedAsyncTask(() -> {
try {
storageAPI.updateNetworkName(newId, network.getName(), network.getStorageType());
} catch (StorageWriteException e) {
Stargate.log(e);
new StargateQueuedAsyncTask() {
@Override
public void run() {
try {
storageAPI.updateNetworkName(newId, network.getName(), network.getStorageType());
} catch (StorageWriteException e) {
Stargate.log(e);
}
}
}).run();
}.runNow();
}
}
throw new NameConflictException("network of id '" + name + "' already exists", true);
Expand Down Expand Up @@ -213,7 +216,12 @@ public void loadPortals(StargateAPI stargateAPI) {
* region scheduler. Temporary solution right now is to just have a large delay here.
* TODO: come up with a general solution
*/
new StargateGlobalTask(registry::updateAllPortals).runDelayed(20);
new StargateGlobalTask() {
@Override
public void run() {
registry.updateAllPortals();
}
}.runDelayed(20);
}

@Override
Expand All @@ -238,13 +246,16 @@ public void rename(Portal portal, String newName) throws NameConflictException {
@Override
public void savePortal(RealPortal portal, Network network) throws NameConflictException {
network.addPortal(portal);
new StargateQueuedAsyncTask(() -> {
try {
storageAPI.savePortalToStorage(portal);
} catch (StorageWriteException e) {
Stargate.log(e);
new StargateQueuedAsyncTask() {
@Override
public void run() {
try {
storageAPI.savePortalToStorage(portal);
} catch (StorageWriteException e) {
Stargate.log(e);
}
}
}).run();
}.runNow();
network.getPluginMessageSender().sendCreatePortal(portal);
network.updatePortals();
}
Expand All @@ -256,13 +267,16 @@ public void destroyPortal(RealPortal portal) {
portal.destroy();
registry.unregisterPortal(portal);
network.updatePortals();
new StargateQueuedAsyncTask(() -> {
try {
storageAPI.removePortalFromStorage(portal);
} catch (StorageWriteException e) {
Stargate.log(e);
new StargateQueuedAsyncTask() {
@Override
public void run() {
try {
storageAPI.removePortalFromStorage(portal);
} catch (StorageWriteException e) {
Stargate.log(e);
}
}
}).run();
}.runNow();
portal.getNetwork().getPluginMessageSender().sendDeletePortal(portal);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,13 +311,16 @@ public Map<BlockLocation, PortalPosition> getPortalPositionsOwnedByPlugin(Plugin
public PortalPosition savePortalPosition(RealPortal portal, Location location, PositionType type, Plugin plugin) {
BlockVector relativeVector = portal.getGate().getRelativeVector(location).toBlockVector();
PortalPosition portalPosition = new PortalPosition(type, relativeVector, plugin.getName());
new StargateQueuedAsyncTask(() -> {
try {
storageAPI.addPortalPosition(portal, portal.getStorageType(), portalPosition);
} catch (StorageWriteException e) {
Stargate.log(e);
new StargateQueuedAsyncTask(){
@Override
public void run() {
try {
storageAPI.addPortalPosition(portal, portal.getStorageType(), portalPosition);
} catch (StorageWriteException e) {
Stargate.log(e);
}
}
}).run();
}.runNow();
return portalPosition;
}

Expand All @@ -332,13 +335,16 @@ public void removePortalPosition(Location location) {
portalPositionPluginNameMap.get(portalPosition.getPluginName()).remove(blockLocation);
RealPortal portal = portalPosition.getPortal();
portal.getGate().removePortalPosition(portalPosition);
new StargateQueuedAsyncTask(() -> {
try {
storageAPI.removePortalPosition(portal, portal.getStorageType(), portalPosition);
} catch (StorageWriteException e) {
Stargate.log(e);
new StargateQueuedAsyncTask() {
@Override
public void run() {
try {
storageAPI.removePortalPosition(portal, portal.getStorageType(), portalPosition);
} catch (StorageWriteException e) {
Stargate.log(e);
}
}
}).run();
}.runNow();
}

@Override
Expand Down
Loading

0 comments on commit 69426c7

Please sign in to comment.