Skip to content

Commit

Permalink
24w38a
Browse files Browse the repository at this point in the history
  • Loading branch information
Fallen-Breath committed Sep 19, 2024
1 parent 8238b3d commit 1085e8a
Show file tree
Hide file tree
Showing 9 changed files with 257 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,119 +20,10 @@

package carpettisaddition.helpers.rule.largeBarrel;

import carpettisaddition.CarpetTISAdditionSettings;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.state.property.DirectionProperty;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.BlockView;
import net.minecraft.world.IWorld;
import net.minecraft.world.World;

import java.util.function.BiPredicate;
import java.util.function.Function;

/**
* Totally stolen from 1.15.2 for easier porting to 1.14.4
*/
public class DoubleBlockProperties {
/**
* Stolen from 1.15.2 net.minecraft.block.entity.BlockEntityType#get
*/
@SuppressWarnings("unchecked")
private static <S extends BlockEntity> S getBlockEntity(BlockEntityType<S> blockEntityType, BlockView world, BlockPos pos) {
BlockEntity blockEntity = world.getBlockEntity(pos);
if (CarpetTISAdditionSettings.largeBarrel && LargeBarrelHelper.enabledOffThreadBlockEntityAccess.get())
{
if (world instanceof World)
{
blockEntity = ((World)world).getChunk(pos).getBlockEntity(pos);
}
}
return blockEntity != null && blockEntity.getType() == blockEntityType ? (S)blockEntity : null;
}

public static <S extends BlockEntity> DoubleBlockProperties.PropertySource<S> toPropertySource(BlockEntityType<S> blockEntityType, Function<BlockState, DoubleBlockProperties.Type> typeMapper, Function<BlockState, Direction> function, DirectionProperty directionProperty, BlockState state, IWorld world, BlockPos pos, BiPredicate<IWorld, BlockPos> fallbackTester) {
S blockEntity = getBlockEntity(blockEntityType, world, pos);
if (blockEntity == null) {
return DoubleBlockProperties.PropertyRetriever::getFallback;
} else if (fallbackTester.test(world, pos)) {
return DoubleBlockProperties.PropertyRetriever::getFallback;
} else {
DoubleBlockProperties.Type type = (DoubleBlockProperties.Type)typeMapper.apply(state);
boolean bl = type == DoubleBlockProperties.Type.SINGLE;
boolean bl2 = type == DoubleBlockProperties.Type.FIRST;
if (bl) {
return new DoubleBlockProperties.PropertySource.Single<>(blockEntity);
} else {
BlockPos blockPos = pos.offset((Direction)function.apply(state));
BlockState blockState = world.getBlockState(blockPos);
if (blockState.getBlock() == state.getBlock()) {
DoubleBlockProperties.Type type2 = (DoubleBlockProperties.Type)typeMapper.apply(blockState);
if (type2 != DoubleBlockProperties.Type.SINGLE && type != type2 && blockState.get(directionProperty) == state.get(directionProperty)) {
if (fallbackTester.test(world, blockPos)) {
return DoubleBlockProperties.PropertyRetriever::getFallback;
}

S blockEntity2 = getBlockEntity(blockEntityType, world, blockPos);
if (blockEntity2 != null) {
S blockEntity3 = (S)(bl2 ? blockEntity : blockEntity2);
S blockEntity4 = (S)(bl2 ? blockEntity2 : blockEntity);
return new DoubleBlockProperties.PropertySource.Pair<>(blockEntity3, blockEntity4);
}
}
}

return new DoubleBlockProperties.PropertySource.Single<>(blockEntity);
}
}
}

public interface PropertyRetriever<S, T> {
T getFromBoth(S first, S second);

T getFrom(S single);

T getFallback();
}

public interface PropertySource<S> {
<T> T apply(DoubleBlockProperties.PropertyRetriever<? super S, T> retriever);

public static final class Pair<S> implements DoubleBlockProperties.PropertySource<S> {
private final S first;
private final S second;

public Pair(S first, S second) {
this.first = first;
this.second = second;
}

@Override
public <T> T apply(DoubleBlockProperties.PropertyRetriever<? super S, T> propertyRetriever) {
return propertyRetriever.getFromBoth(this.first, this.second);
}
}

public static final class Single<S> implements DoubleBlockProperties.PropertySource<S> {
private final S single;

public Single(S single) {
this.single = single;
}

@Override
public <T> T apply(DoubleBlockProperties.PropertyRetriever<? super S, T> propertyRetriever) {
return propertyRetriever.getFrom(this.single);
}
}
}

public static enum Type {
SINGLE,
FIRST,
SECOND;
}
public class DoubleBlockProperties
{
// impl in mc1.14.4 subproject
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;

//#if MC >= 12102
//$$ import net.minecraft.entity.EntityType;
//$$ import net.minecraft.entity.SpawnReason;
//#endif

//#if MC >= 12004
//$$ import net.minecraft.server.world.ServerWorld;
//#endif
Expand All @@ -42,7 +47,14 @@ public class MinecartPlaceableOnGroundImpl
/**
* Reference: {@link net.minecraft.item.MinecartItem#useOnBlock}
*/
public static ActionResult placeMinecartOnGround(ItemUsageContext context, AbstractMinecartEntity.Type minecartType)
public static ActionResult placeMinecartOnGround(
ItemUsageContext context,
//#if MC >= 12102
//$$ EntityType<? extends AbstractMinecartEntity> minecartType
//#else
AbstractMinecartEntity.Type minecartType
//#endif
)
{
PlayerEntity player = context.getPlayer();
World world = context.getWorld();
Expand All @@ -57,7 +69,9 @@ public static ActionResult placeMinecartOnGround(ItemUsageContext context, Abstr
//#endif
)
{
//#if MC >= 12004
//#if MC >= 12102
//$$ AbstractMinecartEntity minecartEntity = AbstractMinecartEntity.create(serverWorld, vec3d.getX(), vec3d.getY(), vec3d.getZ(), minecartType, SpawnReason.DISPENSER, itemStack, player);
//#elseif MC >= 12004
//$$ AbstractMinecartEntity minecartEntity = AbstractMinecartEntity.create(serverWorld, vec3d.getX(), vec3d.getY(), vec3d.getZ(), minecartType, itemStack, player);
//#else
AbstractMinecartEntity minecartEntity = AbstractMinecartEntity.create(world, vec3d.getX(), vec3d.getY(), vec3d.getZ(), minecartType);
Expand Down Expand Up @@ -92,7 +106,14 @@ public static ActionResult placeMinecartOnGround(ItemUsageContext context, Abstr
// For proper client swing-hand animation. AbstractMinecartEntity.create() needs ServerWorld in mc1.20.4+, we can't use it on the clientside
// Assumption: The simply-initialized minecart entity has the same size of the serverside-created one,
// i.e., no nbt scaling tricks, modded new minecart variant

//#if MC >= 12102
//$$ MinecartEntity minecartEntity = new MinecartEntity(minecartType, world);
//$$ minecartEntity.setPos(vec3d.getX(), vec3d.getY(), vec3d.getZ());
//$$ if (!isMinecartPositionValid(world, minecartEntity))
//#else
if (!isMinecartPositionValid(world, new MinecartEntity(world, vec3d.getX(), vec3d.getY(), vec3d.getZ())))
//#endif
{
return ActionResult.FAIL;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import java.util.List;

//#if MC >= 12102
//$$ import net.minecraft.class_10209;
//$$ import net.minecraft.util.profiler.Profilers;
//#endif

public class LightThreadSynchronizer
Expand All @@ -56,7 +56,7 @@ public static void waitForLightThread(ServerWorld serverWorld)
{
Profiler profiler =
//#if MC >= 12102
//$$ class_10209.method_64146();
//$$ Profilers.get();
//#else
serverWorld.getProfiler();
//#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,25 @@
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyArg;

@Mixin(targets = "net/minecraft/item/MinecartItem$1")
//#if MC >= 12102
//$$ import net.minecraft.item.MinecartItem;
//#endif

@Mixin(
//#if MC >= 12102
//$$ MinecartItem.class
//#else
targets = "net/minecraft/item/MinecartItem$1"
//#endif
)
public abstract class MinecartItemDispenseBehaviorMixin
{
@ModifyArg(
//#if MC >= 12102
//$$ method = "useOnBlock",
//#else
method = "dispenseSilently",
//#endif
at = @At(
value = "INVOKE",
//#if MC >= 12004
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

/**
* mc1.14 ~ mc1.21.1: subproject 1.15.2 (main project) <--------
* mc1.21.2+ : subproject future
*/
@Mixin(ServerPlayNetworkHandler.class)
public abstract class ServerPlayNetworkHandlerMixin
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,20 @@
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;

//#if MC >= 12102
//$$ import net.minecraft.entity.EntityType;
//#endif

@Mixin(MinecartItem.class)
public abstract class MinecartItemMixin
{
@Shadow @Final private AbstractMinecartEntity.Type type;
@Shadow @Final
//#if MC >= 12102
//$$ private EntityType<? extends AbstractMinecartEntity>
//#else
private AbstractMinecartEntity.Type
//#endif
type;

@ModifyReturnValue(
method = "useOnBlock",
Expand All @@ -56,7 +66,7 @@ private ActionResult minecartPlaceableOnGround_hook(
// if not, in case other mod is doing their thing, then cancel the rule impl for safety
if (actionResult == ActionResult.FAIL)
{
actionResult = MinecartPlaceableOnGroundImpl.placeMinecartOnGround(context,this.type);
actionResult = MinecartPlaceableOnGroundImpl.placeMinecartOnGround(context, this.type);
}
}
return actionResult;
Expand Down
Loading

0 comments on commit 1085e8a

Please sign in to comment.