forked from SteamWar/SteamWar
Extract obfuscation config into HiderCore
ObfuscationConfig resolves the hidden blocks, block states (including all waterlogged states when water is hidden) and block entity types plus the obfuscation block, either from a gamemode's techhider config or from plain materials. TechHiderWrapper now only contains the fight specific access rules.
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
* This file is a part of the SteamWar software.
|
||||
*
|
||||
* Copyright (C) 2026 SteamWar.de-Serverteam
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package de.steamwar.techhider;
|
||||
|
||||
import de.steamwar.sql.GameModeConfig;
|
||||
import lombok.Getter;
|
||||
import net.minecraft.core.Holder;
|
||||
import net.minecraft.core.registries.BuiltInRegistries;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.world.level.block.Block;
|
||||
import net.minecraft.world.level.block.entity.BlockEntityType;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraft.world.level.material.FluidState;
|
||||
import net.minecraft.world.level.material.Fluids;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.craftbukkit.util.CraftMagicNumbers;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* Which blocks, block states and block entities to hide, and the block to replace them with.
|
||||
*/
|
||||
@Getter
|
||||
public final class ObfuscationConfig {
|
||||
|
||||
public static ObfuscationConfig of(GameModeConfig.TechhiderConfig<Material> config) {
|
||||
return new ObfuscationConfig(config.ObfuscateWith, config.HiddenBlocks, config.HiddenBlockEntities);
|
||||
}
|
||||
|
||||
private final Block obfuscateWith;
|
||||
private final Set<Block> blocks;
|
||||
private final Set<BlockState> blockStates;
|
||||
private final Set<BlockEntityType<?>> blockEntityTypes;
|
||||
|
||||
/**
|
||||
* @param hiddenBlocks "water" also hides all waterlogged block states
|
||||
* @param hiddenBlockEntities block entity type ids, e.g. "minecraft:sign"
|
||||
*/
|
||||
public ObfuscationConfig(Material obfuscateWith, Set<Material> hiddenBlocks, Set<String> hiddenBlockEntities) {
|
||||
this.obfuscateWith = CraftMagicNumbers.getBlock(obfuscateWith);
|
||||
|
||||
this.blocks = hiddenBlocks.stream()
|
||||
.map(CraftMagicNumbers::getBlock)
|
||||
.collect(Collectors.toUnmodifiableSet());
|
||||
|
||||
this.blockStates = getBlockStatesToHideFromMaterials(hiddenBlocks);
|
||||
|
||||
this.blockEntityTypes = hiddenBlockEntities.stream()
|
||||
.map(id -> {
|
||||
ResourceLocation loc = ResourceLocation.parse(id);
|
||||
return BuiltInRegistries.BLOCK_ENTITY_TYPE.get(loc).orElse(null);
|
||||
})
|
||||
.filter(Objects::nonNull)
|
||||
.map(Holder.Reference::value)
|
||||
.collect(Collectors.toUnmodifiableSet());
|
||||
}
|
||||
|
||||
public boolean hidesBlock(Block block) {
|
||||
return blocks.contains(block);
|
||||
}
|
||||
|
||||
public boolean hidesBlockState(BlockState blockState) {
|
||||
return blockStates.contains(blockState);
|
||||
}
|
||||
|
||||
public boolean hidesBlockEntity(BlockEntityType<?> type) {
|
||||
return blockEntityTypes.contains(type);
|
||||
}
|
||||
|
||||
private static Stream<BlockState> getWaterloggedBlockStates() {
|
||||
FluidState waterFluidState = Fluids.WATER.getSource(false);
|
||||
|
||||
return BuiltInRegistries.BLOCK.stream()
|
||||
.map((block) -> block.getStateDefinition().getPossibleStates())
|
||||
.flatMap(Collection::stream)
|
||||
.filter((blockState -> blockState.getFluidState() == waterFluidState));
|
||||
}
|
||||
|
||||
private static Set<BlockState> getBlockStatesToHideFromMaterials(Set<Material> materials) {
|
||||
Stream<BlockState> allStatesFromMaterials = materials.stream()
|
||||
.map(CraftMagicNumbers::getBlock)
|
||||
.map((block) -> block.getStateDefinition().getPossibleStates())
|
||||
.flatMap(Collection::stream);
|
||||
|
||||
if(materials.contains(Material.WATER)) {
|
||||
return Stream.concat(allStatesFromMaterials, getWaterloggedBlockStates()).collect(Collectors.toUnmodifiableSet());
|
||||
}
|
||||
else {
|
||||
return allStatesFromMaterials.collect(Collectors.toUnmodifiableSet());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user