Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/api-10' into api-11
Browse files Browse the repository at this point in the history
  • Loading branch information
Faithcaio committed Oct 13, 2023
2 parents dc9a3ee + 7ab397a commit 8b6bc57
Show file tree
Hide file tree
Showing 20 changed files with 220 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void callItemDestroyedEvent(
public CriticalHitResult callCriticalHitEvent(
final Player player, final Entity targetEntity, final boolean isCriticalAttack, final float v
) {
final CriticalHitEvent hitResult = ForgeHooks.getCriticalHit(player, targetEntity, isCriticalAttack, v);
final CriticalHitEvent hitResult = ForgeHooks.getCriticalHit(player, targetEntity, isCriticalAttack, v + 1.0F);
if (hitResult != null) {
return new CriticalHitResult(true, hitResult.getDamageModifier() - 1.0F);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,13 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.common.mixin.core.world.level.block.entity;
package org.spongepowered.common.accessor.network;

import net.minecraft.commands.CommandSourceStack;
import org.spongepowered.api.event.Cause;
import net.minecraft.network.PacketSendListener;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.common.bridge.commands.CommandSourceProviderBridge;

@Mixin(targets = "net.minecraft.world.level.block.entity.CommandBlockEntity$1")
public abstract class CommandBlockEntity_Mixin implements CommandSourceProviderBridge {

@Shadow public abstract CommandSourceStack shadow$createCommandSourceStack();

@Override
public CommandSourceStack bridge$getCommandSource(final Cause cause) {
return this.shadow$createCommandSourceStack();
}
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(targets = "net/minecraft/network/Connection$PacketHolder")
public interface Connection_PacketHolderAccessor {
@Accessor("listener") PacketSendListener accessor$listener();
}
1 change: 1 addition & 0 deletions src/accessors/resources/mixins.sponge.accessors.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"commands.arguments.selector.options.EntitySelectorOptions_OptionAccessor",
"commands.arguments.selector.options.EntitySelectorOptionsAccessor",
"entity.passive.AbstractChestedHorseEntityAccessor",
"network.Connection_PacketHolderAccessor",
"network.ConnectionAccessor",
"network.chat.HoverEvent_ItemStackInfoAccessor",
"network.chat.StyleAccessor",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ private static void setInternal(Tag base, byte type, DataView view, String key)
checkNotNull(view);
checkNotNull(key);
checkArgument(!key.isEmpty());
checkArgument(type > Constants.NBT.TAG_END && type <= Constants.NBT.TAG_INT_ARRAY);
checkArgument(type > Constants.NBT.TAG_END && type <= Constants.NBT.TAG_LONG_ARRAY);
switch (type) {
case Constants.NBT.TAG_BYTE:
if (key.contains(NBTTranslator.BOOLEAN_IDENTIFIER)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import net.minecraft.world.item.PickaxeItem;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.AbstractFurnaceBlockEntity;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.api.block.BlockType;
import org.spongepowered.api.data.Keys;
import org.spongepowered.api.effect.potion.PotionEffect;
Expand Down Expand Up @@ -122,10 +123,7 @@ public static void register(final DataProviderRegistrator registrator) {
tag.putInt(Constants.Item.CUSTOM_MODEL_DATA, v);
})
.delete(h -> {
final CompoundTag tag = h.getTag();
if (tag != null) {
tag.remove(Constants.Item.CUSTOM_MODEL_DATA);
}
h.removeTagKey(Constants.Item.CUSTOM_MODEL_DATA);
})
.create(Keys.CUSTOM_NAME)
.get(h -> {
Expand Down Expand Up @@ -211,18 +209,21 @@ private static void setIsUnbrekable(final ItemStack stack, final Boolean value)
if (value == null || (!value && !stack.hasTag())) {
return;
}
final CompoundTag tag = stack.getOrCreateTag();
if (value) {
final CompoundTag tag = stack.getOrCreateTag();
tag.putBoolean(Constants.Item.ITEM_UNBREAKABLE, true);
} else {
tag.remove(Constants.Item.ITEM_UNBREAKABLE);
stack.removeTagKey(Constants.Item.ITEM_UNBREAKABLE);
}
}

private static void deleteLore(final ItemStack stack) {
final CompoundTag tag = stack.getTag();
if (tag != null && tag.contains(Constants.Item.ITEM_DISPLAY)) {
tag.getCompound(Constants.Item.ITEM_DISPLAY).remove(Constants.Item.ITEM_LORE);
final @Nullable CompoundTag tag = stack.getTagElement(Constants.Item.ITEM_DISPLAY);
if (tag != null) {
tag.remove(Constants.Item.ITEM_LORE);
if (tag.isEmpty()) {
stack.removeTagKey(Constants.Item.ITEM_DISPLAY);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* This file is part of Sponge, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) 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 org.spongepowered.common.world.level.chunk;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtIo;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.chunk.storage.RegionFile;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.api.data.persistence.DataContainer;
import org.spongepowered.api.world.chunk.OfflineChunk;
import org.spongepowered.common.data.persistence.NBTTranslator;
import org.spongepowered.math.vector.Vector3i;

import java.io.DataInputStream;
import java.io.IOException;

public final class SpongeOfflineChunk implements OfflineChunk {

private final CompoundTag nbt;
private final Vector3i chunkPos;

public SpongeOfflineChunk(final CompoundTag nbt, final int cx, final int cz) {
this.nbt = nbt;
this.chunkPos = new Vector3i(cx, 0, cz);
}

@Nullable
public static OfflineChunk of(final RegionFile regionFile, final ChunkPos pos) {
CompoundTag chunkNbt;
try (DataInputStream $$2 = regionFile.getChunkDataInputStream(pos)) {
if ($$2 == null) {
return null;
}

chunkNbt = NbtIo.read($$2);
} catch (IOException e) {
throw new RuntimeException(e);
}

return new SpongeOfflineChunk(chunkNbt, pos.x, pos.z);
}

@Override
public org.spongepowered.math.vector.Vector3i chunkPosition() {
return this.chunkPos;
}

@Override
public int contentVersion() {
return 2; // TODO get from actual data?, assuming compression type is 2
}

@Override
public DataContainer toContainer() {
return NBTTranslator.INSTANCE.translate(this.nbt);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ public CompletableFuture<Optional<ServerWorldProperties>> loadProperties(final R
((PrimaryLevelDataBridge) levelData).bridge$populateFromLevelStem(scratch);
}

((ResourceKeyBridge) levelData).bridge$setKey(key);
return CompletableFuture.completedFuture(Optional.of((ServerWorldProperties) levelData));
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.raid.Raid;
import net.minecraft.world.entity.raid.Raids;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.CollisionGetter;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraft.world.level.chunk.storage.RegionFile;
import net.minecraft.world.level.entity.PersistentEntitySectionManager;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.world.level.storage.LevelResource;
Expand All @@ -56,6 +58,7 @@
import org.spongepowered.api.world.BlockChangeFlag;
import org.spongepowered.api.world.SerializationBehavior;
import org.spongepowered.api.world.border.WorldBorder;
import org.spongepowered.api.world.chunk.OfflineChunk;
import org.spongepowered.api.world.chunk.WorldChunk;
import org.spongepowered.api.world.generation.ChunkGenerator;
import org.spongepowered.api.world.server.ChunkManager;
Expand All @@ -66,26 +69,35 @@
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.common.SpongeCommon;
import org.spongepowered.common.accessor.world.entity.raid.RaidsAccessor;
import org.spongepowered.common.bridge.server.level.ServerLevelBridge;
import org.spongepowered.common.bridge.world.level.border.WorldBorderBridge;
import org.spongepowered.common.bridge.world.level.storage.PrimaryLevelDataBridge;
import org.spongepowered.common.data.holder.SpongeServerLocationBaseDataHolder;
import org.spongepowered.common.mixin.api.minecraft.world.level.LevelMixin_API;
import org.spongepowered.common.util.VecHelper;
import org.spongepowered.common.world.level.chunk.SpongeOfflineChunk;
import org.spongepowered.common.world.storage.SpongeChunkLayout;
import org.spongepowered.math.vector.Vector3d;
import org.spongepowered.math.vector.Vector3i;
import org.spongepowered.math.vector.Vector4i;

import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import javax.annotation.Nonnull;
Expand Down Expand Up @@ -182,7 +194,7 @@ public BlockSnapshot createSnapshot(final int x, final int y, final int z) {

@Override
public boolean restoreSnapshot(final BlockSnapshot snapshot, final boolean force, final BlockChangeFlag flag) {
return snapshot.restore(force, Objects.requireNonNull(flag, "flag"));
return Objects.requireNonNull(snapshot, "snapshot").withLocation(this.location(snapshot.position())).restore(force, Objects.requireNonNull(flag, "flag"));
}

@Override
Expand Down Expand Up @@ -319,4 +331,68 @@ public ChunkManager chunkManager() {
return (ChunkManager) this.shadow$getChunkSource().chunkMap;
}

public <T> Stream<T> api$chunkPosStream(BiFunction<RegionFile, Stream<ChunkPos>, Stream<T>> mapper) {
final Path dimensionPath = ((ServerLevelBridge) this).bridge$getLevelSave().getDimensionPath(this.shadow$dimension());
final Path regionPath = dimensionPath.resolve("region");

return Stream
.generate(() -> {
try {
// open directory stream over all region files of this world
return Stream.of(Files.newDirectoryStream(regionPath, "*.mca"));
} catch (IOException ex) {
SpongeCommon.logger().error("Could not find region files", ex);
return Stream.<DirectoryStream<Path>>empty();
}
})
.limit(1)
.flatMap(Function.identity())
.flatMap(stream -> StreamSupport.stream(stream.spliterator(), false)
.flatMap(path -> {
try { // For every region file
RegionFile regionFile = new RegionFile(path, regionPath, true);
final Vector4i regionBound = this.api$pathToRegionPos(path);
// Find all chunks in bounds
final Stream<ChunkPos> chunkPosStream = IntStream.range(regionBound.x(), regionBound.z())
.mapToObj(x -> IntStream.range(regionBound.y(), regionBound.w()).
mapToObj(z -> new ChunkPos(x, z)))
.flatMap(Function.identity());
return mapper.apply(regionFile, chunkPosStream).onClose(() -> this.api$close(regionFile));
} catch (IOException ignored) {
return Stream.empty();
}
})
.onClose(() -> this.api$close(stream)));
}

@Override
public Stream<Vector3i> chunkPositions() {
return this.api$chunkPosStream((regionFile, stream) ->
stream.filter(regionFile::doesChunkExist) // filter out non-existent chunks
.map(cp -> new Vector3i(cp.x, 0, cp.z)) // map to API type
);
}

@Override
public Stream<OfflineChunk> offlineChunks() {
return this.api$chunkPosStream((regionFile, stream) ->
stream.map(cp -> SpongeOfflineChunk.of(regionFile, cp)) // map to API type
.filter(Objects::nonNull)); // filter out non-existent chunks
}

private void api$close(final AutoCloseable closeable) {
try {
closeable.close();
} catch (Exception ignored) {
}
}

private Vector4i api$pathToRegionPos(Path regionPath) {
final String[] split = regionPath.getFileName().toString().split("\\.");
final int rx = Integer.parseInt(split[1]);
final int ry = Integer.parseInt(split[2]);
final ChunkPos min = ChunkPos.minFromRegion(rx, ry);
final ChunkPos max = ChunkPos.maxFromRegion(rx, ry);
return new Vector4i(min.x, min.z, max.x, max.z);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -395,4 +395,9 @@ public HoverEvent<HoverEvent.ShowEntity> asHoverEvent(final UnaryOperator<HoverE

return values;
}

@Override
public net.kyori.adventure.text.Component teamRepresentation() {
return net.kyori.adventure.text.Component.text(this.shadow$getUUID().toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import static java.util.Objects.requireNonNull;

import com.google.common.base.Preconditions;
import net.kyori.adventure.text.Component;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.AttributeInstance;
Expand Down Expand Up @@ -56,11 +55,6 @@ public abstract class LivingEntityMixin_API extends EntityMixin_API implements L
@Shadow public abstract boolean addEffect(MobEffectInstance p_195064_1_);
// @formatter:on

@Override
public Component teamRepresentation() {
return Component.text(this.shadow$getUUID().toString());
}

@Override
public Optional<Attribute> attribute(final AttributeType type) {
Preconditions.checkNotNull(type, "AttributeType cannot be null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@
public abstract class MinecartCommandBlockMixin_API extends AbstractMinecartMixin_API implements CommandBlockMinecart {

// @formatter:off
@Shadow public abstract BaseCommandBlock getCommandBlock();
@Shadow public abstract BaseCommandBlock shadow$getCommandBlock();
// @formatter:on

@Override
public String identifier() {
return this.getCommandBlock().getName().getString();
return this.shadow$getCommandBlock().getName().getString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@
*/
package org.spongepowered.common.mixin.api.minecraft.world.level.block.entity;

import net.kyori.adventure.audience.MessageType;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.minecraft.world.level.BaseCommandBlock;
import net.minecraft.world.level.block.entity.CommandBlockEntity;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.spongepowered.api.block.entity.CommandBlock;
import org.spongepowered.api.data.persistence.DataContainer;
import org.spongepowered.api.data.value.Value;
Expand Down Expand Up @@ -84,4 +88,9 @@ public DataContainer toContainer() {
public String identifier() {
return this.shadow$getCommandBlock().getName().getString();
}

@Override
public void sendMessage(final @NonNull Identity identity, final @NonNull Component message, final @NonNull MessageType type) {
this.shadow$getCommandBlock().sendSystemMessage(SpongeAdventure.asVanilla(message));
}
}
Loading

0 comments on commit 8b6bc57

Please sign in to comment.