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

Switched to BlockFormEvent

This commit is contained in:
TheBusyBiscuit 2020-12-10 12:11:25 +01:00
parent b07a54d515
commit eac09eea9e
4 changed files with 55 additions and 64 deletions

View File

@ -29,6 +29,8 @@
#### Changes
* Performance optimizations for Cargo networks
* Removed an old version of bStats
* Performance Improvements and Optimizations for Cobblestone/Stone/Basalt generators and mining androids
* Androids operating on a Cobblestone/Stone/Basalt generator now work faster
## Release Candidate 18 (03 Dec 2020)
@ -123,8 +125,6 @@
* General performance improvements
* /sf cheat now shows seasonal categories all year through
* GPS devices now require chest-access in that area to be used
* Performance Improvements and Optimizations for Cobblestone/Stone/Basalt generators and mining androids
* Androids operating on a Cobblestone/Stone/Basalt generator now work faster
#### Fixes
* Fixed #2300

View File

@ -104,14 +104,17 @@ public class MinerAndroid extends ProgrammableAndroid {
menu.pushItem(drop, getOutputSlots());
}
InfiniteBlockGenerator generator = InfiniteBlockGenerator.findAt(block, firesEvent.getValue());
InfiniteBlockGenerator generator = InfiniteBlockGenerator.findAt(block);
if (generator != null) {
if (firesEvent.getValue()) {
generator.callEvent(block);
}
// "poof" a "new" block was generated
block.getWorld().playSound(block.getLocation(), Sound.BLOCK_FIRE_EXTINGUISH, 0.075F, 0.8F);
block.getWorld().spawnParticle(Particle.SMOKE_NORMAL, block.getX() + 0.5, block.getY() + 1.25, block.getZ() + 0.5, 8, 0.5, 0.5, 0.5, 0.015);
}
else {
} else {
block.setType(Material.AIR);
}
}

View File

@ -11,10 +11,12 @@ import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.event.block.BlockFromToEvent;
import org.bukkit.block.BlockState;
import org.bukkit.event.block.BlockFormEvent;
import io.github.thebusybiscuit.slimefun4.api.MinecraftVersion;
import io.github.thebusybiscuit.slimefun4.implementation.items.androids.MinerAndroid;
import io.papermc.lib.PaperLib;
/**
* This enum holds various ways of infinite block generators.
@ -62,62 +64,49 @@ public enum InfiniteBlockGenerator implements Predicate<Block> {
return material;
}
@Override
public boolean test(@Nonnull Block b) {
return testAndTrigger(b, false);
}
/**
* Similar to {@link #test(Block)} this tests whether this {@link InfiniteBlockGenerator}
* exists at the given {@link Block}.
* Optionally this will also trigger a {@link BlockFromToEvent}.
*
* @param b
* The {@link Block}
* @param firesEvent
* Whether or not to to trigger a {@link BlockFromToEvent}.
*
* @return Whether this {@link InfiniteBlockGenerator} exists at the given {@link Block}
*/
public boolean testAndTrigger(@Nonnull Block b, boolean firesEvent) {
@Override
public boolean test(@Nonnull Block b) {
Validate.notNull(b, "Block cannot be null!");
// This will eliminate non-matching base materials
// If we are on a version without Basalt, it will be null here and not match.
/*
* This will eliminate non-matching base materials If we
* are on a version without Basalt, it will be null here and not match.
*/
if (b.getType() == getGeneratedMaterial()) {
switch (this) {
case COBBLESTONE_GENERATOR:
return hasSurroundingMaterials(b, firesEvent, Material.WATER, Material.LAVA);
return hasSurroundingMaterials(b, Material.WATER, Material.LAVA);
case STONE_GENERATOR:
if (b.getRelative(BlockFace.UP).getType() == Material.LAVA) {
if (firesEvent) {
// We manually call the event here since it actually flows from the top
callEvent(b.getRelative(BlockFace.UP), b);
}
return hasSurroundingMaterials(b, false, Material.WATER);
}
else {
return hasSurroundingMaterials(b, Material.WATER);
} else {
return false;
}
case BASALT_GENERATOR:
if (b.getRelative(BlockFace.DOWN).getType() == Material.SOUL_SOIL) {
return hasSurroundingMaterials(b, firesEvent, Material.LAVA, Material.BLUE_ICE);
}
else {
return hasSurroundingMaterials(b, Material.LAVA, Material.BLUE_ICE);
} else {
return false;
}
default:
return false;
}
}
else {
} else {
return false;
}
}
@ParametersAreNonnullByDefault
private boolean hasSurroundingMaterials(Block b, boolean firesEvent, Material... materials) {
private boolean hasSurroundingMaterials(Block b, Material... materials) {
Validate.notNull(b, "The Block cannot be null!");
Validate.notEmpty(materials, "Materials need to have a size of at least one!");
@ -132,12 +121,6 @@ public enum InfiniteBlockGenerator implements Predicate<Block> {
if (neighbourType == materials[i] && !matches[i]) {
matches[i] = true;
count++;
// This is our "trigger" material for the Event
if (firesEvent && i == 0) {
callEvent(neighbour, b);
}
break;
}
}
@ -151,7 +134,7 @@ public enum InfiniteBlockGenerator implements Predicate<Block> {
}
/**
* This method calls a {@link BlockFromToEvent} for this {@link InfiniteBlockGenerator}.
* This method calls a {@link BlockFormEvent} for this {@link InfiniteBlockGenerator}.
* There are a few plugins who catch these events to inject custom {@link Material Materials} into
* Cobblestone Generators, so we wanna give them the oppurtunity to catch this as well.
*
@ -160,11 +143,12 @@ public enum InfiniteBlockGenerator implements Predicate<Block> {
* @param to
* The {@link Block} our liquid has flown to / solidified at.
*
* @return Our called {@link BlockFromToEvent}
* @return Our called {@link BlockFormEvent}
*/
@Nonnull
private BlockFromToEvent callEvent(@Nonnull Block from, @Nonnull Block to) {
BlockFromToEvent event = new BlockFromToEvent(from, to);
public BlockFormEvent callEvent(@Nonnull Block block) {
BlockState state = PaperLib.getBlockState(block, false).getState();
BlockFormEvent event = new BlockFormEvent(block, state);
Bukkit.getPluginManager().callEvent(event);
return event;
}
@ -174,17 +158,15 @@ public enum InfiniteBlockGenerator implements Predicate<Block> {
*
* @param b
* The {@link Block}
* @param firesEvent
* Whether ot not a {@link BlockFromToEvent} should be fired
*
* @return An {@link InfiniteBlockGenerator} or null if none was found.
*/
@Nullable
public static InfiniteBlockGenerator findAt(@Nonnull Block b, boolean firesEvent) {
public static InfiniteBlockGenerator findAt(@Nonnull Block b) {
Validate.notNull(b, "Cannot find a generator without a Location!");
for (InfiniteBlockGenerator generator : values) {
if (generator.testAndTrigger(b, firesEvent)) {
if (generator.test(b)) {
return generator;
}
}

View File

@ -7,7 +7,7 @@ import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.event.block.BlockFromToEvent;
import org.bukkit.event.block.BlockFormEvent;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
@ -47,11 +47,11 @@ class TestInfiniteBlockGenerators {
block.setType(Material.STONE);
Assertions.assertFalse(InfiniteBlockGenerator.COBBLESTONE_GENERATOR.test(block));
Assertions.assertNull(InfiniteBlockGenerator.findAt(block, false));
Assertions.assertNull(InfiniteBlockGenerator.findAt(block));
block.setType(Material.COBBLESTONE);
Assertions.assertFalse(InfiniteBlockGenerator.COBBLESTONE_GENERATOR.test(block));
Assertions.assertNull(InfiniteBlockGenerator.findAt(block, false));
Assertions.assertNull(InfiniteBlockGenerator.findAt(block));
}
@ParameterizedTest
@ -67,11 +67,13 @@ class TestInfiniteBlockGenerators {
block.getRelative(water).setType(Material.WATER);
block.getRelative(lava).setType(Material.LAVA);
Assertions.assertTrue(generator.testAndTrigger(block, true));
server.getPluginManager().assertEventFired(BlockFromToEvent.class);
Assertions.assertTrue(generator.test(block));
Assertions.assertNotNull(generator.callEvent(block));
server.getPluginManager().assertEventFired(BlockFormEvent.class);
server.getPluginManager().clearEvents();
Assertions.assertEquals(generator, InfiniteBlockGenerator.findAt(block, false));
Assertions.assertEquals(generator, InfiniteBlockGenerator.findAt(block));
}
@Test
@ -82,15 +84,15 @@ class TestInfiniteBlockGenerators {
block.setType(Material.COBBLESTONE);
Assertions.assertFalse(InfiniteBlockGenerator.BASALT_GENERATOR.test(block));
Assertions.assertNull(InfiniteBlockGenerator.findAt(block, false));
Assertions.assertNull(InfiniteBlockGenerator.findAt(block));
block.setType(Material.BASALT);
Assertions.assertFalse(InfiniteBlockGenerator.BASALT_GENERATOR.test(block));
Assertions.assertNull(InfiniteBlockGenerator.findAt(block, false));
Assertions.assertNull(InfiniteBlockGenerator.findAt(block));
block.getRelative(BlockFace.DOWN).setType(Material.SOUL_SOIL);
Assertions.assertFalse(InfiniteBlockGenerator.BASALT_GENERATOR.test(block));
Assertions.assertNull(InfiniteBlockGenerator.findAt(block, false));
Assertions.assertNull(InfiniteBlockGenerator.findAt(block));
}
@ParameterizedTest
@ -107,11 +109,13 @@ class TestInfiniteBlockGenerators {
block.getRelative(ice).setType(Material.BLUE_ICE);
block.getRelative(lava).setType(Material.LAVA);
Assertions.assertTrue(generator.testAndTrigger(block, true));
server.getPluginManager().assertEventFired(BlockFromToEvent.class);
Assertions.assertTrue(generator.test(block));
Assertions.assertNotNull(generator.callEvent(block));
server.getPluginManager().assertEventFired(BlockFormEvent.class);
server.getPluginManager().clearEvents();
Assertions.assertEquals(generator, InfiniteBlockGenerator.findAt(block, false));
Assertions.assertEquals(generator, InfiniteBlockGenerator.findAt(block));
}
@Test
@ -122,15 +126,15 @@ class TestInfiniteBlockGenerators {
block.setType(Material.COBBLESTONE);
Assertions.assertFalse(InfiniteBlockGenerator.STONE_GENERATOR.test(block));
Assertions.assertNull(InfiniteBlockGenerator.findAt(block, false));
Assertions.assertNull(InfiniteBlockGenerator.findAt(block));
block.setType(Material.STONE);
Assertions.assertFalse(InfiniteBlockGenerator.STONE_GENERATOR.test(block));
Assertions.assertNull(InfiniteBlockGenerator.findAt(block, false));
Assertions.assertNull(InfiniteBlockGenerator.findAt(block));
block.getRelative(BlockFace.UP).setType(Material.LAVA);
Assertions.assertFalse(InfiniteBlockGenerator.STONE_GENERATOR.test(block));
Assertions.assertNull(InfiniteBlockGenerator.findAt(block, false));
Assertions.assertNull(InfiniteBlockGenerator.findAt(block));
}
@ParameterizedTest
@ -146,11 +150,13 @@ class TestInfiniteBlockGenerators {
block.getRelative(BlockFace.UP).setType(Material.LAVA);
block.getRelative(water).setType(Material.WATER);
Assertions.assertTrue(generator.testAndTrigger(block, true));
server.getPluginManager().assertEventFired(BlockFromToEvent.class);
Assertions.assertTrue(generator.test(block));
Assertions.assertNotNull(generator.callEvent(block));
server.getPluginManager().assertEventFired(BlockFormEvent.class);
server.getPluginManager().clearEvents();
Assertions.assertEquals(generator, InfiniteBlockGenerator.findAt(block, false));
Assertions.assertEquals(generator, InfiniteBlockGenerator.findAt(block));
}
private static Stream<Arguments> provideFaces() {