From b3fd39c42cbd5909830e3e9ee2983cfdb489199e Mon Sep 17 00:00:00 2001
From: Electroblob77 <35599699+Electroblob77@users.noreply.github.com>
Date: Fri, 10 Apr 2020 22:42:31 +0100
Subject: [PATCH] Add config options for bookshelf blocks and bookshelf search
radius
---
.../java/electroblob/wizardry/Settings.java | 54 ++++++++++++++++++-
.../wizardry/block/BlockBookshelf.java | 7 +--
.../wizardry/packet/PacketSyncSettings.java | 23 ++++++++
.../assets/ebwizardry/lang/en_gb.lang | 4 ++
.../assets/ebwizardry/lang/en_us.lang | 4 ++
5 files changed, 88 insertions(+), 4 deletions(-)
diff --git a/src/main/java/electroblob/wizardry/Settings.java b/src/main/java/electroblob/wizardry/Settings.java
index 4d2ebf30..0d607205 100644
--- a/src/main/java/electroblob/wizardry/Settings.java
+++ b/src/main/java/electroblob/wizardry/Settings.java
@@ -278,6 +278,23 @@ public final class Settings {
public boolean replaceVanillaFireballs = true;
/** [Synchronised] Chance of 'misreading' an undiscovered spell and triggering a forfeit instead. */
public double forfeitChance = 0.2;
+ /**
+ * [Synchronised] 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;
+ /**
+ * [Synchronised] List of registry names of blocks that count as bookshelves for the arcane workbench and
+ * lectern.
+ */
+ public Pair[] 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[] 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[] 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[] 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[] array, ResourceLocation id, short metadata){
return Arrays.asList(array).contains(Pair.of(id, metadata)) || Arrays.asList(array).contains(Pair.of(id, OreDictionary.WILDCARD_VALUE));
}
diff --git a/src/main/java/electroblob/wizardry/block/BlockBookshelf.java b/src/main/java/electroblob/wizardry/block/BlockBookshelf.java
index ee4de190..42d6317e 100644
--- a/src/main/java/electroblob/wizardry/block/BlockBookshelf.java
+++ b/src/main/java/electroblob/wizardry/block/BlockBookshelf.java
@@ -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 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);
}
diff --git a/src/main/java/electroblob/wizardry/packet/PacketSyncSettings.java b/src/main/java/electroblob/wizardry/packet/PacketSyncSettings.java
index 6a408b81..55958461 100644
--- a/src/main/java/electroblob/wizardry/packet/PacketSyncSettings.java
+++ b/src/main/java/electroblob/wizardry/packet/PacketSyncSettings.java
@@ -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;
/**
* [Server -> Client] This packet is sent to synchronise the config settings with clients on player login.
@@ -31,7 +37,10 @@ public class PacketSyncSettings implements IMessageHandler {
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 {
}
@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 {
settings.slowTimeAffectsPlayers = buf.readBoolean();
settings.replaceVanillaFireballs = buf.readBoolean();
settings.forfeitChance = buf.readFloat();
+ settings.bookshelfSearchRadius = buf.readInt();
+ int length = buf.readInt();
+ List> entries = new ArrayList<>();
+ for(int i=0; i {
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 entry : settings.bookshelfBlocks){
+ ByteBufUtils.writeUTF8String(buf, entry.getLeft().toString());
+ buf.writeShort(entry.getRight());
+ }
}
}
}
diff --git a/src/main/resources/assets/ebwizardry/lang/en_gb.lang b/src/main/resources/assets/ebwizardry/lang/en_gb.lang
index 688732dd..d23663ff 100644
--- a/src/main/resources/assets/ebwizardry/lang/en_gb.lang
+++ b/src/main/resources/assets/ebwizardry/lang/en_gb.lang
@@ -1200,6 +1200,10 @@ config.ebwizardry.sword_item_whitelist=Sword Item Whitelist
config.ebwizardry.sword_item_whitelist.tooltip=List of registry names of items which should count as swords for imbuement spells. Most swords should work automatically, but those that don't can be added manually here. Item names are not case sensitive. For mod items, prefix with the mod ID (e.g. tconstruct:broadsword).
config.ebwizardry.bow_item_whitelist=Bow Item Whitelist
config.ebwizardry.bow_item_whitelist.tooltip=List of registry names of items which should count as bows for imbuement spells. Most bows should work automatically, but those that don't can be added manually here. Item names are not case sensitive. For mod items, prefix with the mod ID (e.g. tconstruct:shortbow).
+config.ebwizardry.bookshelf_blocks=Bookshelf Blocks
+config.ebwizardry.bookshelf_blocks.tooltip=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. ebwizardry:oak_bookshelf).
+config.ebwizardry.bookshelf_search_radius=Bookshelf Search Radius
+config.ebwizardry.bookshelf_search_radius.tooltip=The maximum number of blocks a bookshelf can be from an arcane workbench or lectern to be able to link to it.
config.ebwizardry.currency_items=Currency Items
config.ebwizardry.currency_items.tooltip=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.
diff --git a/src/main/resources/assets/ebwizardry/lang/en_us.lang b/src/main/resources/assets/ebwizardry/lang/en_us.lang
index e628f760..2e43452c 100644
--- a/src/main/resources/assets/ebwizardry/lang/en_us.lang
+++ b/src/main/resources/assets/ebwizardry/lang/en_us.lang
@@ -1200,6 +1200,10 @@ config.ebwizardry.sword_item_whitelist=Sword Item Whitelist
config.ebwizardry.sword_item_whitelist.tooltip=List of registry names of items which should count as swords for imbuement spells. Most swords should work automatically, but those that don't can be added manually here. Item names are not case sensitive. For mod items, prefix with the mod ID (e.g. tconstruct:broadsword).
config.ebwizardry.bow_item_whitelist=Bow Item Whitelist
config.ebwizardry.bow_item_whitelist.tooltip=List of registry names of items which should count as bows for imbuement spells. Most bows should work automatically, but those that don't can be added manually here. Item names are not case sensitive. For mod items, prefix with the mod ID (e.g. tconstruct:shortbow).
+config.ebwizardry.bookshelf_blocks=Bookshelf Blocks
+config.ebwizardry.bookshelf_blocks.tooltip=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. ebwizardry:oak_bookshelf).
+config.ebwizardry.bookshelf_search_radius=Bookshelf Search Radius
+config.ebwizardry.bookshelf_search_radius.tooltip=The maximum number of blocks a bookshelf can be from an arcane workbench or lectern to be able to link to it.
config.ebwizardry.currency_items=Currency Items
config.ebwizardry.currency_items.tooltip=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.