Skip to content

Commit

Permalink
Support Minecraft 1.21(.1)
Browse files Browse the repository at this point in the history
Signed-off-by: Hendrix-Shen <[email protected]>
  • Loading branch information
Hendrix-Shen committed Aug 18, 2024
1 parent 9164bd3 commit c2f7a3b
Show file tree
Hide file tree
Showing 25 changed files with 246 additions and 142 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ preprocess {
Node mc12002 = createNode("1.20.2-fabric", 1_20_02, "mojang")
Node mc12004 = createNode("1.20.4-fabric", 1_20_04, "mojang")
Node mc12006 = createNode("1.20.6-fabric", 1_20_06, "mojang")
Node mc12101 = createNode("1.21.1-fabric", 1_21_01, "mojang")

mc11404.link(mc11502, null)
mc11502.link(mc11605, null)
Expand All @@ -32,6 +33,7 @@ preprocess {
mc12001.link(mc12002, file("versions/mapping-1.20.1-1.20.2.txt"))
mc12002.link(mc12004, null)
mc12004.link(mc12006, null)
mc12006.link(mc12101, null)
}

ext {
Expand Down
15 changes: 5 additions & 10 deletions common.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ preprocess {
repositories {
mavenLocal()

maven {
name("NyanMaven Mirror")
url("https://maven.hendrixshen.top/mirror")
}

maven {
name("Curse Maven")
url("https://www.cursemaven.com")
Expand All @@ -37,16 +42,6 @@ repositories {
}
}

maven {
name("Masa Maven")
url("https://masa.dy.fi/maven")
}

maven {
name("JitPack")
url("https://www.jitpack.io")
}

mavenCentral()
}

Expand Down
12 changes: 6 additions & 6 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ mod.sources=https://github.com/plusls/plusls-carpet-addition
mod.version=0.3

# Required Libraries
# MagicLib - 0.8.581
dependencies.magiclib_dependency=0.8.581-beta
dependencies.magiclib_version=0.8.581-beta
dependencies.magiclib_legacy_compat_dependency=0.8.24-beta
dependencies.magiclib_legacy_compat_version=0.8.24-beta
# MagicLib
dependencies.magiclib_dependency=0.8.603-beta
dependencies.magiclib_version=0.8.603-beta
dependencies.magiclib_legacy_compat_dependency=0.8.30-beta
dependencies.magiclib_legacy_compat_version=0.8.30-beta

# Annotation processor
dependencies.lombok_version=1.18.30
dependencies.lombok_version=1.18.34

# Gradle Plugins
fabric_loom_version=1.7-SNAPSHOT
Expand Down
9 changes: 5 additions & 4 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ import groovy.json.JsonSlurper
pluginManagement {
repositories {
mavenLocal()
mavenCentral()
gradlePluginPortal()

maven {
name("Fabric")
url("https://maven.fabricmc.net")
name("NyanMaven Mirror")
url("https://maven.hendrixshen.top/mirror")
}

maven {
name("Jitpack")
url("https://jitpack.io")
}

mavenCentral()
gradlePluginPortal()
}

resolutionStrategy {
Expand Down
3 changes: 2 additions & 1 deletion settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"1.20.1-fabric",
"1.20.2-fabric",
"1.20.4-fabric",
"1.20.6-fabric"
"1.20.6-fabric",
"1.21.1-fabric"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import top.hendrixshen.magiclib.api.compat.minecraft.resources.ResourceLocationCompat;
import top.hendrixshen.magiclib.util.FabricUtil;

public class PluslsCarpetAdditionReference {
Expand All @@ -28,6 +29,6 @@ public class PluslsCarpetAdditionReference {

@Contract(value = "_ -> new", pure = true)
public static @NotNull ResourceLocation identifier(String path) {
return new ResourceLocation(PluslsCarpetAdditionReference.modIdentifier, path);
return ResourceLocationCompat.fromNamespaceAndPath(PluslsCarpetAdditionReference.modIdentifier, path);
}
}
22 changes: 14 additions & 8 deletions src/main/java/com/plusls/carpet/impl/network/PcaSyncProtocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import top.hendrixshen.magiclib.api.compat.minecraft.world.entity.player.PlayerCompat;
import top.hendrixshen.magiclib.api.compat.minecraft.world.level.state.BlockStateCompat;
import top.hendrixshen.magiclib.impl.compat.minecraft.world.level.dimension.DimensionWrapper;

import java.util.HashMap;
Expand Down Expand Up @@ -139,7 +140,7 @@ public static void disablePcaSyncProtocol(@NotNull ServerPlayer player) {
// 传输 World 是为了通知客户端该 Entity 属于哪个 World
public static void updateEntity(@NotNull ServerPlayer player, @NotNull Entity entity) {
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer());
buf.writeResourceLocation(DimensionWrapper.of(PlayerCompat.of(player).getLevel().get()).getResourceLocation());
buf.writeResourceLocation(DimensionWrapper.of(PlayerCompat.of(player).getLevel()).getResourceLocation());
buf.writeInt(entity.getId());
buf.writeNbt(entity.saveWithoutId(new CompoundTag()));
ServerPlayNetworking.send(
Expand Down Expand Up @@ -277,25 +278,27 @@ public static void syncBlockEntityHandler(
BlockPos pos = buf.readBlockPos();
//#endif

ServerLevel level = (ServerLevel) PlayerCompat.of(player).getLevel().get();
ServerLevel level = (ServerLevel) PlayerCompat.of(player).getLevel();
BlockState blockState = level.getBlockState(pos);
BlockStateCompat blockStateCompat = BlockStateCompat.of(blockState);
clearPlayerWatchData(player);
PluslsCarpetAdditionReference.getLogger().debug("{} watch blockpos {}: {}", player.getName().getString(), pos, blockState);

BlockEntity blockEntityAdj = null;

// 不是单个箱子则需要更新隔壁箱子
if (blockState.getBlock() instanceof ChestBlock) {
if (blockState.getValue(ChestBlock.TYPE) != ChestType.SINGLE) {
BlockPos posAdj = pos.relative(ChestBlock.getConnectedDirection(blockState));
// The method in World now checks that the caller is from the same thread...
blockEntityAdj = level.getChunkAt(posAdj).getBlockEntity(posAdj);
}
} else if (PluslsCarpetAdditionReference.tisCarpetLoaded && blockState.is(Blocks.BARREL) && CarpetServer.settingsManager.getRule("largeBarrel").getBoolValue()) {
} else if (PluslsCarpetAdditionReference.tisCarpetLoaded && blockStateCompat.is(Blocks.BARREL) && CarpetServer.settingsManager.getRule("largeBarrel").getBoolValue()) {
Direction directionOpposite = blockState.getValue(BarrelBlock.FACING).getOpposite();
BlockPos posAdj = pos.relative(directionOpposite);
BlockState blockStateAdj = level.getBlockState(posAdj);
BlockStateCompat blockStateCompatAdj = BlockStateCompat.of(blockStateAdj);

if (blockStateAdj.is(Blocks.BARREL) && blockStateAdj.getValue(BarrelBlock.FACING) == directionOpposite) {
if (blockStateCompatAdj.is(Blocks.BARREL) && blockStateAdj.getValue(BarrelBlock.FACING) == directionOpposite) {
blockEntityAdj = level.getChunkAt(posAdj).getBlockEntity(posAdj);
}
}
Expand Down Expand Up @@ -352,7 +355,7 @@ public static void syncEntityHandler(
int entityId = buf.readInt();
//#endif

ServerLevel level = (ServerLevel) PlayerCompat.of(player).getLevel().get();
ServerLevel level = (ServerLevel) PlayerCompat.of(player).getLevel();
Entity entity = level.getEntity(entityId);

if (entity == null) {
Expand Down Expand Up @@ -456,6 +459,7 @@ public static boolean syncBlockEntityToClient(@NotNull BlockEntity blockEntity)
}

BlockState blockState = world.getBlockState(pos);
BlockStateCompat blockStateCompat = BlockStateCompat.of(blockState);
lock.lock();
Set<ServerPlayer> playerList = getWatchPlayerList(world, blockEntity.getBlockPos());
Set<ServerPlayer> playerListAdj = null;
Expand All @@ -467,11 +471,13 @@ public static boolean syncBlockEntityToClient(@NotNull BlockEntity blockEntity)
BlockPos posAdj = pos.relative(ChestBlock.getConnectedDirection(blockState));
playerListAdj = getWatchPlayerList(world, posAdj);
}
} else if (PluslsCarpetAdditionReference.tisCarpetLoaded && blockState.is(Blocks.BARREL) && CarpetServer.settingsManager.getRule("largeBarrel").getBoolValue()) {
} else if (PluslsCarpetAdditionReference.tisCarpetLoaded && blockStateCompat.is(Blocks.BARREL) && CarpetServer.settingsManager.getRule("largeBarrel").getBoolValue()) {
Direction directionOpposite = blockState.getValue(BarrelBlock.FACING).getOpposite();
BlockPos posAdj = pos.relative(directionOpposite);
BlockState blockStateAdj = world.getBlockState(posAdj);
if (blockStateAdj.is(Blocks.BARREL) && blockStateAdj.getValue(BarrelBlock.FACING) == directionOpposite) {
BlockStateCompat blockStateCompatAdj = BlockStateCompat.of(blockStateAdj);

if (blockStateCompatAdj.is(Blocks.BARREL) && blockStateAdj.getValue(BarrelBlock.FACING) == directionOpposite) {
playerListAdj = getWatchPlayerList(world, posAdj);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import top.hendrixshen.magiclib.api.compat.minecraft.world.item.ItemStackCompat;
import top.hendrixshen.magiclib.api.compat.minecraft.world.level.state.BlockStateCompat;

import java.util.List;

Expand All @@ -48,31 +50,39 @@ public class MixinDispenserBlock {
Item item = itemStack.getItem();

for (int i = 0; !itemStack.isEmpty() && i < container.getContainerSize(); ++i) {
ItemStack tmpItemStack = container.getItem(i);
ItemStack stack = container.getItem(i);
ItemStackCompat stackCompat = ItemStackCompat.of(stack);

if (!tmpItemStack.isEmpty() && tmpItemStack.is(item)) {
int count = Math.min(itemStack.getCount(), tmpItemStack.getCount());
if (!stack.isEmpty() && stackCompat.is(item)) {
int count = Math.min(itemStack.getCount(), stack.getCount());
itemStack.setCount(itemStack.getCount() - count);
tmpItemStack.setCount(tmpItemStack.getCount() - count);
stack.setCount(stack.getCount() - count);
}
}
}

@Unique
private static ItemStack pca$getItemFromInventory(@NotNull ItemStack itemStack, Container container) {
if (itemStack.isEmpty()) {
return ItemStack.EMPTY;
}

Item item = itemStack.getItem();
ItemStack ret = new ItemStack(item, 0);

for (int i = 0; i < container.getContainerSize(); ++i) {
ItemStack tmpStack = container.getItem(i);
if (!tmpStack.isEmpty() && tmpStack.is(item)) {
ret.setCount(Math.min(tmpStack.getCount() + ret.getCount(), ret.getMaxStackSize()));
ItemStack stack = container.getItem(i);
ItemStackCompat stackCompat = ItemStackCompat.of(stack);

if (!stack.isEmpty() && stackCompat.is(item)) {
ret.setCount(Math.min(stack.getCount() + ret.getCount(), ret.getMaxStackSize()));

if (ret.getCount() == ret.getMaxStackSize()) {
break;
}
}
}

return ret;
}

Expand All @@ -92,53 +102,65 @@ private void autoTrade(
if (!PluslsCarpetAdditionSettings.autoTrade) {
return;
}

BlockState state = level.getBlockState(blockPos.below());
BlockStateCompat blockStateCompat = BlockStateCompat.of(state);
boolean tradeAll;
if (state.is(Blocks.EMERALD_BLOCK)) {

if (blockStateCompat.is(Blocks.EMERALD_BLOCK)) {
tradeAll = false;
} else if (state.is(Blocks.DIAMOND_BLOCK)) {
} else if (blockStateCompat.is(Blocks.DIAMOND_BLOCK)) {
tradeAll = true;
} else {
return;
}

BlockPos faceBlockPos = blockPos.relative(level.getBlockState(blockPos).getValue(DispenserBlock.FACING));
List<AbstractVillager> villagerList = level.getEntitiesOfClass(AbstractVillager.class,
new AABB(faceBlockPos), Entity::isAlive);

if (villagerList.isEmpty()) {
return;
}

AbstractVillager merchantEntity = villagerList.get(0);
MerchantOffers offerList = merchantEntity.getOffers();

if (offerList.isEmpty()) {
return;
}

int tradeId = level.getBestNeighborSignal(blockPos);

if (tradeId == 0) {
return;
}

MerchantOffer offer = offerList.get(tradeId > offerList.size() ? offerList.size() - 1 : tradeId - 1);
ItemStack firstItemStack = offer.getCostA();
ItemStack secondItemStack = offer.getCostB();
ItemStack firstDepleteItem = firstItemStack.copy();
ItemStack secondDepleteItem = secondItemStack.copy();

BlockEntity blockEntity = level.getBlockEntity(blockPos);

if (!(blockEntity instanceof DispenserBlockEntity)) {
return;
}

//#if MC > 12001
//$$ BlockSource blockPointer = new BlockSource(level, blockPos, blockState, (DispenserBlockEntity) blockEntity);
//#else
BlockSourceImpl blockPointer = new BlockSourceImpl(level, blockPos);
//#endif
DispenserBlockEntity dispenserBlockEntity = (DispenserBlockEntity) blockEntity;
boolean success = false;

while (!offer.isOutOfStock()) {
ItemStack firstInventoryItemStack = pca$getItemFromInventory(firstItemStack, dispenserBlockEntity);
ItemStack secondInventoryItemStack = pca$getItemFromInventory(secondItemStack, dispenserBlockEntity);
int firstItemCount = firstInventoryItemStack.getCount();
int secondItemCount = secondInventoryItemStack.getCount();

if (offer.take(firstInventoryItemStack, secondInventoryItemStack)) {
firstDepleteItem.setCount(firstItemCount - firstInventoryItemStack.getCount());
secondDepleteItem.setCount(secondItemCount - secondInventoryItemStack.getCount());
Expand All @@ -156,6 +178,7 @@ private void autoTrade(
} else {
break;
}

if (!tradeAll) {
break;
}
Expand All @@ -165,5 +188,4 @@ private void autoTrade(
ci.cancel();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
import top.hendrixshen.magiclib.api.compat.minecraft.world.entity.EntityCompat;

@Mixin(ArmorStand.class)
public abstract class MixinArmorStand extends LivingEntity {
Expand All @@ -25,9 +26,10 @@ protected MixinArmorStand(EntityType<? extends LivingEntity> entityType, Level w
)
)
private boolean redirectIsSourceCreativePlayer(DamageSource instance) {
if (!this.getLevelCompat().isClientSide() && PluslsCarpetAdditionSettings.creativePlayerNoDirectKillArmorStand) {
if (!EntityCompat.of(this).getLevel().isClientSide() && PluslsCarpetAdditionSettings.creativePlayerNoDirectKillArmorStand) {
return false;
}

return instance.isCreativePlayer();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@
import net.minecraft.world.phys.BlockHitResult;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import top.hendrixshen.magiclib.api.compat.minecraft.world.entity.player.PlayerCompat;

@Mixin(BlockRotator.class)
public class MixinBlockRotator {
@Unique
private static boolean pca$playerHoldsTotemOfUndyingMainHand(@NotNull Player player) {
return player.getMainHandItem().getItem() == Items.TOTEM_OF_UNDYING;
}
Expand All @@ -37,11 +40,12 @@ private static void postFlipBlockWithCactus(BlockState state, Level level, Playe
if (!cir.getReturnValue() && PluslsCarpetAdditionSettings.flippingTotemOfUndying &&
level.getGameTime() != FlipCooldown.getCoolDown(player)) {
// 能修改世界且副手为空
if (!player.getAbilitiesCompat().mayBuild ||
if (!PlayerCompat.of(player).getAbilities().mayBuild ||
!pca$playerHoldsTotemOfUndyingMainHand(player) ||
!player.getOffhandItem().isEmpty()) {
return;
}

//#if MC > 11502
CarpetSettings.impendingFillSkipUpdates.set(true);
//#else
Expand All @@ -53,9 +57,11 @@ private static void postFlipBlockWithCactus(BlockState state, Level level, Playe
//#else
//$$ CarpetSettings.impendingFillSkipUpdates = false;
//#endif

if (ret) {
FlipCooldown.setCoolDown(player, level.getGameTime());
}

cir.setReturnValue(ret);
}
}
Expand Down
Loading

0 comments on commit c2f7a3b

Please sign in to comment.