Skip to content
Merged
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
674 changes: 674 additions & 0 deletions LICENSE.NewHorizonsCoreMod

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,10 @@ This can be used as a standalone mod with several dependencies. The vending mach

## Credits

We would like to thank the contributors of BetterQuesting for their implementation of networking and file serialization/deserialization, which are used in this project. We do not reference the classes directly to avoid having BetterQuesting as a required dependency. The BetterQuesting license may be found in the root directory of this project as LICENSE.betterquesting.
Credits to **BetterQuesting** for their implementation of networking and file serialization/deserialization, which are used in this project. We do not reference the classes directly to avoid having BetterQuesting as a required dependency.

Credits to **NH Coremod** for the coin textures used in the GUI.

The BetterQuesting license may be found in the root directory of this project as LICENSE.betterquesting.

The NHCoreMod licenese may be found in the root directory of this project as LICENSE.NewHorizonsCoreMod.
8 changes: 0 additions & 8 deletions src/main/java/com/cubefury/vendingmachine/ClientProxy.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.cubefury.vendingmachine;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraftforge.common.MinecraftForge;

import com.cubefury.vendingmachine.integration.nei.NEIConfig;

import cpw.mods.fml.client.FMLClientHandler;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;

Expand All @@ -28,12 +26,6 @@ public boolean isClient() {
return true;
}

@Override
public EntityPlayer getThePlayer() {
return FMLClientHandler.instance()
.getClientPlayerEntity();
}

@Override
public void registerHandlers() {
super.registerHandlers();
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/com/cubefury/vendingmachine/CommonProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import net.minecraft.command.ICommandManager;
import net.minecraft.command.ServerCommandManager;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.server.MinecraftServer;
import net.minecraftforge.common.MinecraftForge;

Expand Down Expand Up @@ -49,8 +48,4 @@ public void registerHandlers() {
.bus()
.register(EventHandler.INSTANCE);
}

public EntityPlayer getThePlayer() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@
import static com.gtnewhorizon.structurelib.structure.StructureUtility.lazy;
import static com.gtnewhorizon.structurelib.structure.StructureUtility.ofBlock;

import java.util.HashMap;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.LinkedBlockingQueue;

import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.ChatComponentTranslation;
import net.minecraft.world.World;
import net.minecraftforge.common.util.Constants;
import net.minecraftforge.common.util.ForgeDirection;

Expand All @@ -23,8 +29,11 @@
import com.cubefury.vendingmachine.network.handlers.NetAvailableTradeSync;
import com.cubefury.vendingmachine.network.handlers.NetTradeRequestSync;
import com.cubefury.vendingmachine.network.handlers.NetTradeStateSync;
import com.cubefury.vendingmachine.storage.NameCache;
import com.cubefury.vendingmachine.trade.CurrencyItem;
import com.cubefury.vendingmachine.trade.Trade;
import com.cubefury.vendingmachine.trade.TradeDatabase;
import com.cubefury.vendingmachine.trade.TradeManager;
import com.cubefury.vendingmachine.trade.TradeRequest;
import com.cubefury.vendingmachine.util.BigItemStack;
import com.gtnewhorizon.structurelib.StructureLibAPI;
Expand All @@ -45,14 +54,13 @@
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.metatileentity.implementations.MTEMultiBlockBase;
import gregtech.api.render.TextureFactory;
import gregtech.api.util.GTUtil;
import gregtech.api.util.GTUtility;
import gregtech.api.util.MultiblockTooltipBuilder;

public class MTEVendingMachine extends MTEMultiBlockBase
implements ISurvivalConstructable, ISecondaryDescribable, IAlignment {

public static final int CUSTOM_UI_HEIGHT = 300;

public static final int INPUT_SLOTS = 6;
public static final int OUTPUT_SLOTS = 4;

Expand Down Expand Up @@ -88,6 +96,8 @@ public class MTEVendingMachine extends MTEMultiBlockBase
private boolean newBufferedOutputs = false;
private int ticksSinceOutput = 0;

private EntityPlayer currentUser = null;

public MTEVendingMachine(final int aID, final String aName, final String aNameRegional) {
super(aID, aName, aNameRegional);
}
Expand All @@ -106,7 +116,6 @@ public void sendTradeRequest(TradeItemDisplay trade) {
}

public void addTradeRequest(TradeRequest trade) {
VendingMachine.LOG.info("received new trade request");
this.pendingTrades.add(trade);
}

Expand Down Expand Up @@ -196,6 +205,21 @@ private boolean processTradeOnServer(TradeRequest tradeRequest) {
Trade trade = TradeDatabase.INSTANCE.getTradeGroupFromId(tradeRequest.tradeGroup)
.getTrades()
.get(tradeRequest.tradeGroupOrder);
Map<CurrencyItem.CurrencyType, Integer> coinInventory = TradeManager.INSTANCE.playerCurrency
.get(NameCache.INSTANCE.getUUIDFromPlayer(this.getCurrentUser()));
Map<CurrencyItem.CurrencyType, Integer> newCoinInventory = new HashMap<>();
if (coinInventory == null) {
return false;
}
for (CurrencyItem ci : trade.fromCurrency) {
int oldValue = coinInventory.get(ci.type);
if (!coinInventory.containsKey(ci.type) || oldValue < ci.value) {
return false;
} else {
newCoinInventory.put(ci.type, oldValue - ci.value);
}
}

for (BigItemStack stack : trade.fromItems) {
ItemStack requiredStack = stack.getBaseStack();
int requiredAmount = stack.stackSize;
Expand Down Expand Up @@ -224,6 +248,14 @@ private boolean processTradeOnServer(TradeRequest tradeRequest) {
}
}

for (Map.Entry<CurrencyItem.CurrencyType, Integer> entry : newCoinInventory.entrySet()) {
if (entry.getValue() == 0) {
coinInventory.remove(entry.getKey());
} else {
coinInventory.replace(entry.getKey(), entry.getValue());
}
}

for (int i = 0; i < MTEVendingMachine.INPUT_SLOTS; i++) {
this.inputItems.setStackInSlot(i, inputSlots[i]);
}
Expand Down Expand Up @@ -278,12 +310,7 @@ protected boolean forceUseMui2() {
NetAvailableTradeSync.requestSync();
NetTradeStateSync.requestSync();
}
return new MTEVendingMachineGui(this, CUSTOM_UI_HEIGHT);
}

@Override
public int getGUIHeight() {
return CUSTOM_UI_HEIGHT;
return new MTEVendingMachineGui(this);
}

@Override
Expand Down Expand Up @@ -476,4 +503,71 @@ public void construct(ItemStack stackSize, boolean hintsOnly) {
0,
hintsOnly);
}

@Override
public boolean onRightclick(IGregTechTileEntity aBaseMetaTileEntity, EntityPlayer aPlayer) {
if (GTUtil.hasMultiblockInputConfiguration(aPlayer.getHeldItem())) {
if (aBaseMetaTileEntity.isServerSide()) {
if (GTUtil.loadMultiblockInputConfiguration(this, aPlayer)) {
aPlayer.addChatComponentMessage(new ChatComponentTranslation("GT5U.MULTI_MACHINE_CONFIG.LOAD"));
} else {
aPlayer
.addChatComponentMessage(new ChatComponentTranslation("GT5U.MULTI_MACHINE_CONFIG.LOAD.FAIL"));
}
}
return true;
}
if (canUse(aPlayer)) {
this.currentUser = aPlayer;
openGui(aPlayer);
} else {
aPlayer.addChatComponentMessage(new ChatComponentTranslation("vendingmachine.gui.error.player_using"));
}
return true;
}

private boolean canUse(EntityPlayer aPlayer) {
return this.currentUser == null || this.currentUser == aPlayer;
}

public EntityPlayer getCurrentUser() {
return this.currentUser;
}

public void resetCurrentUser(EntityPlayer aPlayer) {
if (this.currentUser == aPlayer) {
this.currentUser = null;
}
}

public void spawnItem(ItemStack stack) {
if (stack == null || this.getBaseMetaTileEntity() == null) {
return;
}
World world = this.getBaseMetaTileEntity()
.getWorld();
int posX = this.getBaseMetaTileEntity()
.getXCoord();
int posY = this.getBaseMetaTileEntity()
.getYCoord();
int posZ = this.getBaseMetaTileEntity()
.getZCoord();
int offsetX = this.getExtendedFacing()
.getDirection().offsetX;
int offsetY = this.getExtendedFacing()
.getDirection().offsetY;
int offsetZ = this.getExtendedFacing()
.getDirection().offsetZ;
final EntityItem itemEntity = new EntityItem(
world,
posX + offsetX * 0.5,
posY + offsetY * 0.5,
posZ + offsetZ * 0.5,
stack);
itemEntity.delayBeforeCanPickup = 0;
itemEntity.motionX = 0.05f * offsetX;
itemEntity.motionY = 0.05f * offsetY;
itemEntity.motionZ = 0.05f * offsetZ;
world.spawnEntityInWorld(itemEntity);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.cubefury.vendingmachine.blocks.gui;

import org.jetbrains.annotations.NotNull;

import com.cleanroommc.modularui.api.drawable.IDrawable;
import com.cleanroommc.modularui.api.widget.Interactable;
import com.cleanroommc.modularui.widgets.ToggleButton;
import com.cubefury.vendingmachine.trade.CurrencyItem;

public class CoinButton extends ToggleButton {

private final TradeMainPanel panel;
private final CurrencyItem.CurrencyType type;

public CoinButton(TradeMainPanel panel, CurrencyItem.CurrencyType type) {
super();
background(IDrawable.EMPTY);
selectedBackground(IDrawable.EMPTY);

this.panel = panel;
this.type = type;
}

@Override
public @NotNull Result onMousePressed(int mouseButton) {
if (!panel.shiftHeld) {
return Result.IGNORE;
}
switch (mouseButton) {
case 0:
next();
Interactable.playButtonClickSound();
return Result.SUCCESS;
case 1:
prev();
Interactable.playButtonClickSound();
return Result.SUCCESS;
}
return Result.IGNORE;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.cubefury.vendingmachine.blocks.gui;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;

import com.cleanroommc.modularui.utils.item.ItemStackHandler;
import com.cleanroommc.modularui.widgets.slot.ModularSlot;
import com.cubefury.vendingmachine.network.handlers.NetTradeStateSync;
import com.cubefury.vendingmachine.storage.NameCache;
import com.cubefury.vendingmachine.trade.CurrencyItem;
import com.cubefury.vendingmachine.trade.TradeManager;

public class InterceptingSlot extends ModularSlot {

public InterceptingSlot(ItemStackHandler inputItems, int index) {
super(inputItems, index);
}

// intercept item on both ends, but only do the post-intercept actions on server side
public boolean intercept(ItemStack newItem, boolean client, EntityPlayer player) {
CurrencyItem mapped = mapToCurrency(newItem);
if (mapped != null) {
this.putStack(null);
if (!client) {
TradeManager.INSTANCE.addCurrency(NameCache.INSTANCE.getUUIDFromPlayer(player), mapped);
NetTradeStateSync.sendPlayerCurrency((EntityPlayerMP) player, mapped);
}
return true;
}
return false;
}

private CurrencyItem mapToCurrency(ItemStack newItem) {
if (newItem == null) {
return null;
}
return CurrencyItem.fromItemStack(newItem);
}

}
Loading
Loading