1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-19 19:25:48 +00:00

Improved performance for GEO Miner and Oil Pump

This commit is contained in:
TheBusyBiscuit 2020-01-12 11:52:20 +01:00
parent 7c993ead57
commit 51dfbaed8a
5 changed files with 75 additions and 12 deletions

View File

@ -32,6 +32,7 @@
### Changes
* Removed Solar Array
* A lot of internal cleanup
* Performance improvements for GEO Miner and Oil Pump
### Fixes
* Fixed #1355

View File

@ -5,6 +5,7 @@ import java.util.concurrent.ThreadLocalRandom;
import java.util.logging.Level;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.block.Biome;
import io.github.thebusybiscuit.cscorelib2.config.Config;
@ -57,6 +58,12 @@ public final class OreGenSystem {
}
}
public static void setSupplies(OreGenResource resource, Location l, int amount) {
if (resource != null) {
BlockStorage.setChunkInfo(l, "resources_" + resource.getName().toUpperCase(), String.valueOf(amount));
}
}
public static int generateSupplies(OreGenResource resource, Chunk chunk) {
if (resource == null) return 0;
@ -64,6 +71,14 @@ public final class OreGenSystem {
BlockStorage.setChunkInfo(chunk, "resources_" + resource.getName().toUpperCase(), String.valueOf(supplies));
return supplies;
}
public static int generateSupplies(OreGenResource resource, Location l) {
if (resource == null) return 0;
int supplies = getDefault(resource, l.getBlock().getBiome());
BlockStorage.setChunkInfo(l, "resources_" + resource.getName().toUpperCase(), String.valueOf(supplies));
return supplies;
}
public static int getSupplies(OreGenResource resource, Chunk chunk, boolean generate) {
if (resource == null) return 0;
@ -80,10 +95,31 @@ public final class OreGenSystem {
return generateSupplies(resource, chunk);
}
}
public static int getSupplies(OreGenResource resource, Location l, boolean generate) {
if (resource == null) return 0;
String supply = BlockStorage.getChunkInfo(l, "resources_" + resource.getName().toUpperCase());
if (supply != null) {
return Integer.parseInt(supply);
}
else if (!generate) {
return 0;
}
else {
return generateSupplies(resource, l);
}
}
public static boolean wasResourceGenerated(OreGenResource resource, Chunk chunk) {
if (resource == null) return false;
return BlockStorage.hasChunkInfo(chunk, "resources_" + resource.getName().toUpperCase());
}
public static boolean wasResourceGenerated(OreGenResource resource, Location l) {
if (resource == null) return false;
return BlockStorage.hasChunkInfo(l, "resources_" + resource.getName().toUpperCase());
}
}

View File

@ -3,7 +3,6 @@ package me.mrCookieSlime.Slimefun.Objects.SlimefunItem.machines.electric.geo;
import java.util.LinkedList;
import java.util.List;
import org.bukkit.Chunk;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
@ -180,16 +179,14 @@ public abstract class GEOMiner extends AContainer implements InventoryBlock, Rec
SimpleHologram.update(b, "&4GEO-Scan required!");
}
else {
Chunk chunk = b.getChunk();
for (OreGenResource resource : OreGenSystem.listResources()) {
if (!resource.isLiquid()) {
if (!OreGenSystem.wasResourceGenerated(resource, chunk)) {
if (!OreGenSystem.wasResourceGenerated(resource, b.getLocation())) {
SimpleHologram.update(b, "&4GEO-Scan required!");
return;
}
else {
int supplies = OreGenSystem.getSupplies(resource, chunk, false);
int supplies = OreGenSystem.getSupplies(resource, b.getLocation(), false);
if (supplies > 0) {
MachineRecipe r = new MachineRecipe(getProcessingTime() / getSpeed(), new ItemStack[0], new ItemStack[] {resource.getItem().clone()});
@ -197,7 +194,7 @@ public abstract class GEOMiner extends AContainer implements InventoryBlock, Rec
processing.put(b, r);
progress.put(b, r.getTicks());
OreGenSystem.setSupplies(resource, chunk, supplies - 1);
OreGenSystem.setSupplies(resource, b.getLocation(), supplies - 1);
SimpleHologram.update(b, "&7Mining: &r" + resource.getName());
return;
}

View File

@ -3,7 +3,6 @@ package me.mrCookieSlime.Slimefun.Objects.SlimefunItem.machines.electric.geo;
import java.util.Arrays;
import java.util.List;
import org.bukkit.Chunk;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
@ -47,7 +46,7 @@ public abstract class OilPump extends AContainer implements RecipeDisplayItem {
return false;
}
if (!OreGenSystem.wasResourceGenerated(OreGenSystem.getResource("Oil"), b.getChunk())) {
if (!OreGenSystem.wasResourceGenerated(OreGenSystem.getResource("Oil"), b.getLocation())) {
SlimefunPlugin.getLocal().sendMessage(p, "gps.geo.scan-required", true);
return false;
}
@ -109,8 +108,7 @@ public abstract class OilPump extends AContainer implements RecipeDisplayItem {
for (int slot : getInputSlots()) {
if (SlimefunManager.isItemSimilar(inv.getItemInSlot(slot), new ItemStack(Material.BUCKET), true)) {
OreGenResource oil = OreGenSystem.getResource("Oil");
Chunk chunk = b.getChunk();
int supplies = OreGenSystem.getSupplies(oil, chunk, false);
int supplies = OreGenSystem.getSupplies(oil, b.getLocation(), false);
if (supplies > 0) {
MachineRecipe r = new MachineRecipe(26, new ItemStack[0], new ItemStack[] {SlimefunItems.BUCKET_OF_OIL});
@ -118,7 +116,7 @@ public abstract class OilPump extends AContainer implements RecipeDisplayItem {
inv.consumeItem(slot);
processing.put(b, r);
progress.put(b, r.getTicks());
OreGenSystem.setSupplies(oil, chunk, supplies - 1);
OreGenSystem.setSupplies(oil, b.getLocation(), supplies - 1);
}
else {
ItemStack item = inv.getItemInSlot(slot).clone();

View File

@ -80,7 +80,7 @@ public class BlockStorage {
return null;
}
public BlockStorage(final World w) {
public BlockStorage(World w) {
if (SlimefunPlugin.getUtilities().worlds.containsKey(w.getName())) return;
this.world = w;
@ -717,6 +717,16 @@ public class BlockStorage {
return new BlockInfoConfig();
}
}
public static Config getChunkInfo(Location l) {
try {
BlockInfoConfig cfg = SlimefunPlugin.getUtilities().mapChunks.get(locationToChunkString(l));
return cfg == null ? new BlockInfoConfig() : cfg;
} catch (Exception x) {
Slimefun.getLogger().log(Level.SEVERE, "Failed to parse ChunkInfo for Location: " + (l == null ? "?": l.getBlockX()) + ", " + (l == null ? "?": l.getBlockZ()) + " for Slimefun " + Slimefun.getVersion(), x);
return new BlockInfoConfig();
}
}
public static boolean hasChunkInfo(Chunk chunk) {
return SlimefunPlugin.getUtilities().mapChunks.containsKey(serializeChunk(chunk));
@ -734,15 +744,36 @@ public class BlockStorage {
chunkChanges++;
}
public static void setChunkInfo(Location l, String key, String value) {
BlockInfoConfig cfg = SlimefunPlugin.getUtilities().mapChunks.get(locationToChunkString(l));
if (cfg == null) {
cfg = new BlockInfoConfig();
SlimefunPlugin.getUtilities().mapChunks.put(locationToChunkString(l), cfg);
}
cfg.setValue(key, value);
chunkChanges++;
}
public static String getChunkInfo(Chunk chunk, String key) {
return getChunkInfo(chunk).getString(key);
}
public static String getChunkInfo(Location l, String key) {
return getChunkInfo(l).getString(key);
}
public static boolean hasChunkInfo(Chunk chunk, String key) {
return getChunkInfo(chunk, key) != null;
}
public static boolean hasChunkInfo(Location l, String key) {
return getChunkInfo(l, key) != null;
}
public static void clearChunkInfo(Chunk chunk) {
SlimefunPlugin.getUtilities().mapChunks.remove(serializeChunk(chunk));
}