Skip to content

Commit

Permalink
Rewrote the spellbook pages system
Browse files Browse the repository at this point in the history
  • Loading branch information
Sollace committed Feb 8, 2019
1 parent fc26e84 commit 8c3ad67
Show file tree
Hide file tree
Showing 27 changed files with 703 additions and 357 deletions.
3 changes: 3 additions & 0 deletions src/main/java/com/minelittlepony/unicopia/Unicopia.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import com.minelittlepony.unicopia.advancements.UAdvancements;
import com.minelittlepony.unicopia.block.ITillable;
import com.minelittlepony.unicopia.command.Commands;
import com.minelittlepony.unicopia.enchanting.Pages;
import com.minelittlepony.unicopia.enchanting.SpellRecipe;
import com.minelittlepony.unicopia.forgebullshit.FBS;
import com.minelittlepony.unicopia.inventory.gui.ContainerSpellBook;
Expand Down Expand Up @@ -112,6 +113,8 @@ protected void registerRecipeTypes(Map<String, Function<JsonObject, IRecipe>> ty
}
};

Pages.instance().load();

Biome.REGISTRY.forEach(UEntities::registerSpawnEntries);
UClient.instance().posInit(event);

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.minelittlepony.unicopia.enchanting;

import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Stream;

import com.google.common.collect.Lists;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

public class CompoundCondition implements IUnlockCondition<IUnlockEvent> {

final Op operation;

final List<IUnlockCondition<IUnlockEvent>> conditions = Lists.newArrayList();

CompoundCondition(JsonObject json) {
require(json, "operation");
require(json, "conditions");

operation = Op.valueOf(json.get("operation").getAsString().toUpperCase());

json.get("conditions").getAsJsonArray().forEach(this::addElement);
}

void addElement(JsonElement element) {
JsonObject obj = element.getAsJsonObject();

conditions.add(Pages.instance().createCondition(obj));
}

@Override
public boolean accepts(IUnlockEvent event) {
return true;
}

@Override
public boolean matches(IPageOwner owner, IUnlockEvent event) {
return operation.test.apply(conditions.stream(), condition -> condition.accepts(event) && condition.matches(owner, event));
}

enum Op {
AND(Stream::allMatch),
OR(Stream::anyMatch);

final Test test;

Op(Test test) {
this.test = test;
}

interface Test {
boolean apply(Stream<IUnlockCondition<IUnlockEvent>> stream, Predicate<IUnlockCondition<IUnlockEvent>> predicate);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.minelittlepony.unicopia.enchanting;

import com.google.gson.JsonObject;

@FunctionalInterface
public interface IConditionFactory {
IUnlockCondition<?> create(JsonObject json);
}
40 changes: 40 additions & 0 deletions src/main/java/com/minelittlepony/unicopia/enchanting/IPage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.minelittlepony.unicopia.enchanting;

import net.minecraft.util.ResourceLocation;

/**
* A spellbook page
*/
public interface IPage extends Comparable<IPage> {
/**
* Gets the index.
* This is the position the page appears in the book gui.
*/
int getIndex();

/**
* The unique name of this page.
*/
ResourceLocation getName();

/**
* Tests unlock conditions for this page.
* Returns true if the owner is permitted to read this page.
*/
boolean canUnlock(IPageOwner owner, IUnlockEvent event);

/**
* Gets the texture.
* This is what's shown when this page is opened in the book gui.
*/
ResourceLocation getTexture();

/**
* The default state.
*/
PageState getDefaultState();

IPage next();

IPage prev();
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
package com.minelittlepony.unicopia.enchanting;

import java.util.List;
import java.util.Map;

import javax.annotation.Nonnull;

import com.minelittlepony.unicopia.network.ITransmittable;

import net.minecraft.util.ResourceLocation;

/**
* Interface for things that own and can unlock pages.
*
*/
public interface IPageOwner extends ITransmittable {

@Nonnull
List<Integer> getUnlockedPages();

default boolean hasPageUnlock(int pageIndex) {
return getUnlockedPages().contains(pageIndex);
}

default boolean unlockPage(int pageIndex) {
if (!hasPageUnlock(pageIndex)) {
if (getUnlockedPages().add(pageIndex)) {
sendCapabilities(true);
Map<ResourceLocation, PageState> getPageStates();

return true;
}
default void setPageState(IPage page, PageState state) {
if (state == PageState.LOCKED) {
getPageStates().remove(page.getName());
} else {
getPageStates().put(page.getName(), state);
}
return false;
sendCapabilities(true);
}

default boolean hasUnlockedPages() {
return getUnlockedPages().size() > 0;
default PageState getPageState(IPage page) {
return getPageStates().getOrDefault(page.getName(), page.getDefaultState());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package com.minelittlepony.unicopia.enchanting;

@FunctionalInterface
public interface IPageUnlockListener {
public void onPageUnlocked();
/**
* Called when a page is unlocked.
*
* @param page The page that has been unlocked
* @return True to allow, false to block.
*/
boolean onPageUnlocked(IPage page);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.minelittlepony.unicopia.enchanting;

import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;

/**
* A PageEvent for determining when certain pages must be unlocked.
*/
public interface IUnlockCondition<T extends IUnlockEvent> {

/**
* Returns true if event instanceof T
*/
default boolean accepts(IUnlockEvent event) {
return true;
}

/**
* Checks if this event's conditions are met.
* @param prop PlayerExtension for the player doing the crafting
* @param stack ItemStack crafted
*/
boolean matches(IPageOwner owner, T event);

default void require(JsonObject json, String memberName) {
if (!json.has(memberName)) {
throw new JsonParseException(String.format("%s condition must have a %s", getClass().getSimpleName(), memberName));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.minelittlepony.unicopia.enchanting;

public interface IUnlockEvent {

}

This file was deleted.

Loading

0 comments on commit 8c3ad67

Please sign in to comment.