Add config options for bookshelf blocks and bookshelf search radius

This commit is contained in:
Electroblob77
2020-04-10 22:42:31 +01:00
parent 40ed431ee2
commit b3fd39c42c
5 changed files with 88 additions and 4 deletions
@@ -278,6 +278,23 @@ public final class Settings {
public boolean replaceVanillaFireballs = true;
/** <b>[Synchronised]</b> Chance of 'misreading' an undiscovered spell and triggering a forfeit instead. */
public double forfeitChance = 0.2;
/**
* <b>[Synchronised]</b> The maximum number of blocks a bookshelf can be from an arcane workbench or lectern to be
* able to link to it.
*/
public int bookshelfSearchRadius = 4;
/**
* <b>[Synchronised]</b> List of registry names of blocks that count as bookshelves for the arcane workbench and
* lectern.
*/
public Pair<ResourceLocation, Short>[] bookshelfBlocks = parseItemMetaStrings(
Wizardry.MODID + ":oak_bookshelf",
Wizardry.MODID + ":spruce_bookshelf",
Wizardry.MODID + ":birch_bookshelf",
Wizardry.MODID + ":jungle_bookshelf",
Wizardry.MODID + ":acacia_bookshelf",
Wizardry.MODID + ":dark_oak_bookshelf"
);
// Client-only settings. These settings only affect client-side code and hence are not synced. Each client obeys
// its own values for these, and changing them on a dedicated server will have no effect.
@@ -708,6 +725,21 @@ public final class Settings {
bowItemWhitelist = parseItemMetaStrings(property.getStringList());
propOrder.add(property.getName());
property = config.get(TWEAKS_CATEGORY, "bookshelfBlocks", new String[0], "List of registry names of blocks that count as bookshelves for the arcane workbench and lectern. Block names are not case sensitive. For mod blocks, prefix with the mod ID (e.g. " + Wizardry.MODID + ":oak_bookshelf).");
property.setLanguageKey("config." + Wizardry.MODID + ".bookshelf_blocks");
property.setRequiresWorldRestart(true);
bookshelfBlocks = parseItemMetaStrings(property.getStringList());
propOrder.add(property.getName());
property = config.get(TWEAKS_CATEGORY, "bookshelfSearchRadius", 4,
"The maximum number of blocks a bookshelf can be from an arcane workbench or lectern to be able to link to it.",
1, 10);
property.setLanguageKey("config." + Wizardry.MODID + ".bookshelf_search_radius");
Wizardry.proxy.setToNumberSliderEntry(property);
property.setRequiresMcRestart(true);
bookshelfSearchRadius = property.getInt();
propOrder.add(property.getName());
property = config.get(TWEAKS_CATEGORY, "currencyItems", new String[]{"gold_ingot 3", "emerald 6"}, "List of registry names of items which wizard trades can use as currency (in the first slot; the second slot is unaffected). Each entry in this list should consist of an item registry name, followed by a single space, then an integer which defines the 'value' of the item. Higher values mean fewer of that currency item are required for a given trade.",
Pattern.compile("[A-Za-z0-9:_]+ [0-9]+"));
property.setLanguageKey("config." + Wizardry.MODID + ".currency_items");
@@ -1119,7 +1151,7 @@ public final class Settings {
return Arrays.stream(strings).map(s -> new ResourceLocation(s.toLowerCase(Locale.ROOT).trim())).toArray(ResourceLocation[]::new);
}
/** Applies {@link Settings#parseItemMetaString(String)} to each input string and returns and array of the resulting
/** Applies {@link Settings#parseItemMetaString(String)} to each input string and returns an array of the resulting
* {@link Pair}s. */
@SuppressWarnings("unchecked") // Shut up java
public static Pair<ResourceLocation, Short>[] parseItemMetaStrings(String... strings){
@@ -1147,14 +1179,34 @@ public final class Settings {
return Pair.of(new ResourceLocation(item), meta);
}
/**
* Checks a metadata-sensitive list option (see {@link Settings#parseItemMetaStrings(String...)} for the given block.
* @param array The config option to check
* @param block The block state to search for
* @return True if the given array contains an entry that matches the given block, false if not.
*/
public static boolean containsMetaBlock(Pair<ResourceLocation, Short>[] array, IBlockState block){
return containsMetaThing(array, block.getBlock().getRegistryName(), (short)block.getBlock().getMetaFromState(block));
}
/**
* Checks a metadata-sensitive list option (see {@link Settings#parseItemMetaStrings(String...)} for the given item.
* @param array The config option to check
* @param stack An item stack with the item and metadata to search for
* @return True if the given array contains an entry that matches the given stack, false if not.
*/
public static boolean containsMetaItem(Pair<ResourceLocation, Short>[] array, ItemStack stack){
return containsMetaThing(array, stack.getItem().getRegistryName(), (short)stack.getMetadata());
}
/**
* Checks a metadata-sensitive list option (see {@link Settings#parseItemMetaStrings(String...)} for the given
* id/metadata pair.
* @param array The config option to check
* @param id The id to search for
* @param metadata The metadata value to search for
* @return True if the given array contains the given id/metadata pair, or the given id paired with the wildcard value.
*/
public static boolean containsMetaThing(Pair<ResourceLocation, Short>[] array, ResourceLocation id, short metadata){
return Arrays.asList(array).contains(Pair.of(id, metadata)) || Arrays.asList(array).contains(Pair.of(id, OreDictionary.WILDCARD_VALUE));
}
@@ -1,5 +1,6 @@
package electroblob.wizardry.block;
import electroblob.wizardry.Settings;
import electroblob.wizardry.Wizardry;
import electroblob.wizardry.WizardryGuiHandler;
import electroblob.wizardry.registry.WizardryTabs;
@@ -150,15 +151,15 @@ public class BlockBookshelf extends BlockHorizontal implements ITileEntityProvid
List<IInventory> bookshelves = new ArrayList<>();
int searchRadius = 4; // TODO: Config option for this
int searchRadius = Wizardry.settings.bookshelfSearchRadius;
for(int x = -searchRadius; x <= searchRadius; x++){
for(int y = -searchRadius; y <= searchRadius; y++){
for(int z = -searchRadius; z <= searchRadius; z++){
BlockPos pos = centre.add(x, y, z);
// TODO: Config option for allowed containers
if(world.getBlockState(pos).getBlock() instanceof BlockBookshelf){
if(Settings.containsMetaBlock(Wizardry.settings.bookshelfBlocks, world.getBlockState(pos))){
TileEntity te = world.getTileEntity(pos);
if(te instanceof IInventory && !ArrayUtils.contains(exclude, te)) bookshelves.add((IInventory)te);
}
@@ -4,9 +4,15 @@ import electroblob.wizardry.Settings;
import electroblob.wizardry.Wizardry;
import electroblob.wizardry.packet.PacketSyncSettings.Message;
import io.netty.buffer.ByteBuf;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.common.network.ByteBufUtils;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
import org.apache.commons.lang3.tuple.Pair;
import java.util.ArrayList;
import java.util.List;
/**
* <b>[Server -> Client]</b> This packet is sent to synchronise the config settings with clients on player login.
@@ -31,7 +37,10 @@ public class PacketSyncSettings implements IMessageHandler<Message, IMessage> {
Wizardry.settings.discoveryMode = message.settings.discoveryMode;
Wizardry.settings.creativeBypassesArcaneLock = message.settings.creativeBypassesArcaneLock;
Wizardry.settings.slowTimeAffectsPlayers = message.settings.slowTimeAffectsPlayers;
Wizardry.settings.replaceVanillaFireballs = message.settings.replaceVanillaFireballs;
Wizardry.settings.forfeitChance = message.settings.forfeitChance;
Wizardry.settings.bookshelfSearchRadius = message.settings.bookshelfSearchRadius;
Wizardry.settings.bookshelfBlocks = message.settings.bookshelfBlocks;
}
public static class Message implements IMessage {
@@ -48,6 +57,7 @@ public class PacketSyncSettings implements IMessageHandler<Message, IMessage> {
}
@Override
@SuppressWarnings("unchecked")
public void fromBytes(ByteBuf buf){
// I'm guessing the settings field will be null here, so it needs initialising.
// This is also a great reason to have the settings as an actual object.
@@ -58,6 +68,13 @@ public class PacketSyncSettings implements IMessageHandler<Message, IMessage> {
settings.slowTimeAffectsPlayers = buf.readBoolean();
settings.replaceVanillaFireballs = buf.readBoolean();
settings.forfeitChance = buf.readFloat();
settings.bookshelfSearchRadius = buf.readInt();
int length = buf.readInt();
List<Pair<ResourceLocation, Short>> entries = new ArrayList<>();
for(int i=0; i<length; i++){
entries.add(Pair.of(new ResourceLocation(ByteBufUtils.readUTF8String(buf)), buf.readShort()));
}
settings.bookshelfBlocks = entries.toArray(new Pair[0]);
}
@Override
@@ -67,6 +84,12 @@ public class PacketSyncSettings implements IMessageHandler<Message, IMessage> {
buf.writeBoolean(settings.slowTimeAffectsPlayers);
buf.writeBoolean(settings.replaceVanillaFireballs);
buf.writeFloat((float)settings.forfeitChance); // Configs don't have floats but this can only be 0-1 anyway
buf.writeInt(settings.bookshelfSearchRadius);
buf.writeInt(settings.bookshelfBlocks.length);
for(Pair<ResourceLocation, Short> entry : settings.bookshelfBlocks){
ByteBufUtils.writeUTF8String(buf, entry.getLeft().toString());
buf.writeShort(entry.getRight());
}
}
}
}