/
BlockApiCache.java
105 lines (91 loc) · 3.76 KB
/
BlockApiCache.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.fabricmc.fabric.api.lookup.v1.block;
import java.util.Objects;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
import net.fabricmc.fabric.impl.lookup.block.BlockApiCacheImpl;
import net.fabricmc.fabric.impl.lookup.block.BlockApiLookupImpl;
/**
* A {@link BlockApiLookup} bound to a {@link ServerWorld} and a position, providing much faster API access.
* Refer to {@link BlockApiLookup} for example code.
*
* <p>This object caches the block entity at the target position, and the last used API provider, removing those queries.
* If a block entity is available or if the block state is passed as a parameter, the block state doesn't have to be looked up either.
*
* @param <A> The type of the API.
* @param <C> The type of the additional context object.
* @see BlockApiLookup
*/
@ApiStatus.NonExtendable
public interface BlockApiCache<A, C> {
/**
* Attempt to retrieve an API from a block in the world, using the world and the position passed at creation time.
*
* <p>Note: If the block state is known, it is more efficient to use {@link BlockApiCache#find(BlockState, Object)}.
*
* @param context Additional context for the query, defined by type parameter C.
* @return The retrieved API, or {@code null} if no API was found.
*/
@Nullable
default A find(C context) {
return find(null, context);
}
/**
* Attempt to retrieve an API from a block in the world, using the world and the position passed at creation time.
*
* @param state The block state at the target position, or null if unknown.
* @param context Additional context for the query, defined by type parameter C.
* @return The retrieved API, or {@code null} if no API was found.
*/
@Nullable
A find(@Nullable BlockState state, C context);
/**
* Return the block entity at the target position of this lookup.
*
* <p>This is the most efficient way to query the block entity at the target position repeatedly:
* unless the block entity has been loaded or unloaded since the last query, the result will be cached.
*/
@Nullable
BlockEntity getBlockEntity();
/**
* Return the lookup this cache is bound to.
*/
BlockApiLookup<A, C> getLookup();
/**
* Return the world this cache is bound to.
*/
ServerWorld getWorld();
/**
* Return the position this cache is bound to.
*/
BlockPos getPos();
/**
* Create a new instance bound to the passed {@link ServerWorld} and position, and querying the same API as the passed lookup.
*/
static <A, C> BlockApiCache<A, C> create(BlockApiLookup<A, C> lookup, ServerWorld world, BlockPos pos) {
Objects.requireNonNull(pos, "BlockPos may not be null.");
Objects.requireNonNull(world, "ServerWorld may not be null.");
if (!(lookup instanceof BlockApiLookupImpl)) {
throw new IllegalArgumentException("Cannot cache foreign implementation of BlockApiLookup. Use `BlockApiLookup#get(Identifier, Class<A>, Class<C>);` to get instances.");
}
return new BlockApiCacheImpl<>((BlockApiLookupImpl<A, C>) lookup, world, pos);
}
}