Make HolderSet an interface

This commit is contained in:
Nassim Jahnke 2024-03-15 12:45:44 +01:00
parent b6489b8343
commit 936dcafc11
No known key found for this signature in database
GPG Key ID: EF6771C01F6EF02F
6 changed files with 123 additions and 28 deletions

View File

@ -22,18 +22,58 @@
*/
package com.viaversion.viaversion.api.minecraft;
import com.viaversion.viaversion.util.EitherImpl;
/**
* Set of ids that either holds a string tag key or an array of ids.
*/
public final class HolderSet extends EitherImpl<String, int[]> {
public interface HolderSet {
public HolderSet(final String tagKey) {
super(tagKey, null);
/**
* Creates a new holder set for the given tag.
*
* @param tagKey the tag key
* @return a new holder set
*/
static HolderSet of(final String tagKey) {
return new HolderSetImpl(tagKey);
}
public HolderSet(final int[] ids) {
super(null, ids);
/**
* Creates a new holder set for the given ids.
*
* @param ids the direct ids
* @return a new holder set
*/
static HolderSet of(final int[] ids) {
return new HolderSetImpl(ids);
}
/**
* Gets the tag key.
*
* @return the tag key
* @see #hasTagKey()
*/
String tagKey();
/**
* Returns whether this holder set has a tag key.
*
* @return true if this holder set has a tag key, false if it has direct ids
*/
boolean hasTagKey();
/**
* Gets the direct ids.
*
* @return direct ids
* @see #hasIds()
*/
int[] ids();
/**
* Returns whether this holder set has direct ids.
*
* @return true if this holder set has direct ids, false if it has a tag key
*/
boolean hasIds();
}

View File

@ -0,0 +1,56 @@
/*
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
* Copyright (C) 2016-2024 ViaVersion and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.viaversion.viaversion.api.minecraft;
import com.viaversion.viaversion.util.EitherImpl;
final class HolderSetImpl extends EitherImpl<String, int[]> implements HolderSet {
HolderSetImpl(final String tagKey) {
super(tagKey, null);
}
HolderSetImpl(final int[] ids) {
super(null, ids);
}
@Override
public String tagKey() {
return left();
}
@Override
public boolean hasTagKey() {
return isLeft();
}
@Override
public int[] ids() {
return right();
}
@Override
public boolean hasIds() {
return isRight();
}
}

View File

@ -42,7 +42,7 @@ public final class BlockPredicate {
@Override
public void write(final ByteBuf buffer, final BlockPredicate value) throws Exception {
Type.OPTIONAL_HOLDER_SET.write(buffer, value.holders);
Type.OPTIONAL_HOLDER_SET.write(buffer, value.holderSet);
buffer.writeBoolean(value.propertyMatchers != null);
if (value.propertyMatchers != null) {
@ -54,18 +54,18 @@ public final class BlockPredicate {
};
public static final Type<BlockPredicate[]> ARRAY_TYPE = new ArrayType<>(TYPE);
private final HolderSet holders;
private final HolderSet holderSet;
private final StatePropertyMatcher[] propertyMatchers;
private final CompoundTag tag;
public BlockPredicate(@Nullable final HolderSet holders, final StatePropertyMatcher @Nullable [] propertyMatchers, @Nullable final CompoundTag tag) {
this.holders = holders;
public BlockPredicate(@Nullable final HolderSet holderSet, final StatePropertyMatcher @Nullable [] propertyMatchers, @Nullable final CompoundTag tag) {
this.holderSet = holderSet;
this.propertyMatchers = propertyMatchers;
this.tag = tag;
}
public @Nullable HolderSet predicates() {
return holders;
public @Nullable HolderSet holderSet() {
return holderSet;
}
public StatePropertyMatcher @Nullable [] propertyMatchers() {

View File

@ -38,23 +38,23 @@ public class HolderSetType extends Type<HolderSet> {
final int size = Type.VAR_INT.readPrimitive(buffer) - 1;
if (size == -1) {
final String tag = Type.STRING.read(buffer);
return new HolderSet(tag);
return HolderSet.of(tag);
}
final int[] values = new int[size];
for (int i = 0; i < size; i++) {
values[i] = Type.VAR_INT.readPrimitive(buffer);
}
return new HolderSet(values);
return HolderSet.of(values);
}
@Override
public void write(final ByteBuf buffer, final HolderSet object) throws Exception {
if (object.isLeft()) {
if (object.hasTagKey()) {
Type.VAR_INT.writePrimitive(buffer, 0);
Type.STRING.write(buffer, object.left());
Type.STRING.write(buffer, object.tagKey());
} else {
final int[] values = object.right();
final int[] values = object.ids();
Type.VAR_INT.writePrimitive(buffer, values.length + 1);
for (final int value : values) {
Type.VAR_INT.writePrimitive(buffer, value);

View File

@ -452,9 +452,9 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter<Clientboun
return null;
}
holders = new HolderSet(new int[]{Protocol1_20_5To1_20_3.MAPPINGS.getNewBlockId(id)});
holders = HolderSet.of(new int[]{Protocol1_20_5To1_20_3.MAPPINGS.getNewBlockId(id)});
} else {
holders = new HolderSet(identifier.substring(1));
holders = HolderSet.of(identifier.substring(1));
}
final int propertiesEndIndex = rawPredicate.indexOf(']');
@ -468,7 +468,7 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter<Clientboun
final String propertyId = property.substring(0, propertySplitIndex).trim();
final String propertyValue = property.substring(propertySplitIndex + 1).trim();
propertyMatchers.add(new StatePropertyMatcher(propertyId, Either.left(propertyValue))); // TODO Also handle ranged matchers
propertyMatchers.add(new StatePropertyMatcher(propertyId, Either.left(propertyValue)));
}
}

View File

@ -349,17 +349,16 @@ final class StructuredDataConverter {
private static void convertBlockPredicates(final CompoundTag tag, final AdventureModePredicate data, final String key, final int hideFlag) {
final ListTag<StringTag> predicatedListTag = new ListTag<>(StringTag.class);
for (final BlockPredicate predicate : data.predicates()) {
final HolderSet holders = predicate.predicates();
final HolderSet holders = predicate.holderSet();
if (holders == null) {
// Can't do (nicely)
continue;
}
if (holders.isLeft()) {
final String identifier = holders.left();
predicatedListTag.add(serializeBlockPredicate(predicate, identifier));
if (holders.hasTagKey()) {
final String tagKey = "#" + holders.tagKey();
predicatedListTag.add(serializeBlockPredicate(predicate, tagKey));
} else {
for (final int id : holders.right()) {
for (final int id : holders.ids()) {
final int oldId = Protocol1_20_5To1_20_3.MAPPINGS.getOldItemId(id);
final String identifier = Protocol1_20_5To1_20_3.MAPPINGS.itemName(oldId);
predicatedListTag.add(serializeBlockPredicate(predicate, identifier));
@ -377,7 +376,7 @@ final class StructuredDataConverter {
final StringBuilder builder = new StringBuilder(identifier);
if (predicate.propertyMatchers() != null) {
for (final StatePropertyMatcher matcher : predicate.propertyMatchers()) {
// I'm not sure if ranged values were possible in 1.20.4 (if so, there's no trace of how)
// Ranges were introduced in 1.20.5, so only handle the simple case
if (matcher.matcher().isLeft()) {
builder.append(matcher.name()).append('=');
builder.append(matcher.matcher().left());