Skip to content

Commit

Permalink
Ignore trash blocks when auto-extending (GriefPrevention#2255)
Browse files Browse the repository at this point in the history
This should ideally help prevent some issues with the main reason for having claims extend down instead of snapping to the bottom immediately, namely player mineshafts.

Remove biomes from unsupported versions
Update to current tags/mats
  • Loading branch information
Jikoo authored Apr 26, 2024
1 parent 4c71b71 commit f6388bd
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private int findLowerBuiltY(ChunkSnapshot chunkSnapshot, int y)
if (yTooSmall(y)) return this.minY;

// Because we found a player block, repeatedly check the next block in the column.
while (isPlayerBlock(chunkSnapshot, x, newY--, z))
while (isPlayerBlock(chunkSnapshot, x, --newY, z))
{
// If we've hit minimum Y we're done searching.
if (yTooSmall(y)) return this.minY;
Expand Down Expand Up @@ -174,7 +174,12 @@ private boolean isPlayerBlock(ChunkSnapshot chunkSnapshot, int x, int y, int z)

private Set<Material> getBiomePlayerBlocks(Biome biome)
{
return biomePlayerMaterials.computeIfAbsent(biome, newBiome -> RestoreNatureProcessingTask.getPlayerBlocks(this.worldType, newBiome));
return biomePlayerMaterials.computeIfAbsent(biome, newBiome ->
{
Set<Material> playerBlocks = RestoreNatureProcessingTask.getPlayerBlocks(this.worldType, newBiome);
playerBlocks.removeAll(BlockEventHandler.TRASH_BLOCKS);
return playerBlocks;
});
}

//runs in the main execution thread, where it can safely change claims and save those changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,33 +88,34 @@
//event handlers related to blocks
public class BlockEventHandler implements Listener
{

protected static final Set<Material> TRASH_BLOCKS;

static
{
//create the list of blocks which will not trigger a warning when they're placed outside of land claims
TRASH_BLOCKS = new HashSet<>();
TRASH_BLOCKS.add(Material.COBBLESTONE);
TRASH_BLOCKS.add(Material.TORCH);
TRASH_BLOCKS.add(Material.DIRT);
TRASH_BLOCKS.addAll(Tag.SAPLINGS.getValues());
TRASH_BLOCKS.add(Material.GRAVEL);
TRASH_BLOCKS.add(Material.SAND);
TRASH_BLOCKS.add(Material.SANDSTONE);
TRASH_BLOCKS.add(Material.TNT);
TRASH_BLOCKS.add(Material.CRAFTING_TABLE);
TRASH_BLOCKS.add(Material.TUFF);
TRASH_BLOCKS.add(Material.COBBLED_DEEPSLATE);
}

//convenience reference to singleton datastore
private final DataStore dataStore;

private final Set<Material> trashBlocks;

//constructor
public BlockEventHandler(DataStore dataStore)
{
this.dataStore = dataStore;

//create the list of blocks which will not trigger a warning when they're placed outside of land claims
this.trashBlocks = new HashSet<>();
this.trashBlocks.add(Material.COBBLESTONE);
this.trashBlocks.add(Material.TORCH);
this.trashBlocks.add(Material.DIRT);
this.trashBlocks.add(Material.OAK_SAPLING);
this.trashBlocks.add(Material.SPRUCE_SAPLING);
this.trashBlocks.add(Material.BIRCH_SAPLING);
this.trashBlocks.add(Material.JUNGLE_SAPLING);
this.trashBlocks.add(Material.ACACIA_SAPLING);
this.trashBlocks.add(Material.DARK_OAK_SAPLING);
this.trashBlocks.add(Material.GRAVEL);
this.trashBlocks.add(Material.SAND);
this.trashBlocks.add(Material.TNT);
this.trashBlocks.add(Material.CRAFTING_TABLE);
this.trashBlocks.add(Material.TUFF);
this.trashBlocks.add(Material.COBBLED_DEEPSLATE);
}

//when a player breaks a block...
Expand Down Expand Up @@ -432,7 +433,7 @@ else if (Tag.SAPLINGS.isTagged(block.getType()) && GriefPrevention.instance.conf
}

//FEATURE: warn players when they're placing non-trash blocks outside of their claimed areas
else if (!this.trashBlocks.contains(block.getType()) && GriefPrevention.instance.claimsEnabledForWorld(block.getWorld()))
else if (!this.TRASH_BLOCKS.contains(block.getType()) && GriefPrevention.instance.claimsEnabledForWorld(block.getWorld()))
{
if (!playerData.warnedAboutBuildingOutsideClaims && !player.hasPermission("griefprevention.adminclaims")
&& player.hasPermission("griefprevention.createclaims") && ((playerData.lastClaim == null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,14 @@ class RestoreNatureProcessingTask implements Runnable
// Definitions of biomes with particularly dense log distribution. These biomes will not have logs reduced.
private static final Set<NamespacedKey> DENSE_LOG_BIOMES = Set.of(
NamespacedKey.minecraft("jungle"),
NamespacedKey.minecraft("bamboo_jungle"),
// Variants for versions < 1.18
NamespacedKey.minecraft("modified_jungle"),
NamespacedKey.minecraft("jungle_hills"),
NamespacedKey.minecraft("bamboo_jungle_hills")
NamespacedKey.minecraft("bamboo_jungle")
);

// Definitions of biomes where sand covers surfaces instead of grass.
private static final Set<NamespacedKey> SAND_SOIL_BIOMES = Set.of(
NamespacedKey.minecraft("snowy_beach"),
NamespacedKey.minecraft("beach"),
NamespacedKey.minecraft("desert"),
// Variants for versions < 1.18
NamespacedKey.minecraft("desert_hills"),
NamespacedKey.minecraft("desert_lakes")
NamespacedKey.minecraft("desert")
);

//world information captured from the main thread
Expand Down Expand Up @@ -118,31 +111,22 @@ public RestoreNatureProcessingTask(BlockSnapshot[][][] snapshots, int miny, Envi
//like a single-block tower of iron ore or a giant penis constructed with melons
if (this.aggressiveMode || this.creativeMode)
{
this.playerBlocks.add(Material.IRON_ORE);
this.playerBlocks.add(Material.GOLD_ORE);
this.playerBlocks.add(Material.DIAMOND_ORE);
this.playerBlocks.addAll(Tag.COPPER_ORES.getValues());
this.playerBlocks.addAll(Tag.IRON_ORES.getValues());
this.playerBlocks.addAll(Tag.GOLD_ORES.getValues());
this.playerBlocks.addAll(Tag.DIAMOND_ORES.getValues());
this.playerBlocks.addAll(Tag.COAL_ORES.getValues());
this.playerBlocks.add(Material.MELON);
this.playerBlocks.add(Material.MELON_STEM);
this.playerBlocks.add(Material.BEDROCK);
this.playerBlocks.add(Material.COAL_ORE);
this.playerBlocks.add(Material.PUMPKIN);
this.playerBlocks.add(Material.PUMPKIN_STEM);
}

if (this.aggressiveMode)
{
this.playerBlocks.add(Material.OAK_LEAVES);
this.playerBlocks.add(Material.SPRUCE_LEAVES);
this.playerBlocks.add(Material.BIRCH_LEAVES);
this.playerBlocks.add(Material.JUNGLE_LEAVES);
this.playerBlocks.add(Material.ACACIA_LEAVES);
this.playerBlocks.add(Material.DARK_OAK_LEAVES);
this.playerBlocks.add(Material.OAK_LOG);
this.playerBlocks.add(Material.SPRUCE_LOG);
this.playerBlocks.add(Material.BIRCH_LOG);
this.playerBlocks.add(Material.JUNGLE_LOG);
this.playerBlocks.add(Material.ACACIA_LOG);
this.playerBlocks.add(Material.DARK_OAK_LOG);
this.playerBlocks.addAll(Tag.LEAVES.getValues());
this.playerBlocks.addAll(Tag.LOGS.getValues());
this.playerBlocks.add(Material.VINE);
}
}
Expand Down Expand Up @@ -680,7 +664,7 @@ static Set<Material> getPlayerBlocks(Environment environment, Biome biome)
playerBlocks.addAll(Tag.CAMPFIRES.getValues());
playerBlocks.addAll(Tag.CANDLE_CAKES.getValues());
playerBlocks.addAll(Tag.CANDLES.getValues());
playerBlocks.addAll(Tag.CARPETS.getValues());
playerBlocks.addAll(Tag.WOOL_CARPETS.getValues());
playerBlocks.addAll(Tag.CAULDRONS.getValues());
playerBlocks.addAll(Tag.DOORS.getValues());
playerBlocks.addAll(Tag.FENCE_GATES.getValues());
Expand Down Expand Up @@ -797,6 +781,7 @@ static Set<Material> getPlayerBlocks(Environment environment, Biome biome)
playerBlocks.add(Material.RAW_IRON_BLOCK);
playerBlocks.add(Material.RAW_GOLD_BLOCK);
playerBlocks.add(Material.LIGHTNING_ROD);
playerBlocks.add(Material.DECORATED_POT);

//these are unnatural in the nether and end
if (environment != Environment.NORMAL && environment != Environment.CUSTOM)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
package me.ryanhamshire.GriefPrevention;

import com.griefprevention.test.ServerMocks;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.Tag;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryPickupItemEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.plugin.Plugin;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Set;
import java.util.UUID;

import static org.mockito.ArgumentMatchers.notNull;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.never;
Expand All @@ -24,6 +32,29 @@ public class BlockEventHandlerTest
{
private static final UUID PLAYER_UUID = UUID.fromString("fa8d60a7-9645-4a9f-b74d-173966174739");

@BeforeAll
static void beforeAll()
{
Server server = ServerMocks.newServer();
doAnswer(invocation ->
{
Tag<?> tag = mock();
doReturn(Set.of()).when(tag).getValues();
return tag;
}).when(server).getTag(notNull(), notNull(), notNull());
Bukkit.setServer(server);

// Touch class to load material list.
//noinspection ResultOfMethodCallIgnored
BlockEventHandler.class.getName();
}

@AfterAll
static void afterAll()
{
ServerMocks.unsetBukkitServer();
}

@Test
void verifyNormalHopperPassthrough()
{
Expand Down

0 comments on commit f6388bd

Please sign in to comment.