Skip to content

Commit

Permalink
Cleaned up code
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphiMC committed Apr 9, 2024
1 parent 87cb125 commit 9fcdaac
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
*/
package net.raphimc.vialegacy.api.remapper;

import com.viaversion.viaversion.api.connection.StoredObject;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.connection.StorableObject;
import com.viaversion.viaversion.api.minecraft.Position;
import com.viaversion.viaversion.api.minecraft.chunks.*;
import com.viaversion.viaversion.libs.fastutil.ints.Int2IntMap;
Expand All @@ -32,16 +31,14 @@
import java.util.HashMap;
import java.util.Map;

public abstract class AbstractChunkTracker extends StoredObject {
public abstract class AbstractChunkTracker implements StorableObject {

private final Map<ChunkCoord, Chunk> chunks = new HashMap<>();
private final IntSet toTrack = new IntOpenHashSet();
private final boolean trackAll;
private final Int2IntMap replacements = new Int2IntOpenHashMap();

public AbstractChunkTracker(final UserConnection user, final int... toTrack) {
super(user);

public AbstractChunkTracker(final int... toTrack) {
for (final int trackedBlock : toTrack) {
this.toTrack.add(trackedBlock);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

public class MetadataRewriter {

public static void transform(EntityTypes1_10.EntityType type, List<Metadata> list) {
public static void transform(final EntityTypes1_10.EntityType type, final List<Metadata> list) {
for (Metadata entry : new ArrayList<>(list)) {
final MetaIndex1_6_1to1_5_2 metaIndex = MetaIndex1_6_1to1_5_2.searchIndex(type, entry.id());
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
public class ChunkTracker extends AbstractChunkTracker {

private static final Map<Integer, Boolean> SOLID_BLOCKS = new HashMap<>();
private final boolean b173;

static {
SOLID_BLOCKS.put(0, false);
Expand Down Expand Up @@ -206,7 +207,8 @@ public class ChunkTracker extends AbstractChunkTracker {
}

public ChunkTracker(final UserConnection user) {
super(user, 0);
super(0);
this.b173 = user.getProtocolInfo().serverProtocolVersion().olderThanOrEqualTo(LegacyProtocolVersion.b1_7tob1_7_3);

this.registerReplacement(new IdAndData(BlockList1_6.signWall.blockID, 0), new IdAndData(BlockList1_6.signWall.blockID, 3));
for (int i = 0; i < 16; i++) {
Expand Down Expand Up @@ -239,7 +241,7 @@ public ChunkTracker(final UserConnection user) {

@Override
protected void remapBlock(final IdAndData block, final int x, final int y, final int z) {
if (block.id == BlockList1_6.chest.blockID && this.getUser().getProtocolInfo().serverProtocolVersion().olderThanOrEqualTo(LegacyProtocolVersion.b1_7tob1_7_3)) {
if (this.b173 && block.id == BlockList1_6.chest.blockID) {
byte blockData = 3;
int rot1 = this.getBlockNotNull(x, y, z - 1).id;
int rot2 = this.getBlockNotNull(x, y, z + 1).id;
Expand Down Expand Up @@ -307,7 +309,7 @@ protected void remapBlock(final IdAndData block, final int x, final int y, final

@Override
protected void postRemap(DataPalette palette) {
if (this.getUser().getProtocolInfo().serverProtocolVersion().olderThanOrEqualTo(LegacyProtocolVersion.b1_7tob1_7_3)) {
if (this.b173) {
palette.replaceId(BlockList1_6.chest.blockID << 4, 0);
palette.replaceId(BlockList1_6.chest.blockID << 4 | 1, 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public void register() {
wrapper.write(Type.SHORT, (short) currentItem.identifier());

final List<Metadata> metadata = wrapper.read(Types1_7_6.METADATA_LIST); // metadata
metadataRewriter.transform(EntityTypes1_10.EntityType.PLAYER, metadata);
metadataRewriter.transform(wrapper.user(), EntityTypes1_10.EntityType.PLAYER, metadata);
wrapper.write(Types1_8.METADATA_LIST, metadata);

tablistStorage.sendTempEntry(tempTabEntry);
Expand Down Expand Up @@ -352,7 +352,7 @@ public void register() {
tracker.updateEntityLocation(entityID, x, y, z, false);
tracker.updateEntityMetadata(entityID, metadataList);

metadataRewriter.transform(entityType, metadataList);
metadataRewriter.transform(wrapper.user(), entityType, metadataList);
});
}
});
Expand Down Expand Up @@ -558,7 +558,7 @@ public void register() {
final int entityID = wrapper.get(Type.VAR_INT, 0);
if (tracker.getTrackedEntities().containsKey(entityID)) {
tracker.updateEntityMetadata(entityID, metadataList);
metadataRewriter.transform(tracker.getTrackedEntities().get(entityID), metadataList);
metadataRewriter.transform(wrapper.user(), tracker.getTrackedEntities().get(entityID), metadataList);
if (metadataList.isEmpty()) wrapper.cancel();
} else {
wrapper.cancel();
Expand Down Expand Up @@ -1488,7 +1488,7 @@ public void init(UserConnection userConnection) {
userConnection.put(new EntityTracker(userConnection));
userConnection.put(new MapStorage());
userConnection.put(new DimensionTracker());
userConnection.put(new ChunkTracker(userConnection));
userConnection.put(new ChunkTracker());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
*/
package net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.metadata;

import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_10;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.minecraft.metadata.Metadata;
import net.raphimc.vialegacy.ViaLegacy;
import net.raphimc.vialegacy.api.model.IdAndData;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.Protocol1_8to1_7_6_10;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.storage.ChunkTracker;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -35,7 +38,7 @@ public MetadataRewriter(final Protocol1_8to1_7_6_10 protocol) {
this.protocol = protocol;
}

public void transform(EntityTypes1_10.EntityType type, List<Metadata> list) {
public void transform(final UserConnection user, final EntityTypes1_10.EntityType type, final List<Metadata> list) {
for (Metadata entry : new ArrayList<>(list)) {
final MetaIndex1_8to1_7_6 metaIndex = MetaIndex1_8to1_7_6.searchIndex(type, entry.id());
try {
Expand Down Expand Up @@ -67,7 +70,9 @@ public void transform(EntityTypes1_10.EntityType type, List<Metadata> list) {
}
}
final byte data = blockDataMeta != null ? (Byte) blockDataMeta.getValue() : 0;
entry.setValue((short) (id | (data << 12)));
final IdAndData block = new IdAndData(id, data);
user.get(ChunkTracker.class).remapBlockParticle(block);
entry.setValue((short) (block.id | (block.data << 12)));
continue;
} else if (metaIndex == MetaIndex1_8to1_7_6.HUMAN_SKIN_FLAGS) {
byte flags = (byte) value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ private List<Metadata> get1_8Metadata() {
for (final Map.Entry<MetaIndex1_8to1_7_6, Object> entry : this.metadata.entrySet()) {
metadataList.add(new Metadata(entry.getKey().getOldIndex(), entry.getKey().getOldType(), entry.getValue()));
}
Via.getManager().getProtocolManager().getProtocol(Protocol1_8to1_7_6_10.class).getMetadataRewriter().transform(this.entityType, metadataList);
Via.getManager().getProtocolManager().getProtocol(Protocol1_8to1_7_6_10.class).getMetadataRewriter().transform(this.user, this.entityType, metadataList);
return metadataList;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@
*/
package net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.storage;

import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.chunks.DataPalette;
import net.raphimc.vialegacy.api.data.BlockList1_6;
import net.raphimc.vialegacy.api.model.IdAndData;
import net.raphimc.vialegacy.api.remapper.AbstractChunkTracker;

public class ChunkTracker extends AbstractChunkTracker {

public ChunkTracker(final UserConnection user) {
super(user, BlockList1_6.obsidian.blockID, BlockList1_6.portal.blockID);
public ChunkTracker() {
super(BlockList1_6.obsidian.blockID, BlockList1_6.portal.blockID);

for (int i = 9; i < 16; i++) { // double plant
this.registerReplacement(new IdAndData(175, i), new IdAndData(175, 0));
Expand Down Expand Up @@ -57,7 +56,7 @@ protected void remapBlock(IdAndData block, int x, int y, int z) {
protected void postRemap(DataPalette palette) {
palette.replaceId(BlockList1_6.portal.blockID << 4, 0);
}

private void registerInvalidDirectionReplacements(final int blockId, final IdAndData replacement) {
for (int i = 0; i < 2; i++) {
this.registerReplacement(new IdAndData(blockId, i), replacement);
Expand Down

0 comments on commit 9fcdaac

Please sign in to comment.