Permalink
Browse files

Updated ModularFramework, updated to latest SpongeAPI, and started ba…

…se of DispenserRecipes
  • Loading branch information...
me4502 committed Dec 2, 2017
1 parent ca8fc8e commit 8c51ae95b8cc6c4d20f8c2dc16903400649c054e
View
@@ -98,7 +98,6 @@ subprojects {
targetCompatibility = JavaVersion.VERSION_1_8
repositories {
mavenLocal()
mavenCentral()
maven { url "http://repo.spongepowered.org/maven/" }
maven { url "http://maven.sk89q.com/repo/" }
@@ -122,6 +121,7 @@ subprojects {
// Logging
compile 'org.slf4j:slf4j-api:1.7.22'
compile 'com.me4502:modularframework-core:1.9.1'
compile 'com.sk89q.worldedit:worldedit-core:6.1.4-SNAPSHOT'
testCompile 'org.mockito:mockito-core:2.+'
testCompile 'junit:junit:5.+'
@@ -16,10 +16,9 @@ def spongeFullVersion = spongeVersion + ".0-SNAPSHOT"
dependencies {
compile project(':craftbook-core')
compileOnly "org.spongepowered:spongeapi:${spongeFullVersion}"
compile 'com.me4502:ModularFramework:1.8.4'
compile "org.spongepowered:spongeapi:${spongeFullVersion}"
compile 'com.me4502:modularframework-sponge:1.9.1'
compile 'com.sk89q.worldedit:worldedit-sponge:6.1.7-SNAPSHOT'
testCompile "org.mockito:mockito-core:2.+"
}
project.archivesBaseName = "${project.archivesBaseName}-S${spongeVersion}"
@@ -34,8 +33,10 @@ shadowJar {
dependencies {
include(dependency(':craftbook-core'))
relocate ("com.me4502.modularframework", "com.sk89q.craftbook.util.modularframework") {
include(dependency("com.me4502:ModularFramework"))
exclude "com.me4502.modularframework.ModularFramework"
include(dependency("com.me4502:modularframework-core"))
include(dependency("com.me4502:modularframework-sponge"))
exclude "com.me4502.modularframework.modularframework-core"
exclude "com.me4502.modularframework.modularframework-sponge"
}
}
@@ -17,10 +17,11 @@
package com.sk89q.craftbook.sponge;
import com.google.inject.Inject;
import com.me4502.modularframework.ModuleController;
import com.me4502.modularframework.ShadedModularFramework;
import com.me4502.modularframework.SpongeModuleController;
import com.me4502.modularframework.exception.ModuleNotInstantiatedException;
import com.me4502.modularframework.module.ModuleWrapper;
import com.me4502.modularframework.module.SpongeModuleWrapper;
import com.sk89q.craftbook.core.CraftBookAPI;
import com.sk89q.craftbook.core.Mechanic;
import com.sk89q.craftbook.core.st.SelfTriggerManager;
@@ -79,7 +80,7 @@
@Inject
public PluginContainer container;
public ModuleController<CraftBookPlugin> moduleController;
public SpongeModuleController<CraftBookPlugin> moduleController;
private SelfTriggerManager selfTriggerManager;
@@ -274,6 +275,7 @@ public void discoverMechanics() {
moduleController.registerModule("com.sk89q.craftbook.sponge.mechanics.LightSwitch");
moduleController.registerModule("com.sk89q.craftbook.sponge.mechanics.Marquee");
moduleController.registerModule("com.sk89q.craftbook.sponge.mechanics.signcopier.SignCopier");
moduleController.registerModule("com.sk89q.craftbook.sponge.mechanics.dispenser.DispenserRecipes");
moduleController.registerModule("com.sk89q.craftbook.sponge.mechanics.XPStorer", GameState.PRE_INITIALIZATION);
//Circuit Mechanics
@@ -309,7 +311,7 @@ public void discoverMechanics() {
private void loadMechanics(GameState loadState) {
moduleController.enableModules(input -> {
if (loadState == input.getLoadState() && (config.enabledMechanics.getValue().contains(input.getName())
if (loadState == ((SpongeModuleWrapper) input).getLoadState() && (config.enabledMechanics.getValue().contains(input.getName())
|| "true".equalsIgnoreCase(System.getProperty("craftbook.enable-all"))
|| "true".equalsIgnoreCase(System.getProperty("craftbook.generate-docs")))) {
logger.debug("Enabled: " + input.getName());
@@ -322,7 +324,7 @@ private void loadMechanics(GameState loadState) {
for (ModuleWrapper module : moduleController.getModules()) {
if (!module.isEnabled()) continue;
try {
if (module.getModuleUnchecked() instanceof SelfTriggeringMechanic && !getSelfTriggerManager().isPresent()) {
if (((SpongeModuleWrapper) module).getModuleUnchecked() instanceof SelfTriggeringMechanic && !getSelfTriggerManager().isPresent()) {
this.selfTriggerManager = new SpongeSelfTriggerManager();
getSelfTriggerManager().ifPresent(SelfTriggerManager::initialize);
break;
@@ -336,7 +338,7 @@ private void loadMechanics(GameState loadState) {
for (ModuleWrapper module : moduleController.getModules()) {
if(!module.isEnabled()) continue;
try {
Mechanic mechanic = (Mechanic) module.getModuleUnchecked();
Mechanic mechanic = (Mechanic) ((SpongeModuleWrapper) module).getModuleUnchecked();
if(mechanic instanceof DocumentationProvider)
DocumentationGenerator.generateDocumentation((DocumentationProvider) mechanic);
} catch (ModuleNotInstantiatedException e) {
@@ -18,6 +18,7 @@
import com.me4502.modularframework.exception.ModuleNotInstantiatedException;
import com.me4502.modularframework.module.ModuleWrapper;
import com.me4502.modularframework.module.SpongeModuleWrapper;
import com.sk89q.craftbook.core.Mechanic;
import com.sk89q.craftbook.core.util.documentation.DocumentationGenerator;
import com.sk89q.craftbook.core.util.documentation.DocumentationProvider;
@@ -35,7 +36,7 @@ public CommandResult execute(CommandSource src, CommandContext args) throws Comm
for (ModuleWrapper module : CraftBookPlugin.spongeInst().moduleController.getModules()) {
if(!module.isEnabled()) continue;
try {
Mechanic mechanic = (Mechanic) module.getModuleUnchecked();
Mechanic mechanic = (Mechanic) ((SpongeModuleWrapper) module).getModuleUnchecked();
if(mechanic instanceof DocumentationProvider)
DocumentationGenerator.generateDocumentation((DocumentationProvider) mechanic);
} catch (ModuleNotInstantiatedException e) {
@@ -51,13 +51,11 @@
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.block.ChangeBlockEvent;
import org.spongepowered.api.event.block.InteractBlockEvent;
import org.spongepowered.api.event.cause.Cause;
import org.spongepowered.api.event.cause.entity.damage.source.EntityDamageSource;
import org.spongepowered.api.event.cause.entity.spawn.SpawnTypes;
import org.spongepowered.api.event.filter.cause.First;
import org.spongepowered.api.event.item.inventory.DropItemEvent;
import org.spongepowered.api.item.Enchantments;
import org.spongepowered.api.item.ItemTypes;
import org.spongepowered.api.item.enchantment.EnchantmentTypes;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.profile.GameProfile;
import org.spongepowered.api.service.permission.PermissionDescription;
@@ -221,7 +219,7 @@ public void onItemDrops(DropItemEvent.Destruct event, @First Entity spawnCause)
if (killer != null && killer instanceof Player) {
int level = ((Player) killer).getItemInHand(HandTypes.MAIN_HAND).filter(item -> item.get(Keys.ITEM_ENCHANTMENTS).isPresent())
.map(item -> item.get(Keys.ITEM_ENCHANTMENTS).get().stream()
.filter(enchant -> enchant.getEnchantment().equals(Enchantments.LOOTING)).findFirst())
.filter(enchant -> enchant.getType().equals(EnchantmentTypes.LOOTING)).findFirst())
.filter(Optional::isPresent).map(enchant -> enchant.get().getLevel()).orElse(0);
chance = Math.min(1, chance + (lootingRateModifier.getValue() * level));
}
@@ -0,0 +1,37 @@
package com.sk89q.craftbook.sponge.mechanics.dispenser;
import com.flowpowered.math.vector.Vector3d;
import org.spongepowered.api.block.tileentity.carrier.Dispenser;
import org.spongepowered.api.data.key.Keys;
import org.spongepowered.api.entity.EntityTypes;
import org.spongepowered.api.entity.explosive.PrimedTNT;
import org.spongepowered.api.item.ItemTypes;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.util.Direction;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;
public class Cannon extends SimpleDispenserRecipe {
public Cannon() {
super(new ItemStack[]{
ItemStack.of(ItemTypes.FIRE_CHARGE, 1), ItemStack.of(ItemTypes.GUNPOWDER, 1), ItemStack.of(ItemTypes.FIRE_CHARGE, 1),
ItemStack.of(ItemTypes.GUNPOWDER, 1), ItemStack.of(ItemTypes.TNT, 1), ItemStack.of(ItemTypes.GUNPOWDER, 1),
ItemStack.of(ItemTypes.FIRE_CHARGE, 1), ItemStack.of(ItemTypes.GUNPOWDER, 1), ItemStack.of(ItemTypes.FIRE_CHARGE, 1),
});
}
@Override
public boolean doAction(Dispenser dispenser, ItemStack[] recipe, Vector3d velocity) {
Direction face = dispenser.getLocation().get(Keys.DIRECTION).orElse(Direction.NONE);
if (face != Direction.NONE) {
Location<World> location = dispenser.getLocation().getRelative(face).add(0.5, 0.5, 0.5);
PrimedTNT tnt = (PrimedTNT) dispenser.getWorld().createEntity(EntityTypes.PRIMED_TNT, location.getPosition());
tnt.setVelocity(velocity.normalize().mul(2f));
dispenser.getWorld().spawnEntity(tnt);
return true;
}
return false;
}
}
@@ -0,0 +1,12 @@
package com.sk89q.craftbook.sponge.mechanics.dispenser;
import com.flowpowered.math.vector.Vector3d;
import org.spongepowered.api.block.tileentity.carrier.Dispenser;
import org.spongepowered.api.item.inventory.ItemStack;
public interface DispenserRecipe {
boolean doesPass(ItemStack[] recipe);
boolean doAction(Dispenser dispenser, ItemStack[] recipe, Vector3d velocity);
}
@@ -0,0 +1,85 @@
package com.sk89q.craftbook.sponge.mechanics.dispenser;
import com.flowpowered.math.vector.Vector3d;
import com.me4502.modularframework.module.Module;
import com.sk89q.craftbook.core.util.CraftBookException;
import com.sk89q.craftbook.core.util.documentation.DocumentationProvider;
import com.sk89q.craftbook.sponge.mechanics.types.SpongeBlockMechanic;
import org.spongepowered.api.block.BlockTypes;
import org.spongepowered.api.block.tileentity.carrier.Dispenser;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.entity.ConstructEntityEvent;
import org.spongepowered.api.event.filter.cause.First;
import org.spongepowered.api.event.item.inventory.DropItemEvent;
import org.spongepowered.api.item.inventory.Inventory;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.world.LocatableBlock;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
@Module(id = "dispenserrecipes", name = "DispenserRecipes", onEnable = "onInitialize", onDisable = "onDisable")
public class DispenserRecipes extends SpongeBlockMechanic implements DocumentationProvider {
private List<DispenserRecipe> recipes = new ArrayList<>();
@Override
public void onInitialize() throws CraftBookException {
super.onInitialize();
recipes.add(new Cannon());
recipes.add(new Fan());
}
@Override
public String getPath() {
return "mechanics/dispenser_recipes";
}
@Override
public boolean isValid(Location<World> location) {
return location.getBlockType() == BlockTypes.DISPENSER;
}
@Listener
public void onDispense(DropItemEvent.Dispense event, @First LocatableBlock dispenser) {
if (dispenser.getBlockState().getType() != BlockTypes.DISPENSER) {
return;
}
Dispenser dispenserTile = (Dispenser) dispenser.getLocation().getTileEntity().get();
if (handleDispenserAction(dispenserTile, event.getEntities().get(0).getVelocity())) {
event.setCancelled(true);
}
}
@Listener
public void onCreateEntity(ConstructEntityEvent event, @First LocatableBlock dispenser) {
// TODO
}
// TODO Water/Lava form
public boolean handleDispenserAction(Dispenser dispenser, Vector3d velocity) {
for (DispenserRecipe recipe : recipes) {
ItemStack[] items = StreamSupport.stream(dispenser.getInventory().slots().spliterator(), false)
.map(Inventory::peek).filter(Optional::isPresent)
.map(Optional::get).toArray(ItemStack[]::new);
if (recipe.doesPass(items)) {
if (recipe.doAction(dispenser, items, velocity)) {
dispenser.getInventory().slots().forEach(Inventory::poll); // Take one of every stack.
return true;
}
break;
}
}
return false;
}
}
@@ -0,0 +1,22 @@
package com.sk89q.craftbook.sponge.mechanics.dispenser;
import com.flowpowered.math.vector.Vector3d;
import org.spongepowered.api.block.tileentity.carrier.Dispenser;
import org.spongepowered.api.item.ItemTypes;
import org.spongepowered.api.item.inventory.ItemStack;
public class Fan extends SimpleDispenserRecipe {
public Fan() {
super(new ItemStack[]{
ItemStack.of(ItemTypes.WEB, 1), ItemStack.of(ItemTypes.LEAVES, 1), ItemStack.of(ItemTypes.WEB, 1),
ItemStack.of(ItemTypes.LEAVES, 1), ItemStack.of(ItemTypes.PISTON, 1), ItemStack.of(ItemTypes.LEAVES, 1),
ItemStack.of(ItemTypes.WEB, 1), ItemStack.of(ItemTypes.LEAVES, 1), ItemStack.of(ItemTypes.WEB, 1),
});
}
@Override
public boolean doAction(Dispenser dispenser, ItemStack[] recipe, Vector3d velocity) {
return false;
}
}
@@ -0,0 +1,24 @@
package com.sk89q.craftbook.sponge.mechanics.dispenser;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.item.inventory.ItemStackComparators;
public abstract class SimpleDispenserRecipe implements DispenserRecipe {
private ItemStack[] recipe;
public SimpleDispenserRecipe(ItemStack[] recipe) {
this.recipe = recipe;
}
@Override
public boolean doesPass(ItemStack[] checkRecipe) {
for (int i = 0; i < checkRecipe.length; i++) {
if (ItemStackComparators.TYPE.compare(recipe[i], checkRecipe[i]) != 0) {
return false;
}
}
return true;
}
}
@@ -19,6 +19,7 @@
import com.google.common.collect.ImmutableMap;
import com.me4502.modularframework.exception.ModuleNotInstantiatedException;
import com.me4502.modularframework.module.ModuleWrapper;
import com.me4502.modularframework.module.SpongeModuleWrapper;
import com.sk89q.craftbook.core.st.SelfTriggerClock;
import com.sk89q.craftbook.core.st.SelfTriggerManager;
import com.sk89q.craftbook.sponge.CraftBookPlugin;
@@ -83,7 +84,7 @@ private void registerAll(Chunk chunk) {
for (ModuleWrapper module : CraftBookPlugin.spongeInst().moduleController.getModules()) {
if(!module.isEnabled()) continue;
try {
SpongeMechanic mechanic = (SpongeMechanic) module.getModuleUnchecked();
SpongeMechanic mechanic = (SpongeMechanic) ((SpongeModuleWrapper) module).getModuleUnchecked();
if (mechanic instanceof SpongeBlockMechanic && mechanic instanceof SelfTriggeringMechanic) {
if (((SpongeBlockMechanic) mechanic).isValid(tileEntity.getLocation())) {
register((SelfTriggeringMechanic) mechanic, tileEntity.getLocation());

0 comments on commit 8c51ae9

Please sign in to comment.