Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pipe #376

Draft
wants to merge 23 commits into
base: master
Choose a base branch
from
Draft

Pipe #376

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ tasks.withType<KotlinCompile> {
}

minecraft {
accessWidener("src/main/resources/galaxy-tweak.accesswidener")
}

dependencies {
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/one/oktw/galaxy/mixin/interfaces/FakeEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* OKTW Galaxy Project
* Copyright (C) 2018-2021
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package one.oktw.galaxy.mixin.interfaces;

public interface FakeEntity {
void setFake(boolean value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,37 @@

package one.oktw.galaxy.mixin.tweak;

import net.minecraft.block.AbstractBlock;
import net.minecraft.block.BarrierBlock;
import net.minecraft.block.Block;
import net.minecraft.block.BlockEntityProvider;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityTicker;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.Inventory;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.ItemScatterer;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.World;
import net.minecraft.world.WorldAccess;
import one.oktw.galaxy.block.CustomBlock;
import one.oktw.galaxy.block.CustomBlockEntityTicker;
import one.oktw.galaxy.block.listener.CustomBlockClickListener;
import one.oktw.galaxy.block.listener.CustomBlockNeighborUpdateListener;
import one.oktw.galaxy.block.listener.CustomBlockTickListener;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;

import java.util.Random;

@Mixin(BarrierBlock.class)
public abstract class MixinCustomBlockEntity_BarrierBlock extends AbstractBlock implements BlockEntityProvider {
public abstract class MixinCustomBlockEntity_BarrierBlock extends Block implements BlockEntityProvider {
public MixinCustomBlockEntity_BarrierBlock(Settings settings) {
super(settings);
}
Expand All @@ -60,4 +74,54 @@ public int getComparatorOutput(BlockState state, World world, BlockPos pos) {
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(World world, BlockState state, BlockEntityType<T> type) {
return new CustomBlockEntityTicker<>();
}

@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
BlockEntity entity = world.getBlockEntity(pos);
if (entity instanceof CustomBlockClickListener) return ((CustomBlockClickListener) entity).onClick(player, hand, hit);
return super.onUse(state, world, pos, player, hand, hit);
}

@Override
public void neighborUpdate(BlockState state, World world, BlockPos pos, Block block, BlockPos fromPos, boolean notify) {
BlockEntity entity = world.getBlockEntity(pos);
if (entity instanceof CustomBlockNeighborUpdateListener) ((CustomBlockNeighborUpdateListener) entity).onNeighborUpdate(false);
super.neighborUpdate(state, world, pos, block, fromPos, notify);
}

@Override
public BlockState getStateForNeighborUpdate(BlockState state, Direction direction, BlockState newState, WorldAccess world, BlockPos pos, BlockPos posFrom) {
BlockEntity entity = world.getBlockEntity(pos);
if (entity instanceof CustomBlockNeighborUpdateListener) ((CustomBlockNeighborUpdateListener) entity).onNeighborUpdate(true);
return super.getStateForNeighborUpdate(state, direction, newState, world, pos, posFrom);
}

@Override
public void onStateReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean moved) {
if (!state.isOf(newState.getBlock())) {
BlockEntity blockEntity = world.getBlockEntity(pos);
if (blockEntity instanceof Inventory) {
ItemScatterer.spawn(world, pos, (Inventory) blockEntity);
world.updateComparators(pos, this);
}

super.onStateReplaced(state, world, pos, newState, moved);
}
}

@Override
public void scheduledTick(BlockState state, ServerWorld world, BlockPos pos, Random random) {
BlockEntity entity = world.getBlockEntity(pos);
if (entity instanceof CustomBlockTickListener) ((CustomBlockTickListener) entity).scheduledTick();

super.scheduledTick(state, world, pos, random);
}

@Override
public void randomTick(BlockState state, ServerWorld world, BlockPos pos, Random random) {
BlockEntity entity = world.getBlockEntity(pos);
if (entity instanceof CustomBlockTickListener) ((CustomBlockTickListener) entity).randomTick();

super.randomTick(state, world, pos, random);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* OKTW Galaxy Project
* Copyright (C) 2018-2021
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package one.oktw.galaxy.mixin.tweak;

import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.block.pattern.CachedBlockPosition;
import net.minecraft.server.command.CloneCommand;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockBox;
import net.minecraft.util.math.BlockPos;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import java.util.Deque;
import java.util.Iterator;
import java.util.List;
import java.util.function.Predicate;

@Mixin(CloneCommand.class)
public class MixinCustomBlockEntity_Clone {
@Inject(method = "execute", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/world/ServerWorld;setBlockState(Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/BlockState;I)Z", ordinal = 3), locals = LocalCapture.CAPTURE_FAILSOFT)
private static void hackClone(ServerCommandSource source, BlockPos begin, BlockPos end, BlockPos destination, Predicate<CachedBlockPosition> filter, CloneCommand.Mode mode, CallbackInfoReturnable<Integer> cir, BlockBox blockBox, BlockPos blockPos, BlockBox blockBox2, ServerWorld serverWorld, List<CloneCommand.BlockInfo> list, List<CloneCommand.BlockInfo> list2, List<CloneCommand.BlockInfo> list3, Deque<BlockPos> deque, BlockPos blockPos2, List<CloneCommand.BlockInfo> list4, List<CloneCommand.BlockInfo> list5, int m, Iterator<CloneCommand.BlockInfo> var19, CloneCommand.BlockInfo blockInfo) {
// Workaround clone override custom block NBT
if (blockInfo.state.getBlock() == Blocks.BARRIER) {
serverWorld.setBlockState(blockInfo.pos, Blocks.AIR.getDefaultState(), Block.NO_REDRAW | Block.FORCE_STATE);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package one.oktw.galaxy.mixin.tweak;

import com.mojang.datafixers.util.Pair;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.fluid.FluidState;
Expand All @@ -44,6 +45,8 @@ public class MixinCustomBlockEntity_Structure {
at = @At(value = "INVOKE", target = "Lnet/minecraft/world/ServerWorldAccess;setBlockState(Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/BlockState;I)Z", ordinal = 1), locals = LocalCapture.CAPTURE_FAILSOFT)
private void hackPlace(ServerWorldAccess world, BlockPos pos, BlockPos blockPos, StructurePlacementData placementData, Random random, int i, CallbackInfoReturnable<Boolean> cir, List<Structure.StructureBlockInfo> list, BlockBox blockBox, List<BlockPos> list2, List<BlockPos> list3, List<Pair<BlockPos, NbtCompound>> list4, int j, int k, int l, int m, int n, int o, List<Structure.StructureBlockInfo> list5, Iterator<Structure.StructureBlockInfo> var19, Structure.StructureBlockInfo structureBlockInfo, BlockPos blockPos2, FluidState fluidState, BlockState blockState) {
// Workaround structure barrier bug
if (structureBlockInfo.state.getBlock() == Blocks.BARRIER) world.setBlockState(blockPos2, Blocks.AIR.getDefaultState(), 20);
if (structureBlockInfo.state.getBlock() == Blocks.BARRIER) {
world.setBlockState(blockPos2, Blocks.AIR.getDefaultState(), Block.NO_REDRAW | Block.FORCE_STATE);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* OKTW Galaxy Project
* Copyright (C) 2018-2021
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package one.oktw.galaxy.mixin.tweak;

import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.MovementType;
import net.minecraft.util.collection.ReusableStream;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import one.oktw.galaxy.mixin.interfaces.FakeEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(ItemEntity.class)
public abstract class MixinFakeItem_ItemEntity extends Entity implements FakeEntity {
private boolean fake = false;

public MixinFakeItem_ItemEntity(EntityType<?> type, World world) {
super(type, world);
}

@Override
public void setFake(boolean value) {
noClip = value;
fake = value;
}

@Inject(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/ItemEntity;getVelocity()Lnet/minecraft/util/math/Vec3d;", ordinal = 0, shift = At.Shift.BEFORE), cancellable = true)
private void fakeTick(CallbackInfo ci) {
if (fake) {
Vec3d velocity = getVelocity();
Vec3d adjustedVelocity = adjustMovementForCollisions(velocity, getBoundingBox(), new ReusableStream<>(world.getBlockCollisions(this, getBoundingBox().stretch(velocity))));
// Tick velocity
move(MovementType.SELF, velocity);

// Send velocity to client
if (!adjustedVelocity.equals(velocity)) {
velocityDirty = true;
velocityModified = true;
}
if ((this.age + this.getId()) % 4 == 0) {
velocityModified = true;
}

ci.cancel();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* OKTW Galaxy Project
* Copyright (C) 2018-2021
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package one.oktw.galaxy.mixin.tweak;

import net.minecraft.inventory.DoubleInventory;
import net.minecraft.inventory.Inventory;
import one.oktw.galaxy.mixin.interfaces.InventoryAvailableSlots;
import org.spongepowered.asm.mixin.Mixin;

import java.util.Arrays;

@Mixin(DoubleInventory.class)
public abstract class MixinOptimizeContainer_AvailableSlots2 implements InventoryAvailableSlots, Inventory {
private int[] availableSlots;

@Override
public int[] getAvailableSlots() {
if (availableSlots == null) {
int[] array = new int[size()];
Arrays.setAll(array, i -> i);
availableSlots = array;
}

return availableSlots;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* OKTW Galaxy Project
* Copyright (C) 2018-2021
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package one.oktw.galaxy.mixin.tweak;

import net.minecraft.block.ComparatorBlock;
import net.minecraft.entity.decoration.ItemFrameEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(ComparatorBlock.class)
public class MixinPipe_ComparatorBlock {
@Inject(method = "getAttachedItemFrame", at = @At("RETURN"), cancellable = true)
private void ignoreInvulnerableItemFrame(World world, Direction facing, BlockPos pos, CallbackInfoReturnable<ItemFrameEntity> cir) {
ItemFrameEntity entity = cir.getReturnValue();
if (entity != null && entity.isInvulnerable()) cir.setReturnValue(null);
}
}
1 change: 1 addition & 0 deletions src/main/kotlin/one/oktw/galaxy/block/CustomBlock.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ open class CustomBlock(final override val identifier: Identifier, val baseBlock:
val TELEPORTER_CORE_BASIC = registry.register(ModelCustomBlock("teleporter_core_basic", CustomBlockItem.TELEPORTER_CORE_BASIC.createItemStack()))
val TELEPORTER_CORE_ADVANCE = registry.register(ModelCustomBlock("teleporter_core_advance", CustomBlockItem.TELEPORTER_CORE_ADVANCE.createItemStack()))
val TELEPORTER_FRAME = registry.register(ModelCustomBlock("teleporter_frame", CustomBlockItem.TELEPORTER_FRAME.createItemStack()))
val PIPE = registry.register(PipeBlock("pipe", CustomBlockItem.PIPE.createItemStack()))
}

open fun toItem(): CustomBlockItem? = null
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/one/oktw/galaxy/block/DummyBlock.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

package one.oktw.galaxy.block

import net.minecraft.util.Identifier
import net.minecraft.util.math.BlockPos
import one.oktw.galaxy.block.entity.DummyBlockEntity
import one.oktw.galaxy.item.CustomBlockItem

class DummyBlock : CustomBlock(Identifier("galaxy", "block/dummy")) {
class DummyBlock : ModelCustomBlock("dummy", CustomBlockItem.DUMMY.createItemStack()) {
override fun createBlockEntity(pos: BlockPos) = DummyBlockEntity(blockEntityType, pos)
}
2 changes: 1 addition & 1 deletion src/main/kotlin/one/oktw/galaxy/block/ModelCustomBlock.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import one.oktw.galaxy.block.entity.ModelCustomBlockEntity
import one.oktw.galaxy.item.CustomBlockItem
import one.oktw.galaxy.item.CustomItemHelper

class ModelCustomBlock(identifier: Identifier, private val modelItem: ItemStack) : CustomBlock(identifier, BARRIER) {
open class ModelCustomBlock(identifier: Identifier, protected val modelItem: ItemStack) : CustomBlock(identifier, BARRIER) {
constructor(id: String, modelItem: ItemStack) : this(Identifier("galaxy", "block/$id"), modelItem)

override fun toItem() = modelItem.let { CustomItemHelper.getItem(it) as? CustomBlockItem }
Expand Down
Loading