Improve bookshelf search and account for undiscovered spells

This commit is contained in:
Electroblob77
2020-04-04 21:32:57 +01:00
parent 71302b3219
commit a67eb2381d
4 changed files with 65 additions and 16 deletions
@@ -19,6 +19,7 @@ import net.minecraft.util.text.translation.I18n;
import net.minecraft.world.World;
import net.minecraftforge.common.config.Property;
import javax.annotation.Nullable;
import java.util.List;
import java.util.Set;
@@ -74,7 +75,7 @@ public class CommonProxy {
// SECTION Items
// ===============================================================================================================
public boolean shouldDisplayDiscovered(Spell spell, ItemStack stack){
public boolean shouldDisplayDiscovered(Spell spell, @Nullable ItemStack stack){
return false;
}
@@ -79,6 +79,7 @@ import net.minecraftforge.fml.client.registry.ClientRegistry;
import net.minecraftforge.fml.client.registry.RenderingRegistry;
import org.lwjgl.input.Keyboard;
import javax.annotation.Nullable;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.List;
@@ -208,7 +209,7 @@ public class ClientProxy extends CommonProxy {
// ===============================================================================================================
@Override
public boolean shouldDisplayDiscovered(Spell spell, ItemStack stack){
public boolean shouldDisplayDiscovered(Spell spell, @Nullable ItemStack stack){
EntityPlayerSP player = Minecraft.getMinecraft().player;
@@ -288,7 +288,6 @@ public class ContainerArcaneWorkbench extends Container {
* or null if no slots are appropriate. Note that this does mean the stack <i>will</i> fit, only that it is valid
* for all of the slots in the given range, and will fit if there is space for it.
* @param stack The stack to find a slot for
* @param excludeBookshelves Whether to exclude the bookshelf slots (useful when transferring from them)
* @return A 2-element int array of the minimum and maximum slot IDs respectively
*/
@Nullable
@@ -118,9 +118,15 @@ public abstract class Spell extends IForgeRegistryEntry.Impl<Spell> implements C
public static final String SPLASH_EFFECT_STRENGTH = "splash_effect_strength";
// TODO: Translations for these?
public static final String TIER_MATCH_PREFIX = "tier=";
public static final String ELEMENT_MATCH_PREFIX = "element=";
public static final String TYPE_MATCH_PREFIX = "type=";
public static final String TIER_MATCH_PREFIX = "tier";
public static final String ELEMENT_MATCH_PREFIX = "element";
public static final String TYPE_MATCH_PREFIX = "type";
public static final String DISCOVERED_MATCH_PREFIX = "discovered";
public static final String MODID_MATCH_PREFIX = "modid";
public static final String MATCH_CONDITION_SEPARATOR = ";";
public static final String MATCH_KEY_VALUE_SEPARATOR = "=";
public static final String MATCH_VALUE_SEPARATOR = ",";
/** Forge registry-based replacement for the internal spells list. */
public static IForgeRegistry<Spell> registry;
@@ -682,26 +688,68 @@ public abstract class Spell extends IForgeRegistryEntry.Impl<Spell> implements C
/**
* Returns whether this spell matches the given string. <b>Client-side only!</b> A spell matches a particular string
* if any of the following conditions are true:<p></p>
* if any of the following conditions are true:
* <p></p>
* - The spell's localised name contains the given string<br>
* - The string starts with "tier:", and the localised name of the spell's tier contains the given string<br>
* - The string starts with "element:", and the localised name of the spell's element contains the given string<br>
* - The string starts with "type:", and the localised name of the spell's type contains the given string<p></p>
* - The string starts with "type:", and the localised name of the spell's type contains the given string
* <p></p>
* <i>Matches are not case-sensitive.</i>
* @param text The string to tested
* @return True if this spell matches the given string, false otherwise.
*/
public boolean matches(@Nonnull String text){
if(text.startsWith(TIER_MATCH_PREFIX)){
return getTier().getDisplayName().toLowerCase(Locale.ROOT).contains(text.substring(TIER_MATCH_PREFIX.length()));
}else if(text.startsWith(ELEMENT_MATCH_PREFIX)){
return getElement().getDisplayName().toLowerCase(Locale.ROOT).contains(text.substring(ELEMENT_MATCH_PREFIX.length()));
}else if(text.startsWith(TYPE_MATCH_PREFIX)){
return getType().getDisplayName().toLowerCase(Locale.ROOT).contains(text.substring(TYPE_MATCH_PREFIX.length()));
}else{
return getDisplayName().toLowerCase(Locale.ROOT).contains(text);
if(text.isEmpty()) return true;
boolean discovered = Wizardry.proxy.shouldDisplayDiscovered(this, null);
String[] conditions = text.split(MATCH_CONDITION_SEPARATOR);
for(String condition : conditions){
String[] args = condition.split(MATCH_KEY_VALUE_SEPARATOR, 2);
// Invalid condition, treat the whole lot as a spell name instead
if(args.length < 2) return discovered && getDisplayName().toLowerCase(Locale.ROOT).contains(text);
String key = args[0];
String[] values = args[1].split(MATCH_VALUE_SEPARATOR);
String target;
switch(key){
case TIER_MATCH_PREFIX:
// Tier IS known for undiscovered spells so we can match it
target = getTier().getDisplayName().toLowerCase(Locale.ROOT);
break;
case ELEMENT_MATCH_PREFIX:
if(!discovered) return false; // Element is unknown so doesn't match
target = getElement().getDisplayName().toLowerCase(Locale.ROOT);
break;
case TYPE_MATCH_PREFIX:
if(!discovered) return false; // Type is unknown so doesn't match
target = getType().getDisplayName().toLowerCase(Locale.ROOT);
break;
case MODID_MATCH_PREFIX:
if(!discovered) return false; // Mod ID is unknown so doesn't match
target = getRegistryName().getNamespace().toLowerCase(Locale.ROOT);
break;
case DISCOVERED_MATCH_PREFIX:
target = Boolean.toString(discovered);
break;
default:
// Invalid condition, treat the whole lot as a spell name instead
return discovered && getDisplayName().toLowerCase(Locale.ROOT).contains(text);
}
if(Arrays.stream(values).noneMatch(target::contains)) return false; // Didn't match
}
return true; // Matched all the conditions, yay!
}
// ============================================ Sound methods ==============================================