Skip to content

Commit

Permalink
Correctly load chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
mattysweeps committed Mar 14, 2019
1 parent 5a02f30 commit 0f5dbac
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 45 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pluginGroup=io.github.m0pt0pmatt.survivalgames
pluginId=survivalgames
pluginVersion=1.1.3
pluginVersion=1.1.4
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@
@Plugin(
id = "survival-games",
name = "Survival Games",
version = "1.1.3",
version = "1.1.4",
description = "Survival Games for Sponge."
)
public class SurvivalGamesPlugin {

private static final Logger LOGGER = LoggerFactory.getLogger(SurvivalGamesPlugin.class);
public static final Logger LOGGER = LoggerFactory.getLogger(SurvivalGamesPlugin.class);

public static SpongeExecutorService EXECUTOR;
public static PluginContainer PLUGIN_CONTAINER;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class SurvivalGameStateManager {

private static final List<Task> START_TASKS =
Arrays.asList(
LoadChunksTask.getInstance(),
ClearEntitiesTask.getInstance(),
SetCommandBlocksTask.getInstance(),
FillChestsTask.getInstance(),
CreateCageSnapshotsTask.getInstance(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.github.m0pt0pmatt.survivalgames.task;

import io.github.m0pt0pmatt.survivalgames.command.CommandKeys;
import io.github.m0pt0pmatt.survivalgames.game.SurvivalGame;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.util.TextMessageException;
import org.spongepowered.api.world.World;

import static io.github.m0pt0pmatt.survivalgames.Util.getOrThrow;

public class ClearEntitiesTask implements Task {

private static ClearEntitiesTask INSTANCE = new ClearEntitiesTask();

private ClearEntitiesTask() {

}

@Override
public void execute(SurvivalGame survivalGame) throws TextMessageException {
String worldName =
getOrThrow(survivalGame.getConfig().getWorldName(), CommandKeys.WORLD_NAME);
World world = getOrThrow(Sponge.getServer().getWorld(worldName), CommandKeys.WORLD_NAME);

world.getEntities().forEach(Entity::remove);
}

public static ClearEntitiesTask getInstance() {
return INSTANCE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,19 @@

import static io.github.m0pt0pmatt.survivalgames.Util.getOrThrow;

import io.github.m0pt0pmatt.survivalgames.SurvivalGamesPlugin;
import io.github.m0pt0pmatt.survivalgames.command.CommandKeys;
import io.github.m0pt0pmatt.survivalgames.game.SurvivalGame;

import java.util.Optional;
import java.util.Random;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.block.tileentity.carrier.Chest;
import org.spongepowered.api.item.inventory.ItemStackSnapshot;
import org.spongepowered.api.util.TextMessageException;
import org.spongepowered.api.world.Chunk;
import org.spongepowered.api.world.Locatable;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;

/** Fills chests with items. */
Expand All @@ -52,43 +58,30 @@ public void execute(SurvivalGame survivalGame) throws TextMessageException {
Integer chestRange =
getOrThrow(survivalGame.getConfig().getChestRange(), CommandKeys.CHEST_RANGE);

world.getTileEntities()
.forEach(
tileEntity -> {
if (tileEntity instanceof Chest) {
Chest chest = (Chest) tileEntity;
chest.getInventory().clear();
world.getTileEntities().forEach(
tileEntity -> {
if (tileEntity instanceof Chest) {
Chest chest = (Chest) tileEntity;
chest.getInventory().clear();

if (!survivalGame
.getConfig()
.getItemConfig()
.getItems()
.isEmpty()) {
if (!survivalGame.getConfig().getItemConfig().getItems().isEmpty()) {

double itemCount =
(chestMidpoint
+ ((RANDOM.nextDouble() * chestRange)
* (RANDOM.nextDouble() > 0.5
? 1
: -1)));
for (int i = 0; i < itemCount; i++) {
ItemStackSnapshot stackSnapshot =
survivalGame
double itemCount = (chestMidpoint + ((RANDOM.nextDouble() * chestRange) * (RANDOM.nextDouble() > 0.5 ? 1 : -1)));
for (int i = 0; i < itemCount; i++) {
ItemStackSnapshot stackSnapshot = survivalGame
.getConfig()
.getItemConfig()
.getItems()
.get(RANDOM.nextInt(survivalGame
.getConfig()
.getItemConfig()
.getItems()
.get(
RANDOM.nextInt(
survivalGame
.getConfig()
.getItemConfig()
.getItems()
.size()));
chest.getInventory().offer(stackSnapshot.createStack());
}
}
.size()));
chest.getInventory().offer(stackSnapshot.createStack());
}
});
}
}
});
}

public static Task getInstance() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.github.m0pt0pmatt.survivalgames.task;

import io.github.m0pt0pmatt.survivalgames.command.CommandKeys;
import io.github.m0pt0pmatt.survivalgames.game.SurvivalGame;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.util.TextMessageException;
import org.spongepowered.api.world.World;

import static io.github.m0pt0pmatt.survivalgames.Util.getOrThrow;

public class LoadChunksTask implements Task {

private static LoadChunksTask INSTANCE = new LoadChunksTask();

private LoadChunksTask() {

}

@Override
public void execute(SurvivalGame survivalGame) throws TextMessageException {

String worldName =
getOrThrow(survivalGame.getConfig().getWorldName(), CommandKeys.WORLD_NAME);
World world = getOrThrow(Sponge.getServer().getWorld(worldName), CommandKeys.WORLD_NAME);

// Force chunks to be loaded
int minX = world.getLocation(survivalGame.getConfig().getBlockArea().getLesserBoundary().get()).getChunkPosition().getX();
int maxX = world.getLocation(survivalGame.getConfig().getBlockArea().getGreaterBoundary().get()).getChunkPosition().getX();
int minZ = world.getLocation(survivalGame.getConfig().getBlockArea().getLesserBoundary().get()).getChunkPosition().getZ();
int maxZ = world.getLocation(survivalGame.getConfig().getBlockArea().getGreaterBoundary().get()).getChunkPosition().getZ();
for (; minX <= maxX; minX++) {
for (int z = minZ; z <= maxZ; z++) {
world.loadChunk(minX, 0, z, false);
}
}
}

public static LoadChunksTask getInstance() {
return INSTANCE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,6 @@ public void execute(SurvivalGame survivalGame) throws TextMessageException {

survivalGame.getCommandBlocks().clear();

// Force chunks to be loaded
int minX = world.getLocation(survivalGame.getConfig().getBlockArea().getLesserBoundary().get()).getChunkPosition().getX();
int maxX = world.getLocation(survivalGame.getConfig().getBlockArea().getGreaterBoundary().get()).getChunkPosition().getX();
int minZ = world.getLocation(survivalGame.getConfig().getBlockArea().getLesserBoundary().get()).getChunkPosition().getZ();
int maxZ = world.getLocation(survivalGame.getConfig().getBlockArea().getGreaterBoundary().get()).getChunkPosition().getZ();
for (; minX <= maxX; minX++) {
for (; minZ <= maxZ; minZ++) {
world.loadChunk(minX, 0, minZ, false);
}
}

world.getTileEntities()
.stream()
.filter(tileEntity -> tileEntity instanceof CommandBlock)
Expand Down

0 comments on commit 0f5dbac

Please sign in to comment.