Skip to content

Commit

Permalink
[#25] Current process
Browse files Browse the repository at this point in the history
Completed connection logic. ItemSelection still needs to be fixed.
  • Loading branch information
Danielxs01 committed Nov 16, 2021
1 parent 0f3ec58 commit d659e64
Show file tree
Hide file tree
Showing 13 changed files with 369 additions and 221 deletions.
3 changes: 3 additions & 0 deletions src/main/java/net/landofrails/stellwand/Stellwand.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.landofrails.stellwand.content.entities.storage.*;
import net.landofrails.stellwand.content.guis.CustomGuis;
import net.landofrails.stellwand.content.items.CustomItems;
import net.landofrails.stellwand.content.items.connector.AItemConnector;
import net.landofrails.stellwand.content.network.CustomPackets;
import net.landofrails.stellwand.content.recipes.CustomRecipes;
import net.landofrails.stellwand.content.tabs.CustomTabs;
Expand Down Expand Up @@ -37,6 +38,8 @@ public static void commonEvent(ModEvent event) {
if (!StellwandConfig.disableStellwand) {
Loader.init();

AItemConnector.registerConnectors();

CustomGuis.register();
CustomTabs.register();
CustomItems.register();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,24 @@ public ItemStack onPick() {

@Override
public boolean onClick(Player player, Hand hand, Facing facing, Vec3d hit) {
ItemStack item = player.getHeldItem(hand);

ItemStack heldItem = player.getHeldItem(hand);
if (heldItem != null && heldItem.is(CustomItems.ITEMMAGNIFYINGGLASS))

// @formatter:off
if (heldItem != null &&
(
heldItem.is(CustomItems.ITEMMAGNIFYINGGLASS) ||
heldItem.is(CustomItems.ITEMCONNECTOR1) ||
heldItem.is(CustomItems.ITEMCONNECTOR2) ||
heldItem.is(CustomItems.ITEMCONNECTOR3)
)
)
return false;
// @formatter:on

if (isAir(item) && getWorld().isServer) {
if (!entity.signals.isEmpty()) {
if (isAir(heldItem) && hand.equals(Hand.PRIMARY) && getWorld().isServer) {
Vec3i signalPos = entity.getFirstSignal();
if (signalPos != null) {

Vec3i signalPos = entity.signals.get(0);
getWorld().keepLoaded(signalPos);
if (getWorld().hasBlockEntity(signalPos, BlockSignalStorageEntity.class)) {
BlockSignalStorageEntity signalEntity = getWorld().getBlockEntity(signalPos, BlockSignalStorageEntity.class);
Expand All @@ -72,13 +80,11 @@ public boolean onClick(Player player, Hand hand, Facing facing, Vec3d hit) {
packet.sendToPlayer(player);
} else {
entity.signals.remove(signalPos);
if (!getWorld().isClient)
ServerMessagePacket.send(player, EMessage.MESSAGE_NO_SIGNAL_FOUND, EMessage.MESSAGE_ERROR1.getRaw());
ServerMessagePacket.send(player, EMessage.MESSAGE_NO_SIGNAL_FOUND, EMessage.MESSAGE_ERROR1.getRaw());
}

} else {
if (!getWorld().isClient)
ServerMessagePacket.send(player, EMessage.MESSAGE_NO_SIGNALS_CONNECTED);
ServerMessagePacket.send(player, EMessage.MESSAGE_NO_SIGNALS_CONNECTED);
}

return true;
Expand All @@ -101,6 +107,7 @@ public void onNeighborChange(Vec3i neighbor) {
}

public void updateSignals() {
entity.refreshSignals();
for (Vec3i signalPos : entity.signals) {
getWorld().keepLoaded(signalPos);

Expand Down Expand Up @@ -152,7 +159,9 @@ public void onBreak() {
}

private boolean isAir(ItemStack item) {
return item.is(ItemStack.EMPTY) || item.equals(ItemStack.EMPTY);
if (item == null)
return true;
return (item.is(ItemStack.EMPTY) || item.equals(ItemStack.EMPTY)) && item.getTagCompound().isEmpty();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,19 @@ public void setSignal(SignalContainer<BlockEntity> signalEntity) {
this.signalEntity = signalEntity;
}

public Vec3i getFirstSignal() {
refreshSignals();
for (Vec3i pos : this.signals)
if (SignalContainer.isSignal(getWorld(), pos))
return pos;
return null;
}

public void refreshSignals() {
this.signals.forEach(signal -> getWorld().keepLoaded(signal));
this.signals.removeIf(vec3i -> !SignalContainer.isSignal(getWorld(), vec3i) && getWorld().isBlockLoaded(vec3i));
}

public SignalContainer<BlockEntity> getSignal() {
return this.signalEntity;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
import net.landofrails.stellwand.content.items.CustomItems;
import net.landofrails.stellwand.contentpacks.Content;
import net.landofrails.stellwand.contentpacks.entries.ContentPack;
import net.landofrails.stellwand.contentpacks.entries.parent.ContentPackEntry;
import net.landofrails.stellwand.contentpacks.types.EntryType;

import java.util.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;

public class SelectItem implements IScreen {
Expand All @@ -26,7 +28,7 @@ public class SelectItem implements IScreen {
// Initialized every call
private List<Button> contentPackButtons = new LinkedList<>();
private Consumer<ItemStack> selectedItem;
private ItemStack current;
private ItemStack current = null;
private EntryType entryType;
private ItemStack defaultItem;

Expand All @@ -48,7 +50,8 @@ public void onEnterKey(IScreenBuilder builder) {

@Override
public void onClose() {
selectedItem.accept(current);
if (current != null)
selectedItem.accept(current);
// Reset variables
contentPackButtons.clear();
selectedItem = null;
Expand All @@ -64,14 +67,13 @@ public void draw(IScreenBuilder builder) {

private List<ItemStack> getItemStackForContentPack(ContentPack pack, EntryType type) {
List<ItemStack> items = new ArrayList<>();
for (Map.Entry<ContentPackEntry, String> entry : Content.getBlocks(pack, type).entrySet()) {
ContentPackEntry cpe = entry.getKey();
Content.getBlocks(pack, type).forEach((cpe, value) -> {
ItemStack is = new ItemStack(getCustomItemForEntryType(Objects.requireNonNull(type)), 1);
TagCompound tag = is.getTagCompound();
tag.setString("itemId", cpe.getBlockId(entry.getValue()));
tag.setString("itemId", cpe.getBlockId(value));
is.setTagCompound(tag);
items.add(is);
}
});
if (items.isEmpty())
items.add(defaultItem);
return items;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public ClickResult onClickBlock(Player player, World world, Vec3i pos, Hand hand
float g = (float) (RANDOM.nextFloat() / 2f + 0.5);
float b = (float) (RANDOM.nextFloat() / 2f + 0.5);

sender.refreshSignals();
sender.signals.forEach(vec -> {
if (SignalContainer.isSignal(sender.getWorld(), vec)) {
SignalContainer<BlockEntity> signalContainer = SignalContainer.of(sender.getWorld(), vec);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package net.landofrails.stellwand.content.items.connector;

import cam72cam.mod.entity.Player;
import cam72cam.mod.item.ItemStack;
import cam72cam.mod.math.Vec3i;
import cam72cam.mod.serialization.TagCompound;
import cam72cam.mod.world.World;
import net.landofrails.stellwand.content.entities.storage.BlockSenderStorageEntity;
import net.landofrails.stellwand.content.items.CustomItems;
import net.landofrails.stellwand.utils.compact.SignalContainer;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
Expand All @@ -14,25 +18,89 @@ public abstract class AItemConnector {

private static final Map<Class<? extends AItemConnector>, BiPredicate<World, Vec3i>> CONNECTORS = new HashMap<>();

private static final long serialVersionUID = -4963471115414960689L;
private static final String KEY_ISCLIENT = "isClient";
private static final String KEY_WORLD = "world";
private static final String KEY_POS = "pos";

public static <T extends AItemConnector> void registerConnector(Class<T> connector, BiPredicate<World, Vec3i> isConnectable) {
private static final String KEY_CLASSNAME = "className";

/**
* Used by connectors to register class and bipredicate
*
* @param connector Connector's class
* @param isConnectable Method of identifying the correct connector
* @param <T> Class extending AItemConnector.class
*/
protected static <T extends AItemConnector> void registerConnector(Class<T> connector, BiPredicate<World, Vec3i> isConnectable) {
CONNECTORS.put(connector, isConnectable);
}

/**
* Called by commonEvent
*/
public static void registerConnectors() {
ItemConnectorSignal.register();
ItemConnectorSender.register();
}

/**
* Returns true if itemstack contains a connector
*
* @param itemStack The itemstack
* @return true if it contains a connector
*/
public static boolean hasConnectorData(ItemStack itemStack) {
if (itemStack.is(CustomItems.ITEMCONNECTOR2) || itemStack.is(CustomItems.ITEMCONNECTOR3)) {
TagCompound tag = itemStack.getTagCompound();
return tag.hasKey(KEY_WORLD) && tag.hasKey(KEY_POS) && tag.hasKey(KEY_CLASSNAME);
}
return false;
}

/**
* Returns true if a connector is suited for the position
*
* @param world World
* @param pos Position
* @return true if a suitable connector exists
*/
public static boolean suitableConnectorExists(World world, Vec3i pos) {
assert !CONNECTORS.isEmpty();
return CONNECTORS.values().stream().anyMatch(c -> c.test(world, pos));
}

/**
* Returns new instance of a connector if a suitable one exists
*
* @param world World
* @param pos Position
* @return Suitable Connector, may be null
*/
public static AItemConnector getConnector(World world, Vec3i pos) {
Class<? extends AItemConnector> clazz = getImplementor(world, pos);

if (clazz != null) {
try {
TagCompound tag = worldPosTag(world, pos);
return clazz.getDeclaredConstructor().newInstance();
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace();
}
}

return null;
}

/**
* Returns instance of a connector if one exists
*
* @param itemStack ItemStack
* @return The suitable connector, may be null
*/
public static AItemConnector getConnector(ItemStack itemStack) {
Class<? extends AItemConnector> clazz = getImplementor(itemStack);

if (clazz != null) {
try {
TagCompound tag = itemStack.getTagCompound();
return clazz.getDeclaredConstructor(TagCompound.class).newInstance(tag);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace();
Expand All @@ -42,10 +110,65 @@ public static AItemConnector getConnector(World world, Vec3i pos) {
return null;
}

/**
* Returns class of a connector if a suitable one exists
*
* @param world World
* @param pos Position
* @return Class of the suitable connector, may be null
*/
public static Class<? extends AItemConnector> getImplementor(World world, Vec3i pos) {
return CONNECTORS.entrySet().stream().filter(es -> es.getValue().test(world, pos)).map(Map.Entry::getKey).findFirst().orElse(null);
}

/**
* Return class of connector if a suitable one exists
*
* @param itemStack ItemStack
* @return Class of the connector in the itemstack, may be null
*/
public static Class<? extends AItemConnector> getImplementor(ItemStack itemStack) {
if (itemStack != null) {
TagCompound tag = itemStack.getTagCompound();
if (tag.hasKey(KEY_CLASSNAME)) {
try {
Class<?> aClass = Class.forName(tag.getString(KEY_CLASSNAME));
return aClass.asSubclass(AItemConnector.class);
} catch (ClassNotFoundException e) {
return null;
}
}
}
return null;
}

/**
* Returns a new SignalContainer if a signal exists at the give position
*
* @param world World
* @param pos Position
* @return The SignalContainer for this position, may be null
*/
protected static SignalContainer<?> getSignal(World world, Vec3i pos) {
world.keepLoaded(pos);
boolean isSignal = SignalContainer.isSignal(world, pos);
if (!isSignal)
return null;
return SignalContainer.of(world, pos);
}

/**
* Returns a BlockSenderStorageEntity if a signal exists at the give position
*
* @param world World
* @param pos Position
* @return Blocksender
*/
protected static BlockSenderStorageEntity getSender(World world, Vec3i pos) {
world.keepLoaded(pos);
return world.getBlockEntity(pos, BlockSenderStorageEntity.class);
}

// Connector

protected AItemConnector() {
Expand All @@ -55,32 +178,32 @@ protected AItemConnector() {
protected AItemConnector(TagCompound tag) {
if (tag != null && !tag.isEmpty()) {
Boolean isClient = tag.getBoolean(KEY_ISCLIENT);
this.world = tag.getWorld(KEY_WORLD, isClient);
this.pos = tag.getVec3i(KEY_POS);
this.startWorld = tag.getWorld(KEY_WORLD, isClient);
this.startPos = tag.getVec3i(KEY_POS);
}
}

private World world;
private Vec3i pos;
private World startWorld = null;
private Vec3i startPos = null;

public World getWorld() {
return world;
public World getStartWorld() {
return startWorld;
}

public void setWorld(World world) {
this.world = world;
public void setStartWorld(World startWorld) {
this.startWorld = startWorld;
}

public Vec3i getPos() {
return pos;
public Vec3i getStartPos() {
return startPos;
}

public void setPos(Vec3i pos) {
this.pos = pos;
public void setStartPos(Vec3i startPos) {
this.startPos = startPos;
}

public TagCompound toTag() {
return worldPosTag(world, pos);
return worldPosTag(startWorld, startPos).setString(KEY_CLASSNAME, getImplementingClass().getName());
}

protected static TagCompound worldPosTag(World world, Vec3i pos) {
Expand All @@ -91,6 +214,12 @@ protected static TagCompound worldPosTag(World world, Vec3i pos) {
return tag;
}

public abstract boolean shouldOverrideConnector(World world, Vec3i pos);

protected abstract boolean connect(World world, Vec3i pos, Player player, Player.Hand hand);

protected abstract boolean canConnect(World world, Vec3i pos);

protected abstract Class<? extends AItemConnector> getImplementingClass();

}
Loading

0 comments on commit d659e64

Please sign in to comment.