I am helping a friend on a plugin that is a basic chunk claiming and protection system. In the plugin it has a config that adds blocks/items to a white-list that other players can interact with if there not the claim owner in that chunk. right now you have to list each block one by one like
minecraft:chest
or
appliedenergistics2:drive
I am sort of new to sponge with a lil knowledge in forge.
can anyone help point me in the right direction to adding a option to allow a * wildcard so we can input items/blocks like that if needed?
appliedenergistics2:*
^^^ how i want to be able to input into the config for every item/block from that mod
Assuming that your current code is something like this
ConfigurationNode node;
Collection<BlockType> blocks = node.getChildrenList().stream().map(c -> {
String value = c.getString();
Optional<BlockType> opBlock = Sponge.getRegistery().getType(BlockType.class, value);
if(opBlock.isPresent()){
return opBlock.get();
}
//something went wrong
}).collect(Collectors.toList()));
then you would know that this gets a single value and parses that value, multiple parses from one value isnt allowed. So you need to go for a more … Bukkit styled.
ConfigurationNode node;
Collection<BlockType> blocks = new HashSet<>();
node.getChildrenList().map(ConfigurationNode::toString).forEach(value -> {
if(value.endsWith("*")){
//'*' WILDCARD DETECTED
String from = value.split(":")[0]; //gets the mod id from the block id
Sponge.getRegistery().getAllOf(BlockType.class).stream().filter(block -> block.getId().startsWith(from)).forEach(blockType -> blocks.add(blockType));
}else{
Optional<BlockType> opBlock = Sponge.getRegistery().getType(BlockType.class, value);
if(opBlock.isPresent()){
blocks.add(opBlock.get());
}else{
//BLOCK NOT FOUND
}
}
});
Im doing this from my phone so spelling and correct names may not be correct, but should get the idea over.
edit thanks to RedNesto:
ConfigurationNode node;
Collection<BlockType> blocks = new HashSet<>();
node.getChildrenList().map(ConfigurationNode::toString).forEach(value -> {
if(value.endsWith("*")){
//'*' WILDCARD DETECTED
String from = value.split(":")[0]; //gets the mod id from the block id
blocks.addAll(Sponge.getRegistery().getAllFor(from, BlockType.class));
}else{
Optional<BlockType> opBlock = Sponge.getRegistery().getType(BlockType.class, value);
if(opBlock.isPresent()){
blocks.add(opBlock.get());
}else{
//BLOCK NOT FOUND
}
}
});
I think you can event use GameRegistry#getAllFor with the mod id to not have to filter types at all.
1 Like