-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
703 additions
and
357 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 0 additions & 32 deletions
32
src/main/java/com/minelittlepony/unicopia/enchanting/BasicCraftingEvent.java
This file was deleted.
Oops, something went wrong.
56 changes: 56 additions & 0 deletions
56
src/main/java/com/minelittlepony/unicopia/enchanting/CompoundCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/minelittlepony/unicopia/enchanting/IConditionFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
40
src/main/java/com/minelittlepony/unicopia/enchanting/IPage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
32 changes: 16 additions & 16 deletions
32
src/main/java/com/minelittlepony/unicopia/enchanting/IPageOwner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
9 changes: 8 additions & 1 deletion
9
src/main/java/com/minelittlepony/unicopia/enchanting/IPageUnlockListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/com/minelittlepony/unicopia/enchanting/IUnlockCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/com/minelittlepony/unicopia/enchanting/IUnlockEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.minelittlepony.unicopia.enchanting; | ||
|
||
public interface IUnlockEvent { | ||
|
||
} |
41 changes: 0 additions & 41 deletions
41
src/main/java/com/minelittlepony/unicopia/enchanting/MultiPageUnlockEvent.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.