Java源码示例:org.spongepowered.api.block.BlockState
示例1
@Listener(order = Order.FIRST, beforeModifications = true)
public void onFlow(ChangeBlockEvent.Pre e, @First LocatableBlock locatable) {
RedProtect.get().logger.debug(LogLevel.BLOCKS, "Is BlockListener - onFlow event");
BlockState sourceState = locatable.getBlockState();
//liquid check
MatterProperty mat = sourceState.getProperty(MatterProperty.class).orElse(null);
if (mat != null && mat.getValue() == MatterProperty.Matter.LIQUID) {
e.getLocations().forEach(loc -> {
Region r = RedProtect.get().rm.getTopRegion(loc, this.getClass().getName());
if (r == null) {
boolean flow = RedProtect.get().config.globalFlagsRoot().worlds.get(locatable.getLocation().getExtent().getName()).allow_changes_of.liquid_flow;
boolean allowWater = RedProtect.get().config.globalFlagsRoot().worlds.get(locatable.getLocation().getExtent().getName()).allow_changes_of.water_flow;
boolean allowLava = RedProtect.get().config.globalFlagsRoot().worlds.get(locatable.getLocation().getExtent().getName()).allow_changes_of.lava_flow;
if (!flow)
e.setCancelled(true);
if (!allowWater && (loc.getBlockType() == BlockTypes.WATER || loc.getBlockType() == BlockTypes.FLOWING_WATER))
e.setCancelled(true);
if (!allowLava && (loc.getBlockType() == BlockTypes.LAVA || loc.getBlockType() == BlockTypes.FLOWING_LAVA))
e.setCancelled(true);
}
});
}
}
示例2
@Listener(order = Order.FIRST, beforeModifications = true)
public void onBlockBreakGeneric(ChangeBlockEvent.Break e) {
RedProtect.get().logger.debug(LogLevel.BLOCKS, "GlobalListener - Is onBlockBreakGeneric event");
LocatableBlock locatable = e.getCause().first(LocatableBlock.class).orElse(null);
if (locatable != null) {
BlockState sourceState = locatable.getBlockState();
//liquid check
MatterProperty mat = sourceState.getProperty(MatterProperty.class).orElse(null);
if (mat != null && mat.getValue() == MatterProperty.Matter.LIQUID) {
boolean allowdamage = RedProtect.get().config.globalFlagsRoot().worlds.get(locatable.getLocation().getExtent().getName()).allow_changes_of.flow_damage;
Region r = RedProtect.get().rm.getTopRegion(locatable.getLocation(), this.getClass().getName());
if (r == null && !allowdamage && locatable.getLocation().getBlockType() != BlockTypes.AIR) {
e.setCancelled(true);
}
}
}
}
示例3
@Listener(order = Order.FIRST, beforeModifications = true)
public void onFireSpread(ChangeBlockEvent.Place e, @First LocatableBlock locatable) {
RedProtect.get().logger.debug(LogLevel.BLOCKS, "GlobalListener - Is onFireSpread event!");
BlockState sourceState = locatable.getBlockState();
if (e.getTransactions().get(0).getFinal().getState().getType().equals(BlockTypes.FIRE) &&
(sourceState.getType() == BlockTypes.FIRE || sourceState.getType() == BlockTypes.LAVA || sourceState.getType() == BlockTypes.FLOWING_LAVA)) {
boolean fireDamage = RedProtect.get().config.globalFlagsRoot().worlds.get(locatable.getLocation().getExtent().getName()).fire_spread;
if (!fireDamage) {
Region r = RedProtect.get().rm.getTopRegion(e.getTransactions().get(0).getOriginal().getLocation().get(), this.getClass().getName());
if (r == null) {
RedProtect.get().logger.debug(LogLevel.BLOCKS, "Tryed to PLACE FIRE!");
e.setCancelled(true);
}
}
}
}
示例4
@Listener(order = Order.FIRST, beforeModifications = true)
public void onFireSpread(ChangeBlockEvent.Break e, @First LocatableBlock locatable) {
RedProtect.get().logger.debug(LogLevel.BLOCKS, "GlobalListener - Is onBlockBreakGeneric event");
BlockState sourceState = locatable.getBlockState();
if (sourceState.getType() == BlockTypes.FIRE) {
BlockSnapshot b = e.getTransactions().get(0).getOriginal();
boolean fireDamage = RedProtect.get().config.globalFlagsRoot().worlds.get(locatable.getLocation().getExtent().getName()).fire_block_damage;
if (!fireDamage && b.getState().getType() != BlockTypes.FIRE) {
Region r = RedProtect.get().rm.getTopRegion(b.getLocation().get(), this.getClass().getName());
if (r == null) {
RedProtect.get().logger.debug(LogLevel.BLOCKS, "Tryed to break from FIRE!");
e.setCancelled(true);
}
}
}
}
示例5
@Listener(order = Order.FIRST, beforeModifications = true)
public void onFlow(ChangeBlockEvent.Pre e, @First LocatableBlock locatable) {
RedProtect.get().logger.debug(LogLevel.BLOCKS, "Is BlockListener - onFlow event");
BlockState sourceState = locatable.getBlockState();
//liquid check
MatterProperty mat = sourceState.getProperty(MatterProperty.class).orElse(null);
if (mat != null && mat.getValue() == MatterProperty.Matter.LIQUID) {
e.getLocations().forEach(loc -> {
Region r = RedProtect.get().rm.getTopRegion(loc, this.getClass().getName());
if (r != null && !r.canFlow()) {
e.setCancelled(true);
}
});
}
}
示例6
private Set<Context> addBlockPropertyContexts(Set<Context> contexts, BlockState block) {
Matcher matcher = BLOCKSTATE_PATTERN.matcher(block.toString());
if (matcher.find()) {
final String properties[] = matcher.group(0).split(",");
for (String property : properties) {
contexts.add(new Context(ContextKeys.STATE, property.replace("=", ":")));
}
}
return contexts;
}
示例7
private static boolean validateItemTarget(String target) {
Optional<ItemType> itemType = Sponge.getRegistry().getType(ItemType.class, target);
if (itemType.isPresent()) {
return true;
}
// target could be an item block, so validate blockstate
Optional<BlockState> blockState = Sponge.getRegistry().getType(BlockState.class, target);
if (blockState.isPresent()) {
return true;
}
return false;
}
示例8
private static boolean validateBlockTarget(String target) {
Optional<BlockType> blockType = Sponge.getRegistry().getType(BlockType.class, target);
if (blockType.isPresent()) {
return true;
}
Optional<BlockState> blockState = Sponge.getRegistry().getType(BlockState.class, target);
if (blockState.isPresent()) {
return true;
}
return false;
}
示例9
public ObjectMapper getDefaultObjectMapper(boolean xml, boolean details, TreeNode perms) {
if (perms == null) {
throw new NullPointerException("Permissions may not be null");
}
ObjectMapper om = xml ? new XmlMapper() : new ObjectMapper();
if (xml) {
((XmlMapper)om).configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);
}
om.setSerializationInclusion(JsonInclude.Include.NON_NULL);
SimpleModule mod = new SimpleModule();
for (Map.Entry<Class, BaseSerializer> entry : serializers.entrySet()) {
mod.addSerializer(entry.getKey(), entry.getValue());
}
mod.addDeserializer(ItemStack.class, new ItemStackDeserializer());
mod.addDeserializer(BlockState.class, new BlockStateDeserializer());
mod.addDeserializer(ItemStackSnapshot.class, new ItemStackSnapshotDeserializer());
mod.addDeserializer(CachedLocation.class, new CachedLocationDeserializer());
mod.addDeserializer(CachedPlayer.class, new CachedPlayerDeserializer());
mod.addDeserializer(CachedWorld.class, new CachedWorldDeserializer());
mod.addDeserializer(CachedCatalogType.class, new CachedCatalogTypeDeserializer<>(CatalogType.class));
om.registerModule(mod);
SimpleFilterProvider filterProvider = new SimpleFilterProvider();
filterProvider.addFilter(BaseFilter.ID, new BaseFilter(details, perms));
om.setFilterProvider(filterProvider);
om.setAnnotationIntrospector(new AnnotationIntrospector());
return om;
}
示例10
/**
* Gets the block in the specified world at the specified position.
* @param world The world to get the block from.
* @param pos The position of the block.
* @return The block state of the block that was requested
*/
public BlockState getBlockAt(CachedWorld world, Vector3i pos) {
return WebAPI.runOnMain(() -> {
Optional<?> obj = world.getLive();
if (!obj.isPresent())
throw new InternalServerErrorException("Could not get live world");
World w = (World)obj.get();
return w.getBlock(pos).copy();
});
}
示例11
@Override
protected void processBlock(World world, Vector3i pos) {
BlockState state = newStates.get(pos);
if (state == null)
return;
world.setBlock(pos, state, BlockChangeFlags.NONE);
}
示例12
private static boolean isTransparent(BlockState blockstate, boolean waterIsTransparent) {
if (blockstate.getType() == BlockTypes.SNOW_LAYER) {
return false;
}
IBlockState iblockstate = (IBlockState)(Object) blockstate;
Optional<MatterProperty> matterProperty = blockstate.getProperty(MatterProperty.class);
if (!waterIsTransparent && matterProperty.isPresent() && matterProperty.get().getValue() == MatterProperty.Matter.LIQUID) {
return false;
}
return !iblockstate.isOpaqueCube();
}
示例13
boolean hasSurfaceFluids() {
Location<World> lesser = this.getLesserBoundaryCorner();
Location<World> greater = this.getGreaterBoundaryCorner();
// don't bother for very large claims, too expensive
if (this.getClaimBlocks() > MAX_AREA) {
return false;
}
int seaLevel = 0; // clean up all fluids in the end
// respect sea level in normal worlds
if (lesser.getExtent().getDimension().getType().equals(DimensionTypes.OVERWORLD)) {
seaLevel = GriefPreventionPlugin.instance.getSeaLevel(lesser.getExtent());
}
for (int x = lesser.getBlockX(); x <= greater.getBlockX(); x++) {
for (int z = lesser.getBlockZ(); z <= greater.getBlockZ(); z++) {
for (int y = seaLevel - 1; y < lesser.getExtent().getDimension().getBuildHeight(); y++) {
// dodge the exclusion claim
BlockState block = lesser.getExtent().getBlock(x, y, z);
if (block.getType() == BlockTypes.WATER || block.getType() == BlockTypes.FLOWING_WATER
|| block.getType() == BlockTypes.LAVA || block.getType() == BlockTypes.FLOWING_LAVA) {
return true;
}
}
}
}
return false;
}
示例14
public static int getBlockStateMeta(BlockState state) {
Integer meta = BLOCKSTATE_META_CACHE.get(state);
if (meta == null) {
Block mcBlock = (net.minecraft.block.Block) state.getType();
meta = mcBlock.getMetaFromState((IBlockState) state);
BLOCKSTATE_META_CACHE.put(state, meta);
}
return meta;
}
示例15
private static boolean validateItemTarget(String target) {
Optional<ItemType> itemType = Sponge.getRegistry().getType(ItemType.class, target);
if (itemType.isPresent()) {
return true;
}
// target could be an item block, so validate blockstate
Optional<BlockState> blockState = Sponge.getRegistry().getType(BlockState.class, target);
if (blockState.isPresent()) {
return true;
}
return false;
}
示例16
private static boolean validateBlockTarget(String target) {
Optional<BlockType> blockType = Sponge.getRegistry().getType(BlockType.class, target);
if (blockType.isPresent()) {
return true;
}
Optional<BlockState> blockState = Sponge.getRegistry().getType(BlockState.class, target);
if (blockState.isPresent()) {
return true;
}
return false;
}
示例17
@Listener(order = Order.FIRST, beforeModifications = true)
public void onFireSpread(ChangeBlockEvent.Place e, @First LocatableBlock locatable) {
RedProtect.get().logger.debug(LogLevel.BLOCKS, "BlockListener - Is onFireSpread event!");
BlockState sourceState = locatable.getBlockState();
if (sourceState.getType() == BlockTypes.FIRE || sourceState.getType() == BlockTypes.LAVA || sourceState.getType() == BlockTypes.FLOWING_LAVA) {
Region r = RedProtect.get().rm.getTopRegion(e.getTransactions().get(0).getOriginal().getLocation().get(), this.getClass().getName());
if (r != null && !r.canFire()) {
RedProtect.get().logger.debug(LogLevel.BLOCKS, "Tryed to PLACE FIRE!");
e.setCancelled(true);
}
}
}
示例18
@Listener(order = Order.FIRST, beforeModifications = true)
public void onFireSpread(ChangeBlockEvent.Break e, @First LocatableBlock locatable) {
RedProtect.get().logger.debug(LogLevel.BLOCKS, "BlockListener - Is onBlockBreakGeneric event");
BlockState sourceState = locatable.getBlockState();
if (sourceState.getType() == BlockTypes.FIRE) {
BlockSnapshot b = e.getTransactions().get(0).getOriginal();
Region r = RedProtect.get().rm.getTopRegion(b.getLocation().get(), this.getClass().getName());
if (r != null && !r.canFire() && b.getState().getType() != BlockTypes.FIRE) {
RedProtect.get().logger.debug(LogLevel.BLOCKS, "Tryed to break from FIRE!");
e.setCancelled(true);
}
}
}
示例19
public BloodEffect(boolean enabled, BlockState state, Vector3d c_offset, Vector3d p_offset, int count) {
this.enabled = enabled;
this.state = state;
this.c_offset = c_offset;
this.p_offset = p_offset;
this.count = count;
}
示例20
@Override
public BloodEffect deserialize(TypeToken<?> type, ConfigurationNode node) throws ObjectMappingException {
Vector3d coffset = node.getNode("center-offset").getValue(TypeToken.of(Vector3d.class));
Vector3d poffset = node.getNode("particle-offset").getValue(TypeToken.of(Vector3d.class));
BlockState state = Serializers.BLOCKSTATE.deserialize(TypeToken.of(BlockState.class), node.getNode("blockstate"));
int count = node.getNode("count").getInt();
boolean enabled = node.getNode("enabled").getBoolean();
return new BloodEffect(enabled, state, coffset, poffset, count);
}
示例21
@Override
public void serialize(TypeToken<?> type, BloodEffect ef, ConfigurationNode node) throws ObjectMappingException {
Serializers.BLOCKSTATE.serialize(TypeToken.of(BlockState.class), ef.getState(), node.getNode("blockstate"));
node.getNode("center-offset").setValue(TypeToken.of(Vector3d.class), ef.getCenterOffset());
node.getNode("count").setValue(ef.getCount());
node.getNode("enabled").setValue(ef.isEnabled());
node.getNode("particle-offset").setValue(TypeToken.of(Vector3d.class), ef.getParticleOffset());
}
示例22
@Override
public BlockState deserialize(TypeToken<?> type, ConfigurationNode node) throws ObjectMappingException {
try {
return Sponge.getRegistry().getType(BlockState.class, node.getString()).get();
} catch (Throwable t) {
throw new ObjectMappingException("Can't deserialize a BlockState", t);
}
}
示例23
@Override
public void serialize(TypeToken<?> type, BlockState obj, ConfigurationNode node) throws ObjectMappingException {
try {
node.setValue(obj.getId());
} catch (Throwable t) {
throw new ObjectMappingException("Can't serialize a BlockState", t);
}
}
示例24
public static boolean matches(BlockState a, BlockState b) {
if (!a.getType().equals(b.getType())) return false;
Set<Key<?>> aKeys = a.getKeys();
for (Key bKey : b.getKeys()) {
if (aKeys.contains(bKey) && a.get(bKey).equals(b.get(bKey))) {
continue;
}
return false;
}
return true;
}
示例25
@ApiModelProperty("The block that we want to change all other blocks into (when using an UPDATE operation")
public BlockState getBlock() {
return block;
}
示例26
@ApiModelProperty("An array of blocks defining what each block in the spanned cube")
public BlockState[][][] getBlocks() {
return blocks;
}
示例27
public BlockStateView(BlockState value) {
super(value);
}
示例28
public BlockStateDeserializer() {
super(BlockState.class);
}
示例29
@Override
public BlockState deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
JsonNode root = p.readValueAsTree();
if (root.path("type").isMissingNode()) {
throw new IOException("Missing block type");
}
String typeStr = root.path("type").isTextual()
? root.path("type").asText()
: root.path("type").path("id").asText();
Optional<BlockType> optType = Sponge.getRegistry().getType(BlockType.class, typeStr);
if (!optType.isPresent()) {
throw new IOException("Invalid block type " + typeStr);
}
BlockType type = optType.get();
BlockState state = type.getDefaultState();
Collection<BlockTrait<?>> traits = type.getTraits();
if (!root.path("data").isMissingNode()) {
Iterator<Map.Entry<String, JsonNode>> it = root.path("data").fields();
while (it.hasNext()) {
Map.Entry<String, JsonNode> entry = it.next();
Optional<BlockTrait<?>> optTrait = traits.stream().filter(t ->
t.getName().equalsIgnoreCase(entry.getKey())
).findAny();
if (!optTrait.isPresent())
throw new IOException("Unknown trait '" + entry.getKey() + "'");
BlockTrait trait = optTrait.get();
Object value = null;
JsonNode nodeValue = entry.getValue();
if (nodeValue.isBoolean()) {
value = nodeValue.asBoolean();
} else if (nodeValue.isInt()) {
value = nodeValue.asInt();
} else if (nodeValue.isTextual()) {
Collection<?> values = trait.getPossibleValues();
Optional<?> val = values.stream()
.filter(v -> v.toString().equalsIgnoreCase(nodeValue.asText()))
.findAny();
if (!val.isPresent()) {
String allowedValues = values.stream()
.map(Object::toString)
.collect(Collectors.joining(", "));
throw new IOException("Trait '" + trait.getName() + "' has value '" +
nodeValue.asText() + "' but can only have one of: " + allowedValues);
} else {
value = val.get();
}
}
Optional<BlockState> newState = state.withTrait(trait, value);
if (!newState.isPresent())
throw new IOException("Could not apply trait '" + trait.getName() + " to block state");
state = newState.get();
}
}
return state;
}
示例30
@JsonDetails
public BlockState[][][] getBlocks() {
return blockStates;
}