Skip to content

Commit

Permalink
Merge pull request #10 from SlimefunGuguProject/chore/performance
Browse files Browse the repository at this point in the history
  • Loading branch information
ybw0014 authored Jul 19, 2022
2 parents c394c41 + 39f6fae commit ead2ba1
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 18 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
<dependency>
<groupId>com.github.Slimefun</groupId>
<artifactId>Slimefun4</artifactId>
<version>2c4f886</version>
<version>d191599</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
20 changes: 12 additions & 8 deletions src/main/java/dev/sefiraat/netheopoiesis/Purification.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import org.bukkit.block.Block;

import javax.annotation.Nonnull;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class Purification {

Expand Down Expand Up @@ -53,23 +53,27 @@ public class Purification {

private static Purification instance;

private final Map<BlockPosition, Integer> purificationModifiers = new HashMap<>();
private final Map<ChunkPosition, Integer> chunkValues = new HashMap<>();
private final Map<BlockPosition, Integer> purificationModifiers = new ConcurrentHashMap<>();
private final Map<ChunkPosition, Integer> chunkValues = new ConcurrentHashMap<>();

public Purification() {
Preconditions.checkArgument(instance == null, "Cannot create a new instance of Purification");
instance = this;
Bukkit.getScheduler().runTaskTimer(Netheopoiesis.getInstance(), this::collateChunkValues, 1, 100);
Bukkit.getScheduler().runTaskTimerAsynchronously(Netheopoiesis.getInstance(), this::collateChunkValues, 1, 100);
}

private void collateChunkValues() {
chunkValues.clear();
for (Map.Entry<BlockPosition, Integer> entry : purificationModifiers.entrySet()) {
final BlockPosition blockPosition = entry.getKey();
final ChunkPosition chunkPosition = new ChunkPosition(blockPosition.getChunk());
final int currentValue = chunkValues.getOrDefault(chunkPosition, 0);
final BlockPosition blockPos = entry.getKey();
final ChunkPosition chunkPos = new ChunkPosition(
blockPos.getWorld(),
blockPos.getChunkX(),
blockPos.getChunkZ()
);
final int currentValue = chunkValues.getOrDefault(chunkPos, 0);
final int newValue = currentValue + entry.getValue();
chunkValues.put(chunkPosition, newValue);
chunkValues.put(chunkPos, newValue);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import io.github.bakedlibs.dough.collections.RandomizedSet;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack;
import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config;
import me.mrCookieSlime.Slimefun.api.BlockStorage;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;

import javax.annotation.Nonnull;
Expand All @@ -19,6 +21,9 @@
*/
public class DroppingSeed extends NetherSeed {

private static final String WITHER_KEY = "wither_chance";
private static final int WITHER_CHANCE = 5;

private final RandomizedSet<ItemStack> stacksToDrop = new RandomizedSet<>();
private double chance = 0.1;

Expand All @@ -32,6 +37,19 @@ public void onTickFullyGrown(Location location, NetherSeed seed, Config data) {
double randomChance = ThreadLocalRandom.current().nextDouble();
if (randomChance <= chance) {
location.getWorld().dropItem(location, this.stacksToDrop.getRandom());
updateGrowthStage(location, 1);
final String currentChance = BlockStorage.getLocationInfo(location, WITHER_KEY);
if (currentChance == null) {
BlockStorage.addBlockInfo(location, WITHER_KEY, String.valueOf(WITHER_CHANCE));
} else {
final int currentChanceInt = Integer.parseInt(currentChance);
if (ThreadLocalRandom.current().nextInt(101) < currentChanceInt) {
location.getBlock().setType(Material.AIR);
BlockStorage.clearBlockInfo(location);
} else {
BlockStorage.addBlockInfo(location, WITHER_KEY, String.valueOf(currentChanceInt + WITHER_CHANCE));
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ private Groups() {
static {
final Netheopoiesis plugin = Netheopoiesis.getInstance();

SEEDS.setCrossAddonItemGroup(true);

// Slimefun Registry
MAIN.register(plugin);
CRAFTING.register(plugin);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -602,15 +602,15 @@ null, new ItemStack(Material.BEACON), null

new DroppingSeed(Stacks.GRAINY_SEED)
.addDrop(new ItemStack(Material.REDSTONE), 1)
.setTriggerChance(0.05)
.setTriggerChance(0.01)
.setGrowth(new Growth(GrowthStages.VINEY_RED, Placements.ALL, 1, 0.09))
.addBreedingPair(Stacks.PURIFICATION_SEED.getItemId(), Stacks.PURIFICATION_SEED.getItemId(), 0.1, 0.2)
.addFlavourProfile(0, 0, 0, 1, 1)
.tryRegister(addon);

new DroppingSeed(Stacks.STRINGY_SEED)
.addDrop(new ItemStack(Material.STRING), 1)
.setTriggerChance(0.05)
.setTriggerChance(0.01)
.setGrowth(new Growth(GrowthStages.VINEY_GREEN, Placements.ALL, 1, 0.09))
.addBreedingPair(Stacks.PURIFICATION_SEED.getItemId(), Stacks.PURIFICATION_SEED.getItemId(), 0.1, 0.2)
.addFlavourProfile(0, 1, 1, 0, 0)
Expand All @@ -621,15 +621,15 @@ null, new ItemStack(Material.BEACON), null
.addDrop(new ItemStack(Material.TALL_GRASS), 2)
.addDrop(new ItemStack(Material.SEAGRASS), 1)
.addDrop(new ItemStack(Material.TALL_SEAGRASS), 1)
.setTriggerChance(0.05)
.setTriggerChance(0.01)
.setGrowth(new Growth(GrowthStages.VINEY_GREEN, Placements.ALL, 1, 0.09))
.addBreedingPair(Stacks.SPINDLE_SEED.getItemId(), Stacks.STRINGY_SEED.getItemId(), 0.1, 0.2)
.addFlavourProfile(2, 0, 0, 0, 0)
.tryRegister(addon);

new DroppingSeed(Stacks.COBBLED_SEED)
.addDrop(new ItemStack(Material.COBBLESTONE), 1)
.setTriggerChance(0.05)
.setTriggerChance(0.01)
.setGrowth(new Growth(GrowthStages.VINEY_PURPLE, Placements.ALL, 1, 0.09))
.addBreedingPair(Stacks.SPINDLE_SEED.getItemId(), Stacks.GRAINY_SEED.getItemId(), 0.1, 0.2)
.addFlavourProfile(0, 0, 1, 1, 0)
Expand Down Expand Up @@ -744,7 +744,7 @@ null, new ItemStack(Material.BEACON), null
.addDrop(new ItemStack(Material.MELON_SEEDS), 1)
.addDrop(new ItemStack(Material.BEETROOT_SEEDS), 1)
.addDrop(new ItemStack(Material.PUMPKIN_SEEDS), 1)
.setTriggerChance(0.05)
.setTriggerChance(0.01)
.setGrowth(new Growth(GrowthStages.FUNGAL_YELLOW, Placements.PURIFIED_AND_UP, 2, 0.08))
.addBreedingPair(Stacks.SMOOTH_SEED.getItemId(), Stacks.GRASS_SEED.getItemId(), 0.05, 0.2)
.addFlavourProfile(2, 1, 0, 1, 0)
Expand Down Expand Up @@ -816,7 +816,7 @@ null, new ItemStack(Material.BEACON), null
.addDrop(new ItemStack(Material.GREEN_DYE), 1)
.addDrop(new ItemStack(Material.RED_DYE), 1)
.addDrop(new ItemStack(Material.BLACK_DYE), 1)
.setTriggerChance(0.1)
.setTriggerChance(0.01)
.setGrowth(new Growth(GrowthStages.VINEY_CYAN, Placements.VORACIOUS_AND_UP, 8, 0.06))
.addBreedingPair(Stacks.SPIRIT_SEED.getItemId(), Stacks.SPIRIT_SEED.getItemId(), 0.05, 0.2)
.addFlavourProfile(4, 2, 2, 0, 0)
Expand All @@ -826,7 +826,7 @@ null, new ItemStack(Material.BEACON), null
.addDrop(new ItemStack(Material.GLOW_LICHEN), 1)
.addDrop(new ItemStack(Material.GLOW_BERRIES), 1)
.addDrop(new ItemStack(Material.GLOW_INK_SAC), 1)
.setTriggerChance(0.08)
.setTriggerChance(0.01)
.setGrowth(new Growth(GrowthStages.FUNGAL_RED, Placements.VORACIOUS_AND_UP, 8, 0.06))
.addBreedingPair(Stacks.SPIRIT_SEED.getItemId(), Stacks.SPIRIT_SEED.getItemId(), 0.05, 0.2)
.addFlavourProfile(0, 0, 4, 0, 4)
Expand Down Expand Up @@ -863,7 +863,7 @@ null, new ItemStack(Material.BEACON), null
new DroppingSeed(Stacks.PRISMATIC_SEED)
.addDrop(new ItemStack(Material.PRISMARINE_SHARD), 2)
.addDrop(new ItemStack(Material.PRISMARINE_CRYSTALS), 1)
.setTriggerChance(0.05)
.setTriggerChance(0.005)
.setGrowth(new Growth(GrowthStages.SPIKEY_GREEN, Placements.VORACIOUS_AND_UP, 9, 0.06))
.addBreedingPair(Stacks.RAINBOW_SEED.getItemId(), Stacks.SPLINTERED_SEED.getItemId(), 0.1, 0.15)
.addFlavourProfile(0, 0, 8, 0, 0)
Expand Down Expand Up @@ -1004,7 +1004,7 @@ null, new ItemStack(Material.BEACON), null
new DroppingSeed(Stacks.TREASURED_SEED)
.addDrop(new ItemStack(Material.HEART_OF_THE_SEA), 1)
.addDrop(new ItemStack(Material.NAUTILUS_SHELL), 9)
.setTriggerChance(0.01)
.setTriggerChance(0.001)
.setGrowth(new Growth(GrowthStages.SPIKEY_GREEN, Placements.BEACH_BIOME, 15, 0.06))
.addBreedingPair(Stacks.BEACH_SEED.getItemId(), Stacks.SHINY_SEED.getItemId(), 0.1, 0.5)
.addFlavourProfile(4, 4, 4, 4, 4)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ public void onTickFullyGrown(Location location, NetherSeed seed, Config data) {
final double randomZ = ThreadLocalRandom.current().nextInt(-3, 4);
// For loop to make sure the purification can creep up and down.

final Location spreadLocation = location.clone().add(randomX, randomY, randomZ);

if (!WorldUtils.chunkLoaded(spreadLocation)) {
return;
}

final Block block = location.clone().add(randomX, randomY, randomZ).getBlock();
if (materials.contains(block.getType())
&& ProtectionUtils.hasPermission(getOwner(location), block, Interaction.BREAK_BLOCK)
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/dev/sefiraat/netheopoiesis/utils/WorldUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,14 @@ public static Location randomLocation(@Nonnull Location centreLocation, int rang
public static Location centre(@Nonnull Location location) {
return location.clone().add(0.5, 0.5, 0.5);
}

/**
* Checks if the chunk is loaded using location (doesn't load the chunk)
* @param location The location to check
* @return True if the chunk is loaded.
*/
public static boolean chunkLoaded(@Nonnull Location location) {
return location.getWorld().isChunkLoaded(location.getBlockX() / 16, location.getBlockZ() / 16);
}

}

0 comments on commit ead2ba1

Please sign in to comment.