forked from QuiltServerTools/HeyThatsMine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInteractionManager.java
More file actions
192 lines (166 loc) · 8.14 KB
/
InteractionManager.java
File metadata and controls
192 lines (166 loc) · 8.14 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package com.github.fabricservertools.htm.interactions;
import com.github.fabricservertools.htm.HTMContainerLock;
import com.github.fabricservertools.htm.api.LockInteraction;
import com.github.fabricservertools.htm.api.LockableObject;
import com.mojang.authlib.GameProfile;
import eu.pb4.common.protection.api.ProtectionProvider;
import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import it.unimi.dsi.fastutil.objects.ObjectSet;
import net.minecraft.block.ChestBlock;
import net.minecraft.block.DoubleBlockProperties;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.ChestBlockEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.Text;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Box;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;
import java.util.Optional;
import java.util.UUID;
public class InteractionManager implements ProtectionProvider {
public static final Object2ObjectMap<ServerPlayerEntity, LockInteraction> pendingActions = new Object2ObjectOpenHashMap<>();
public static final ObjectSet<UUID> persisting = new ObjectOpenHashSet<>();
public static final ObjectSet<UUID> noMessage = new ObjectOpenHashSet<>();
private static final DoubleBlockProperties.PropertyRetriever<BlockEntity, LockableObject> LOCKABLE_RETRIEVER = new DoubleBlockProperties.PropertyRetriever<>() {
@Override
public LockableObject getFromBoth(BlockEntity first, BlockEntity second) {
if (first instanceof LockableObject lockable) {
if (lockable.getLock().isPresent()) {
return lockable;
} else if (second instanceof LockableObject secondLockable && secondLockable.getLock().isPresent()) {
return secondLockable;
}
return lockable;
}
return null;
}
@Override
public LockableObject getFrom(BlockEntity single) {
if (single instanceof LockableObject lockable) {
return lockable;
}
return null;
}
@Override
public LockableObject getFallback() {
return null;
}
};
private static final DoubleBlockProperties.PropertyRetriever<BlockEntity, LockableObject> UNLOCKED_LOCKABLE_RETRIEVER = new DoubleBlockProperties.PropertyRetriever<>() {
@Override
public LockableObject getFromBoth(BlockEntity first, BlockEntity second) {
if (first instanceof LockableObject lockable) {
if (lockable.getLock().isEmpty()) {
return lockable;
} else if (second instanceof LockableObject secondLockable && secondLockable.getLock().isEmpty()) {
return secondLockable;
}
}
return null;
}
@Override
public LockableObject getFrom(BlockEntity single) {
if (single instanceof LockableObject lockable && lockable.getLock().isEmpty()) {
return lockable;
}
return null;
}
@Override
public LockableObject getFallback() {
return null;
}
};
public static void execute(MinecraftServer server, ServerPlayerEntity player, BlockPos pos) {
LockInteraction action = pendingActions.get(player);
Optional<LockableObject> lockableObject = getLockable(player, pos);
lockableObject.ifPresentOrElse(object -> {
Optional<HTMContainerLock> containerLock = object.getLock();
if (action.requiresLock()) {
containerLock.ifPresentOrElse(
lock -> action.execute(server, player, pos, object, lock),
() -> player.sendMessage(Text.translatable("text.htm.error.no_lock"), false));
} else {
action.execute(server, player, pos, object, containerLock.orElse(null));
}
}, () -> player.sendMessage(Text.translatable("text.htm.error.unlockable"), false));
if (!persisting.contains(player.getUuid())) {
pendingActions.remove(player);
}
}
public static boolean canOpen(ServerPlayerEntity player, BlockPos pos) {
return getLock(player, pos).map(lock -> lock.canOpen(player)).orElse(true);
}
public static Optional<HTMContainerLock> getLock(ServerPlayerEntity player, BlockPos pos) {
return getLockable(player, pos).flatMap(LockableObject::getLock);
}
public static Optional<LockableObject> getLockable(ServerPlayerEntity player, BlockPos pos) {
return getLockable(player.getWorld(), pos);
}
public static Optional<HTMContainerLock> getLock(ServerWorld world, BlockPos pos) {
return getLockable(world, pos).flatMap(LockableObject::getLock);
}
public static Optional<LockableObject> getLockable(ServerWorld world, BlockPos pos) {
BlockEntity blockEntity = world.getBlockEntity(pos);
if (blockEntity == null) {
return Optional.empty();
}
return getLockable(world, pos, blockEntity);
}
public static Optional<HTMContainerLock> getLock(ServerWorld world, BlockPos pos, BlockEntity blockEntity) {
return getLockable(world, pos, blockEntity).flatMap(LockableObject::getLock);
}
public static Optional<LockableObject> getLockable(ServerWorld world, BlockPos pos, BlockEntity blockEntity) {
if (blockEntity instanceof ChestBlockEntity chest) {
DoubleBlockProperties.PropertySource<? extends BlockEntity> propertySource = DoubleBlockProperties.toPropertySource(chest.getType(), ChestBlock::getDoubleBlockType,
ChestBlock::getFacing, ChestBlock.FACING, world.getBlockState(pos), world, pos, (access, blockPos) -> false);
return Optional.ofNullable(propertySource.apply(LOCKABLE_RETRIEVER));
} else if (blockEntity instanceof LockableObject lockable) {
return Optional.of(lockable);
}
return Optional.empty();
}
public static Optional<LockableObject> getUnlockedLockable(ServerWorld world, BlockPos pos, BlockEntity blockEntity) {
if (blockEntity instanceof ChestBlockEntity chest) {
DoubleBlockProperties.PropertySource<? extends BlockEntity> propertySource = DoubleBlockProperties.toPropertySource(chest.getType(), ChestBlock::getDoubleBlockType,
ChestBlock::getFacing, ChestBlock.FACING, world.getBlockState(pos), world, pos, (access, blockPos) -> false);
return Optional.ofNullable(propertySource.apply(UNLOCKED_LOCKABLE_RETRIEVER));
} else if (blockEntity instanceof LockableObject lockable && lockable.getLock().isEmpty()) {
return Optional.of(lockable);
}
return Optional.empty();
}
public static void togglePersist(ServerPlayerEntity player) {
if (persisting.contains(player.getUuid())) {
persisting.remove(player.getUuid());
} else {
persisting.add(player.getUuid());
}
}
public static void toggleNoMessage(ServerPlayerEntity player) {
if (noMessage.contains(player.getUuid())) {
noMessage.remove(player.getUuid());
} else {
noMessage.add(player.getUuid());
}
}
@Override
public boolean isProtected(World world, BlockPos pos) {
var lock = InteractionManager.getLockable((ServerWorld) world, pos);
return lock.isPresent();
}
@Override
public boolean canBreakBlock(World world, BlockPos pos, GameProfile profile, @Nullable PlayerEntity player) {
var lockable = InteractionManager.getLockable((ServerWorld) world, pos);
return lockable.flatMap(LockableObject::getLock).map(htmContainerLock -> htmContainerLock.owner().equals(profile.getId())).orElse(true);
}
@Override
public boolean isAreaProtected(World world, Box area) {
return false;
}
}