Permalink
Browse files

Added CartEjector as a test mechanic for the Minecart Block Mechanics.

  • Loading branch information...
me4502 committed Jul 8, 2017
1 parent 44d0ea5 commit a00cdef312d4158be84b5d2988e4487e6f0049ec
@@ -299,6 +299,8 @@ public void discoverMechanics() {
moduleController.registerModule("com.sk89q.craftbook.sponge.mechanics.minecart.MoreRails");
moduleController.registerModule("com.sk89q.craftbook.sponge.mechanics.minecart.NoCollide");
moduleController.registerModule("com.sk89q.craftbook.sponge.mechanics.minecart.RemoveEntities");
//Minecart - Block
moduleController.registerModule("com.sk89q.craftbook.sponge.mechanics.minecart.block.CartEjector");
logger.info("Found " + moduleController.getModules().size());
}
@@ -30,6 +30,7 @@
import org.spongepowered.api.entity.EntityTypes;
import org.spongepowered.api.entity.Item;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.entity.living.player.gamemode.GameModes;
import org.spongepowered.api.entity.vehicle.minecart.Minecart;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.entity.RideEntityEvent;
@@ -93,7 +94,7 @@ public String getPath() {
public void run () {
if(minecart.isRemoved()) return;
if(giveItem.getValue()) {
if(giveItem.getValue() && player.gameMode().get() != GameModes.CREATIVE) {
ItemStack stack = ItemStack.of(ItemTypes.MINECART, 1);
if(!((Player) player).getInventory().offer(stack).getRejectedItems().isEmpty()) {
@@ -0,0 +1,117 @@
/*
* CraftBook Copyright (C) 2010-2017 sk89q <http://www.sk89q.com>
* CraftBook Copyright (C) 2011-2017 me4502 <http://www.me4502.com>
* CraftBook Copyright (C) Contributors
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not,
* see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.craftbook.sponge.mechanics.minecart.block;
import com.google.common.reflect.TypeToken;
import com.google.inject.Inject;
import com.me4502.modularframework.module.Module;
import com.me4502.modularframework.module.guice.ModuleConfiguration;
import com.sk89q.craftbook.core.util.ConfigValue;
import com.sk89q.craftbook.core.util.CraftBookException;
import com.sk89q.craftbook.core.util.PermissionNode;
import com.sk89q.craftbook.core.util.TernaryState;
import com.sk89q.craftbook.core.util.documentation.DocumentationProvider;
import com.sk89q.craftbook.sponge.util.BlockFilter;
import com.sk89q.craftbook.sponge.util.SignUtil;
import com.sk89q.craftbook.sponge.util.SpongePermissionNode;
import ninja.leaping.configurate.ConfigurationNode;
import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.entity.vehicle.minecart.Minecart;
import org.spongepowered.api.service.permission.PermissionDescription;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;
import java.util.List;
@Module(id = "cartejector", name = "CartEjector", onEnable="onInitialize", onDisable="onDisable")
public class CartEjector extends SpongeCartBlockMechanic implements DocumentationProvider {
@Inject
@ModuleConfiguration
public ConfigurationNode config;
private SpongePermissionNode createPermissions = new SpongePermissionNode("craftbook.cartejector", "Allows the user to create the " + getName() + " mechanic.", PermissionDescription.ROLE_USER);
private ConfigValue<BlockFilter> allowedBlocks = new ConfigValue<>("material", "The block that this mechanic requires.", new BlockFilter("IRON_BLOCK"), TypeToken.of(BlockFilter.class));
@Override
public void onInitialize() throws CraftBookException {
super.onInitialize();
allowedBlocks.load(config);
createPermissions.register();
}
@Override
public void impact(Minecart minecart, CartMechanismBlocks blocks, boolean minor) {
super.impact(minecart, blocks, minor);
if (minecart.getPassengers().isEmpty() || isActive(blocks) == TernaryState.FALSE) {
return;
}
Location<World> teleportTarget = null;
if (blocks.matches("eject")) {
teleportTarget = blocks.getRail().getRelative(SignUtil.getFront(blocks.getSign()));
}
List<Entity> passengers = minecart.getPassengers();
minecart.clearPassengers();
if (teleportTarget != null) {
for (Entity passenger : passengers) {
passenger.setLocationSafely(teleportTarget);
}
}
}
@Override
public String[] getValidSigns() {
return new String[] {
"[Eject]"
};
}
@Override
public SpongePermissionNode getCreatePermission() {
return createPermissions;
}
@Override
public BlockFilter getBlockFilter() {
return allowedBlocks.getValue();
}
@Override
public String getPath() {
return "mechanics/minecart/block/ejector";
}
@Override
public ConfigValue<?>[] getConfigurationNodes() {
return new ConfigValue[] {
allowedBlocks
};
}
@Override
public PermissionNode[] getPermissionNodes() {
return new PermissionNode[] {
createPermissions
};
}
}
@@ -83,7 +83,7 @@ public boolean matches(BlockState blockState) {
*/
public boolean matches(String text) {
return hasSign() && SignUtil.getTextRaw(sign.getTileEntity()
.map(sign -> (Sign) sign).get(), 1).equals('[' + text + ']');
.map(sign -> (Sign) sign).get(), 1).equalsIgnoreCase('[' + text + ']');
}
/**
@@ -16,13 +16,21 @@
*/
package com.sk89q.craftbook.sponge.mechanics.minecart.block;
import com.flowpowered.math.vector.Vector3d;
import com.sk89q.craftbook.core.util.TernaryState;
import com.sk89q.craftbook.sponge.mechanics.types.SpongeSignMechanic;
import com.sk89q.craftbook.sponge.util.BlockFilter;
import com.sk89q.craftbook.sponge.util.BlockUtil;
import org.spongepowered.api.entity.vehicle.minecart.Minecart;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.entity.MoveEntityEvent;
import org.spongepowered.api.event.filter.Getter;
import org.spongepowered.api.util.Direction;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;
import java.util.Optional;
public abstract class SpongeCartBlockMechanic extends SpongeSignMechanic {
public TernaryState isActive(CartMechanismBlocks blocks) {
@@ -35,6 +43,7 @@ public TernaryState isActive(CartMechanismBlocks blocks) {
break;
case FALSE:
isConnected = true;
break;
}
}
@@ -51,9 +60,43 @@ public TernaryState isActive(Location<World> block) {
break;
case FALSE:
isConnected = true;
break;
}
}
return isConnected ? TernaryState.FALSE : TernaryState.NONE;
}
@Listener
public void onVehicleMove(MoveEntityEvent event, @Getter("getTargetEntity") Minecart minecart) {
if (event.getFromTransform().getPosition().distanceSquared(event.getToTransform().getPosition()) > 2 * 2) {
return;
}
CartMechanismBlocks.findByRail(minecart.getLocation()).filter(this::isValid).ifPresent(cartMechanismBlocks -> {
Vector3d from = event.getFromTransform().getPosition();
Vector3d to = event.getToTransform().getPosition();
boolean minor = from.getFloorX() == to.getFloorX() && from.getFloorY() == to.getFloorY() && from.getFloorZ() == to.getFloorZ();
impact(minecart, cartMechanismBlocks, minor);
});
}
public void impact(Minecart minecart, CartMechanismBlocks blocks, boolean minor) {}
// TODO if needed. public void enter(Minecart minecart, Player player, CartMechanismBlocks blocks) {}
public abstract BlockFilter getBlockFilter();
public boolean requiresSign() {
return false;
}
public boolean isValid(CartMechanismBlocks blocks) {
return BlockUtil.doesStatePassFilter(getBlockFilter(), blocks.getBase().getBlock()) && (!requiresSign() || blocks.hasSign());
}
@Override
public boolean isValid(Location<World> location) {
Optional<CartMechanismBlocks> blocks = CartMechanismBlocks.find(location);
return blocks.filter(this::isValid).isPresent();
}
}
@@ -140,7 +140,7 @@ public static boolean isPowerSource(Location<World> block) {
public static TernaryState isPowered(Location<World> block, Direction face) {
Location<World> pow = block.getRelative(face);
if (isPowerSource(block)) {
if (isPowerSource(pow)) {
if (getBlockPowerLevel(pow).orElse(0) > 0
|| getBlockIndirectPowerLevel(pow).orElse(0) > 0) return TernaryState.TRUE;
return TernaryState.FALSE;
@@ -241,6 +241,18 @@ public static BlockState getBlockStateFromString(String rule) {
return locations;
}
/**
* Gets whether or not the specified {@link BlockState} passes the {@link BlockFilter}.
*
* @param filter The filter
* @param state The state to test
* @return If it passes
*/
public static boolean doesStatePassFilter(BlockFilter filter, BlockState state) {
return filter.getApplicableBlockStates().stream()
.anyMatch(blockState -> blockState.equals(state));
}
/**
* Gets whether or not the specified {@link BlockState} passes the {@link BlockFilter}s.
*
@@ -250,9 +262,8 @@ public static BlockState getBlockStateFromString(String rule) {
*/
public static boolean doesStatePassFilters(Collection<BlockFilter> filters, BlockState state) {
for(BlockFilter filter : filters)
for(BlockState blockState : filter.getApplicableBlockStates())
if(blockState.equals(state))
return true;
if(doesStatePassFilter(filter, state))
return true;
return false;
}

0 comments on commit a00cdef

Please sign in to comment.