2021-03-22 23:06:40 +01:00
|
|
|
/*
|
|
|
|
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
|
2023-01-12 12:45:53 +01:00
|
|
|
* Copyright (C) 2016-2023 ViaVersion and contributors
|
2021-03-22 23:06:40 +01:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
2021-04-26 20:52:34 +02:00
|
|
|
package com.viaversion.viaversion.protocols.protocol1_9to1_8.storage;
|
2016-11-02 18:14:57 +01:00
|
|
|
|
2017-05-14 14:34:51 +02:00
|
|
|
import com.github.steveice10.opennbt.tag.builtin.ByteTag;
|
|
|
|
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
|
2021-05-26 21:35:28 +02:00
|
|
|
import com.viaversion.viaversion.api.connection.StorableObject;
|
2021-04-26 20:52:34 +02:00
|
|
|
import com.viaversion.viaversion.api.minecraft.Position;
|
2021-04-27 13:41:39 +02:00
|
|
|
import com.viaversion.viaversion.util.Pair;
|
2016-11-02 18:14:57 +01:00
|
|
|
import java.util.Map;
|
2019-09-21 11:57:28 +02:00
|
|
|
import java.util.Optional;
|
2016-11-02 18:14:57 +01:00
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
2021-05-26 21:35:28 +02:00
|
|
|
public class CommandBlockStorage implements StorableObject {
|
2020-04-13 20:58:24 +02:00
|
|
|
private final Map<Pair<Integer, Integer>, Map<Position, CompoundTag>> storedCommandBlocks = new ConcurrentHashMap<>();
|
2023-10-08 12:19:48 +02:00
|
|
|
private boolean permissions;
|
2016-11-02 18:14:57 +01:00
|
|
|
|
|
|
|
public void unloadChunk(int x, int z) {
|
|
|
|
Pair<Integer, Integer> chunkPos = new Pair<>(x, z);
|
2018-09-30 20:48:23 +02:00
|
|
|
storedCommandBlocks.remove(chunkPos);
|
2016-11-02 18:14:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void addOrUpdateBlock(Position position, CompoundTag tag) {
|
|
|
|
Pair<Integer, Integer> chunkPos = getChunkCoords(position);
|
|
|
|
|
2023-10-08 12:19:48 +02:00
|
|
|
if (!storedCommandBlocks.containsKey(chunkPos)) {
|
2019-11-22 19:13:02 +01:00
|
|
|
storedCommandBlocks.put(chunkPos, new ConcurrentHashMap<>());
|
2023-10-08 12:19:48 +02:00
|
|
|
}
|
2016-11-02 18:14:57 +01:00
|
|
|
|
|
|
|
Map<Position, CompoundTag> blocks = storedCommandBlocks.get(chunkPos);
|
|
|
|
|
2023-10-08 12:19:48 +02:00
|
|
|
if (blocks.containsKey(position) && blocks.get(position).equals(tag)) {
|
|
|
|
return;
|
|
|
|
}
|
2016-11-02 18:14:57 +01:00
|
|
|
|
|
|
|
blocks.put(position, tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Pair<Integer, Integer> getChunkCoords(Position position) {
|
2021-08-28 22:15:28 +02:00
|
|
|
int chunkX = Math.floorDiv(position.x(), 16);
|
|
|
|
int chunkZ = Math.floorDiv(position.z(), 16);
|
2016-11-02 18:14:57 +01:00
|
|
|
|
|
|
|
return new Pair<>(chunkX, chunkZ);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Optional<CompoundTag> getCommandBlock(Position position) {
|
|
|
|
Pair<Integer, Integer> chunkCoords = getChunkCoords(position);
|
|
|
|
|
|
|
|
Map<Position, CompoundTag> blocks = storedCommandBlocks.get(chunkCoords);
|
2018-09-30 20:48:23 +02:00
|
|
|
if (blocks == null)
|
2019-09-21 11:57:28 +02:00
|
|
|
return Optional.empty();
|
2016-11-02 18:14:57 +01:00
|
|
|
|
2018-09-30 21:19:18 +02:00
|
|
|
CompoundTag tag = blocks.get(position);
|
|
|
|
if (tag == null)
|
2019-09-21 11:57:28 +02:00
|
|
|
return Optional.empty();
|
2018-09-30 21:19:18 +02:00
|
|
|
|
2023-12-31 13:38:14 +01:00
|
|
|
tag = tag.copy();
|
2021-03-14 16:57:37 +01:00
|
|
|
tag.put("powered", new ByteTag((byte) 0));
|
|
|
|
tag.put("auto", new ByteTag((byte) 0));
|
|
|
|
tag.put("conditionMet", new ByteTag((byte) 0));
|
2016-11-02 18:14:57 +01:00
|
|
|
return Optional.of(tag);
|
|
|
|
}
|
2016-11-06 11:30:28 +01:00
|
|
|
|
|
|
|
public void unloadChunks() {
|
|
|
|
storedCommandBlocks.clear();
|
|
|
|
}
|
2020-04-13 20:58:24 +02:00
|
|
|
|
|
|
|
public boolean isPermissions() {
|
|
|
|
return permissions;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPermissions(boolean permissions) {
|
|
|
|
this.permissions = permissions;
|
|
|
|
}
|
2016-11-02 18:14:57 +01:00
|
|
|
}
|