Skip to content

Commit

Permalink
Merge pull request #132 from Chainmail-Studios/daniel
Browse files Browse the repository at this point in the history
Fix up holo bridges + bump to 1.0.22
  • Loading branch information
shedaniel authored Jul 1, 2020
2 parents b02d9d3 + 27f8624 commit 56e1ac5
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 50 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ api_version = 0.14.1+build.372-1.16

# Mod
mod_name = astromine
mod_version = 1.0.21
mod_version = 1.0.22
mod_group = com.github.chainmailstudios
version_meta = fabric-1.16.1

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.github.chainmailstudios.astromine.common.block;

import com.github.chainmailstudios.astromine.access.DyeColorAccess;
import com.github.chainmailstudios.astromine.common.block.base.DefaultedHorizontalFacingBlockWithEntity;
import com.github.chainmailstudios.astromine.common.block.entity.HolographicBridgeProjectorBlockEntity;
import net.minecraft.block.AbstractBlock;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
Expand All @@ -12,11 +15,6 @@
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.BlockView;
import net.minecraft.world.World;

import com.github.chainmailstudios.astromine.access.DyeColorAccess;
import com.github.chainmailstudios.astromine.common.block.base.DefaultedHorizontalFacingBlockWithEntity;
import com.github.chainmailstudios.astromine.common.block.entity.HolographicBridgeProjectorBlockEntity;
import com.github.chainmailstudios.astromine.mixin.DyeColorMixin;
import spinnery.widget.api.Color;

public class HolographicBridgeProjectorBlock extends DefaultedHorizontalFacingBlockWithEntity {
Expand All @@ -33,21 +31,23 @@ public ActionResult onUse(BlockState state, World world, BlockPos position, Play

HolographicBridgeProjectorBlockEntity entity = (HolographicBridgeProjectorBlockEntity) world.getBlockEntity(position);

if (entity != null) {
entity.color = Color.of(0x7e000000 >> 2 | ((DyeColorAccess)(Object)dye.getColor()).getColor());
if(!world.isClient()) entity.sync();
if(entity.hasChild()) {
entity.getChild().color = Color.of(0x7e000000 >> 2 | ((DyeColorAccess)(Object)dye.getColor()).getColor());
if(!world.isClient()) entity.getChild().sync();
if (!world.isClient() && entity != null) {
entity.color = Color.of(0x7e000000 | ((DyeColorAccess) (Object) dye.getColor()).getColor());
entity.sync();

if (entity.hasChild()) {
entity.getChild().color = Color.of(0x7e000000 | ((DyeColorAccess) (Object) dye.getColor()).getColor());
entity.getChild().sync();
}
if(entity.hasParent()) {
entity.getParent().color = Color.of(0x7e000000 >> 2 | ((DyeColorAccess)(Object)dye.getColor()).getColor());
if(!world.isClient()) entity.getParent().sync();
if (entity.hasParent()) {
entity.getParent().color = Color.of(0x7e000000 | ((DyeColorAccess) (Object) dye.getColor()).getColor());
entity.getParent().sync();
}

if (!player.isCreative()) {
stack.decrement(1);
}
return ActionResult.SUCCESS;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,43 +1,80 @@
package com.github.chainmailstudios.astromine.common.item;

import com.github.chainmailstudios.astromine.common.block.HolographicBridgeProjectorBlock;
import com.github.chainmailstudios.astromine.common.block.entity.HolographicBridgeProjectorBlockEntity;
import com.github.chainmailstudios.astromine.registry.client.AstromineSounds;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.block.HorizontalFacingBlock;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUsageContext;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.sound.SoundCategory;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;
import net.minecraft.util.Pair;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.World;

import com.github.chainmailstudios.astromine.common.block.HolographicBridgeProjectorBlock;
import com.github.chainmailstudios.astromine.common.block.entity.HolographicBridgeProjectorBlockEntity;
import com.github.chainmailstudios.astromine.registry.client.AstromineSounds;
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
import java.util.List;

public class HolographicConnectorItem extends Item {
public static final Object2ObjectArrayMap<World, Object> CACHE = new Object2ObjectArrayMap<>();

public HolographicConnectorItem(Settings settings) {
super(settings);
}

@Environment(EnvType.CLIENT)
@Override
public void appendTooltip(ItemStack stack, World world, List<Text> tooltip, TooltipContext context) {
super.appendTooltip(stack, world, tooltip, context);

Pair<RegistryKey<World>, BlockPos> pair = readBlock(stack);
if (pair != null) {
tooltip.add(Text.method_30163(null));
tooltip.add(new TranslatableText("text.astromine.selected.dimension.pos", pair.getLeft().getValue(), pair.getRight().getX(), pair.getRight().getY(), pair.getRight().getZ()).formatted(Formatting.GRAY));
}
}

@Override
public ActionResult useOnBlock(ItemUsageContext context) {
World world = context.getWorld();

BlockPos position = context.getBlockPos();

if (context.shouldCancelInteraction()) return super.useOnBlock(context);

if (world.getBlockState(position).getBlock() instanceof HolographicBridgeProjectorBlock) {
HolographicBridgeProjectorBlockEntity entity = (HolographicBridgeProjectorBlockEntity) world.getBlockEntity(position);

if (CACHE.getOrDefault(world, null) == null) {
CACHE.put(world, entity);
context.getPlayer().sendMessage(new TranslatableText("text.astromine.message.holographic_connector_select", toShortString(entity.getPos())).formatted(Formatting.BLUE), true);
world.playSound(context.getPlayer(), context.getBlockPos(), AstromineSounds.HOLOGRAPHIC_CONNECTOR_CLICK, SoundCategory.PLAYERS, 0.5f, 0.33f);
Pair<RegistryKey<World>, BlockPos> pair = readBlock(context.getStack());
if (pair == null || !pair.getLeft().getValue().equals(world.getRegistryKey().getValue())) {
if (!world.isClient) {
context.getPlayer().setStackInHand(context.getHand(), selectBlock(context.getStack(), entity.getWorld().getRegistryKey(), entity.getPos()));
} else {
context.getPlayer().sendMessage(new TranslatableText("text.astromine.message.holographic_connector_select", toShortString(entity.getPos())).formatted(Formatting.BLUE), true);
world.playSound(context.getPlayer(), context.getBlockPos(), AstromineSounds.HOLOGRAPHIC_CONNECTOR_CLICK, SoundCategory.PLAYERS, 0.5f, 0.33f);
}
} else {
HolographicBridgeProjectorBlockEntity parent = (HolographicBridgeProjectorBlockEntity) CACHE.get(world);
BlockEntity blockEntity = world.getBlockEntity(pair.getRight());
if (!(blockEntity instanceof HolographicBridgeProjectorBlockEntity)) {
if (!world.isClient) {
context.getPlayer().setStackInHand(context.getHand(), selectBlock(context.getStack(), entity.getWorld().getRegistryKey(), entity.getPos()));
} else {
context.getPlayer().sendMessage(new TranslatableText("text.astromine.message.holographic_connector_select", toShortString(entity.getPos())).formatted(Formatting.BLUE), true);
world.playSound(context.getPlayer(), context.getBlockPos(), AstromineSounds.HOLOGRAPHIC_CONNECTOR_CLICK, SoundCategory.PLAYERS, 0.5f, 0.33f);
}
return ActionResult.SUCCESS;
}
HolographicBridgeProjectorBlockEntity parent = (HolographicBridgeProjectorBlockEntity) blockEntity;

BlockPos nP = entity.getPos();
BlockPos oP = parent.getPos();
Expand All @@ -61,45 +98,88 @@ public ActionResult useOnBlock(ItemUsageContext context) {
}

if ((parent.getPos().getX() != entity.getPos().getX() && parent.getPos().getZ() != entity.getPos().getZ()) || oP.getSquaredDistance(nP) > 65536) {
CACHE.put(world, null);
context.getPlayer().sendMessage(new TranslatableText("text.astromine.message.holographic_connection_failed", toShortString(parent.getPos()), toShortString(entity.getPos())).formatted(Formatting.RED), true);
world.playSound(context.getPlayer(), context.getBlockPos(), AstromineSounds.HOLOGRAPHIC_CONNECTOR_CLICK, SoundCategory.PLAYERS, 0.5f, 0.33f);
return ActionResult.FAIL;
if (!world.isClient) {
context.getPlayer().setStackInHand(context.getHand(), unselect(context.getStack()));
} else {
context.getPlayer().sendMessage(new TranslatableText("text.astromine.message.holographic_connection_failed", toShortString(parent.getPos()), toShortString(entity.getPos())).formatted(Formatting.RED), true);
world.playSound(context.getPlayer(), context.getBlockPos(), AstromineSounds.HOLOGRAPHIC_CONNECTOR_CLICK, SoundCategory.PLAYERS, 0.5f, 0.33f);
}
return ActionResult.SUCCESS;
} else if (parent.getCachedState().get(HorizontalFacingBlock.FACING).getOpposite() != entity.getCachedState().get(HorizontalFacingBlock.FACING)) {
CACHE.put(world, null);
context.getPlayer().sendMessage(new TranslatableText("text.astromine.message.holographic_connection_failed", toShortString(parent.getPos()), toShortString(entity.getPos())).formatted(Formatting.RED), true);
world.playSound(context.getPlayer(), context.getBlockPos(), AstromineSounds.HOLOGRAPHIC_CONNECTOR_CLICK, SoundCategory.PLAYERS, 0.5f, 0.33f);
return ActionResult.FAIL;
if (!world.isClient) {
context.getPlayer().setStackInHand(context.getHand(), unselect(context.getStack()));
} else {
context.getPlayer().sendMessage(new TranslatableText("text.astromine.message.holographic_connection_failed", toShortString(parent.getPos()), toShortString(entity.getPos())).formatted(Formatting.RED), true);
world.playSound(context.getPlayer(), context.getBlockPos(), AstromineSounds.HOLOGRAPHIC_CONNECTOR_CLICK, SoundCategory.PLAYERS, 0.5f, 0.33f);
}
return ActionResult.SUCCESS;
}

parent.setChild(entity);
entity.setParent(parent);

if (parent.getParent() == entity.getParent()) {
parent.setParent(null);
}

parent.direction = d;

parent.buildBridge();

if (world.isClient) {
context.getPlayer().sendMessage(new TranslatableText("text.astromine.message.holographic_connection_successful", toShortString(parent.getPos()), toShortString(entity.getPos())).formatted(Formatting.GREEN), true);
world.playSound(context.getPlayer(), context.getBlockPos(), AstromineSounds.HOLOGRAPHIC_CONNECTOR_CLICK, SoundCategory.PLAYERS, 0.5f, 0.33f);
} else {
parent.setChild(entity);
entity.setParent(parent);

if (parent.getParent() == entity.getParent()) {
parent.setParent(null);
}

parent.direction = d;
parent.buildBridge();
parent.sync();
context.getPlayer().setStackInHand(context.getHand(), unselect(context.getStack()));
}

CACHE.put(world, null);
}
} else {
if (world.isClient) {
context.getPlayer().sendMessage(new TranslatableText("text.astromine.message.holographic_connection_clear").formatted(Formatting.YELLOW), true);
world.playSound(context.getPlayer(), context.getBlockPos(), AstromineSounds.HOLOGRAPHIC_CONNECTOR_CLICK, SoundCategory.PLAYERS, 0.5f, 0.33f);
} else {
context.getPlayer().setStackInHand(context.getHand(), unselect(context.getStack()));
}

CACHE.put(world, null);
}
return ActionResult.SUCCESS;
}

private ItemStack unselect(ItemStack stack) {
stack = stack.copy();
CompoundTag tag = stack.getOrCreateTag();
tag.remove("SelectedConnectorBlock");
return stack;
}

private ItemStack selectBlock(ItemStack stack, RegistryKey<World> registryKey, BlockPos pos) {
stack = stack.copy();
CompoundTag tag = stack.getOrCreateTag();
tag.remove("SelectedConnectorBlock");
tag.put("SelectedConnectorBlock", writePos(registryKey, pos));
return stack;
}

private Pair<RegistryKey<World>, BlockPos> readBlock(ItemStack stack) {
CompoundTag tag = stack.getTag();
if (tag == null) return null;
if (!tag.contains("SelectedConnectorBlock")) return null;
return readPos(tag.getCompound("SelectedConnectorBlock"));
}

private CompoundTag writePos(RegistryKey<World> registryKey, BlockPos pos) {
CompoundTag tag = new CompoundTag();
tag.putString("World", registryKey.getValue().toString());
tag.putInt("X", pos.getX());
tag.putInt("Y", pos.getY());
tag.putInt("Z", pos.getZ());
return tag;
}

return super.useOnBlock(context);
private Pair<RegistryKey<World>, BlockPos> readPos(CompoundTag tag) {
RegistryKey<World> registryKey = RegistryKey.of(Registry.DIMENSION, Identifier.tryParse(tag.getString("World")));
int x = tag.getInt("X");
int y = tag.getInt("Y");
int z = tag.getInt("Z");
return new Pair<>(registryKey, new BlockPos(x, y, z));
}

public String toShortString(BlockPos pos) {
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/astromine/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@
"category.astromine.consuming.energy": "%s consumed",
"category.astromine.fluid.generating.consumed": "%s (%s) consumed/tick",
"category.astromine.fluid.generating.generated": "%s (%s) generated/tick",
"text.astromine.selected.dimension.pos": "Selected %s (%d, %d, %d)",
"text.astromine.patchouli.astronautics.title": "Astronautics",
"text.astromine.patchouli.astronautics.description": "From where one dreams, to where their dreams come true.",
"text.astromine.patchouli.components.title": "Components",
Expand Down

0 comments on commit 56e1ac5

Please sign in to comment.